From 25cb251715d0989adecce2dde67611bf2ea80a72 Mon Sep 17 00:00:00 2001 From: Dianna Ma Date: Thu, 24 Oct 2024 20:57:52 +0000 Subject: [PATCH] migrate Aggregate tests --- .../Aggregate/Aggregate.Article.swift | 57 ---------------- Sources/MongoDBTests/Main.swift | 2 - .../Aggregate.AuthorStats.swift | 0 .../Aggregate.TagStats.swift | 0 .../Aggregate.swift | 65 +++++++++---------- Sources/MongoDBTests2/Documents/Article.swift | 54 +++++++++++++++ 6 files changed, 86 insertions(+), 92 deletions(-) delete mode 100644 Sources/MongoDBTests/Aggregate/Aggregate.Article.swift rename Sources/{MongoDBTests/Aggregate => MongoDBTests2}/Aggregate.AuthorStats.swift (100%) rename Sources/{MongoDBTests/Aggregate => MongoDBTests2}/Aggregate.TagStats.swift (100%) rename Sources/{MongoDBTests/Aggregate => MongoDBTests2}/Aggregate.swift (73%) create mode 100644 Sources/MongoDBTests2/Documents/Article.swift diff --git a/Sources/MongoDBTests/Aggregate/Aggregate.Article.swift b/Sources/MongoDBTests/Aggregate/Aggregate.Article.swift deleted file mode 100644 index 2720a0de..00000000 --- a/Sources/MongoDBTests/Aggregate/Aggregate.Article.swift +++ /dev/null @@ -1,57 +0,0 @@ -import BSON -import MongoQL - -extension Aggregate -{ - struct Article:Equatable, Hashable, - BSONDocumentDecodable, - BSONDocumentEncodable, - Mongo.MasterCodingModel - { - let id:BSON.Identifier - let author:String - let title:String - let views:Int - let tags:[String] - - init(id:BSON.Identifier, - author:String, - title:String, - views:Int, - tags:[String]) - { - self.id = id - self.author = author - self.title = title - self.views = views - self.tags = tags - } - - enum CodingKey:String, Sendable - { - case id = "_id" - case author - case title - case views - case tags - } - - init(bson:BSON.DocumentDecoder) throws - { - self.init(id: try bson[.id].decode(), - author: try bson[.author].decode(), - title: try bson[.title].decode(), - views: try bson[.views].decode(), - tags: try bson[.tags].decode()) - } - - func encode(to bson:inout BSON.DocumentEncoder) - { - bson[.id] = self.id - bson[.author] = self.author - bson[.title] = self.title - bson[.views] = self.views - bson[.tags] = self.tags - } - } -} diff --git a/Sources/MongoDBTests/Main.swift b/Sources/MongoDBTests/Main.swift index 86e5b7b3..47d7ff0c 100644 --- a/Sources/MongoDBTests/Main.swift +++ b/Sources/MongoDBTests/Main.swift @@ -8,7 +8,6 @@ enum Main:TestMain static let all:[any TestBattery.Type] = [ - Aggregate .self, ChangeStreams .self, Collections .self, Cursors .self, @@ -22,7 +21,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. - Aggregate .self, Collections .self, Cursors .self, Databases .self, diff --git a/Sources/MongoDBTests/Aggregate/Aggregate.AuthorStats.swift b/Sources/MongoDBTests2/Aggregate.AuthorStats.swift similarity index 100% rename from Sources/MongoDBTests/Aggregate/Aggregate.AuthorStats.swift rename to Sources/MongoDBTests2/Aggregate.AuthorStats.swift diff --git a/Sources/MongoDBTests/Aggregate/Aggregate.TagStats.swift b/Sources/MongoDBTests2/Aggregate.TagStats.swift similarity index 100% rename from Sources/MongoDBTests/Aggregate/Aggregate.TagStats.swift rename to Sources/MongoDBTests2/Aggregate.TagStats.swift diff --git a/Sources/MongoDBTests/Aggregate/Aggregate.swift b/Sources/MongoDBTests2/Aggregate.swift similarity index 73% rename from Sources/MongoDBTests/Aggregate/Aggregate.swift rename to Sources/MongoDBTests2/Aggregate.swift index c1457dfd..f9889b9d 100644 --- a/Sources/MongoDBTests/Aggregate/Aggregate.swift +++ b/Sources/MongoDBTests2/Aggregate.swift @@ -1,20 +1,29 @@ -@_spi(session) -import MongoDB -import MongoTesting +@_spi(session) import MongoDB +import Testing -struct Aggregate:MongoTestBattery where Configuration:MongoTestConfiguration +@Suite +struct Aggregate:Mongo.TestBattery { - static - func run(tests:TestGroup, pool:Mongo.SessionPool, database:Mongo.Database) async throws + let collection:Mongo.Collection = "Articles" + let database:Mongo.Database = "Aggregate" + + // This test is based on the tutorial from: + // https://www.mongodb.com/docs/manual/reference/command/update/#examples + @Test(arguments: [.single, .replicated] as [any Mongo.TestConfiguration]) + func aggregate(_ configuration:any Mongo.TestConfiguration) async throws { - let session:Mongo.Session = try await .init(from: pool) - let collection:Mongo.Collection = "articles" + try await self.run(under: configuration) + } - await tests.do + func run(with pool:Mongo.SessionPool) async throws + { + let session:Mongo.Session = try await .init(from: pool) + do { let expected:Mongo.InsertResponse = .init(inserted: 4) let response:Mongo.InsertResponse = try await session.run( - command: Mongo.Insert.init(collection, encoding: [ + command: Mongo.Insert.init(self.collection, + encoding: [ .init(id: 0x5276_9ea0_f3dc_6ead_47c9_a1b2, author: "barbie", title: "Brain Surgery for Beginners", @@ -39,15 +48,14 @@ struct Aggregate:MongoTestBattery where Configuration:MongoTestCo views: 115, tags: ["history", "autobiography"]), ] as [Article]), - against: database) + against: self.database) - tests.expect(response ==? expected) + #expect(response == expected) } - await tests.do + do { - let expected:[TagStats] = - [ + let expected:Set = [ .init(id: "medicine", count: 1), .init(id: "neuroscience", count: 1), .init(id: "education", count: 1), @@ -56,7 +64,7 @@ struct Aggregate:MongoTestBattery where Configuration:MongoTestCo .init(id: "autobiography", count: 2), ] let response:[TagStats] = try await session.run( - command: Mongo.Aggregate>.init(collection, + command: Mongo.Aggregate>.init(self.collection, writeConcern: .majority, readConcern: .majority, stride: 10) @@ -70,23 +78,22 @@ struct Aggregate:MongoTestBattery where Configuration:MongoTestCo $0[TagStats[.count]] = .init { $0[.sum] = 1 } } }, - against: database) + against: self.database) { try await $0.reduce(into: []) { $0 += $1 } } - tests.expect(response **? expected) + #expect(Set.init(response) == expected) } - await tests.do + do { - let expected:[AuthorStats] = - [ + let expected:Set = [ .init(id: "barbie", views: 527 + 760), .init(id: "raquelle", views: 288 + 115), ] let response:[AuthorStats] = try await session.run( - command: Mongo.Aggregate>.init(collection, + command: Mongo.Aggregate>.init(self.collection, writeConcern: .majority, readConcern: .majority, stride: 10) @@ -103,22 +110,14 @@ struct Aggregate:MongoTestBattery where Configuration:MongoTestCo $0[Article[.views]] = .init { $0[.sum] = Article[.views] } } }, - against: database) + against: self.database) { try await $0.reduce(into: []) { $0 += $1 } } - tests.expect(response **? expected) - } - - guard - let tests:TestGroup = tests / "CollectionStats" - else - { - return + #expect(Set.init(response) == expected) } - let _:Mongo.CollectionStats? = tests.expect(value: try await session.stats( - collection: database | collection)) + #expect(try await session.stats(collection: self.database | self.collection) != nil) } } diff --git a/Sources/MongoDBTests2/Documents/Article.swift b/Sources/MongoDBTests2/Documents/Article.swift new file mode 100644 index 00000000..718850b2 --- /dev/null +++ b/Sources/MongoDBTests2/Documents/Article.swift @@ -0,0 +1,54 @@ +import BSON +import MongoQL + +struct Article:Equatable, Hashable, + BSONDocumentDecodable, + BSONDocumentEncodable, + Mongo.MasterCodingModel +{ + let id:BSON.Identifier + let author:String + let title:String + let views:Int + let tags:[String] + + init(id:BSON.Identifier, + author:String, + title:String, + views:Int, + tags:[String]) + { + self.id = id + self.author = author + self.title = title + self.views = views + self.tags = tags + } + + enum CodingKey:String, Sendable + { + case id = "_id" + case author + case title + case views + case tags + } + + init(bson:BSON.DocumentDecoder) throws + { + self.init(id: try bson[.id].decode(), + author: try bson[.author].decode(), + title: try bson[.title].decode(), + views: try bson[.views].decode(), + tags: try bson[.tags].decode()) + } + + func encode(to bson:inout BSON.DocumentEncoder) + { + bson[.id] = self.id + bson[.author] = self.author + bson[.title] = self.title + bson[.views] = self.views + bson[.tags] = self.tags + } +}