@@ -19,46 +19,16 @@ import Fluent
19
19
import NIOConcurrencyHelpers
20
20
import PostgresNIO
21
21
import SQLKit
22
+ #warning("drop all instances of import XCTVapor")
22
23
import XCTVapor
23
24
24
25
25
- class AppTestCase : XCTestCase {
26
- var app : Application !
27
- let logger = CapturingLogger ( )
26
+ #warning("move/rename")
27
+ enum AppTestCase {
28
28
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 {
60
30
let app = try await Application . make ( environment)
61
- try await configure ( app)
31
+ try await configure ( app, databasePort : databasePort )
62
32
63
33
// Silence app logging
64
34
app. logger = . init( label: " noop " ) { _ in SwiftLogNoOpLogHandler ( ) }
@@ -67,7 +37,7 @@ extension AppTestCase {
67
37
}
68
38
69
39
70
- static func setupDb( _ environment: Environment ) async throws {
40
+ static func setupDb( _ environment: Environment , databasePort : Int ) async throws {
71
41
await DotEnvFile . load ( for: environment, fileio: . init( threadPool: . singleton) )
72
42
73
43
// Ensure DATABASE_HOST is from a restricted set db hostnames and nothing else.
@@ -83,27 +53,29 @@ extension AppTestCase {
83
53
// Create initial db snapshot on first run
84
54
try await snapshotCreated. withValue { snapshotCreated in
85
55
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)
88
58
snapshotCreated = true
89
59
}
90
60
}
91
61
92
- try await restoreSnapshot ( original: testDbName, snapshot: snapshotName, environment: environment)
62
+ try await restoreSnapshot ( original: testDbName, snapshot: snapshotName, databasePort : databasePort , environment: environment)
93
63
}
94
64
95
65
96
- static func createSchema( _ environment: Environment , databaseName: String ) async throws {
66
+ static func createSchema( _ environment: Environment ,
67
+ databaseName: String ,
68
+ databasePort: Int ) async throws {
97
69
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
99
71
try await $0. query ( PostgresQuery ( unsafeSQL: " DROP DATABASE IF EXISTS \( databaseName) WITH (FORCE) " ) )
100
72
try await $0. query ( PostgresQuery ( unsafeSQL: " CREATE DATABASE \( databaseName) " ) )
101
73
}
102
74
103
75
do { // Use autoMigrate to spin up the schema
104
76
let app = try await Application . make ( environment)
105
77
app. logger = . init( label: " noop " ) { _ in SwiftLogNoOpLogHandler ( ) }
106
- try await configure ( app)
78
+ try await configure ( app, databasePort : databasePort )
107
79
try await app. autoMigrate ( )
108
80
try await app. asyncShutdown ( )
109
81
}
@@ -114,9 +86,12 @@ extension AppTestCase {
114
86
}
115
87
116
88
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 {
118
93
do {
119
- try await withDatabase ( " postgres " , environment) { client in
94
+ try await withDatabase ( " postgres " , port : databasePort , environment) { client in
120
95
try await client. query ( PostgresQuery ( unsafeSQL: " DROP DATABASE IF EXISTS \( snapshot) WITH (FORCE) " ) )
121
96
try await client. query ( PostgresQuery ( unsafeSQL: " CREATE DATABASE \( snapshot) TEMPLATE \( original) " ) )
122
97
}
@@ -127,10 +102,13 @@ extension AppTestCase {
127
102
}
128
103
129
104
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 {
131
109
// delete db and re-create from snapshot
132
110
do {
133
- try await withDatabase ( " postgres " , environment) { client in
111
+ try await withDatabase ( " postgres " , port : databasePort , environment) { client in
134
112
try await client. query ( PostgresQuery ( unsafeSQL: " DROP DATABASE IF EXISTS \( original) WITH (FORCE) " ) )
135
113
try await client. query ( PostgresQuery ( unsafeSQL: " CREATE DATABASE \( original) TEMPLATE \( snapshot) " ) )
136
114
}
@@ -146,42 +124,6 @@ extension AppTestCase {
146
124
}
147
125
148
126
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
-
185
127
// 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.
186
128
extension Database {
187
129
func renderSQL( _ builder: SQLSelectBuilder ) -> String {
@@ -219,10 +161,12 @@ extension Database {
219
161
}
220
162
221
163
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 {
223
167
await DotEnvFile . load ( for: environment, fileio: . init( threadPool: . singleton) )
224
168
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)!
226
170
let username = Environment . get ( " DATABASE_USERNAME " ) !
227
171
let password = Environment . get ( " DATABASE_PASSWORD " ) !
228
172
@@ -232,8 +176,11 @@ private func connect(to databaseName: String, _ environment: Environment) async
232
176
}
233
177
234
178
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)
237
184
try await withThrowingTaskGroup ( of: Void . self) { taskGroup in
238
185
taskGroup. addTask { await client. run ( ) }
239
186
0 commit comments