Skip to content

Commit

Permalink
migrate Aggregate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tayloraswift committed Oct 24, 2024
1 parent 26c57bb commit 25cb251
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 92 deletions.
57 changes: 0 additions & 57 deletions Sources/MongoDBTests/Aggregate/Aggregate.Article.swift

This file was deleted.

2 changes: 0 additions & 2 deletions Sources/MongoDBTests/Main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ enum Main:TestMain
static
let all:[any TestBattery.Type] =
[
Aggregate <ReplicatedConfiguration>.self,
ChangeStreams <ReplicatedConfiguration>.self,
Collections <ReplicatedConfiguration>.self,
Cursors <ReplicatedConfiguration>.self,
Expand All @@ -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 <SingleConfiguration>.self,
Collections <SingleConfiguration>.self,
Cursors <SingleConfiguration>.self,
Databases <SingleConfiguration>.self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
@_spi(session)
import MongoDB
import MongoTesting
@_spi(session) import MongoDB
import Testing

struct Aggregate<Configuration>: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",
Expand All @@ -39,15 +48,14 @@ struct Aggregate<Configuration>: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<TagStats> = [
.init(id: "medicine", count: 1),
.init(id: "neuroscience", count: 1),
.init(id: "education", count: 1),
Expand All @@ -56,7 +64,7 @@ struct Aggregate<Configuration>:MongoTestBattery where Configuration:MongoTestCo
.init(id: "autobiography", count: 2),
]
let response:[TagStats] = try await session.run(
command: Mongo.Aggregate<Mongo.Cursor<TagStats>>.init(collection,
command: Mongo.Aggregate<Mongo.Cursor<TagStats>>.init(self.collection,
writeConcern: .majority,
readConcern: .majority,
stride: 10)
Expand All @@ -70,23 +78,22 @@ struct Aggregate<Configuration>: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<TagStats>.init(response) == expected)
}

await tests.do
do
{
let expected:[AuthorStats] =
[
let expected:Set<AuthorStats> = [
.init(id: "barbie", views: 527 + 760),
.init(id: "raquelle", views: 288 + 115),
]
let response:[AuthorStats] = try await session.run(
command: Mongo.Aggregate<Mongo.Cursor<AuthorStats>>.init(collection,
command: Mongo.Aggregate<Mongo.Cursor<AuthorStats>>.init(self.collection,
writeConcern: .majority,
readConcern: .majority,
stride: 10)
Expand All @@ -103,22 +110,14 @@ struct Aggregate<Configuration>: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<AuthorStats>.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)
}
}
54 changes: 54 additions & 0 deletions Sources/MongoDBTests2/Documents/Article.swift
Original file line number Diff line number Diff line change
@@ -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<CodingKey>) 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<CodingKey>)
{
bson[.id] = self.id
bson[.author] = self.author
bson[.title] = self.title
bson[.views] = self.views
bson[.tags] = self.tags
}
}

0 comments on commit 25cb251

Please sign in to comment.