Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt swift testing #149

Merged
merged 21 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,10 @@ let package:Package = .init(name: "swift-mongodb",
.product(name: "Testing_", package: "swift-grammar"),
]),

.executableTarget(name: "MongoDBTests",
.testTarget(name: "MongoDBTests",
dependencies: [
.target(name: "MongoDB"),
.target(name: "MongoQL"),
.target(name: "MongoTesting"),
]),

.executableTarget(name: "MongoDriverTests",
Expand Down
15 changes: 12 additions & 3 deletions Scripts/TestAll
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#!/bin/bash
set -e
swift --version
swift test -c release
swift build -c release

# 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.
swift test -c release \
--no-parallel \
--explicit-target-dependency-import-check=error

swift build -c release \
--explicit-target-dependency-import-check=error

for f in .build/release/*Tests; do
$f
$f
done
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
@_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"

@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 +46,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 +62,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 +76,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 +108,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)
}
}
57 changes: 0 additions & 57 deletions Sources/MongoDBTests/Aggregate/Aggregate.Article.swift

This file was deleted.

Loading
Loading