Skip to content

Commit

Permalink
Add tests for search
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Sep 25, 2021
1 parent ea4da31 commit 9c65e8d
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 98 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Sources/QuoteKit/Endpoint/URLQueryItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions Sources/QuoteKit/Model/Quote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()))
}
41 changes: 0 additions & 41 deletions Sources/QuoteKit/Model/TagType.swift

This file was deleted.

4 changes: 4 additions & 0 deletions Sources/QuoteKit/Model/Tags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public struct Tag: Decodable, Identifiable {
case v = "__v"
case quoteCount
}

public var capitalisedName: String {
name.capitalized.replacingOccurrences(of: "-", with: " ")
}
}
10 changes: 3 additions & 7 deletions Sources/QuoteKit/QuoteKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import Foundation

public struct QuoteKit {
static func execute<Model: Decodable>(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)
}
}
Expand All @@ -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,
Expand Down Expand Up @@ -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? {

Expand Down
52 changes: 52 additions & 0 deletions Tests/QuoteKitTests/Authors/AuthorsDataTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// QuoteKitAuthorsURLTests.swift
// QuoteKitAuthorsURLTests
// AuthorsURLTests.swift
// AuthorsURLTests
//
// Created by Rudrank Riyam on 30/08/21.
//

import XCTest
@testable import QuoteKit

final class QuoteKitAuthorsURLTests: XCTestCase {
final class AuthorsURLTests: XCTestCase {
let host = QuotableURLHost.production

func testURLForParticularID() {
Expand Down
27 changes: 0 additions & 27 deletions Tests/QuoteKitTests/Authors/QuoteKitAuthorsDataTests.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//
// QuoteKitQuotesDataTests.swift
// QuoteKitQuotesDataTests
// QuotesDataTests.swift
// QuotesDataTests
//
// Created by Rudrank Riyam on 30/08/21.
//

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.")
Expand All @@ -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).")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// QuoteKitQuotesURLTests.swift
// QuoteKitQuotesURLTests
// QuotesURLTests.swift
// QuotesURLTests
//
// Created by Rudrank Riyam on 30/08/21.
//

import XCTest
@testable import QuoteKit

final class QuoteKitQuotesURLTests: XCTestCase {
final class QuotesURLTests: XCTestCase {
let host = QuotableURLHost.production

func testURLForParticularID() {
Expand All @@ -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"))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//
// QuoteKitRandomQuoteURLTests.swift
// QuoteKitRandomQuoteURLTests
// RandomQuoteURLTests.swift
// RandomQuoteURLTests
//
// Created by Rudrank Riyam on 30/08/21.
//

import XCTest
@testable import QuoteKit

final class QuoteKitRandomQuoteURLTests: XCTestCase {
final class RandomQuoteURLTests: XCTestCase {
let host = QuotableURLHost.production

func testURLForRandomQuote() {
Expand All @@ -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"))
}

Expand Down

0 comments on commit 9c65e8d

Please sign in to comment.