Skip to content

Commit 79a3fa3

Browse files
committed
Update AppTestCase
1 parent 2a6a622 commit 79a3fa3

File tree

1 file changed

+33
-86
lines changed

1 file changed

+33
-86
lines changed

Tests/AppTests/AppTestCase.swift

Lines changed: 33 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,16 @@ import Fluent
1919
import NIOConcurrencyHelpers
2020
import PostgresNIO
2121
import SQLKit
22+
#warning("drop all instances of import XCTVapor")
2223
import XCTVapor
2324

2425

25-
class AppTestCase: XCTestCase {
26-
var app: Application!
27-
let logger = CapturingLogger()
26+
#warning("move/rename")
27+
enum AppTestCase {
2828

29-
override func setUp() async throws {
30-
try await super.setUp()
31-
app = try await setup(.testing)
32-
33-
@Dependency(\.logger) var logger
34-
logger.set(to: self.logger)
35-
}
36-
37-
func setup(_ environment: Environment) async throws -> Application {
38-
try await withDependencies {
39-
// Setting builderToken here when it's also set in all tests may seem redundant but it's
40-
// what allows test_post_buildReport_large to work.
41-
// See https://github.com/pointfreeco/swift-dependencies/discussions/300#discussioncomment-11252906
42-
// for details.
43-
$0.environment.builderToken = { "secr3t" }
44-
} operation: {
45-
try await Self.setupDb(environment)
46-
return try await Self.setupApp(environment)
47-
}
48-
}
49-
50-
override func tearDown() async throws {
51-
try await app.asyncShutdown()
52-
try await super.tearDown()
53-
}
54-
}
55-
56-
57-
extension AppTestCase {
58-
59-
static func setupApp(_ environment: Environment) async throws -> Application {
29+
static func setupApp(_ environment: Environment, databasePort: Int) async throws -> Application {
6030
let app = try await Application.make(environment)
61-
try await configure(app)
31+
try await configure(app, databasePort: databasePort)
6232

6333
// Silence app logging
6434
app.logger = .init(label: "noop") { _ in SwiftLogNoOpLogHandler() }
@@ -67,7 +37,7 @@ extension AppTestCase {
6737
}
6838

6939

70-
static func setupDb(_ environment: Environment) async throws {
40+
static func setupDb(_ environment: Environment, databasePort: Int) async throws {
7141
await DotEnvFile.load(for: environment, fileio: .init(threadPool: .singleton))
7242

7343
// Ensure DATABASE_HOST is from a restricted set db hostnames and nothing else.
@@ -83,27 +53,29 @@ extension AppTestCase {
8353
// Create initial db snapshot on first run
8454
try await snapshotCreated.withValue { snapshotCreated in
8555
if !snapshotCreated {
86-
try await createSchema(environment, databaseName: testDbName)
87-
try await createSnapshot(original: testDbName, snapshot: snapshotName, environment: environment)
56+
try await createSchema(environment, databaseName: testDbName, databasePort: databasePort)
57+
try await createSnapshot(original: testDbName, snapshot: snapshotName, databasePort: databasePort, environment: environment)
8858
snapshotCreated = true
8959
}
9060
}
9161

92-
try await restoreSnapshot(original: testDbName, snapshot: snapshotName, environment: environment)
62+
try await restoreSnapshot(original: testDbName, snapshot: snapshotName, databasePort: databasePort, environment: environment)
9363
}
9464

9565

96-
static func createSchema(_ environment: Environment, databaseName: String) async throws {
66+
static func createSchema(_ environment: Environment,
67+
databaseName: String,
68+
databasePort: Int) async throws {
9769
do {
98-
try await withDatabase("postgres", environment) { // Connect to `postgres` db in order to reset the test db
70+
try await withDatabase("postgres", port: databasePort, environment) { // Connect to `postgres` db in order to reset the test db
9971
try await $0.query(PostgresQuery(unsafeSQL: "DROP DATABASE IF EXISTS \(databaseName) WITH (FORCE)"))
10072
try await $0.query(PostgresQuery(unsafeSQL: "CREATE DATABASE \(databaseName)"))
10173
}
10274

10375
do { // Use autoMigrate to spin up the schema
10476
let app = try await Application.make(environment)
10577
app.logger = .init(label: "noop") { _ in SwiftLogNoOpLogHandler() }
106-
try await configure(app)
78+
try await configure(app, databasePort: databasePort)
10779
try await app.autoMigrate()
10880
try await app.asyncShutdown()
10981
}
@@ -114,9 +86,12 @@ extension AppTestCase {
11486
}
11587

11688

117-
static func createSnapshot(original: String, snapshot: String, environment: Environment) async throws {
89+
static func createSnapshot(original: String,
90+
snapshot: String,
91+
databasePort: Int,
92+
environment: Environment) async throws {
11893
do {
119-
try await withDatabase("postgres", environment) { client in
94+
try await withDatabase("postgres", port: databasePort, environment) { client in
12095
try await client.query(PostgresQuery(unsafeSQL: "DROP DATABASE IF EXISTS \(snapshot) WITH (FORCE)"))
12196
try await client.query(PostgresQuery(unsafeSQL: "CREATE DATABASE \(snapshot) TEMPLATE \(original)"))
12297
}
@@ -127,10 +102,13 @@ extension AppTestCase {
127102
}
128103

129104

130-
static func restoreSnapshot(original: String, snapshot: String, environment: Environment) async throws {
105+
static func restoreSnapshot(original: String,
106+
snapshot: String,
107+
databasePort: Int,
108+
environment: Environment) async throws {
131109
// delete db and re-create from snapshot
132110
do {
133-
try await withDatabase("postgres", environment) { client in
111+
try await withDatabase("postgres", port: databasePort, environment) { client in
134112
try await client.query(PostgresQuery(unsafeSQL: "DROP DATABASE IF EXISTS \(original) WITH (FORCE)"))
135113
try await client.query(PostgresQuery(unsafeSQL: "CREATE DATABASE \(original) TEMPLATE \(snapshot)"))
136114
}
@@ -146,42 +124,6 @@ extension AppTestCase {
146124
}
147125

148126

149-
extension AppTestCase {
150-
func renderSQL(_ builder: SQLSelectBuilder) -> String {
151-
renderSQL(builder.query)
152-
}
153-
154-
func renderSQL(_ query: SQLExpression) -> String {
155-
var serializer = SQLSerializer(database: app.db as! SQLDatabase)
156-
query.serialize(to: &serializer)
157-
return serializer.sql
158-
}
159-
160-
func binds(_ builder: SQLSelectBuilder?) -> [String] {
161-
binds(builder?.query)
162-
}
163-
164-
func binds(_ query: SQLExpression?) -> [String] {
165-
var serializer = SQLSerializer(database: app.db as! SQLDatabase)
166-
query?.serialize(to: &serializer)
167-
return serializer.binds.reduce(into: []) { result, bind in
168-
switch bind {
169-
case let bind as Date:
170-
result.append(DateFormatter.filterParseFormatter.string(from: bind))
171-
case let bind as Set<Package.PlatformCompatibility>:
172-
let s = bind.map(\.rawValue).sorted().joined(separator: ",")
173-
result.append("{\(s)}")
174-
case let bind as Set<ProductTypeSearchFilter.ProductType>:
175-
let s = bind.map(\.rawValue).sorted().joined(separator: ",")
176-
result.append("{\(s)}")
177-
default:
178-
result.append("\(bind)")
179-
}
180-
}
181-
}
182-
}
183-
184-
185127
// FIXME: Move this once AppTestCase can be removed. These are helpers created during the transition to Swift Testing. Also check if we can just create a PostgresDB object from scratch rather than using withApp and app.db for this at the call site. That does a whole migration + reset just to render the SQL needlessly.
186128
extension Database {
187129
func renderSQL(_ builder: SQLSelectBuilder) -> String {
@@ -219,10 +161,12 @@ extension Database {
219161
}
220162

221163

222-
private func connect(to databaseName: String, _ environment: Environment) async throws -> PostgresClient {
164+
private func connect(to databaseName: String,
165+
port: Int,
166+
_ environment: Environment) async throws -> PostgresClient {
223167
await DotEnvFile.load(for: environment, fileio: .init(threadPool: .singleton))
224168
let host = Environment.get("DATABASE_HOST")!
225-
let port = Environment.get("DATABASE_PORT").flatMap(Int.init)!
169+
// let port = Environment.get("DATABASE_PORT").flatMap(Int.init)!
226170
let username = Environment.get("DATABASE_USERNAME")!
227171
let password = Environment.get("DATABASE_PASSWORD")!
228172

@@ -232,8 +176,11 @@ private func connect(to databaseName: String, _ environment: Environment) async
232176
}
233177

234178

235-
private func withDatabase(_ databaseName: String, _ environment: Environment, _ query: @escaping (PostgresClient) async throws -> Void) async throws {
236-
let client = try await connect(to: databaseName, environment)
179+
private func withDatabase(_ databaseName: String,
180+
port: Int,
181+
_ environment: Environment,
182+
_ query: @escaping (PostgresClient) async throws -> Void) async throws {
183+
let client = try await connect(to: databaseName, port: port, environment)
237184
try await withThrowingTaskGroup(of: Void.self) { taskGroup in
238185
taskGroup.addTask { await client.run() }
239186

0 commit comments

Comments
 (0)