@@ -30,7 +30,11 @@ final class CrudTests: MongoSwiftTestCase {
3030 print ( " \n ------------ \n Executing tests from file \( dir) / \( filename) ... \n " )
3131
3232 // For each file, execute the test cases contained in it
33- for (i, test) in file. tests. enumerated ( ) {
33+ for (i, test) in try file. makeTests ( ) . enumerated ( ) {
34+ if type ( of: test) == CountTest . self {
35+ print ( " Skipping test for old count API, no longer supported by the driver " )
36+ }
37+
3438 print ( " Executing test: \( test. description) " )
3539
3640 // for each test case:
@@ -40,10 +44,18 @@ final class CrudTests: MongoSwiftTestCase {
4044 // 4) verify that expected data is present
4145 // 5) drop the collection to clean up
4246 let collection = db. collection ( self . getCollectionName ( suffix: " \( filename) _ \( i) " ) )
43- try collection. insertMany ( file. data)
47+ if !file. data. isEmpty {
48+ try collection. insertMany ( file. data)
49+ }
4450 try test. execute ( usingCollection: collection)
4551 try test. verifyData ( testCollection: collection, db: db)
46- try collection. drop ( )
52+ do {
53+ try collection. drop ( )
54+ } catch let ServerError . commandError( code, _, _, _) where code == 26 {
55+ // ignore ns not found errors
56+ } catch {
57+ throw error
58+ }
4759 }
4860 }
4961 print ( ) // for readability of results
@@ -64,7 +76,11 @@ final class CrudTests: MongoSwiftTestCase {
6476private struct CrudTestFile : Decodable {
6577 let data : [ Document ]
6678 let testDocs : [ Document ]
67- var tests : [ CrudTest ] { return try ! self . testDocs. map { try makeCrudTest ( $0) } }
79+
80+ func makeTests( ) throws -> [ CrudTest ] {
81+ return try self . testDocs. map { try makeCrudTest ( $0) }
82+ }
83+
6884 let minServerVersion : String ?
6985 let maxServerVersion : String ?
7086
@@ -88,9 +104,11 @@ private var testTypeMap: [String: CrudTest.Type] = [
88104 " aggregate " : AggregateTest . self,
89105 " bulkWrite " : BulkWriteTest . self,
90106 " count " : CountTest . self,
107+ " countDocuments " : CountDocumentsTest . self,
91108 " deleteMany " : DeleteTest . self,
92109 " deleteOne " : DeleteTest . self,
93110 " distinct " : DistinctTest . self,
111+ " estimatedDocumentCount " : EstimatedDocumentCountTest . self,
94112 " find " : FindTest . self,
95113 " findOneAndDelete " : FindOneAndDeleteTest . self,
96114 " findOneAndUpdate " : FindOneAndUpdateTest . self,
@@ -270,10 +288,24 @@ private class BulkWriteTest: CrudTest {
270288
271289/// A class for executing `count` tests
272290private class CountTest : CrudTest {
291+ override func execute( usingCollection _: SyncMongoCollection < Document > ) throws { }
292+ }
293+
294+ /// A class for executing `countDocuments` tests
295+ private class CountDocumentsTest : CrudTest {
273296 override func execute( usingCollection coll: SyncMongoCollection < Document > ) throws {
274297 let filter : Document = try self . args. get ( " filter " )
275- let options = CountOptions ( collation: self . collation, limit: self . limit, skip: self . skip)
276- let result = try coll. count ( filter, options: options)
298+ let options = CountDocumentsOptions ( collation: self . collation, limit: self . limit, skip: self . skip)
299+ let result = try coll. countDocuments ( filter, options: options)
300+ expect ( result) . to ( equal ( self . result? . asInt ( ) ) )
301+ }
302+ }
303+
304+ /// A class for executing `estimatedDocumentCount` tests
305+ private class EstimatedDocumentCountTest : CrudTest {
306+ override func execute( usingCollection coll: SyncMongoCollection < Document > ) throws {
307+ let options = EstimatedDocumentCountOptions ( )
308+ let result = try coll. estimatedDocumentCount ( options: options)
277309 expect ( result) . to ( equal ( self . result? . asInt ( ) ) )
278310 }
279311}
0 commit comments