Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions Tests/MeiliSearchIntegrationTests/SearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import XCTest
import Foundation

// swiftlint:disable force_unwrapping
// swiftlint:disable force_try
private let books: [Book] = [
Book(id: 123, title: "Pride and Prejudice", comment: "A great book", genres: ["Classic Regency nove"]),
Book(id: 456, title: "Le Petit Prince", comment: "A french book", genres: ["Novel"]),
Expand All @@ -15,20 +13,31 @@ private let books: [Book] = [
Book(id: 1844, title: "A Moreninha", comment: "A Book from Joaquim Manuel de Macedo", genres: ["Novel"])
]

// swiftlint:disable force_unwrapping
// swiftlint:disable force_try
private let nestedBooks: [NestedBook] = [
NestedBook(id: 123, title: "Pride and Prejudice", info: InfoNested(comment: "A great book", reviewNb: 100), genres: ["Classic Regency nove"]),
NestedBook(id: 456, title: "Le Petit Prince", info: InfoNested(comment: "A french book", reviewNb: 100), genres: ["Novel"]),
NestedBook(id: 2, title: "Le Rouge et le Noir", info: InfoNested(comment: "Another french book", reviewNb: 100), genres: ["Bildungsroman"])
]

class SearchTests: XCTestCase {
private var client: MeiliSearch!
private var index: Indexes!
private var nestedIndex: Indexes!
private var session: URLSessionProtocol!
private let uid: String = "books_test"
private let nested_uid: String = "nested_books_test"

// MARK: Setup

override func setUp() {
super.setUp()

session = URLSession(configuration: .ephemeral)
client = try! MeiliSearch(host: "http://localhost:7700", apiKey: "masterKey", session: session)
session = URLSession(configuration: .ephemeral)
client = try! MeiliSearch(host: "http://localhost:7700", apiKey: "masterKey", session: session)
index = self.client.index(self.uid)
nestedIndex = self.client.index(self.nested_uid)

let addDocExpectation = XCTestExpectation(description: "Add documents")

Expand All @@ -43,10 +52,21 @@ class SearchTests: XCTestCase {
}
}
self.wait(for: [addDocExpectation], timeout: TESTS_TIME_OUT)
let addNestedDocExpectation = XCTestExpectation(description: "Add documents")
addDocuments(client: self.client, uid: self.nested_uid, dataset: nestedBooks, primaryKey: nil) { result in
switch result {
case .success:
addNestedDocExpectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to create index")
addNestedDocExpectation.fulfill()
}
}
self.wait(for: [addNestedDocExpectation], timeout: TESTS_TIME_OUT)
}

// MARK: Basic search

func testBasicSearch() {
let expectation = XCTestExpectation(description: "Search for Books with query")

Expand Down Expand Up @@ -76,6 +96,32 @@ class SearchTests: XCTestCase {
self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
}

// MARK: Nested search
func testNestedSearch() {
let expectation = XCTestExpectation(description: "Search in Nested Books")

typealias MeiliResult = Result<SearchResult<NestedBook>, Swift.Error>
let query = "A french book"

self.nestedIndex.search(SearchParameters(query: query)) { (result: MeiliResult) in
switch result {
case .success(let response):
if response.hits.count > 0 {
XCTAssertEqual("A french book", response.hits[0].info.comment)
} else {
XCTFail("Failed to find hits in the response")
}
expectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to search in nested books")
expectation.fulfill()
}
}

self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
}

func testBasicSearchWithNoQuery() {
let expectation = XCTestExpectation(description: "Search for Books without query")

Expand Down
40 changes: 40 additions & 0 deletions Tests/MeiliSearchIntegrationTests/Support/NestedBook.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public struct NestedBook: Codable, Equatable {
let id: Int
let title: String
let info: InfoNested
let genres: [String]?
let formatted: FormattedNestedBook?

enum CodingKeys: String, CodingKey {
case id
case title
case info
case genres
case formatted = "_formatted"
}

init(id: Int, title: String, info: InfoNested, genres: [String] = [], formatted: FormattedNestedBook? = nil) {
self.id = id
self.title = title
self.info = info
self.genres = genres
self.formatted = formatted
}
}

public struct InfoNested: Codable, Equatable {
let comment: String
let reviewNb: Int
}

public struct FormattedNestedBook: Codable, Equatable {
let id: String
let title: String
let info: InfoNested

init(id: String, title: String, info: InfoNested) {
self.id = id
self.title = title
self.info = info
}
}