11import Logging
2+ import FluentKit
23import FluentBenchmark
34import FluentPostgresDriver
45import XCTest
6+ import PostgresKit
57
68final class FluentPostgresDriverTests : XCTestCase {
79 func testAll( ) throws { try self . benchmarker. testAll ( ) }
@@ -133,15 +135,7 @@ final class FluentPostgresDriverTests: XCTestCase {
133135 let jsonDecoder = JSONDecoder ( )
134136 jsonDecoder. dateDecodingStrategy = . iso8601
135137
136- let configuration = PostgresConfiguration (
137- hostname: env ( " POSTGRES_HOSTNAME_A " ) ?? " localhost " ,
138- port: env ( " POSTGRES_PORT_A " ) . flatMap ( Int . init) ?? PostgresConfiguration . ianaPortNumber,
139- username: env ( " POSTGRES_USERNAME_A " ) ?? " vapor_username " ,
140- password: env ( " POSTGRES_PASSWORD_A " ) ?? " vapor_password " ,
141- database: env ( " POSTGRES_DATABASE_A " ) ?? " vapor_database "
142- )
143- self . dbs. use ( . postgres(
144- configuration: configuration,
138+ self . dbs. use ( . testPostgres( subconfig: " A " ,
145139 encoder: PostgresDataEncoder ( json: jsonEncoder) ,
146140 decoder: PostgresDataDecoder ( json: jsonDecoder)
147141 ) , as: . iso8601)
@@ -179,27 +173,13 @@ final class FluentPostgresDriverTests: XCTestCase {
179173 override func setUpWithError( ) throws {
180174 try super. setUpWithError ( )
181175
182- let aConfig = PostgresConfiguration (
183- hostname: env ( " POSTGRES_HOSTNAME_A " ) ?? " localhost " ,
184- port: env ( " POSTGRES_PORT_A " ) . flatMap ( Int . init) ?? PostgresConfiguration . ianaPortNumber,
185- username: env ( " POSTGRES_USERNAME_A " ) ?? " vapor_username " ,
186- password: env ( " POSTGRES_PASSWORD_A " ) ?? " vapor_password " ,
187- database: env ( " POSTGRES_DATABASE_A " ) ?? " vapor_database "
188- )
189- let bConfig = PostgresConfiguration (
190- hostname: env ( " POSTGRES_HOSTNAME_B " ) ?? " localhost " ,
191- port: env ( " POSTGRES_PORT_B " ) . flatMap ( Int . init) ?? PostgresConfiguration . ianaPortNumber,
192- username: env ( " POSTGRES_USERNAME_B " ) ?? " vapor_username " ,
193- password: env ( " POSTGRES_PASSWORD_B " ) ?? " vapor_password " ,
194- database: env ( " POSTGRES_DATABASE_B " ) ?? " vapor_database "
195- )
196176 XCTAssert ( isLoggingConfigured)
197177 self . eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: System . coreCount)
198178 self . threadPool = NIOThreadPool ( numberOfThreads: System . coreCount)
199179 self . dbs = Databases ( threadPool: threadPool, on: self . eventLoopGroup)
200180
201- self . dbs. use ( . postgres ( configuration : aConfig , connectionPoolTimeout : . seconds ( 30 ) ) , as: . a)
202- self . dbs. use ( . postgres ( configuration : bConfig , connectionPoolTimeout : . seconds ( 30 ) ) , as: . b)
181+ self . dbs. use ( . testPostgres ( subconfig : " A " ) , as: . a)
182+ self . dbs. use ( . testPostgres ( subconfig : " B " ) , as: . b)
203183
204184 let a = self . dbs. database ( . a, logger: Logger ( label: " test.fluent.a " ) , on: self . eventLoopGroup. next ( ) )
205185 _ = try ( a as! PostgresDatabase ) . query ( " drop schema public cascade " ) . wait ( )
@@ -218,6 +198,23 @@ final class FluentPostgresDriverTests: XCTestCase {
218198 }
219199}
220200
201+ extension DatabaseConfigurationFactory {
202+ static func testPostgres(
203+ subconfig: String ,
204+ encoder: PostgresDataEncoder = . init( ) , decoder: PostgresDataDecoder = . init( )
205+ ) -> DatabaseConfigurationFactory {
206+ let baseSubconfig = PostgresConfiguration (
207+ hostname: env ( " POSTGRES_HOSTNAME_ \( subconfig) " ) ?? " localhost " ,
208+ port: env ( " POSTGRES_PORT_ \( subconfig) " ) . flatMap ( Int . init) ?? PostgresConfiguration . ianaPortNumber,
209+ username: env ( " POSTGRES_USER_ \( subconfig) " ) ?? " vapor_username " ,
210+ password: env ( " POSTGRES_PASSWORD_ \( subconfig) " ) ?? " vapor_password " ,
211+ database: env ( " POSTGRES_DB_ \( subconfig) " ) ?? " vapor_database "
212+ )
213+
214+ return . postgres( configuration: baseSubconfig, connectionPoolTimeout: . seconds( 30 ) , encoder: encoder, decoder: decoder)
215+ }
216+ }
217+
221218extension DatabaseID {
222219 static let iso8601 = DatabaseID ( string: " iso8601 " )
223220 static let a = DatabaseID ( string: " a " )
0 commit comments