Skip to content

Commit 71d159e

Browse files
chore: Add delete by tag CTS test. Add Builder conformance to DeleteBy… (#681)
* feat: Add delete by tag CTS test. Add Builder conformance to DeleteByQuery. * chore: remove debugging lines
1 parent 2c84258 commit 71d159e

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

Sources/AlgoliaSearchClient/Models/Search/Query/DeleteByQuery.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,5 @@ extension DeleteByQuery: URLEncodable {
190190
}
191191

192192
}
193+
194+
extension DeleteByQuery: Builder {}

Tests/AlgoliaSearchClientTests/Helper/TestRecord.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct TestRecord: Codable, Equatable, CustomStringConvertible {
1414
var string: String
1515
var numeric: Int
1616
var bool: Bool
17+
var _tags: [String]?
1718

1819
init(objectID: String) {
1920
self.init(objectID: .init(rawValue: objectID))
@@ -27,7 +28,7 @@ struct TestRecord: Codable, Equatable, CustomStringConvertible {
2728
}
2829

2930
var description: String {
30-
return [objectID.flatMap { "objectID: \($0)" }, "string: \(string)", "numeric: \(numeric)", "bool: \(bool)"].compactMap { $0 }.joined(separator: ", ")
31+
return [objectID.flatMap { "objectID: \($0)" }, "string: \(string)", "numeric: \(numeric)", "bool: \(bool)", _tags.flatMap { "tags: \($0)" }].compactMap { $0 }.joined(separator: ", ")
3132
}
3233

3334
}

Tests/AlgoliaSearchClientTests/Integration/IndexingIntegrationTests.swift

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,52 @@ class IndexingIntegrationTests: OnlineTestCase {
2828
let objectID = ObjectID(rawValue: .init(randomWithLength: 10))
2929
let object = TestRecord(objectID: objectID)
3030

31+
// Add 1 record with saveObject with an objectID and collect taskID/objectID
3132
let objectCreation = try index.saveObject(object)
3233
try objectCreation.wait()
3334
XCTAssertEqual(objectCreation.task.objectID, objectID)
3435

36+
// Add 1 record with saveObject without an objectID and collect taskID/objectID
3537
var objectWithGeneratedID = TestRecord()
3638
let objectWithGeneratedIDCreation = try index.saveObject(objectWithGeneratedID, autoGeneratingObjectID: true)
3739
try objectWithGeneratedIDCreation.wait()
3840

3941
let generatedObjectID = objectWithGeneratedIDCreation.task.objectID
4042
objectWithGeneratedID.objectID = generatedObjectID
4143

44+
// Perform a saveObjects with an empty set of objects and collect taskID
4245
let emptyObjectsCreation = try index.saveObjects([JSON]())
4346
try emptyObjectsCreation.wait()
44-
47+
48+
// Add 2 records with saveObjects with an objectID and collect taskID/objectID
4549
let objectsIDs: [ObjectID] = [.init(rawValue: .init(randomWithLength: 10)), .init(rawValue: .init(randomWithLength: 10))]
4650
let objects = objectsIDs.map(TestRecord.init)
4751
let objectsCreation = try index.saveObjects(objects)
4852
try objectsCreation.wait()
4953

5054
XCTAssertEqual(objectsCreation.batchesResponse.objectIDs, objectsIDs)
5155

56+
// Add 2 records with saveObjects without an objectID and collect taskID/objectID
5257
var objectsWithGeneratedID = [TestRecord(), TestRecord()]
5358
let objectsWithGeneratedIDCreation = try index.saveObjects(objectsWithGeneratedID, autoGeneratingObjectID: true)
5459
try objectWithGeneratedIDCreation.wait()
5560

5661
let generatedObjectIDs = objectsWithGeneratedIDCreation.batchesResponse.objectIDs.compactMap { $0 }
5762
objectsWithGeneratedID = zip(objectsWithGeneratedID, generatedObjectIDs).map { $0.0.set(\.objectID, to: $0.1) }
5863

64+
// Sequentially send 10 batches of 100 objects with objectID from 1 to 1000 with batch and collect taskIDs/objectIDs
5965
var batchRecords: [TestRecord] = []
6066
let chunkSize = 100
6167
let chunkCount = 10
6268
for startIndex in stride(from: 0, to: chunkSize * chunkCount, by: chunkSize) {
6369
let records = (startIndex..<startIndex + chunkSize).map { TestRecord(objectID: "\($0)") }
6470
batchRecords.append(contentsOf: records)
71+
// Wait for all collected tasks to terminate with waitTask
6572
try index.saveObjects(records).wait()
6673
}
67-
68-
74+
75+
// Retrieve the 6 first records with getObject and check their content against original records
76+
// Retrieve the 1000 remaining records with getObjects with objectIDs from 1 to 1000 and check their content against original records
6977
let firstObjectsIDs = [objectID, generatedObjectID] + objectsIDs + generatedObjectIDs
7078

7179
let expectedObjects = [object, objectWithGeneratedID] + objects + objectsWithGeneratedID
@@ -74,6 +82,7 @@ class IndexingIntegrationTests: OnlineTestCase {
7482
XCTAssertEqual(expected, fetched)
7583
}
7684

85+
// Browse all records with browseObjects and make sure we have browsed 1006 records, and check that all objectIDs are found
7786
var response = try index.browse()
7887

7988
var fetchedRecords: [TestRecord] = try response.extractHits()
@@ -94,22 +103,38 @@ class IndexingIntegrationTests: OnlineTestCase {
94103
XCTAssertEqual(value, fdict[key])
95104
}
96105

106+
// Alter 1 record with partialUpdateObject and collect taskID/objectID
97107
try index.partialUpdateObject(withID: objectID, with: .update(attribute: "string", value: "partiallyUpdated"), createIfNotExists: false).wait()
98108

109+
// Alter 2 records with partialUpdateObjects and collect taskID/objectID
99110
try index.partialUpdateObjects(updates: [(objectsIDs[0], .update(attribute: "string", value: "partiallyUpdated")), (objectsIDs[1], .increment(attribute: "numeric", value: 10))], createIfNotExists: false).wait()
100111

101-
112+
// Wait for all collected tasks to terminate with waitTask
113+
// Retrieve all the previously altered records with getObject and check their content against the modified records
102114
let updated: [TestRecord] = try index.getObjects(withIDs: [objectID] + objectsIDs).results.compactMap { $0 }
103115

104116
XCTAssertEqual(updated.first { $0.objectID == objectID }, object.set(\.string, to: "partiallyUpdated"))
105117
XCTAssertEqual(updated.first { $0.objectID == objectsIDs.first! }, objects.first!.set(\.string, to: "partiallyUpdated"))
106118
XCTAssertEqual(updated.first { $0.objectID == objectsIDs.last! }, objects.last!.set(\.numeric, to: objects.last!.numeric + 10))
107119

120+
// Add 1 record with saveObject with an objectID and a tag algolia and wait for the task to finish
121+
let taggedRecord = TestRecord(objectID: "taggedObject").set(\._tags, to: ["algolia"])
122+
try index.saveObject(taggedRecord).wait()
123+
124+
// Delete the first record with deleteObject and collect taskID
108125
try index.deleteObject(withID: objectID).wait()
126+
127+
// Delete the record containing the tag algolia with deleteBy and the tagFilters option and collect taskID
128+
try index.deleteObjects(byQuery: DeleteByQuery().set(\.filters, to: "algolia")).wait()
129+
130+
// Delete the 5 remaining first records with deleteObjects and collect taskID
109131
try index.deleteObjects(withIDs: [generatedObjectID] + objectsIDs + generatedObjectIDs).wait()
110-
try index.clearObjects().wait()
111132

133+
// Delete the 1000 remaining records with clearObjects and collect taskID
134+
// Wait for all collected tasks to terminate
135+
try index.clearObjects().wait()
112136

137+
// Browse all objects with browseObjects and make sure that no records are returned
113138
response = try index.browse()
114139
XCTAssertEqual(response.nbHits, 0)
115140

0 commit comments

Comments
 (0)