Skip to content

Commit 142b2bb

Browse files
authored
SWIFT-710 Audit tests to ensure all are re-enabled (#397)
1 parent 3486d9a commit 142b2bb

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

Tests/LinuxMain.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,21 @@ extension MongoCollection_BulkWriteTests {
190190
]
191191
}
192192

193+
extension MongoCollection_IndexTests {
194+
static var allTests = [
195+
("testCreateIndexFromModel", testCreateIndexFromModel),
196+
("testIndexOptions", testIndexOptions),
197+
("testCreateIndexesFromModels", testCreateIndexesFromModels),
198+
("testCreateIndexFromKeys", testCreateIndexFromKeys),
199+
("testDropIndexByName", testDropIndexByName),
200+
("testDropIndexByModel", testDropIndexByModel),
201+
("testDropIndexByKeys", testDropIndexByKeys),
202+
("testDropAllIndexes", testDropAllIndexes),
203+
("testListIndexNames", testListIndexNames),
204+
("testCreateDropIndexByModelWithMaxTimeMS", testCreateDropIndexByModelWithMaxTimeMS),
205+
]
206+
}
207+
193208
extension MongoCursorTests {
194209
static var allTests = [
195210
("testNonTailableCursor", testNonTailableCursor),
@@ -329,6 +344,7 @@ XCTMain([
329344
testCase(MongoClientTests.allTests),
330345
testCase(MongoCollectionTests.allTests),
331346
testCase(MongoCollection_BulkWriteTests.allTests),
347+
testCase(MongoCollection_IndexTests.allTests),
332348
testCase(MongoCursorTests.allTests),
333349
testCase(MongoDatabaseTests.allTests),
334350
testCase(OptionsTests.allTests),

Tests/MongoSwiftSyncTests/CommandMonitoringTests.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,29 @@ private struct CMTestFile: Decodable {
111111
}
112112
}
113113

114+
extension ReadPreference.Mode: Decodable {}
115+
116+
extension ReadPreference: Decodable {
117+
public convenience init(from decoder: Decoder) throws {
118+
let container = try decoder.container(keyedBy: CodingKeys.self)
119+
let mode = try container.decode(Mode.self, forKey: .mode)
120+
self.init(mode)
121+
}
122+
123+
private enum CodingKeys: String, CodingKey {
124+
case mode
125+
}
126+
}
127+
114128
/// A struct to hold the data for a single test from a CMTestFile.
115129
private struct CMTest: Decodable {
116130
struct Operation: Decodable {
117131
let name: String
118132
let args: Document
119-
let readPreference: Document?
133+
let readPreference: ReadPreference?
120134

121135
enum CodingKeys: String, CodingKey {
122-
case name, args = "arguments", readPreference
136+
case name, args = "arguments", readPreference = "read_preference"
123137
}
124138
}
125139

@@ -148,12 +162,12 @@ private struct CMTest: Decodable {
148162
// events won't match up.
149163
// swiftlint:disable cyclomatic_complexity
150164
func doOperation(withCollection collection: MongoCollection<Document>) throws {
151-
// TODO: SWIFT-31: use readPreferences for commands if provided
152165
let filter: Document = self.op.args["filter"]?.documentValue ?? [:]
153166

154167
switch self.op.name {
155168
case "count":
156-
_ = try? collection.countDocuments(filter)
169+
let options = CountDocumentsOptions(readPreference: self.op.readPreference)
170+
_ = try? collection.countDocuments(filter, options: options)
157171
case "deleteMany":
158172
_ = try? collection.deleteMany(filter)
159173
case "deleteOne":
@@ -173,6 +187,7 @@ private struct CMTest: Decodable {
173187
max: modifiers?["$max"]?.documentValue,
174188
maxTimeMS: modifiers?["$maxTimeMS"]?.asInt64(),
175189
min: modifiers?["$min"]?.documentValue,
190+
readPreference: self.op.readPreference,
176191
returnKey: modifiers?["$returnKey"]?.boolValue,
177192
showRecordId: modifiers?["$showDiskLoc"]?.boolValue,
178193
skip: self.op.args["skip"]?.asInt64(),

Tests/DisabledTests/MongoCollection+IndexTests.swift renamed to Tests/MongoSwiftSyncTests/MongoCollection+IndexTests.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import MongoSwift
1+
import MongoSwiftSync
22
import Nimble
33
import TestsCommon
44
import XCTest
@@ -161,8 +161,7 @@ final class MongoCollection_IndexTests: MongoSwiftTestCase {
161161
let model = IndexModel(keys: ["cat": 1])
162162
expect(try self.coll.createIndex(model)).to(equal("cat_1"))
163163

164-
let res = try self.coll.dropIndex(model)
165-
expect(res["ok"]?.asDouble()).to(equal(1.0))
164+
expect(try self.coll.dropIndex(model)).toNot(throwError())
166165

167166
// now there should only be _id_ left
168167
let indexes = try coll.listIndexes()
@@ -175,8 +174,7 @@ final class MongoCollection_IndexTests: MongoSwiftTestCase {
175174
let model = IndexModel(keys: ["cat": 1])
176175
expect(try self.coll.createIndex(model)).to(equal("cat_1"))
177176

178-
let res = try self.coll.dropIndex(["cat": 1])
179-
expect(res["ok"]?.asDouble()).to(equal(1.0))
177+
expect(try self.coll.dropIndex(["cat": 1])).toNot(throwError())
180178

181179
// now there should only be _id_ left
182180
let indexes = try coll.listIndexes()
@@ -189,8 +187,7 @@ final class MongoCollection_IndexTests: MongoSwiftTestCase {
189187
let model = IndexModel(keys: ["cat": 1])
190188
expect(try self.coll.createIndex(model)).to(equal("cat_1"))
191189

192-
let res = try self.coll.dropIndexes()
193-
expect(res["ok"]?.asDouble()).to(equal(1.0))
190+
expect(try self.coll.dropIndexes()).toNot(throwError())
194191

195192
// now there should only be _id_ left
196193
let indexes = try coll.listIndexes()
@@ -236,8 +233,7 @@ final class MongoCollection_IndexTests: MongoSwiftTestCase {
236233
expect(try collection.createIndex(model, options: createIndexOpts)).to(equal("cat_1"))
237234

238235
let dropIndexOpts = DropIndexOptions(maxTimeMS: maxTimeMS, writeConcern: wc)
239-
let res = try collection.dropIndex(model, options: dropIndexOpts)
240-
expect(res["ok"]?.asDouble()).to(equal(1.0))
236+
expect(try collection.dropIndex(model, options: dropIndexOpts)).toNot(throwError())
241237

242238
// now there should only be _id_ left
243239
let indexes = try coll.listIndexes()

Tests/MongoSwiftSyncTests/ReadWriteConcernOperationTests.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,16 @@ final class ReadWriteConcernOperationTests: MongoSwiftTestCase {
182182
expect(try coll.deleteMany(["x": 9], options: DeleteOptions(writeConcern: wc1))).toNot(throwError())
183183
expect(try coll.deleteMany(["x": 10], options: DeleteOptions(writeConcern: wc3))).toNot(throwError())
184184

185-
// TODO: SWIFT-702: uncomment these assertions
186-
// expect(try coll.createIndex(
187-
// ["x": 1],
188-
// options: CreateIndexOptions(writeConcern: wc1)
189-
// )).toNot(throwError())
190-
// expect(try coll.createIndexes(
191-
// [IndexModel(keys: ["x": -1])],
192-
// options: CreateIndexOptions(writeConcern: wc3)
193-
// )).toNot(throwError())
194-
195-
// expect(try coll.dropIndex(["x": 1], options: DropIndexOptions(writeConcern: wc1))).toNot(throwError())
196-
// expect(try coll.dropIndexes(options: DropIndexOptions(writeConcern: wc3))).toNot(throwError())
185+
expect(try coll.createIndex(
186+
["x": 1],
187+
options: CreateIndexOptions(writeConcern: wc1)
188+
)).toNot(throwError())
189+
expect(try coll.createIndexes(
190+
[IndexModel(keys: ["x": -1])],
191+
options: CreateIndexOptions(writeConcern: wc3)
192+
)).toNot(throwError())
193+
194+
expect(try coll.dropIndex(["x": 1], options: DropIndexOptions(writeConcern: wc1))).toNot(throwError())
195+
expect(try coll.dropIndexes(options: DropIndexOptions(writeConcern: wc3))).toNot(throwError())
197196
}
198197
}

0 commit comments

Comments
 (0)