Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rocxteady committed Aug 1, 2024
1 parent 798c69c commit c62a4ac
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 125 deletions.
33 changes: 0 additions & 33 deletions Sources/WPSwift/Repositories/PostWithSourceRepository.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Tests/WPSwiftTests/APITests/APIClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
252 changes: 162 additions & 90 deletions Tests/WPSwiftTests/RepositoryTests/PostsRepositoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

}

0 comments on commit c62a4ac

Please sign in to comment.