diff --git a/Sources/WPSwift/Repositories/PostWithSourceRepository.swift b/Sources/WPSwift/Repositories/PostWithSourceRepository.swift deleted file mode 100644 index 6d4ebe9..0000000 --- a/Sources/WPSwift/Repositories/PostWithSourceRepository.swift +++ /dev/null @@ -1,33 +0,0 @@ -// -// PostWithSourceRepository.swift -// -// -// Created by Ulaş Sancak on 6.10.2023. -// - -import Foundation -import Resting - -public struct PostWithSourceRepository { - public init() {} - - public func getPostsClient(page: Int = 1, perPage: Int = 10, order: OrderType = .descending, categories: [Int]? = nil, categoriesToExclude: [Int]? = nil, tags: [Int]? = nil, tagsToExclude: [Int]? = nil, include: [Int]? = nil) throws -> WPClient { - try .init(.init(endpoint: WPEndpoint.Posts.posts.path, parameters: .createParamsForPosts(page: page, perPage: perPage, order: order, categories: categories, categoriesToExclude: categoriesToExclude, tags: tags, tagsToExclude: tagsToExclude, include: include))) - } - - public func getSearchPostsClient(term: String, page: Int = 1, perPage: Int = 10) throws -> WPClient { - try .init(.init(endpoint: WPEndpoint.Posts.search.path, parameters: .createParamsForSearchPosts(term: term, page: page, perPage: perPage))) - } - - public func getPostClient(by id: Int) throws -> WPClient { - try .init(.init(endpoint: WPEndpoint.Posts.post(id: id).path, parameters: .createParamsForPost())) - } - - public func createPostClient(by post: PostToCreate) throws -> WPClient { - try .init(.init(endpoint: WPEndpoint.Posts.posts.path, method: .post, requestModel: post)) - } - - public func updatePostClient(by id: Int, post: PostToUpdate) throws -> WPClient { - try .init(.init(endpoint: WPEndpoint.Posts.post(id: id).path, method: .post, requestModel: post)) - } -} diff --git a/Tests/WPSwiftTests/APITests/APIClientTests.swift b/Tests/WPSwiftTests/APITests/APIClientTests.swift index a64d74f..485bc3b 100644 --- a/Tests/WPSwiftTests/APITests/APIClientTests.swift +++ b/Tests/WPSwiftTests/APITests/APIClientTests.swift @@ -32,14 +32,14 @@ final class APIClientTests: XCTestCase { func testURL() throws { WPSwift.initialize(route: "https://www.example.com/wp-json", namespace: "wp/v2") - let url = try String.initialize(with: "") + let url = try String.initialize(with: .endpoint("")) XCTAssertEqual(url, "https://www.example.com/wp-json/wp/v2/") } func testURLWithFailure() throws { WPSwift.initialize(route: "http://\u{FFFD}\u{FFFE}", namespace: "wp/v2") do { - _ = try String.initialize(with: "") + _ = try String.initialize(with: .endpoint("")) XCTAssert(false, "URL initialize should have been failed.") } catch NetworkError.urlMalformed { XCTAssertEqual(NetworkError.urlMalformed.errorDescription, "URL is malformed.", "Network error message does not match.") diff --git a/Tests/WPSwiftTests/RepositoryTests/PostsRepositoryTests.swift b/Tests/WPSwiftTests/RepositoryTests/PostsRepositoryTests.swift index 9a44284..e7a494f 100644 --- a/Tests/WPSwiftTests/RepositoryTests/PostsRepositoryTests.swift +++ b/Tests/WPSwiftTests/RepositoryTests/PostsRepositoryTests.swift @@ -36,64 +36,58 @@ final class CategoriessRepositoryTests: XCTestCase { let postFromData = postsFromData[0] let post = posts[0] - // Assertions for all fields - XCTAssertEqual(post.id, postFromData.id) - XCTAssertEqual(post.date, postFromData.date) - XCTAssertEqual(post.modified, postFromData.modified) - XCTAssertEqual(post.status, postFromData.status) - XCTAssertEqual(post.title.rendered, postFromData.title.rendered) - XCTAssertEqual(post.content.rendered, postFromData.content.rendered) - XCTAssertEqual(post.excerpt.rendered, postFromData.excerpt.rendered) - XCTAssertEqual(post.author, postFromData.author) - XCTAssertEqual(post.featured_media, postFromData.featured_media) - XCTAssertEqual(post.comment_status, postFromData.comment_status) - - XCTAssertNotNil(post.categories) - XCTAssertNotNil(postFromData.categories) - - XCTAssertNotNil(post.tags) - XCTAssertNotNil(postFromData.tags) + test(post: post, with: postFromData) + } + + func testGetPostsWithBaseURL() async throws { + MockedURLProtocol.observer = { request -> (URLResponse?, Data?) in + let response = HTTPURLResponse(url: URL(string: WPEndpoint.Posts.posts.path)!, statusCode: 200, httpVersion: nil, headerFields: nil) + return (response, try [Post].mockData) + } + + let repository = PostsRepository() + let postsFromData = try await repository.getPostsClient(baseURL: "https://www.example.com/wp-json/wp/v2", categories: [0], categoriesToExclude: [1], tags: [0], tagsToExclude: [1]).fetch() + XCTAssert(!postsFromData.isEmpty, "Posts from json file looks empty!") - let authorFromData = postFromData.embeddedContent.author! - let author = post.embeddedContent.author! + let posts: [Post] = .mock + XCTAssert(!posts.isEmpty, "Posts from mock looks empty!") - // Assertions for author - XCTAssertEqual(author.id, authorFromData.id) - XCTAssertEqual(author.name, authorFromData.name) - XCTAssertEqual(author.description, authorFromData.description) + let postFromData = postsFromData[0] + let post = posts[0] - let featuredMediaFromData = postFromData.embeddedContent.featuredMedia! - let featuredMedia = post.embeddedContent.featuredMedia! + test(post: post, with: postFromData) + } + + func testSearchPosts() async throws { + MockedURLProtocol.observer = { request -> (URLResponse?, Data?) in + let response = HTTPURLResponse(url: URL(string: WPEndpoint.Posts.search.path)!, statusCode: 200, httpVersion: nil, headerFields: nil) + return (response, try [SimplePost].mockData) + } + + let repository = PostsRepository() + let postsFromData = try await repository.getSearchPostsClient(term: "term").fetch() + XCTAssert(!postsFromData.isEmpty, "Posts from json file looks empty!") - // Assertions for featured media - XCTAssertEqual(featuredMedia.id, featuredMediaFromData.id) - XCTAssertEqual(featuredMedia.sourceURL, featuredMediaFromData.sourceURL) - XCTAssertEqual(featuredMedia.caption.rendered, featuredMediaFromData.caption.rendered) - XCTAssertEqual(featuredMedia.altText, featuredMediaFromData.altText) - XCTAssertEqual(featuredMedia.mediaDetails.width, featuredMediaFromData.mediaDetails.width) - XCTAssertEqual(featuredMedia.mediaDetails.height, featuredMediaFromData.mediaDetails.height) - XCTAssertEqual(featuredMedia.thumbnailURL, featuredMediaFromData.thumbnailURL) + let posts: [SimplePost] = .mock + XCTAssert(!posts.isEmpty, "Posts from mock looks empty!") - XCTAssertEqual(featuredMedia.mediaDetails.height, featuredMediaFromData.mediaDetails.height) - XCTAssertEqual(featuredMedia.mediaDetails.width, featuredMediaFromData.mediaDetails.width) - XCTAssertEqual(featuredMedia.mediaDetails.sizes.count, featuredMediaFromData.mediaDetails.sizes.count) - let thumbnailFromData = featuredMediaFromData.mediaDetails.sizes["thumbnail"]! - let thumbnail = featuredMedia.mediaDetails.sizes["thumbnail"]! + let postFromData = postsFromData[0] + let post = posts[0] - // Assertions for thumbnail - XCTAssertEqual(thumbnail.width, thumbnailFromData.width) - XCTAssertEqual(thumbnail.height, thumbnailFromData.height) - XCTAssertEqual(thumbnail.sourceURL, thumbnailFromData.sourceURL) + // Assertions for all fields + XCTAssertEqual(post.id, postFromData.id) + XCTAssertEqual(post.title, postFromData.title) + XCTAssertEqual(post.link, postFromData.link) } - func testSearchPosts() async throws { + func testSearchPostsWithBaseURL() async throws { MockedURLProtocol.observer = { request -> (URLResponse?, Data?) in let response = HTTPURLResponse(url: URL(string: WPEndpoint.Posts.search.path)!, statusCode: 200, httpVersion: nil, headerFields: nil) return (response, try [SimplePost].mockData) } let repository = PostsRepository() - let postsFromData = try await repository.getSearchPostsClient(term: "term").fetch() + let postsFromData = try await repository.getSearchPostsClient(baseURL: "https://www.example.com/wp-json/wp/v2", term: "term").fetch() XCTAssert(!postsFromData.isEmpty, "Posts from json file looks empty!") let posts: [SimplePost] = .mock @@ -118,54 +112,20 @@ final class CategoriessRepositoryTests: XCTestCase { let postFromData = try await repository.getPostClient(by: 1234).fetch() let post: Post = .mock - // Assertions for all fields - XCTAssertEqual(post.id, postFromData.id) - XCTAssertEqual(post.date, postFromData.date) - XCTAssertEqual(post.modified, postFromData.modified) - XCTAssertEqual(post.status, postFromData.status) - XCTAssertEqual(post.title.rendered, postFromData.title.rendered) - XCTAssertEqual(post.content.rendered, postFromData.content.rendered) - XCTAssertEqual(post.excerpt.rendered, postFromData.excerpt.rendered) - XCTAssertEqual(post.author, postFromData.author) - XCTAssertEqual(post.featured_media, postFromData.featured_media) - XCTAssertEqual(post.comment_status, postFromData.comment_status) - - XCTAssertNotNil(post.categories) - XCTAssertNotNil(postFromData.categories) + test(post: post, with: postFromData) + } + + func testGetPostWithBaseURL() async throws { + MockedURLProtocol.observer = { request -> (URLResponse?, Data?) in + let response = HTTPURLResponse(url: URL(string: WPEndpoint.Posts.posts.path)!, statusCode: 200, httpVersion: nil, headerFields: nil) + return (response, try Post.mockData) + } - XCTAssertNotNil(post.tags) - XCTAssertNotNil(postFromData.tags) - - let authorFromData = postFromData.embeddedContent.author! - let author = post.embeddedContent.author! - - // Assertions for author - XCTAssertEqual(author.id, authorFromData.id) - XCTAssertEqual(author.name, authorFromData.name) - XCTAssertEqual(author.description, authorFromData.description) - - let featuredMediaFromData = postFromData.embeddedContent.featuredMedia! - let featuredMedia = post.embeddedContent.featuredMedia! - - // Assertions for featured media - XCTAssertEqual(featuredMedia.id, featuredMediaFromData.id) - XCTAssertEqual(featuredMedia.sourceURL, featuredMediaFromData.sourceURL) - XCTAssertEqual(featuredMedia.caption.rendered, featuredMediaFromData.caption.rendered) - XCTAssertEqual(featuredMedia.altText, featuredMediaFromData.altText) - XCTAssertEqual(featuredMedia.mediaDetails.width, featuredMediaFromData.mediaDetails.width) - XCTAssertEqual(featuredMedia.mediaDetails.height, featuredMediaFromData.mediaDetails.height) - XCTAssertEqual(featuredMedia.thumbnailURL, featuredMediaFromData.thumbnailURL) - - XCTAssertEqual(featuredMedia.mediaDetails.height, featuredMediaFromData.mediaDetails.height) - XCTAssertEqual(featuredMedia.mediaDetails.width, featuredMediaFromData.mediaDetails.width) - XCTAssertEqual(featuredMedia.mediaDetails.sizes.count, featuredMediaFromData.mediaDetails.sizes.count) - let thumbnailFromData = featuredMediaFromData.mediaDetails.sizes["thumbnail"]! - let thumbnail = featuredMedia.mediaDetails.sizes["thumbnail"]! + let repository = PostsRepository() + let postFromData = try await repository.getPostClient(baseURL: "https://www.example.com/wp-json/wp/v2", by: 1234).fetch() + let post: Post = .mock - // Assertions for thumbnail - XCTAssertEqual(thumbnail.width, thumbnailFromData.width) - XCTAssertEqual(thumbnail.height, thumbnailFromData.height) - XCTAssertEqual(thumbnail.sourceURL, thumbnailFromData.sourceURL) + test(post: post, with: postFromData) } func testGetPostWithNoEmbedded() async throws { @@ -231,6 +191,36 @@ final class CategoriessRepositoryTests: XCTestCase { XCTAssertNotNil(postFromData.embeddedContent.author) XCTAssertNotNil(postFromData.embeddedContent.featuredMedia) } + + func testCreatingPostWithBaseURL() async throws { + MockedURLProtocol.observer = { request -> (URLResponse?, Data?) in + let response = HTTPURLResponse(url: URL(string: WPEndpoint.Posts.posts.path)!, statusCode: 200, httpVersion: nil, headerFields: nil) + return (response, try Post.mockData) + } + + let post: PostToCreate = .mock + + let repository = PostsRepository() + let postFromData = try await repository.createPostClient(baseURL: "https://www.example.com/wp-json/wp/v2", by: post).fetch() + + // Assertions for all fields + XCTAssertNotNil(postFromData.date) + XCTAssertNotNil(postFromData.modified) + XCTAssertEqual(post.status, postFromData.status) + XCTAssertEqual(post.title.rendered, postFromData.title.rendered) + XCTAssertEqual(post.content.rendered, postFromData.content.rendered) + XCTAssertEqual(post.excerpt?.rendered, postFromData.excerpt.rendered) + XCTAssertEqual(post.comment_status, postFromData.comment_status) + + XCTAssertNotNil(post.categories) + XCTAssertNotNil(postFromData.categories) + + XCTAssertNotNil(post.tags) + XCTAssertNotNil(postFromData.tags) + + XCTAssertNotNil(postFromData.embeddedContent.author) + XCTAssertNotNil(postFromData.embeddedContent.featuredMedia) + } func testUpdatingPost() async throws { MockedURLProtocol.observer = { request -> (URLResponse?, Data?) in @@ -262,5 +252,87 @@ final class CategoriessRepositoryTests: XCTestCase { XCTAssertNotNil(postFromData.embeddedContent.author) XCTAssertNotNil(postFromData.embeddedContent.featuredMedia) } + + func testUpdatingPostWithBaseURL() async throws { + MockedURLProtocol.observer = { request -> (URLResponse?, Data?) in + let response = HTTPURLResponse(url: URL(string: WPEndpoint.Posts.posts.path)!, statusCode: 200, httpVersion: nil, headerFields: nil) + return (response, try Post.mockData) + } + + let post: PostToUpdate = .mock + + let repository = PostsRepository() + let postFromData = try await repository.updatePostClient(baseURL: "https://www.example.com/wp-json/wp/v2", by: 1234, post: post).fetch() + + // Assertions for all fields + XCTAssertEqual(post.id, postFromData.id) + XCTAssertNotNil(postFromData.date) + XCTAssertNotNil(postFromData.modified) + XCTAssertEqual(post.status, postFromData.status) + XCTAssertEqual(post.title.rendered, postFromData.title.rendered) + XCTAssertEqual(post.content.rendered, postFromData.content.rendered) + XCTAssertEqual(post.excerpt?.rendered, postFromData.excerpt.rendered) + XCTAssertEqual(post.comment_status, postFromData.comment_status) + + XCTAssertNotNil(post.categories) + XCTAssertNotNil(postFromData.categories) + + XCTAssertNotNil(post.tags) + XCTAssertNotNil(postFromData.tags) + + XCTAssertNotNil(postFromData.embeddedContent.author) + XCTAssertNotNil(postFromData.embeddedContent.featuredMedia) + } + + private func test(post: Post, with postFromData: Post) { + // Assertions for all fields + XCTAssertEqual(post.id, postFromData.id) + XCTAssertEqual(post.date, postFromData.date) + XCTAssertEqual(post.modified, postFromData.modified) + XCTAssertEqual(post.status, postFromData.status) + XCTAssertEqual(post.title.rendered, postFromData.title.rendered) + XCTAssertEqual(post.content.rendered, postFromData.content.rendered) + XCTAssertEqual(post.excerpt.rendered, postFromData.excerpt.rendered) + XCTAssertEqual(post.author, postFromData.author) + XCTAssertEqual(post.featured_media, postFromData.featured_media) + XCTAssertEqual(post.comment_status, postFromData.comment_status) + + XCTAssertNotNil(post.categories) + XCTAssertNotNil(postFromData.categories) + + XCTAssertNotNil(post.tags) + XCTAssertNotNil(postFromData.tags) + + let authorFromData = postFromData.embeddedContent.author! + let author = post.embeddedContent.author! + + // Assertions for author + XCTAssertEqual(author.id, authorFromData.id) + XCTAssertEqual(author.name, authorFromData.name) + XCTAssertEqual(author.description, authorFromData.description) + + let featuredMediaFromData = postFromData.embeddedContent.featuredMedia! + let featuredMedia = post.embeddedContent.featuredMedia! + + // Assertions for featured media + XCTAssertEqual(featuredMedia.id, featuredMediaFromData.id) + XCTAssertEqual(featuredMedia.sourceURL, featuredMediaFromData.sourceURL) + XCTAssertEqual(featuredMedia.caption.rendered, featuredMediaFromData.caption.rendered) + XCTAssertEqual(featuredMedia.altText, featuredMediaFromData.altText) + XCTAssertEqual(featuredMedia.mediaDetails.width, featuredMediaFromData.mediaDetails.width) + XCTAssertEqual(featuredMedia.mediaDetails.height, featuredMediaFromData.mediaDetails.height) + XCTAssertEqual(featuredMedia.thumbnailURL, featuredMediaFromData.thumbnailURL) + + XCTAssertEqual(featuredMedia.mediaDetails.height, featuredMediaFromData.mediaDetails.height) + XCTAssertEqual(featuredMedia.mediaDetails.width, featuredMediaFromData.mediaDetails.width) + XCTAssertEqual(featuredMedia.mediaDetails.sizes.count, featuredMediaFromData.mediaDetails.sizes.count) + let thumbnailFromData = featuredMediaFromData.mediaDetails.sizes["thumbnail"]! + let thumbnail = featuredMedia.mediaDetails.sizes["thumbnail"]! + + // Assertions for thumbnail + XCTAssertEqual(thumbnail.width, thumbnailFromData.width) + XCTAssertEqual(thumbnail.height, thumbnailFromData.height) + XCTAssertEqual(thumbnail.sourceURL, thumbnailFromData.sourceURL) + } }