Skip to content

Commit c799343

Browse files
committed
Add missing instrumented tests for settings, update and search routes
1 parent 183f729 commit c799343

22 files changed

+2207
-121
lines changed

--data

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"message":"Unsupported media type","errorCode":"unsupported_media_type","errorType":"invalid_request_error","errorLink":"https://docs.meilisearch.com/errors#unsupported_media_type"}

Sources/MeiliSearch/Client.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public struct MeiliSearch {
526526
*/
527527
public func getDistinctAttribute(
528528
UID: String,
529-
_ completion: @escaping (Result<String, Swift.Error>) -> Void) {
529+
_ completion: @escaping (Result<String?, Swift.Error>) -> Void) {
530530
self.settings.getDistinctAttribute(UID, completion)
531531
}
532532

Sources/MeiliSearch/Constants.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ struct Constants {
55
static let customJSONDecoder: JSONDecoder = {
66
let decoder = JSONDecoder()
77
decoder.dateDecodingStrategy = .formatted(Formatter.iso8601)
8-
98
return decoder
109
}()
1110
}

Sources/MeiliSearch/Documents.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,7 @@ struct Documents {
209209
_ customDecoder: JSONDecoder? = nil,
210210
completion: (Result<T, Swift.Error>) -> Void) {
211211
do {
212-
213-
let decoder: JSONDecoder
214-
if let customDecoder: JSONDecoder = customDecoder {
215-
decoder = customDecoder
216-
} else {
217-
decoder = Constants.customJSONDecoder
218-
}
219-
212+
let decoder: JSONDecoder = customDecoder ?? Constants.customJSONDecoder
220213
let value: T = try decoder.decode(T.self, from: data)
221214
completion(.success(value))
222215
} catch {

Sources/MeiliSearch/Model/Key.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ import Foundation
66
*/
77
public struct Key: Codable, Equatable {
88

9-
// MARK: Properties
9+
// MARK: Properties
1010

11-
///Private key used to access a determined set of API routes.
12-
public let `private`: String
11+
///Private key used to access a determined set of API routes.
12+
public let `private`: String
1313

14-
///Public key used to access a determined set of API routes.
15-
public let `public`: String
14+
///Public key used to access a determined set of API routes.
15+
public let `public`: String
16+
17+
enum CodingKeys: String, CodingKey {
18+
case `private`
19+
case `public`
20+
}
1621

1722
}

Sources/MeiliSearch/Model/SearchParameters.swift

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ public struct SearchParameters: Codable, Equatable {
1212
/// Query string (mandatory).
1313
public let query: String
1414

15-
/// Number of documents to skip.
16-
public let offset: Int
17-
1815
/// Number of documents to take.
1916
public let limit: Int
2017

18+
/// Number of documents to skip.
19+
public let offset: Int
20+
2121
/// Document attributes to show.
2222
public let attributesToRetrieve: [String]?
2323

@@ -82,15 +82,57 @@ public struct SearchParameters: Codable, Equatable {
8282
SearchParameters(query: value)
8383
}
8484

85-
private func commaRepresentation(_ array: [String]) -> String {
86-
array.joined(separator: ",")
85+
enum CodingKeys: String, CodingKey {
86+
case query = "q"
87+
case offset
88+
case limit
89+
case attributesToRetrieve
90+
case attributesToCrop
91+
case cropLength
92+
case attributesToHighlight
93+
case filters
94+
case facetFilters
95+
case facetsDistribution
96+
case matches
8797
}
8898

89-
private func commaRepresentationEscaped(_ array: [String]) -> String {
90-
var value: String = "["
91-
value += array.map({ string in "\"\(string)\"" }).joined(separator: ",")
92-
value += "]"
93-
return value
99+
}
100+
101+
extension SearchParameters {
102+
103+
public func encode(to encoder: Encoder) throws {
104+
var container = encoder.container(keyedBy: CodingKeys.self)
105+
try container.encode(query, forKey: .query)
106+
if limit != 20 {
107+
try container.encode(limit, forKey: .limit)
108+
}
109+
if offset != 0 {
110+
try container.encode(offset, forKey: .offset)
111+
}
112+
if let attributesToRetrieve: [String] = self.attributesToRetrieve, !attributesToRetrieve.isEmpty {
113+
try container.encode(attributesToRetrieve, forKey: .attributesToRetrieve)
114+
}
115+
if !attributesToCrop.isEmpty {
116+
try container.encode(attributesToCrop, forKey: .attributesToCrop)
117+
}
118+
if cropLength != 200 {
119+
try container.encode(cropLength, forKey: .cropLength)
120+
}
121+
if !attributesToHighlight.isEmpty {
122+
try container.encode(attributesToHighlight, forKey: .attributesToHighlight)
123+
}
124+
if let filters: String = self.filters, !filters.isEmpty {
125+
try container.encode(filters, forKey: .filters)
126+
}
127+
if let facetFilters: [[String]] = self.facetFilters {
128+
try container.encode(facetFilters, forKey: .facetFilters)
129+
}
130+
if let facetsDistribution = self.facetsDistribution, !facetsDistribution.isEmpty {
131+
try container.encode(facetsDistribution, forKey: .facetsDistribution)
132+
}
133+
if matches {
134+
try container.encode(matches, forKey: .matches)
135+
}
94136
}
95137

96138
}

Sources/MeiliSearch/Model/SearchResult.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,34 @@ public struct SearchResult<T>: Codable, Equatable where T: Codable, T: Equatable
1616
/// Number of documents taken.
1717
public let limit: Int
1818

19+
/// Total number of matches,
20+
public let nbHits: Int
21+
22+
/// Whether `nbHits` is exhaustive.
23+
public let exhaustiveNbHits: Bool?
24+
25+
/// Distribution of the given facets.
26+
public let facetDistribution: [String]?
27+
28+
/// Whether facetDistribution is exhaustive.
29+
public let exhaustiveFacetsCount: Bool?
30+
1931
/// Time, in milliseconds, to process the query.
2032
public let processingTimeMs: Int?
2133

2234
/// Query string from the search.
2335
public let query: String
2436

37+
enum CodingKeys: String, CodingKey {
38+
case hits
39+
case offset
40+
case limit
41+
case nbHits
42+
case exhaustiveNbHits
43+
case facetDistribution
44+
case exhaustiveFacetsCount
45+
case processingTimeMs
46+
case query
47+
}
48+
2549
}

Sources/MeiliSearch/Model/Setting.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@ public struct Setting: Codable, Equatable {
2323
/// List of synonyms and its values for a given `Index`.
2424
public let synonyms: [String: [String]]
2525

26+
/// Optional distinct attribute set for a given `Index`.
27+
public let distinctAttribute: String?
28+
29+
}
30+
31+
extension Setting {
32+
2633
/// Tries to decode the JSON object to Setting object.
2734
public init(from decoder: Decoder) throws {
2835
let values = try? decoder.container(keyedBy: CodingKeys.self)
29-
3036
rankingRules = (try? values?.decodeIfPresent([String].self, forKey: .rankingRules)) ?? []
3137
searchableAttributes = (try? values?.decodeIfPresent([String].self, forKey: .searchableAttributes)) ?? ["*"]
3238
displayedAttributes = (try? values?.decodeIfPresent([String].self, forKey: .displayedAttributes)) ?? ["*"]
3339
stopWords = (try? values?.decodeIfPresent([String].self, forKey: .stopWords)) ?? []
3440
synonyms = (try? values?.decodeIfPresent([String: [String]].self, forKey: .synonyms)) ?? [:]
41+
distinctAttribute = try? values?.decodeIfPresent(String.self, forKey: .distinctAttribute)
3542
}
43+
3644
}

Sources/MeiliSearch/Model/Stat.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public struct AllStats: Codable, Equatable {
1616
/// Dictionary of all Indexes containing the stat for each Index.
1717
public let indexes: [String: Stat]
1818

19+
enum CodingKeys: String, CodingKey {
20+
case databaseSize
21+
case lastUpdate
22+
case indexes
23+
}
24+
1925
}
2026

2127
/**
@@ -34,4 +40,10 @@ public struct Stat: Codable, Equatable {
3440
/// Usage frequency for each Index field.
3541
public let fieldsFrequency: [String: Int]
3642

43+
enum CodingKeys: String, CodingKey {
44+
case numberOfDocuments
45+
case isIndexing
46+
case fieldsFrequency
47+
}
48+
3749
}

Sources/MeiliSearch/Model/Update.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,22 @@ public struct Update: Codable, Equatable {
2626
public let type: Type
2727

2828
///Duration of the update process.
29-
public let duration: TimeInterval
29+
public let duration: TimeInterval?
3030

3131
///Date when the update has been enqueued.
3232
public let enqueuedAt: Date
3333

3434
///Date when the update has been processed.
35-
public let processedAt: Date
35+
public let processedAt: Date?
36+
37+
enum CodingKeys: String, CodingKey {
38+
case status
39+
case updateId
40+
case type
41+
case duration
42+
case enqueuedAt
43+
case processedAt
44+
}
3645

3746
///Typr of `Update`.
3847
public struct `Type`: Codable, Equatable {
@@ -45,6 +54,11 @@ public struct Update: Codable, Equatable {
4554
/// ID of update type.
4655
public let number: Int
4756

57+
enum CodingKeys: String, CodingKey {
58+
case name
59+
case number
60+
}
61+
4862
}
4963

5064
}

0 commit comments

Comments
 (0)