Skip to content

Commit

Permalink
Add the ability to test all three database drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynne committed May 6, 2024
1 parent 9a78b80 commit 967b195
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ let package = Package(
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.29.2"),
.package(url: "https://github.com/vapor/queues.git", from: "1.13.0"),
.package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.6.0"),
.package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.9.0"),
.package(url: "https://github.com/vapor/fluent-mysql-driver.git", from: "4.0.0"),
],
targets: [
.target(
Expand All @@ -35,6 +37,8 @@ let package = Package(
dependencies: [
.product(name: "XCTVapor", package: "vapor"),
.product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"),
.product(name: "FluentPostgresDriver", package: "fluent-postgres-driver"),
.product(name: "FluentMySQLDriver", package: "fluent-mysql-driver"),
.target(name: "QueuesFluentDriver"),
],
swiftSettings: swiftSettings
Expand Down
4 changes: 4 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ let package = Package(
.package(url: "https://github.com/vapor/sql-kit.git", from: "3.29.2"),
.package(url: "https://github.com/vapor/queues.git", from: "1.13.0"),
.package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.6.0"),
.package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.9.0"),
.package(url: "https://github.com/vapor/fluent-mysql-driver.git", from: "4.0.0"),
],
targets: [
.target(
Expand All @@ -38,6 +40,8 @@ let package = Package(
dependencies: [
.product(name: "XCTVapor", package: "vapor"),
.product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"),
.product(name: "FluentPostgresDriver", package: "fluent-postgres-driver"),
.product(name: "FluentMySQLDriver", package: "fluent-mysql-driver"),
.target(name: "QueuesFluentDriver"),
],
swiftSettings: swiftSettings
Expand Down
49 changes: 38 additions & 11 deletions Tests/QueuesFluentDriverTests/QueuesFluentDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,46 @@ import Logging
@testable import QueuesFluentDriver
@preconcurrency import Queues
import FluentSQLiteDriver
import FluentPostgresDriver
import FluentMySQLDriver
import NIOSSL

final class QueuesFluentDriverTests: XCTestCase {
var dbid: DatabaseID { .sqlite }

private func useDbs(_ app: Application) throws {
app.databases.use(.sqlite(.memory), as: .sqlite)
app.databases.use(DatabaseConfigurationFactory.postgres(configuration: .init(
hostname: Environment.get("DATABASE_HOST") ?? "localhost",
port: Environment.get("DATABASE_PORT").flatMap(Int.init(_:)) ?? SQLPostgresConfiguration.ianaPortNumber,
username: Environment.get("DATABASE_USERNAME") ?? "test_username",
password: Environment.get("DATABASE_PASSWORD") ?? "test_password",
database: Environment.get("DATABASE_NAME") ?? "test_database",
tls: .prefer(try .init(configuration: .clientDefault)))
), as: .psql)
var config = TLSConfiguration.clientDefault
config.certificateVerification = .none
app.databases.use(DatabaseConfigurationFactory.mysql(configuration: .init(
hostname: Environment.get("DATABASE_HOST") ?? "localhost",
port: Environment.get("DATABASE_PORT").flatMap(Int.init(_:)) ?? MySQLConfiguration.ianaPortNumber,
username: Environment.get("DATABASE_USERNAME") ?? "test_username",
password: Environment.get("DATABASE_PASSWORD") ?? "test_password",
database: Environment.get("DATABASE_NAME") ?? "test_database",
tlsConfiguration: config
)), as: .mysql)
}

func testApplication() async throws {
let app = Application(.testing)
defer { app.shutdown() }

app.databases.use(.sqlite(.memory), as: .sqlite)
app.migrations.add(JobModelMigration())
try self.useDbs(app)
app.migrations.add(JobModelMigration(), to: self.dbid)

let email = Email()
app.queues.add(email)

app.queues.use(.fluent())
app.queues.use(.fluent(self.dbid))

try await app.autoMigrate()

Expand All @@ -41,10 +68,10 @@ final class QueuesFluentDriverTests: XCTestCase {
let app = Application(.testing)
defer { app.shutdown() }

app.databases.use(.sqlite(.memory), as: .sqlite)
try self.useDbs(app)
app.queues.add(FailingJob())
app.queues.use(.fluent())
app.migrations.add(JobModelMigration())
app.queues.use(.fluent(self.dbid))
app.migrations.add(JobModelMigration(), to: self.dbid)
try await app.autoMigrate()

let jobId = JobIdentifier()
Expand All @@ -61,7 +88,7 @@ final class QueuesFluentDriverTests: XCTestCase {
XCTAssert($0 is FailingJob.Failure)
}

await XCTAssertNotNilAsync(try await (app.databases.database(logger: .init(label: ""), on: app.eventLoopGroup.any())! as! any SQLDatabase)
await XCTAssertNotNilAsync(try await (app.databases.database(self.dbid, logger: .init(label: ""), on: app.eventLoopGroup.any())! as! any SQLDatabase)
.select().columns("*").from(JobModel.schema).where("id", .equal, jobId.string).first())

try await app.autoRevert()
Expand All @@ -71,13 +98,13 @@ final class QueuesFluentDriverTests: XCTestCase {
let app = Application(.testing)
defer { app.shutdown() }

app.databases.use(.sqlite(.memory), as: .sqlite)
try self.useDbs(app)

app.queues.add(DelayedJob())

app.queues.use(.fluent())
app.queues.use(.fluent(self.dbid))

app.migrations.add(JobModelMigration())
app.migrations.add(JobModelMigration(), to: self.dbid)
try await app.autoMigrate()

let jobId = JobIdentifier()
Expand All @@ -92,7 +119,7 @@ final class QueuesFluentDriverTests: XCTestCase {
XCTAssertEqual(res.status, .ok)
}

await XCTAssertEqualAsync(try await (app.databases.database(logger: .init(label: ""), on: app.eventLoopGroup.any())! as! any SQLDatabase)
await XCTAssertEqualAsync(try await (app.databases.database(self.dbid, logger: .init(label: ""), on: app.eventLoopGroup.any())! as! any SQLDatabase)
.select().columns("*").from(JobModel.schema).where("id", .equal, jobId.string)
.first(decoding: JobModel.self, keyDecodingStrategy: .convertFromSnakeCase)?.state, .pending)

Expand Down

0 comments on commit 967b195

Please sign in to comment.