From 9c65e8d7099c0e47244262c8f5b4d2beb97ac9fa Mon Sep 17 00:00:00 2001 From: Rudrank Date: Sat, 25 Sep 2021 09:08:30 +0530 Subject: [PATCH] Add tests for search --- README.md | 2 +- Sources/QuoteKit/Endpoint/URLQueryItem.swift | 6 +-- Sources/QuoteKit/Model/Quote.swift | 8 ++- Sources/QuoteKit/Model/TagType.swift | 41 --------------- Sources/QuoteKit/Model/Tags.swift | 4 ++ Sources/QuoteKit/QuoteKit.swift | 10 ++-- .../Authors/AuthorsDataTests.swift | 52 +++++++++++++++++++ ...rsURLTests.swift => AuthorsURLTests.swift} | 6 +-- .../Authors/QuoteKitAuthorsDataTests.swift | 27 ---------- ...sDataTests.swift => QuotesDataTests.swift} | 26 ++++++++-- ...tesURLTests.swift => QuotesURLTests.swift} | 10 ++-- ...LTests.swift => RandomQuoteURLTests.swift} | 10 ++-- 12 files changed, 104 insertions(+), 98 deletions(-) delete mode 100644 Sources/QuoteKit/Model/TagType.swift create mode 100644 Tests/QuoteKitTests/Authors/AuthorsDataTests.swift rename Tests/QuoteKitTests/Authors/{QuoteKitAuthorsURLTests.swift => AuthorsURLTests.swift} (95%) delete mode 100644 Tests/QuoteKitTests/Authors/QuoteKitAuthorsDataTests.swift rename Tests/QuoteKitTests/Quotes/{QuoteKitQuotesDataTests.swift => QuotesDataTests.swift} (52%) rename Tests/QuoteKitTests/Quotes/{QuoteKitQuotesURLTests.swift => QuotesURLTests.swift} (94%) rename Tests/QuoteKitTests/Quotes/{QuoteKitRandomQuoteURLTests.swift => RandomQuoteURLTests.swift} (90%) diff --git a/README.md b/README.md index fb8180f..ba12f4b 100644 --- a/README.md +++ b/README.md @@ -309,7 +309,7 @@ The object represents a single quote. You can get the content of the quote using ```swift struct Quote: Decodable, Identifiable { var id: String - var tags: [TagType] + var tags: [String] var content: String var author: String var authorSlug: String diff --git a/Sources/QuoteKit/Endpoint/URLQueryItem.swift b/Sources/QuoteKit/Endpoint/URLQueryItem.swift index 3299940..e54ff57 100644 --- a/Sources/QuoteKit/Endpoint/URLQueryItem.swift +++ b/Sources/QuoteKit/Endpoint/URLQueryItem.swift @@ -16,14 +16,14 @@ public extension URLQueryItem { URLQueryItem(name: "minLength", value: String(length)) } - static func tags(_ tags: [TagType], _ type: URLQueryItemListType = .all) -> Self { + static func tags(_ tags: [String], _ type: URLQueryItemListType = .all) -> Self { var tagsValue = "" switch type { case .all: - tagsValue = tags.map { $0.rawValue }.joined(separator: ",") + tagsValue = tags.map { $0 }.joined(separator: ",") case .either: - tagsValue = tags.map { $0.rawValue }.joined(separator: "|") + tagsValue = tags.map { $0 }.joined(separator: "|") } return URLQueryItem(name: "tags", value: tagsValue) diff --git a/Sources/QuoteKit/Model/Quote.swift b/Sources/QuoteKit/Model/Quote.swift index 883df08..d2c8ead 100644 --- a/Sources/QuoteKit/Model/Quote.swift +++ b/Sources/QuoteKit/Model/Quote.swift @@ -9,7 +9,7 @@ import Foundation public struct Quote: Decodable, Identifiable, Equatable { public var id: String - public var tags: [TagType] + public var tags: [String] public var content: String public var author: String public var authorSlug: String @@ -22,7 +22,7 @@ public struct Quote: Decodable, Identifiable, Equatable { case tags, content, author, authorSlug, length, dateAdded, dateModified } - public init(id: String, tags: [TagType], content: String, author: String, authorSlug: String, length: Int, dateAdded: String, dateModified: String) { + public init(id: String, tags: [String], content: String, author: String, authorSlug: String, length: Int, dateAdded: String, dateModified: String) { self.id = id self.tags = tags self.content = content @@ -33,3 +33,7 @@ public struct Quote: Decodable, Identifiable, Equatable { self.dateModified = dateModified } } + +extension Quote { + public static var preview = Quote(id: UUID().uuidString, tags: ["wisdom"], content: "Financial freedom is all about doing what you really want and not worry about money at all.", author: "Rudrank Riyam", authorSlug: "rudrank-riyam", length: 61, dateAdded: String(describing: Date()), dateModified: String(describing: Date())) +} diff --git a/Sources/QuoteKit/Model/TagType.swift b/Sources/QuoteKit/Model/TagType.swift deleted file mode 100644 index c6d5a22..0000000 --- a/Sources/QuoteKit/Model/TagType.swift +++ /dev/null @@ -1,41 +0,0 @@ -// -// TagType.swift -// TagType -// -// Created by Rudrank Riyam on 10/09/21. -// - -import Foundation - -public enum TagType: String, Decodable { - case business - case civilRights = "civil-rights" - case education - case faith - case famousQuotes = "famous-quotes" - case friendship - case future - case film - case happiness - case history - case inspirational - case life - case literature - case love - case nature - case politics - case proverb - case religion - case science - case success - case technology - case wisdom - - public var name: String { - switch self { - case .civilRights: return "Civil Rights" - case .famousQuotes: return "Famous Quotes" - default: return rawValue.capitalized - } - } -} diff --git a/Sources/QuoteKit/Model/Tags.swift b/Sources/QuoteKit/Model/Tags.swift index 5d4d413..2aa6de8 100644 --- a/Sources/QuoteKit/Model/Tags.swift +++ b/Sources/QuoteKit/Model/Tags.swift @@ -23,4 +23,8 @@ public struct Tag: Decodable, Identifiable { case v = "__v" case quoteCount } + + public var capitalisedName: String { + name.capitalized.replacingOccurrences(of: "-", with: " ") + } } diff --git a/Sources/QuoteKit/QuoteKit.swift b/Sources/QuoteKit/QuoteKit.swift index 15c50fc..838e52f 100644 --- a/Sources/QuoteKit/QuoteKit.swift +++ b/Sources/QuoteKit/QuoteKit.swift @@ -9,11 +9,7 @@ import Foundation public struct QuoteKit { static func execute(with endpoint: QuotableEndpoint) async throws -> Model { - let url = endpoint.url - - debugPrint("THE URL IS \(url)") - - let (data, _) = try await URLSession.shared.data(from: url) + let (data, _) = try await URLSession.shared.data(from: endpoint.url) return try JSONDecoder().decode(Model.self, from: data) } } @@ -26,7 +22,7 @@ public extension QuoteKit { static func quotes(minLength: Int? = nil, maxLength: Int? = nil, - tags: [TagType]? = nil, + tags: [String]? = nil, type: URLQueryItemListType = .all, authors: [String]? = nil, sortBy: QuotesSortType? = nil, @@ -72,7 +68,7 @@ public extension QuoteKit { static func randomQuote(minLength: Int? = nil, maxLength: Int? = nil, - tags: [TagType]? = nil, + tags: [String]? = nil, type: URLQueryItemListType = .all, authors: [String]? = nil) async throws -> Quote? { diff --git a/Tests/QuoteKitTests/Authors/AuthorsDataTests.swift b/Tests/QuoteKitTests/Authors/AuthorsDataTests.swift new file mode 100644 index 0000000..9ec2930 --- /dev/null +++ b/Tests/QuoteKitTests/Authors/AuthorsDataTests.swift @@ -0,0 +1,52 @@ +// +// AuthorsDataTests.swift +// AuthorsDataTests +// +// Created by Rudrank Riyam on 30/08/21. +// + +import XCTest +@testable import QuoteKit + +final class AuthorsDataTests: XCTestCase { + func testAuthorMatchesParticularID() async throws { + do { + let author = try await QuoteKit.author(id: "XYxYtSeixS-o") + let unwrappedAuthor = try XCTUnwrap(author) + + XCTAssertEqual(unwrappedAuthor.link, "https://en.wikipedia.org/wiki/Aesop") + XCTAssertEqual(unwrappedAuthor.bio, "Aesop (c. 620 – 564 BCE) was a Greek fabulist and storyteller credited with a number of fables now collectively known as Aesop's Fables.") + XCTAssertEqual(unwrappedAuthor.name, "Aesop") + XCTAssertEqual(unwrappedAuthor.slug, "aesop") + XCTAssertEqual(unwrappedAuthor.description, "Ancient Greek storyteller") + XCTAssertEqual(unwrappedAuthor.quoteCount, 2) + } catch { + XCTFail("Expected author, but failed \(error).") + } + } + + func testAuthorsReturnsManyAuthors() async throws { + do { + let authors = try await QuoteKit.authors() + let unwrappedAuthors = try XCTUnwrap(authors) + + XCTAssertGreaterThan(unwrappedAuthors.count, 1) + } catch { + XCTFail("Expected authors, but failed \(error).") + } + } + + func testAuthorsSearchForParticularQuery() async throws { + do { + let authors = try await QuoteKit.searchAuthors(for: "aesop") + let unwrappedAuthor = try XCTUnwrap(authors?.results.first) + + XCTAssertEqual(unwrappedAuthor.link, "https://en.wikipedia.org/wiki/Aesop") + XCTAssertEqual(unwrappedAuthor.bio, "Aesop (c. 620 – 564 BCE) was a Greek fabulist and storyteller credited with a number of fables now collectively known as Aesop's Fables.") + XCTAssertEqual(unwrappedAuthor.name, "Aesop") + XCTAssertEqual(unwrappedAuthor.slug, "aesop") + XCTAssertEqual(unwrappedAuthor.description, "Ancient Greek storyteller") + XCTAssertEqual(unwrappedAuthor.quoteCount, 2) + } + } +} diff --git a/Tests/QuoteKitTests/Authors/QuoteKitAuthorsURLTests.swift b/Tests/QuoteKitTests/Authors/AuthorsURLTests.swift similarity index 95% rename from Tests/QuoteKitTests/Authors/QuoteKitAuthorsURLTests.swift rename to Tests/QuoteKitTests/Authors/AuthorsURLTests.swift index ef5e42f..53cec35 100644 --- a/Tests/QuoteKitTests/Authors/QuoteKitAuthorsURLTests.swift +++ b/Tests/QuoteKitTests/Authors/AuthorsURLTests.swift @@ -1,6 +1,6 @@ // -// QuoteKitAuthorsURLTests.swift -// QuoteKitAuthorsURLTests +// AuthorsURLTests.swift +// AuthorsURLTests // // Created by Rudrank Riyam on 30/08/21. // @@ -8,7 +8,7 @@ import XCTest @testable import QuoteKit -final class QuoteKitAuthorsURLTests: XCTestCase { +final class AuthorsURLTests: XCTestCase { let host = QuotableURLHost.production func testURLForParticularID() { diff --git a/Tests/QuoteKitTests/Authors/QuoteKitAuthorsDataTests.swift b/Tests/QuoteKitTests/Authors/QuoteKitAuthorsDataTests.swift deleted file mode 100644 index e19e676..0000000 --- a/Tests/QuoteKitTests/Authors/QuoteKitAuthorsDataTests.swift +++ /dev/null @@ -1,27 +0,0 @@ -// -// QuoteKitAuthorsDataTests.swift -// QuoteKitAuthorsDataTests -// -// Created by Rudrank Riyam on 30/08/21. -// - -import XCTest -@testable import QuoteKit - -final class QuoteKitAuthorsDataTests: XCTestCase { - func testAuthorMatchesParticularID() async throws { - do { - let author = try await QuoteKit.author(id: "XYxYtSeixS-o") - let unwrappedAuthor = try XCTUnwrap(author) - - XCTAssertEqual(unwrappedAuthor.link, "https://en.wikipedia.org/wiki/Aesop") - XCTAssertEqual(unwrappedAuthor.bio, "Aesop (c. 620 – 564 BCE) was a Greek fabulist and storyteller credited with a number of fables now collectively known as Aesop's Fables.") - XCTAssertEqual(unwrappedAuthor.name, "Aesop") - XCTAssertEqual(unwrappedAuthor.slug, "aesop") - XCTAssertEqual(unwrappedAuthor.description, "Ancient Greek storyteller") - XCTAssertEqual(unwrappedAuthor.quoteCount, 2) - } catch { - XCTFail("Expected author, but failed \(error).") - } - } -} diff --git a/Tests/QuoteKitTests/Quotes/QuoteKitQuotesDataTests.swift b/Tests/QuoteKitTests/Quotes/QuotesDataTests.swift similarity index 52% rename from Tests/QuoteKitTests/Quotes/QuoteKitQuotesDataTests.swift rename to Tests/QuoteKitTests/Quotes/QuotesDataTests.swift index aa7c1b4..1c28948 100644 --- a/Tests/QuoteKitTests/Quotes/QuoteKitQuotesDataTests.swift +++ b/Tests/QuoteKitTests/Quotes/QuotesDataTests.swift @@ -1,6 +1,6 @@ // -// QuoteKitQuotesDataTests.swift -// QuoteKitQuotesDataTests +// QuotesDataTests.swift +// QuotesDataTests // // Created by Rudrank Riyam on 30/08/21. // @@ -8,13 +8,13 @@ import XCTest @testable import QuoteKit -final class QuoteKitQuotesDataTests: XCTestCase { +final class QuotesDataTests: XCTestCase { func testQuoteForParticularID() async throws { do { let quote = try await QuoteKit.quote(id: "2xpHvSOQMD") let unwrappedQuote = try XCTUnwrap(quote) - XCTAssertEqual(unwrappedQuote.tags, [.famousQuotes, .inspirational]) + XCTAssertEqual(unwrappedQuote.tags, ["famous-quotes", "inspirational"]) XCTAssertEqual(unwrappedQuote.id, "2xpHvSOQMD") XCTAssertEqual(unwrappedQuote.author, "Helmut Schmidt") XCTAssertEqual(unwrappedQuote.content, "The biggest room in the world is room for improvement.") @@ -37,4 +37,22 @@ final class QuoteKitQuotesDataTests: XCTestCase { XCTFail("Expected quotes, but failed \(error).") } } + + func testQuotesSearchForParticularQuery() async throws { + do { + let quotes = try await QuoteKit.searchQuotes(for: "biggest room") + let unwrappedQuote = try XCTUnwrap(quotes?.results.first) + + XCTAssertEqual(unwrappedQuote.tags, ["famous-quotes", "inspirational"]) + XCTAssertEqual(unwrappedQuote.id, "2xpHvSOQMD") + XCTAssertEqual(unwrappedQuote.author, "Helmut Schmidt") + XCTAssertEqual(unwrappedQuote.content, "The biggest room in the world is room for improvement.") + XCTAssertEqual(unwrappedQuote.authorSlug, "helmut-schmidt") + XCTAssertEqual(unwrappedQuote.length, 54) + XCTAssertEqual(unwrappedQuote.dateAdded, "2021-06-18") + XCTAssertEqual(unwrappedQuote.dateModified, "2021-06-18") + } catch { + XCTFail("Expected quote, but failed \(error).") + } + } } diff --git a/Tests/QuoteKitTests/Quotes/QuoteKitQuotesURLTests.swift b/Tests/QuoteKitTests/Quotes/QuotesURLTests.swift similarity index 94% rename from Tests/QuoteKitTests/Quotes/QuoteKitQuotesURLTests.swift rename to Tests/QuoteKitTests/Quotes/QuotesURLTests.swift index e6f83bc..6915de0 100644 --- a/Tests/QuoteKitTests/Quotes/QuoteKitQuotesURLTests.swift +++ b/Tests/QuoteKitTests/Quotes/QuotesURLTests.swift @@ -1,6 +1,6 @@ // -// QuoteKitQuotesURLTests.swift -// QuoteKitQuotesURLTests +// QuotesURLTests.swift +// QuotesURLTests // // Created by Rudrank Riyam on 30/08/21. // @@ -8,7 +8,7 @@ import XCTest @testable import QuoteKit -final class QuoteKitQuotesURLTests: XCTestCase { +final class QuotesURLTests: XCTestCase { let host = QuotableURLHost.production func testURLForParticularID() { @@ -22,12 +22,12 @@ final class QuoteKitQuotesURLTests: XCTestCase { } func testURLWithEitherOfTheProvidedTagsParameter() { - let url = QuotableEndpoint(.quotes, queryItems: [.tags([.love, .happiness], .either)]).url + let url = QuotableEndpoint(.quotes, queryItems: [.tags(["love", "happiness"], .either)]).url try XCTAssertEqual(url, host.expectedURL(with: "quotes?tags=love|happiness")) } func testURLWithAllOfTheProvidedTagsParameter() { - let url = QuotableEndpoint(.quotes, queryItems: [.tags([.technology, .famousQuotes], .all)]).url + let url = QuotableEndpoint(.quotes, queryItems: [.tags(["technology", "famous-quotes"], .all)]).url try XCTAssertEqual(url, host.expectedURL(with: "quotes?tags=technology,famous-quotes")) } diff --git a/Tests/QuoteKitTests/Quotes/QuoteKitRandomQuoteURLTests.swift b/Tests/QuoteKitTests/Quotes/RandomQuoteURLTests.swift similarity index 90% rename from Tests/QuoteKitTests/Quotes/QuoteKitRandomQuoteURLTests.swift rename to Tests/QuoteKitTests/Quotes/RandomQuoteURLTests.swift index 49c1913..1e78c57 100644 --- a/Tests/QuoteKitTests/Quotes/QuoteKitRandomQuoteURLTests.swift +++ b/Tests/QuoteKitTests/Quotes/RandomQuoteURLTests.swift @@ -1,6 +1,6 @@ // -// QuoteKitRandomQuoteURLTests.swift -// QuoteKitRandomQuoteURLTests +// RandomQuoteURLTests.swift +// RandomQuoteURLTests // // Created by Rudrank Riyam on 30/08/21. // @@ -8,7 +8,7 @@ import XCTest @testable import QuoteKit -final class QuoteKitRandomQuoteURLTests: XCTestCase { +final class RandomQuoteURLTests: XCTestCase { let host = QuotableURLHost.production func testURLForRandomQuote() { @@ -32,12 +32,12 @@ final class QuoteKitRandomQuoteURLTests: XCTestCase { } func testURLWithEitherOfTheProvidedTagsParameter() { - let url = QuotableEndpoint(.randomQuote, queryItems: [.tags([.love, .happiness], .either)]).url + let url = QuotableEndpoint(.randomQuote, queryItems: [.tags(["love", "happiness"], .either)]).url try XCTAssertEqual(url, host.expectedURL(with: "random?tags=love|happiness")) } func testURLWithAllOfTheProvidedTagsParameter() { - let url = QuotableEndpoint(.randomQuote, queryItems: [.tags([.technology, .famousQuotes], .all)]).url + let url = QuotableEndpoint(.randomQuote, queryItems: [.tags(["technology", "famous-quotes"], .all)]).url try XCTAssertEqual(url, host.expectedURL(with: "random?tags=technology,famous-quotes")) }