diff --git a/Sources/MeiliSearch/Model/Setting.swift b/Sources/MeiliSearch/Model/Setting.swift index 92e7f3fa..b9b52017 100644 --- a/Sources/MeiliSearch/Model/Setting.swift +++ b/Sources/MeiliSearch/Model/Setting.swift @@ -26,6 +26,9 @@ public struct Setting: Codable, Equatable { /// Optional distinct attribute set for a given `Index`. public let distinctAttribute: String? + /// List of attributes used for the faceting + public let attributesForFaceting: [String] + } extension Setting { @@ -39,6 +42,7 @@ extension Setting { stopWords = (try? values?.decodeIfPresent([String].self, forKey: .stopWords)) ?? [] synonyms = (try? values?.decodeIfPresent([String: [String]].self, forKey: .synonyms)) ?? [:] distinctAttribute = try? values?.decodeIfPresent(String.self, forKey: .distinctAttribute) + attributesForFaceting = (try? values?.decodeIfPresent([String].self, forKey: .attributesForFaceting)) ?? [] } } diff --git a/Tests/MeiliSearchIntegrationTests/SettingsTests.swift b/Tests/MeiliSearchIntegrationTests/SettingsTests.swift index b41d6279..b84457ac 100644 --- a/Tests/MeiliSearchIntegrationTests/SettingsTests.swift +++ b/Tests/MeiliSearchIntegrationTests/SettingsTests.swift @@ -6,6 +6,21 @@ class SettingsTests: XCTestCase { private var client: MeiliSearch! private let uid: String = "books_test" + private let defaultRankingRules: [String] = [ + "typo", + "words", + "proximity", + "attribute", + "wordsPosition", + "exactness" + ] + private let defaultDistinctAttribute: String? = nil + private let defaultDisplayedAttributes: [String] = ["*"] + private let defaultSearchableAttributes: [String] = ["*"] + private let defaultAttributesForFaceting: [String] = [] + private let defaultStopWords: [String] = [] + private let defaultSynonyms: [String: [String]] = [:] + private var defaultGlobalSettings: Setting? = nil // MARK: Setup @@ -35,6 +50,17 @@ class SettingsTests: XCTestCase { } self.wait(for: [expectation], timeout: 10.0) + + self.defaultGlobalSettings = Setting( + rankingRules: self.defaultRankingRules, + searchableAttributes: self.defaultSearchableAttributes, + displayedAttributes: self.defaultDisplayedAttributes, + stopWords: self.defaultStopWords, + synonyms: self.defaultSynonyms, + distinctAttribute: self.defaultDistinctAttribute, + attributesForFaceting: self.defaultAttributesForFaceting + ) + } // MARK: Attributes for faceting @@ -43,39 +69,18 @@ class SettingsTests: XCTestCase { let expectation = XCTestExpectation(description: "Get current attributes for faceting") - let attributesForFaceting: [String] = ["id", "title"] - - self.client.updateAttributesForFaceting(UID: self.uid, attributesForFaceting) { result in - + self.client.getAttributesForFaceting(UID: self.uid) { result in switch result { - case .success: - - Thread.sleep(forTimeInterval: 0.5) - - self.client.getAttributesForFaceting(UID: self.uid) { result in - switch result { - case .success(let attributes): + case .success(let attributes): - XCTAssertEqual(attributesForFaceting.count, attributes.count) + XCTAssertEqual(self.defaultAttributesForFaceting, attributes) - let lhs: [String] = attributesForFaceting.sorted() - let rhs: [String] = attributes.sorted() - - XCTAssertEqual(lhs, rhs) - - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - } + expectation.fulfill() case .failure(let error): print(error) XCTFail() } - } self.wait(for: [expectation], timeout: 1.0) @@ -85,10 +90,9 @@ class SettingsTests: XCTestCase { let expectation = XCTestExpectation(description: "Update settings for attributes for faceting") - let initialAttributesForFaceting: [String] = ["id", "title"] - - self.client.updateAttributesForFaceting(UID: self.uid, initialAttributesForFaceting) { result in + let newAttributesForFaceting: [String] = ["title"] + self.client.updateAttributesForFaceting(UID: self.uid, newAttributesForFaceting) { result in switch result { case .success: @@ -99,37 +103,9 @@ class SettingsTests: XCTestCase { switch result { case .success(let attributes): - XCTAssertEqual(initialAttributesForFaceting, attributes) - let newAttributesForFaceting: [String] = ["title"] + XCTAssertEqual(newAttributesForFaceting, attributes) - self.client.updateAttributesForFaceting(UID: self.uid, newAttributesForFaceting) { result in - switch result { - case .success: - - Thread.sleep(forTimeInterval: 0.5) - - self.client.getAttributesForFaceting(UID: self.uid) { result in - - switch result { - case .success(let attributes): - - XCTAssertNotEqual(initialAttributesForFaceting, attributes) - XCTAssertEqual(newAttributesForFaceting, attributes) - - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - - } - - case .failure(let error): - print(error) - XCTFail() - } - } + expectation.fulfill() case .failure(let error): print(error) @@ -163,7 +139,7 @@ class SettingsTests: XCTestCase { switch result { case .success(let attributes): - XCTAssertTrue(attributes.isEmpty) + XCTAssertEqual(self.defaultAttributesForFaceting, attributes) expectation.fulfill() case .failure(let error): @@ -188,40 +164,20 @@ class SettingsTests: XCTestCase { let expectation = XCTestExpectation(description: "Get current displayed attributes") - let displayedAttributes = ["id", "title"] - - self.client.updateDisplayedAttributes(UID: self.uid, displayedAttributes) { result in + self.client.getDisplayedAttributes(UID: self.uid) { result in switch result { - case .success: + case .success(let attributes): - Thread.sleep(forTimeInterval: TimeInterval(0.5)) - - self.client.getDisplayedAttributes(UID: self.uid) { result in - - switch result { - case .success(let attributes): - - XCTAssertEqual(displayedAttributes.count, attributes.count) - - let lhs: [String] = displayedAttributes.sorted() - let rhs: [String] = attributes.sorted() + XCTAssertEqual(self.defaultDisplayedAttributes, attributes) - XCTAssertEqual(lhs, rhs) - - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - - } + expectation.fulfill() case .failure(let error): print(error) XCTFail() } + } self.wait(for: [expectation], timeout: 1.0) @@ -231,10 +187,9 @@ class SettingsTests: XCTestCase { let expectation = XCTestExpectation(description: "Update settings for displayed attributes") - let initialDisplayedAttributes: [String] = ["id"] - - self.client.updateDisplayedAttributes(UID: self.uid, initialDisplayedAttributes) { result in + let newDisplayedAttributes: [String] = ["title"] + self.client.updateDisplayedAttributes(UID: self.uid, newDisplayedAttributes) { result in switch result { case .success: @@ -245,37 +200,9 @@ class SettingsTests: XCTestCase { switch result { case .success(let attributes): - XCTAssertEqual(initialDisplayedAttributes, attributes) - let newDisplayedAttributes: [String] = ["title"] - - self.client.updateDisplayedAttributes(UID: self.uid, newDisplayedAttributes) { result in - switch result { - case .success: - - Thread.sleep(forTimeInterval: TimeInterval(0.5)) + XCTAssertEqual(newDisplayedAttributes, attributes) - self.client.getDisplayedAttributes(UID: self.uid) { result in - - switch result { - case .success(let attributes): - - XCTAssertNotEqual(initialDisplayedAttributes, attributes) - XCTAssertEqual(newDisplayedAttributes, attributes) - - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - - } - - case .failure(let error): - print(error) - XCTFail() - } - } + expectation.fulfill() case .failure(let error): print(error) @@ -309,7 +236,7 @@ class SettingsTests: XCTestCase { switch result { case .success(let attribute): - XCTAssertEqual(["*"], attribute) + XCTAssertEqual(self.defaultDisplayedAttributes, attribute) expectation.fulfill() case .failure(let error): @@ -334,34 +261,20 @@ class SettingsTests: XCTestCase { let expectation = XCTestExpectation(description: "Get current distinct attribute") - let distinctAttribute = "product_id" - - self.client.updateDistinctAttribute(UID: self.uid, distinctAttribute) { result in + self.client.getDistinctAttribute(UID: self.uid) { result in switch result { - case .success: - - Thread.sleep(forTimeInterval: TimeInterval(0.5)) + case .success(let attribute): - self.client.getDistinctAttribute(UID: self.uid) { result in + XCTAssertEqual(self.defaultDistinctAttribute, attribute) - switch result { - case .success(let attribute): - - XCTAssertEqual(distinctAttribute, attribute) - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - - } + expectation.fulfill() case .failure(let error): print(error) XCTFail() } + } self.wait(for: [expectation], timeout: 1.0) @@ -371,10 +284,9 @@ class SettingsTests: XCTestCase { let expectation = XCTestExpectation(description: "Update settings for distinct attribute") - let intialDistinctAttribute = "id" - - self.client.updateDistinctAttribute(UID: self.uid, intialDistinctAttribute) { result in + let newDistinctAttribute: String = "title" + self.client.updateDistinctAttribute(UID: self.uid, newDistinctAttribute) { result in switch result { case .success: @@ -385,37 +297,9 @@ class SettingsTests: XCTestCase { switch result { case .success(let attribute): - XCTAssertEqual(intialDistinctAttribute, attribute) - let newDistinctAttribute: String = "title" - - self.client.updateDistinctAttribute(UID: self.uid, newDistinctAttribute) { result in - switch result { - case .success: - - Thread.sleep(forTimeInterval: TimeInterval(0.5)) - - self.client.getDistinctAttribute(UID: self.uid) { result in - - switch result { - case .success(let attribute): - - XCTAssertNotEqual(intialDistinctAttribute, attribute) - XCTAssertEqual(newDistinctAttribute, attribute) - - expectation.fulfill() + XCTAssertEqual(newDistinctAttribute, attribute) - case .failure(let error): - print(error) - XCTFail() - } - - } - - case .failure(let error): - print(error) - XCTFail() - } - } + expectation.fulfill() case .failure(let error): print(error) @@ -428,6 +312,7 @@ class SettingsTests: XCTestCase { print(error) XCTFail() } + } self.wait(for: [expectation], timeout: 10.0) @@ -449,7 +334,8 @@ class SettingsTests: XCTestCase { switch result { case .success(let attribute): - XCTAssertNil(attribute) + XCTAssertEqual(self.defaultDistinctAttribute, attribute) + expectation.fulfill() case .failure(let error): @@ -474,43 +360,18 @@ class SettingsTests: XCTestCase { let expectation = XCTestExpectation(description: "Get current ranking rules") - let newRankingRules: [String] = [ - "typo", - "words", - "proximity" - ] - - self.client.updateRankingRules(UID: self.uid, newRankingRules) { result in - + self.client.getRankingRules(UID: self.uid) { result in switch result { - case .success: - - Thread.sleep(forTimeInterval: 0.5) - - self.client.getRankingRules(UID: self.uid) { result in - switch result { - case .success(let rankingRules): - - XCTAssertEqual(newRankingRules.count, rankingRules.count) + case .success(let rankingRules): - let lhs: [String] = newRankingRules.sorted() - let rhs: [String] = rankingRules.sorted() + XCTAssertEqual(self.defaultRankingRules, rankingRules) - XCTAssertEqual(lhs, rhs) - - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - } + expectation.fulfill() case .failure(let error): print(error) XCTFail() } - } self.wait(for: [expectation], timeout: 1.0) @@ -520,14 +381,13 @@ class SettingsTests: XCTestCase { let expectation = XCTestExpectation(description: "Update settings for ranking rules") - let initialRankingRules: [String] = [ - "typo", + let newRankingRules: [String] = [ "words", + "typo", "proximity" ] - self.client.updateRankingRules(UID: self.uid, initialRankingRules) { result in - + self.client.updateRankingRules(UID: self.uid, newRankingRules) { result in switch result { case .success: @@ -538,41 +398,9 @@ class SettingsTests: XCTestCase { switch result { case .success(let rankingRules): - XCTAssertEqual(initialRankingRules, rankingRules) - let newRankingRules: [String] = [ - "words", - "typo", - "proximity" - ] - - self.client.updateRankingRules(UID: self.uid, newRankingRules) { result in - switch result { - case .success: - - Thread.sleep(forTimeInterval: 0.5) - - self.client.getRankingRules(UID: self.uid) { result in - - switch result { - case .success(let rankingRules): - - XCTAssertNotEqual(initialRankingRules, rankingRules) - XCTAssertEqual(newRankingRules, rankingRules) - - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - - } + XCTAssertEqual(newRankingRules, rankingRules) - case .failure(let error): - print(error) - XCTFail() - } - } + expectation.fulfill() case .failure(let error): print(error) @@ -606,7 +434,7 @@ class SettingsTests: XCTestCase { switch result { case .success(let rankingRules): - XCTAssertEqual(["typo", "words", "proximity", "attribute", "wordsPosition", "exactness"], rankingRules) + XCTAssertEqual(self.defaultRankingRules, rankingRules) expectation.fulfill() case .failure(let error): @@ -631,43 +459,18 @@ class SettingsTests: XCTestCase { let expectation = XCTestExpectation(description: "Get current searchable attributes") - let newSearchableAttributes: [String] = [ - "id", - "title", - "comment" - ] - - self.client.updateSearchableAttributes(UID: self.uid, newSearchableAttributes) { result in - + self.client.getSearchableAttributes(UID: self.uid) { result in switch result { - case .success: + case .success(let searchableAttributes): - Thread.sleep(forTimeInterval: 0.5) + XCTAssertEqual(self.defaultSearchableAttributes, searchableAttributes) - self.client.getSearchableAttributes(UID: self.uid) { result in - switch result { - case .success(let searchableAttributes): - - XCTAssertEqual(newSearchableAttributes.count, searchableAttributes.count) - - let lhs: [String] = newSearchableAttributes.sorted() - let rhs: [String] = searchableAttributes.sorted() - - XCTAssertEqual(lhs, rhs) - - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - } + expectation.fulfill() case .failure(let error): print(error) XCTFail() } - } self.wait(for: [expectation], timeout: 1.0) @@ -677,14 +480,12 @@ class SettingsTests: XCTestCase { let expectation = XCTestExpectation(description: "Update settings for searchable attributes") - let initialRankingRules: [String] = [ + let newSearchableAttributes: [String] = [ "id", - "title", - "comment" + "title" ] - self.client.updateSearchableAttributes(UID: self.uid, initialRankingRules) { result in - + self.client.updateSearchableAttributes(UID: self.uid, newSearchableAttributes) { result in switch result { case .success: @@ -693,42 +494,11 @@ class SettingsTests: XCTestCase { self.client.getSearchableAttributes(UID: self.uid) { result in switch result { - case .success(let rankingRules): - - XCTAssertEqual(initialRankingRules, rankingRules) - let newRankingRules: [String] = [ - "id", - "title" - ] - - self.client.updateSearchableAttributes(UID: self.uid, newRankingRules) { result in - switch result { - case .success: - - Thread.sleep(forTimeInterval: 0.5) - - self.client.getSearchableAttributes(UID: self.uid) { result in - - switch result { - case .success(let rankingRules): - - XCTAssertNotEqual(initialRankingRules, rankingRules) - XCTAssertEqual(newRankingRules, rankingRules) - - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } + case .success(let searchableAttributes): - } + XCTAssertEqual(newSearchableAttributes, searchableAttributes) - case .failure(let error): - print(error) - XCTFail() - } - } + expectation.fulfill() case .failure(let error): print(error) @@ -741,6 +511,7 @@ class SettingsTests: XCTestCase { print(error) XCTFail() } + } self.wait(for: [expectation], timeout: 2.0) @@ -760,9 +531,9 @@ class SettingsTests: XCTestCase { self.client.getSearchableAttributes(UID: self.uid) { result in switch result { - case .success(let rankingRules): + case .success(let searchableAttributes): - XCTAssertEqual(["*"], rankingRules) + XCTAssertEqual(self.defaultSearchableAttributes, searchableAttributes) expectation.fulfill() case .failure(let error): @@ -781,115 +552,48 @@ class SettingsTests: XCTestCase { self.wait(for: [expectation], timeout: 1.0) } - // MARK: Settings + // // MARK: Stop words - func testGetSettings() { - - let expectation = XCTestExpectation(description: "Get current settings") - - let newSettings: Setting = Setting( - rankingRules: ["typo", "words", "proximity", "attribute", "wordsPosition", "exactness"], - searchableAttributes: ["*"], - displayedAttributes: ["*"], - stopWords: [], - synonyms: [:], - distinctAttribute: nil) + func testGetStopWords() { - self.client.updateSetting(UID: self.uid, newSettings) { result in + let expectation = XCTestExpectation(description: "Get current stop words") + self.client.getStopWords(UID: self.uid) { result in switch result { - case .success: - Thread.sleep(forTimeInterval: 0.5) - - self.client.getSetting(UID: self.uid) { result in - switch result { - case .success(let settings): - XCTAssertEqual(newSettings, settings) - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - } + case .success(let stopWords): + XCTAssertEqual(self.defaultStopWords, stopWords) + expectation.fulfill() case .failure(let error): print(error) XCTFail() } - } self.wait(for: [expectation], timeout: 1.0) } - func testUpdateSettings() { - - let expectation = XCTestExpectation(description: "Update settings") + func testUpdateStopWords() { - let initialSettings: Setting = Setting( - rankingRules: ["typo", "words", "proximity", "attribute", "wordsPosition", "exactness"], - searchableAttributes: ["*"], - displayedAttributes: ["*"], - stopWords: [], - synonyms: [:], - distinctAttribute: nil) + let expectation = XCTestExpectation(description: "Update stop words") - self.client.updateSetting(UID: self.uid, initialSettings) { result in + let newStopWords: [String] = ["the"] + self.client.updateStopWords(UID: self.uid, newStopWords) { result in switch result { case .success: Thread.sleep(forTimeInterval: 0.5) - self.client.getSetting(UID: self.uid) { result in + self.client.getStopWords(UID: self.uid) { result in switch result { - case .success(let setting): - - XCTAssertEqual(initialSettings, setting) - - let newSettings: Setting = Setting( - rankingRules: ["words", "typo", "proximity", "attribute", "wordsPosition", "exactness"], - searchableAttributes: ["id", "title"], - displayedAttributes: ["*"], - stopWords: ["the", "a"], - synonyms: [:], - distinctAttribute: nil) - - self.client.updateSetting(UID: self.uid, newSettings) { result in - switch result { - case .success: - - Thread.sleep(forTimeInterval: 0.5) - - self.client.getSetting(UID: self.uid) { result in + case .success(let finalStopWords): - switch result { - case .success(let finalSetting): + XCTAssertEqual(newStopWords, finalStopWords) - XCTAssertNotEqual(initialSettings, finalSetting) - XCTAssertEqual(newSettings.rankingRules.sorted(), finalSetting.rankingRules.sorted()) - XCTAssertEqual(newSettings.searchableAttributes.sorted(), finalSetting.searchableAttributes.sorted()) - XCTAssertEqual(newSettings.displayedAttributes.sorted(), finalSetting.displayedAttributes.sorted()) - XCTAssertEqual(newSettings.stopWords.sorted(), finalSetting.stopWords.sorted()) - XCTAssertEqual(Array(newSettings.synonyms.keys).sorted(by: <), Array(finalSetting.synonyms.keys).sorted(by: <)) - - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - - } - - case .failure(let error): - print(error) - XCTFail() - } - } + expectation.fulfill() case .failure(let error): print(error) @@ -902,35 +606,27 @@ class SettingsTests: XCTestCase { print(error) XCTFail() } + } self.wait(for: [expectation], timeout: 10.0) } - func testResetSettings() { + func testResetStopWords() { - let expectation = XCTestExpectation(description: "Reset settings") + let expectation = XCTestExpectation(description: "Reset stop words") - self.client.resetSetting(UID: self.uid) { result in + self.client.resetStopWords(UID: self.uid) { result in switch result { case .success: Thread.sleep(forTimeInterval: TimeInterval(0.5)) - self.client.getSetting(UID: self.uid) { result in + self.client.getStopWords(UID: self.uid) { result in switch result { - case .success(let settings): - - let expected = Setting( - rankingRules: ["typo", "words", "proximity", "attribute", "wordsPosition", "exactness"], - searchableAttributes: ["*"], - displayedAttributes: ["*"], - stopWords: [], - synonyms: [:], - distinctAttribute: nil) - - XCTAssertEqual(expected, settings) + case .success(let stopWords): + XCTAssertEqual(self.defaultStopWords, stopWords) expectation.fulfill() case .failure(let error): @@ -949,93 +645,54 @@ class SettingsTests: XCTestCase { self.wait(for: [expectation], timeout: 1.0) } - // MARK: Stop words - - func testGetStopWords() { - - let expectation = XCTestExpectation(description: "Get current stop words") + // MARK: Synonyms - let newStopWords: [String] = ["the", "a", "an"] + func testGetSynonyms() { - self.client.updateStopWords(UID: self.uid, newStopWords) { result in + let expectation = XCTestExpectation(description: "Get current synonyms") + self.client.getSynonyms(UID: self.uid) { result in switch result { - case .success: - - Thread.sleep(forTimeInterval: 0.5) - - self.client.getStopWords(UID: self.uid) { result in - switch result { - case .success(let stopWords): - XCTAssertEqual(newStopWords.sorted(), stopWords.sorted()) - expectation.fulfill() + case .success(let synonyms): - case .failure(let error): - print(error) - XCTFail() - } - } + XCTAssertEqual(self.defaultSynonyms, synonyms) + expectation.fulfill() case .failure(let error): print(error) XCTFail() } - } self.wait(for: [expectation], timeout: 1.0) } - func testUpdateStopWords() { - - let expectation = XCTestExpectation(description: "Update stop words") + func testUpdateSynonyms() { - let initialStopWords: [String] = ["the", "a", "an"] + let expectation = XCTestExpectation(description: "Update synonyms") - self.client.updateStopWords(UID: self.uid, initialStopWords) { result in + let newSynonyms: [String: [String]] = [ + "wolverine": ["xmen", "logan"], + "logan": ["wolverine", "xmen"], + "wow": ["world of warcraft"], + "rct": ["rollercoaster tycoon"] + ] + self.client.updateSynonyms(UID: self.uid, newSynonyms) { result in switch result { case .success: Thread.sleep(forTimeInterval: 0.5) - self.client.getStopWords(UID: self.uid) { result in + self.client.getSynonyms(UID: self.uid) { result in switch result { - case .success(let stopWords): - - XCTAssertEqual(initialStopWords.sorted(), stopWords.sorted()) - - let newStopWords: [String] = ["the"] - - self.client.updateStopWords(UID: self.uid, newStopWords) { result in - switch result { - case .success: - - Thread.sleep(forTimeInterval: 0.5) + case .success(let updatedSynonyms): - self.client.getStopWords(UID: self.uid) { result in + let rhs = Array(updatedSynonyms.keys).sorted(by: <) + XCTAssertEqual(Array(newSynonyms.keys).sorted(by: <), rhs) - switch result { - case .success(let finalStopWords): - - XCTAssertNotEqual(initialStopWords.sorted(), finalStopWords.sorted()) - XCTAssertEqual(newStopWords.sorted(), finalStopWords.sorted()) - - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - - } - - case .failure(let error): - print(error) - XCTFail() - } - } + expectation.fulfill() case .failure(let error): print(error) @@ -1050,24 +707,25 @@ class SettingsTests: XCTestCase { } } - self.wait(for: [expectation], timeout: 10.0) + self.wait(for: [expectation], timeout: 2.0) } - func testResetStopWords() { + func testResetSynonyms() { - let expectation = XCTestExpectation(description: "Reset stop words") + let expectation = XCTestExpectation(description: "Reset synonyms") - self.client.resetStopWords(UID: self.uid) { result in + self.client.resetSynonyms(UID: self.uid) { result in switch result { case .success: Thread.sleep(forTimeInterval: TimeInterval(0.5)) - self.client.getStopWords(UID: self.uid) { result in + self.client.getSynonyms(UID: self.uid) { result in switch result { - case .success(let stopWords): - XCTAssertTrue(stopWords.isEmpty) + case .success(let synonyms): + + XCTAssertEqual(self.defaultSynonyms, synonyms) expectation.fulfill() case .failure(let error): @@ -1086,112 +744,59 @@ class SettingsTests: XCTestCase { self.wait(for: [expectation], timeout: 1.0) } - // MARK: Synonyms - - func testGetSynonyms() { - - let expectation = XCTestExpectation(description: "Get current synonyms") + // MARK: Global Settings - let newSynonyms: [String: [String]] = [ - "wolverine": ["xmen", "logan"], - "logan": ["wolverine", "xmen"], - "wow": ["world of warcraft"] - ] + func testGetSettings() { - self.client.updateSynonyms(UID: self.uid, newSynonyms) { result in + let expectation = XCTestExpectation(description: "Get current settings") + self.client.getSetting(UID: self.uid) { result in switch result { - case .success: - - Thread.sleep(forTimeInterval: 0.5) - - self.client.getSynonyms(UID: self.uid) { result in - switch result { - case .success(let synonyms): - - let lhs = Array(newSynonyms.keys).sorted(by: <) - let rhs = Array(synonyms.keys).sorted(by: <) - XCTAssertEqual(lhs, rhs) - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - } + case .success(let settings): + XCTAssertEqual(self.defaultGlobalSettings, settings) + expectation.fulfill() case .failure(let error): print(error) XCTFail() } - } self.wait(for: [expectation], timeout: 1.0) } - func testUpdateSynonyms() { - - let expectation = XCTestExpectation(description: "Update synonyms") + func testUpdateSettings() { - let initialSynonyms: [String: [String]] = [ - "wolverine": ["xmen", "logan"], - "logan": ["wolverine", "xmen"], - "wow": ["world of warcraft"] - ] + let expectation = XCTestExpectation(description: "Update settings") - self.client.updateSynonyms(UID: self.uid, initialSynonyms) { result in + let newSettings: Setting = Setting( + rankingRules: ["words", "typo", "proximity", "attribute", "wordsPosition", "exactness"], + searchableAttributes: ["id", "title"], + displayedAttributes: ["*"], + stopWords: ["the", "a"], + synonyms: [:], + distinctAttribute: nil, + attributesForFaceting: ["title"]) + self.client.updateSetting(UID: self.uid, newSettings) { result in switch result { case .success: Thread.sleep(forTimeInterval: 0.5) - self.client.getSynonyms(UID: self.uid) { result in + self.client.getSetting(UID: self.uid) { result in switch result { - case .success(let synonyms): - - let lhs = Array(initialSynonyms.keys).sorted(by: <) - let rhs = Array(synonyms.keys).sorted(by: <) - XCTAssertEqual(lhs, rhs) - - let newSynonyms: [String: [String]] = [ - "wolverine": ["xmen", "logan"], - "logan": ["wolverine", "xmen"], - "wow": ["world of warcraft"], - "rct": ["rollercoaster tycoon"] - ] + case .success(let finalSetting): - self.client.updateSynonyms(UID: self.uid, newSynonyms) { result in - switch result { - case .success: + XCTAssertEqual(newSettings.rankingRules.sorted(), finalSetting.rankingRules.sorted()) + XCTAssertEqual(newSettings.searchableAttributes.sorted(), finalSetting.searchableAttributes.sorted()) + XCTAssertEqual(newSettings.displayedAttributes.sorted(), finalSetting.displayedAttributes.sorted()) + XCTAssertEqual(newSettings.stopWords.sorted(), finalSetting.stopWords.sorted()) + XCTAssertEqual(newSettings.attributesForFaceting, finalSetting.attributesForFaceting) + XCTAssertEqual(Array(newSettings.synonyms.keys).sorted(by: <), Array(finalSetting.synonyms.keys).sorted(by: <)) - Thread.sleep(forTimeInterval: 0.5) - - self.client.getSynonyms(UID: self.uid) { result in - - switch result { - case .success(let updatedSynonyms): - - let rhs = Array(updatedSynonyms.keys).sorted(by: <) - XCTAssertNotEqual(Array(initialSynonyms.keys).sorted(by: <), rhs) - XCTAssertEqual(Array(newSynonyms.keys).sorted(by: <), rhs) - - expectation.fulfill() - - case .failure(let error): - print(error) - XCTFail() - } - - } - - case .failure(let error): - print(error) - XCTFail() - } - } + expectation.fulfill() case .failure(let error): print(error) @@ -1204,27 +809,28 @@ class SettingsTests: XCTestCase { print(error) XCTFail() } + } - self.wait(for: [expectation], timeout: 2.0) + self.wait(for: [expectation], timeout: 10.0) } - func testResetSynonyms() { + func testResetSettings() { - let expectation = XCTestExpectation(description: "Reset synonyms") + let expectation = XCTestExpectation(description: "Reset settings") - self.client.resetSynonyms(UID: self.uid) { result in + self.client.resetSetting(UID: self.uid) { result in switch result { case .success: Thread.sleep(forTimeInterval: TimeInterval(0.5)) - self.client.getSynonyms(UID: self.uid) { result in + self.client.getSetting(UID: self.uid) { result in switch result { - case .success(let synonyms): + case .success(let settings): - XCTAssertTrue(synonyms.isEmpty) + XCTAssertEqual(self.defaultGlobalSettings, settings) expectation.fulfill() case .failure(let error): diff --git a/Tests/MeiliSearchUnitTests/SettingsTests.swift b/Tests/MeiliSearchUnitTests/SettingsTests.swift index 7f51524e..0783abde 100644 --- a/Tests/MeiliSearchUnitTests/SettingsTests.swift +++ b/Tests/MeiliSearchUnitTests/SettingsTests.swift @@ -957,6 +957,9 @@ class SettingsTests: XCTestCase { ("testGetRankingRules", testGetRankingRules), ("testUpdateRankingRules", testUpdateRankingRules), ("testResetRankingRules", testResetRankingRules), + ("testGetDistinctAttribute", testGetDistinctAttribute), + ("testUpdateDistinctAttribute", testUpdateDistinctAttribute), + ("testResetDistinctAttribute", testResetDistinctAttribute), ("testGetSearchableAttributes", testGetSearchableAttributes), ("testUpdateSearchableAttributes", testUpdateSearchableAttributes), ("testResetSearchableAttributes", testResetSearchableAttributes),