Skip to content

Commit ec7254e

Browse files
committed
Add missing instrumented tests for settings, update and search routes
1 parent a28d4a3 commit ec7254e

File tree

16 files changed

+2063
-87
lines changed

16 files changed

+2063
-87
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/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: 7 additions & 5 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

@@ -95,10 +95,12 @@ public struct SearchParameters: Codable, Equatable {
9595
}
9696

9797
if !attributesToCrop.isEmpty {
98-
dic["attributesToCrop"] = commaRepresentation(attributesToCrop)
98+
dic["attributesToCrop"] = commaRepresentationEscaped(attributesToCrop)
9999
}
100100

101-
dic["cropLength"] = "\(cropLength)"
101+
if cropLength != 200 {
102+
dic["cropLength"] = "\(cropLength)"
103+
}
102104

103105
if !attributesToHighlight.isEmpty {
104106
dic["attributesToHighlight"] = commaRepresentation(attributesToHighlight)

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
}

Sources/MeiliSearch/Model/Version.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ public struct Version: Codable, Equatable {
1616
/// Package version, human readable, overly documented.
1717
public let pkgVersion: String
1818

19+
enum CodingKeys: String, CodingKey {
20+
case commitSha
21+
case buildDate
22+
case pkgVersion
23+
}
24+
1925
}

Sources/MeiliSearch/Settings.swift

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ struct Settings {
3232
}
3333

3434
do {
35-
let decoder: JSONDecoder = JSONDecoder()
36-
let settings: Setting = try decoder.decode(Setting.self, from: data)
35+
let settings: Setting = try Constants.customJSONDecoder.decode(Setting.self, from: data)
3736
completion(.success(settings))
3837
} catch {
3938
completion(.failure(error))
@@ -66,8 +65,7 @@ struct Settings {
6665
case .success(let data):
6766

6867
do {
69-
let decoder: JSONDecoder = JSONDecoder()
70-
let update: Update = try decoder.decode(Update.self, from: data)
68+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
7169
completion(.success(update))
7270
} catch {
7371
completion(.failure(error))
@@ -96,8 +94,7 @@ struct Settings {
9694
}
9795

9896
do {
99-
let decoder: JSONDecoder = JSONDecoder()
100-
let update: Update = try decoder.decode(Update.self, from: data)
97+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
10198
completion(.success(update))
10299
} catch {
103100
completion(.failure(error))
@@ -162,8 +159,7 @@ struct Settings {
162159
case .success(let data):
163160

164161
do {
165-
let decoder: JSONDecoder = JSONDecoder()
166-
let update: Update = try decoder.decode(Update.self, from: data)
162+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
167163
completion(.success(update))
168164
} catch {
169165
completion(.failure(error))
@@ -192,8 +188,7 @@ struct Settings {
192188
}
193189

194190
do {
195-
let decoder: JSONDecoder = JSONDecoder()
196-
let update: Update = try decoder.decode(Update.self, from: data)
191+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
197192
completion(.success(update))
198193
} catch {
199194
completion(.failure(error))
@@ -258,8 +253,7 @@ struct Settings {
258253
case .success(let data):
259254

260255
do {
261-
let decoder: JSONDecoder = JSONDecoder()
262-
let update: Update = try decoder.decode(Update.self, from: data)
256+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
263257
completion(.success(update))
264258
} catch {
265259
completion(.failure(error))
@@ -288,8 +282,7 @@ struct Settings {
288282
}
289283

290284
do {
291-
let decoder: JSONDecoder = JSONDecoder()
292-
let update: Update = try decoder.decode(Update.self, from: data)
285+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
293286
completion(.success(update))
294287
} catch {
295288
completion(.failure(error))
@@ -342,7 +335,7 @@ struct Settings {
342335

343336
let data: Data
344337
do {
345-
data = try JSONSerialization.data(withJSONObject: rankingRules, options: [])
338+
data = try JSONSerialization.data(withJSONObject: rankingRules, options: [])
346339
} catch {
347340
completion(.failure(error))
348341
return
@@ -354,8 +347,7 @@ struct Settings {
354347
case .success(let data):
355348

356349
do {
357-
let decoder: JSONDecoder = JSONDecoder()
358-
let update: Update = try decoder.decode(Update.self, from: data)
350+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
359351
completion(.success(update))
360352
} catch {
361353
completion(.failure(error))
@@ -384,8 +376,7 @@ struct Settings {
384376
}
385377

386378
do {
387-
let decoder: JSONDecoder = JSONDecoder()
388-
let update: Update = try decoder.decode(Update.self, from: data)
379+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
389380
completion(.success(update))
390381
} catch {
391382
completion(.failure(error))
@@ -401,9 +392,20 @@ struct Settings {
401392

402393
// MARK: Distinct attribute
403394

395+
struct DistinctAttributePayload: Codable, Equatable {
396+
397+
/// Distinct attribute key
398+
public let distinctAttribute: String
399+
400+
enum CodingKeys: String, CodingKey {
401+
case distinctAttribute
402+
}
403+
404+
}
405+
404406
func getDistinctAttribute(
405407
_ UID: String,
406-
_ completion: @escaping (Result<String, Swift.Error>) -> Void) {
408+
_ completion: @escaping (Result<String?, Swift.Error>) -> Void) {
407409

408410
self.request.get(api: "/indexes/\(UID)/settings/distinct-attribute") { result in
409411

@@ -415,8 +417,8 @@ struct Settings {
415417
return
416418
}
417419

418-
let distinctAttribute: String = String(
419-
decoding: data, as: UTF8.self)
420+
let distinctAttribute: String? =
421+
try? Constants.customJSONDecoder.decode(String.self, from: data)
420422

421423
completion(.success(distinctAttribute))
422424

@@ -433,19 +435,22 @@ struct Settings {
433435
_ distinctAttribute: String,
434436
_ completion: @escaping (Result<Update, Swift.Error>) -> Void) {
435437

436-
guard let data: Data = distinctAttribute.data(using: .ascii) else {
437-
completion(.failure(MeiliSearch.Error.invalidJSON))
438+
let encoder = JSONEncoder()
439+
let data: Data
440+
do {
441+
data = try encoder.encode(DistinctAttributePayload(distinctAttribute: distinctAttribute))
442+
} catch {
443+
completion(.failure(error))
438444
return
439445
}
440446

441-
self.request.post(api: "/indexes/\(UID)/settings/distinct-attribute", data) { result in
447+
self.request.post(api: "/indexes/\(UID)/settings", data) { result in
442448

443449
switch result {
444450
case .success(let data):
445451

446452
do {
447-
let decoder: JSONDecoder = JSONDecoder()
448-
let update: Update = try decoder.decode(Update.self, from: data)
453+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
449454
completion(.success(update))
450455
} catch {
451456
completion(.failure(error))
@@ -474,8 +479,7 @@ struct Settings {
474479
}
475480

476481
do {
477-
let decoder: JSONDecoder = JSONDecoder()
478-
let update: Update = try decoder.decode(Update.self, from: data)
482+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
479483
completion(.success(update))
480484
} catch {
481485
completion(.failure(error))
@@ -540,8 +544,7 @@ struct Settings {
540544
case .success(let data):
541545

542546
do {
543-
let decoder: JSONDecoder = JSONDecoder()
544-
let update: Update = try decoder.decode(Update.self, from: data)
547+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
545548
completion(.success(update))
546549
} catch {
547550
completion(.failure(error))
@@ -570,8 +573,7 @@ struct Settings {
570573
}
571574

572575
do {
573-
let decoder: JSONDecoder = JSONDecoder()
574-
let update: Update = try decoder.decode(Update.self, from: data)
576+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
575577
completion(.success(update))
576578
} catch {
577579
completion(.failure(error))
@@ -636,8 +638,7 @@ struct Settings {
636638
case .success(let data):
637639

638640
do {
639-
let decoder: JSONDecoder = JSONDecoder()
640-
let update: Update = try decoder.decode(Update.self, from: data)
641+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
641642
completion(.success(update))
642643
} catch {
643644
completion(.failure(error))
@@ -666,8 +667,7 @@ struct Settings {
666667
}
667668

668669
do {
669-
let decoder: JSONDecoder = JSONDecoder()
670-
let update: Update = try decoder.decode(Update.self, from: data)
670+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
671671
completion(.success(update))
672672
} catch {
673673
completion(.failure(error))
@@ -732,8 +732,7 @@ struct Settings {
732732
case .success(let data):
733733

734734
do {
735-
let decoder: JSONDecoder = JSONDecoder()
736-
let update: Update = try decoder.decode(Update.self, from: data)
735+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
737736
completion(.success(update))
738737
} catch {
739738
completion(.failure(error))
@@ -762,8 +761,7 @@ struct Settings {
762761
}
763762

764763
do {
765-
let decoder: JSONDecoder = JSONDecoder()
766-
let update: Update = try decoder.decode(Update.self, from: data)
764+
let update: Update = try Constants.customJSONDecoder.decode(Update.self, from: data)
767765
completion(.success(update))
768766
} catch {
769767
completion(.failure(error))

0 commit comments

Comments
 (0)