From 400ffa38750554c2e3ae8780322ae30f8e44df48 Mon Sep 17 00:00:00 2001 From: Dianna Ma Date: Thu, 24 Oct 2024 21:05:21 +0000 Subject: [PATCH] migrate Collections tests --- .../Collections/Collections.swift | 100 ------------------ Sources/MongoDBTests/Main.swift | 2 - Sources/MongoDBTests2/Collections.swift | 72 +++++++++++++ 3 files changed, 72 insertions(+), 102 deletions(-) delete mode 100644 Sources/MongoDBTests/Collections/Collections.swift create mode 100644 Sources/MongoDBTests2/Collections.swift diff --git a/Sources/MongoDBTests/Collections/Collections.swift b/Sources/MongoDBTests/Collections/Collections.swift deleted file mode 100644 index c0b3df30..00000000 --- a/Sources/MongoDBTests/Collections/Collections.swift +++ /dev/null @@ -1,100 +0,0 @@ -import MongoDB -import MongoTesting - -struct Collections:MongoTestBattery where Configuration:MongoTestConfiguration -{ - static - func run(tests:TestGroup, pool:Mongo.SessionPool, database:Mongo.Database) async throws - { - var collections:[Mongo.Collection] = (0 ..< 32).map { .init($0.description) } - let session:Mongo.Session = try await .init(from: pool) - - do - { - let tests:TestGroup = tests ! "create" - await tests.do - { - for collection:Mongo.Collection in collections - { - await (tests ! collection.name).do - { - try await session.run( - command: Mongo.Create.init(collection), - against: database) - } - } - } - } - if let tests:TestGroup = tests / "list-collections" / "bindings" - { - await tests.do - { - try await session.run( - command: Mongo.ListCollections.init(stride: 10), - against: database) - { - var collections:Set = .init(collections) - for try await batch:[Mongo.CollectionBinding] in $0 - { - tests.expect(true: batch.count <= 10) - for binding:Mongo.CollectionBinding in batch - { - let tests:TestGroup = tests ! binding.collection.name - - tests.expect(value: collections.remove(binding.collection)) - tests.expect(binding.type ==? .collection) - } - } - tests.expect(collections **? []) - } - } - } - if let tests:TestGroup = tests / "list-collections" / "metadata" - { - await tests.do - { - try await session.run( - command: Mongo.ListCollections.init(stride: 10), - against: database) - { - var collections:Set = .init(collections) - for try await batch:[Mongo.CollectionMetadata] in $0 - { - tests.expect(true: batch.count <= 10) - for metadata:Mongo.CollectionMetadata in batch - { - let tests:TestGroup = tests ! metadata.collection.name - - tests.expect(value: collections.remove(metadata.collection)) - tests.expect(metadata.type ==? .collection) - } - } - tests.expect(collections **? []) - } - } - } - if let tests:TestGroup = tests / "rename-collection" - { - await tests.do - { - let target:Mongo.Namespaced = database | "renamed" - try await session.run( - command: Mongo.RenameCollection.init(database | collections[0], to: target), - against: .admin) - - collections[0] = target.collection - } - } - if let tests:TestGroup = tests / "drop" - { - for collection:Mongo.Collection in collections - { - await (tests ! collection.name).do - { - try await session.run(command: Mongo.Drop.init(collection), - against: database) - } - } - } - } -} diff --git a/Sources/MongoDBTests/Main.swift b/Sources/MongoDBTests/Main.swift index 47d7ff0c..711ff417 100644 --- a/Sources/MongoDBTests/Main.swift +++ b/Sources/MongoDBTests/Main.swift @@ -9,7 +9,6 @@ enum Main:TestMain let all:[any TestBattery.Type] = [ ChangeStreams .self, - Collections .self, Cursors .self, Databases .self, Fsync .self, @@ -21,7 +20,6 @@ enum Main:TestMain // Note: these tests generally fail in debug mode because it takes a long time to // complete cryptographic authentication, and the driver will time out before it // completes. - Collections .self, Cursors .self, Databases .self, Fsync .self, diff --git a/Sources/MongoDBTests2/Collections.swift b/Sources/MongoDBTests2/Collections.swift new file mode 100644 index 00000000..5472d99e --- /dev/null +++ b/Sources/MongoDBTests2/Collections.swift @@ -0,0 +1,72 @@ +import MongoDB +import Testing + +@Suite +struct Collections:Mongo.TestBattery +{ + let database:Mongo.Database = "Collections" + + @Test(arguments: [.single, .replicated] as [any Mongo.TestConfiguration]) + func collections(_ configuration:any Mongo.TestConfiguration) async throws + { + try await self.run(under: configuration) + } + + func run(with pool:Mongo.SessionPool) async throws + { + var collections:[Mongo.Collection] = (0 ..< 32).map { .init($0.description) } + let session:Mongo.Session = try await .init(from: pool) + + for collection:Mongo.Collection in collections + { + try await session.run( + command: Mongo.Create.init(collection), + against: self.database) + } + + try await session.run( + command: Mongo.ListCollections.init(stride: 10), + against: self.database) + { + var collections:Set = .init(collections) + for try await batch:[Mongo.CollectionBinding] in $0 + { + #expect(batch.count <= 10) + for binding:Mongo.CollectionBinding in batch + { + #expect(collections.remove(binding.collection) != nil) + #expect(binding.type == .collection) + } + } + #expect(collections == []) + } + try await session.run( + command: Mongo.ListCollections.init(stride: 10), + against: self.database) + { + var collections:Set = .init(collections) + for try await batch:[Mongo.CollectionMetadata] in $0 + { + #expect(batch.count <= 10) + for metadata:Mongo.CollectionMetadata in batch + { + #expect(collections.remove(metadata.collection) != nil) + #expect(metadata.type == .collection) + } + } + #expect(collections == []) + } + + let target:Mongo.Namespaced = self.database | "Renamed" + try await session.run( + command: Mongo.RenameCollection.init(self.database | collections[0], to: target), + against: .admin) + + collections[0] = target.collection + + for collection:Mongo.Collection in collections + { + try await session.run(command: Mongo.Drop.init(collection), against: self.database) + } + } +}