Skip to content

Commit 50264d6

Browse files
bors[bot]curquiza
andauthored
Merge #66
66: Make all the search parameters optional r=curquiza a=curquiza Co-authored-by: Clementine Urquizar <[email protected]>
2 parents 47402cd + 1ae2abd commit 50264d6

File tree

3 files changed

+85
-83
lines changed

3 files changed

+85
-83
lines changed
Lines changed: 15 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22

33
/**
4-
`SearchParameters` instances represent query setup for a search request.
4+
`SearchParameters` instances represent query setup for a search request.
55
Use `SearchParameters.query` to directly create a search query with the
66
default search configuration.
77
*/
@@ -10,25 +10,25 @@ public struct SearchParameters: Codable, Equatable {
1010
// MARK: Properties
1111

1212
/// Query string (mandatory).
13-
public let query: String
13+
public let query: String?
1414

1515
/// Number of documents to take.
16-
public let limit: Int
16+
public let limit: Int?
1717

1818
/// Number of documents to skip.
19-
public let offset: Int
19+
public let offset: Int?
2020

2121
/// Document attributes to show.
2222
public let attributesToRetrieve: [String]?
2323

2424
/// Which attributes to crop.
25-
public let attributesToCrop: [String]
25+
public let attributesToCrop: [String]?
2626

2727
/// Limit length at which to crop specified attributes.
28-
public let cropLength: Int
28+
public let cropLength: Int?
2929

3030
/// Which attributes to highlight.
31-
public let attributesToHighlight: [String]
31+
public let attributesToHighlight: [String]?
3232

3333
/// Attribute with an exact match.
3434
public let filters: String?
@@ -40,22 +40,22 @@ public struct SearchParameters: Codable, Equatable {
4040
public let facetsDistribution: [String]?
4141

4242
/// Whether to return the raw matches or not.
43-
public let matches: Bool
43+
public let matches: Bool?
4444

4545
// MARK: Initializers
4646

4747
init(
48-
query: String,
49-
offset: Int = Default.offset.rawValue,
50-
limit: Int = Default.limit.rawValue,
48+
query: String?,
49+
offset: Int? = nil,
50+
limit: Int? = nil,
5151
attributesToRetrieve: [String]? = nil,
52-
attributesToCrop: [String] = [],
53-
cropLength: Int = Default.cropLength.rawValue,
54-
attributesToHighlight: [String] = [],
52+
attributesToCrop: [String]? = nil,
53+
cropLength: Int? = nil,
54+
attributesToHighlight: [String]? = nil,
5555
filters: String? = nil,
5656
facetFilters: [[String]]? = nil,
5757
facetsDistribution: [String]? = nil,
58-
matches: Bool = false) {
58+
matches: Bool? = false) {
5959
self.query = query
6060
self.offset = offset
6161
self.limit = limit
@@ -97,54 +97,4 @@ public struct SearchParameters: Codable, Equatable {
9797
case matches
9898
}
9999

100-
// MARK: Default value for keys
101-
102-
fileprivate enum Default: Int {
103-
case offset = 0
104-
case limit = 20
105-
case cropLength = 200
106-
}
107-
108-
}
109-
110-
extension SearchParameters {
111-
112-
// MARK: Codable
113-
114-
/// Encodes the `SearchParameters` to a JSON payload, removing any non necessary implicit parameter.
115-
public func encode(to encoder: Encoder) throws {
116-
var container = encoder.container(keyedBy: CodingKeys.self)
117-
try container.encode(query, forKey: .query)
118-
if limit != Default.limit.rawValue {
119-
try container.encode(limit, forKey: .limit)
120-
}
121-
if offset != Default.offset.rawValue {
122-
try container.encode(offset, forKey: .offset)
123-
}
124-
if let attributesToRetrieve: [String] = self.attributesToRetrieve, !attributesToRetrieve.isEmpty {
125-
try container.encode(attributesToRetrieve, forKey: .attributesToRetrieve)
126-
}
127-
if !attributesToCrop.isEmpty {
128-
try container.encode(attributesToCrop, forKey: .attributesToCrop)
129-
}
130-
if cropLength != Default.cropLength.rawValue {
131-
try container.encode(cropLength, forKey: .cropLength)
132-
}
133-
if !attributesToHighlight.isEmpty {
134-
try container.encode(attributesToHighlight, forKey: .attributesToHighlight)
135-
}
136-
if let filters: String = self.filters, !filters.isEmpty {
137-
try container.encode(filters, forKey: .filters)
138-
}
139-
if let facetFilters: [[String]] = self.facetFilters {
140-
try container.encode(facetFilters, forKey: .facetFilters)
141-
}
142-
if let facetsDistribution = self.facetsDistribution, !facetsDistribution.isEmpty {
143-
try container.encode(facetsDistribution, forKey: .facetsDistribution)
144-
}
145-
if matches {
146-
try container.encode(matches, forKey: .matches)
147-
}
148-
}
149-
150100
}

Sources/MeiliSearch/Model/SearchResult.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ public struct SearchResult<T>: Codable, Equatable where T: Codable, T: Equatable
3333
public let processingTimeMs: Int?
3434

3535
/// Query string from the search.
36-
public let query: String
36+
public let query: String?
3737

3838
}

Tests/MeiliSearchIntegrationTests/SearchTests.swift

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,55 @@ class SearchTests: XCTestCase {
134134
self.wait(for: [expectation], timeout: 10.0)
135135
}
136136

137+
// MARK: Basic search
138+
139+
func testBasicSearch() {
140+
141+
let expectation = XCTestExpectation(description: "Search for Books using limit")
142+
143+
typealias MeiliResult = Result<SearchResult<Book>, Swift.Error>
144+
// let limit = 5
145+
let query = "A Moreninha"
146+
147+
self.client.search(UID: self.uid, SearchParameters(query: query)) { (result: MeiliResult) in
148+
switch result {
149+
case .success(let documents):
150+
XCTAssertTrue(documents.query == query)
151+
XCTAssertTrue(documents.limit == 20)
152+
XCTAssertTrue(documents.hits.count == 1)
153+
XCTAssertEqual(query, documents.hits[0].title)
154+
XCTAssertNil(documents.hits[0].formatted)
155+
expectation.fulfill()
156+
case .failure:
157+
XCTFail("Failed to search with testBasicSearch")
158+
}
159+
}
160+
161+
self.wait(for: [expectation], timeout: 1.0)
162+
}
163+
164+
func testBasicSearchWithNoQuery() {
165+
166+
let expectation = XCTestExpectation(description: "Search for Books using limit")
167+
168+
typealias MeiliResult = Result<SearchResult<Book>, Swift.Error>
169+
170+
self.client.search(UID: self.uid, SearchParameters(query: nil)) { (result: MeiliResult) in
171+
switch result {
172+
case .success(let documents):
173+
XCTAssertEqual("", documents.query)
174+
XCTAssertEqual(20, documents.limit)
175+
XCTAssertEqual(books.count, documents.hits.count)
176+
XCTAssertEqual("Pride and Prejudice", documents.hits[0].title)
177+
expectation.fulfill()
178+
case .failure:
179+
XCTFail("Failed to search with testBasicSearchWithNoQuery")
180+
}
181+
}
182+
183+
self.wait(for: [expectation], timeout: 1.0)
184+
}
185+
137186
// MARK: Limit
138187

139188
func testSearchLimit() {
@@ -153,7 +202,7 @@ class SearchTests: XCTestCase {
153202
XCTAssertEqual(query, documents.hits[0].title)
154203
expectation.fulfill()
155204
case .failure:
156-
XCTFail("Failed to get Books documents")
205+
XCTFail("Failed to search with testSearchLimit")
157206
}
158207
}
159208

@@ -176,7 +225,7 @@ class SearchTests: XCTestCase {
176225
XCTAssertTrue(documents.hits.isEmpty)
177226
expectation.fulfill()
178227
case .failure:
179-
XCTFail("Failed to get Books documents")
228+
XCTFail("Failed to search with testSearchZeroLimit")
180229
}
181230
}
182231

@@ -197,9 +246,10 @@ class SearchTests: XCTestCase {
197246
XCTAssertTrue(documents.query == query)
198247
XCTAssertTrue(documents.limit == limit)
199248
XCTAssertTrue(documents.hits.count == limit)
249+
XCTAssertNil(documents.hits[0].formatted)
200250
expectation.fulfill()
201251
case .failure:
202-
XCTFail("Failed to get Books documents")
252+
XCTFail("Failed to search with testSearchLimitBiggerThanNumberOfBooks")
203253
}
204254
}
205255

@@ -220,9 +270,10 @@ class SearchTests: XCTestCase {
220270
XCTAssertTrue(documents.query == query)
221271
XCTAssertTrue(documents.limit == limit)
222272
XCTAssertTrue(documents.hits.count == 5)
273+
XCTAssertNil(documents.hits[0].formatted)
223274
expectation.fulfill()
224275
case .failure:
225-
XCTFail("Failed to get Books documents")
276+
XCTFail("Failed to search with testSearchLimitEmptySearch")
226277
}
227278
}
228279

@@ -249,7 +300,7 @@ class SearchTests: XCTestCase {
249300
XCTAssertTrue(documents.hits.count == 2)
250301
expectation.fulfill()
251302
case .failure:
252-
XCTFail("Failed to get Books documents")
303+
XCTFail("Failed to search with testSearchOffset")
253304
}
254305
}
255306

@@ -274,7 +325,7 @@ class SearchTests: XCTestCase {
274325
XCTAssertTrue(documents.hits.count == 2)
275326
expectation.fulfill()
276327
case .failure:
277-
XCTFail("Failed to get Books documents")
328+
XCTFail("Failed to search with testSearchOffsetZero")
278329
}
279330
}
280331

@@ -297,9 +348,10 @@ class SearchTests: XCTestCase {
297348
XCTAssertTrue(documents.limit == limit)
298349
XCTAssertTrue(documents.offset == offset)
299350
XCTAssertTrue(documents.hits.count == 1)
351+
XCTAssertNil(documents.hits[0].formatted)
300352
expectation.fulfill()
301353
case .failure:
302-
XCTFail("Failed to get Books documents")
354+
XCTFail("Failed to search with testSearchOffsetLastPage")
303355
}
304356
}
305357

@@ -328,7 +380,7 @@ class SearchTests: XCTestCase {
328380
XCTAssertEqual("Manuel de Macedo", book.formatted!.comment!)
329381
expectation.fulfill()
330382
case .failure:
331-
XCTFail("Failed to get Books documents")
383+
XCTFail("Failed to search with testSearchAttributesToCrop")
332384
}
333385
}
334386

@@ -363,7 +415,7 @@ class SearchTests: XCTestCase {
363415

364416
expectation.fulfill()
365417
case .failure:
366-
XCTFail("Failed to get Books documents")
418+
XCTFail("Failed to search with testSearchCropLength")
367419
}
368420
}
369421

@@ -396,7 +448,7 @@ class SearchTests: XCTestCase {
396448
XCTAssertEqual(1, info.length)
397449
expectation.fulfill()
398450
case .failure:
399-
XCTFail("Failed to get Books documents")
451+
XCTFail("Failed to search with testSearchMatches")
400452
}
401453
}
402454

@@ -426,7 +478,7 @@ class SearchTests: XCTestCase {
426478
XCTAssertTrue(book.formatted!.comment!.contains("<em>Joaquim</em> <em>Manuel</em> <em>de</em> <em>Macedo</em>"))
427479
expectation.fulfill()
428480
case .failure:
429-
XCTFail("Failed to get Books documents")
481+
XCTFail("Failed to search with testSearchAttributesToHighlight")
430482
}
431483
}
432484

@@ -457,7 +509,7 @@ class SearchTests: XCTestCase {
457509
XCTAssertNil(book.comment)
458510
expectation.fulfill()
459511
case .failure:
460-
XCTFail("Failed to get Books documents")
512+
XCTFail("Failed to search with testSearchAttributesToRetrieve")
461513
}
462514
}
463515

@@ -488,7 +540,7 @@ class SearchTests: XCTestCase {
488540
XCTAssertEqual("A french book", book.comment)
489541
expectation.fulfill()
490542
case .failure:
491-
XCTFail("Failed to get Books documents")
543+
XCTFail("Failed to search with testSearchFilters")
492544
}
493545
}
494546

@@ -513,7 +565,7 @@ class SearchTests: XCTestCase {
513565
XCTAssertTrue(documents.hits.isEmpty)
514566
expectation.fulfill()
515567
case .failure:
516-
XCTFail("Failed to get Books documents")
568+
XCTFail("Failed to search with testSearchFiltersNotMatching")
517569
}
518570
}
519571

@@ -535,7 +587,7 @@ class SearchTests: XCTestCase {
535587
Thread.sleep(forTimeInterval: 0.5)
536588
completion()
537589
case .failure:
538-
XCTFail("Failed to get Books documents")
590+
XCTFail("Failed to update the settings")
539591
}
540592

541593
}
@@ -568,7 +620,7 @@ class SearchTests: XCTestCase {
568620
XCTAssertEqual("Le Petit Prince", petitBook.title)
569621
expectation.fulfill()
570622
case .failure:
571-
XCTFail("Failed to get Books documents")
623+
XCTFail("Failed to search with testSearchFacetsFilters")
572624
}
573625
}
574626

@@ -614,7 +666,7 @@ class SearchTests: XCTestCase {
614666

615667
expectation.fulfill()
616668
case .failure:
617-
XCTFail("Failed to get Books documents")
669+
XCTFail("Failed to search with testSearchFacetsDistribution")
618670
}
619671
}
620672

0 commit comments

Comments
 (0)