forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.swift
100 lines (86 loc) · 4.09 KB
/
Utilities.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import XCTest
import PostgresNIO
import NIOCore
import Logging
#if canImport(Darwin)
import Darwin.C
#else
import Glibc
#endif
extension PostgresConnection {
static func address() throws -> SocketAddress {
try .makeAddressResolvingHost(env("POSTGRES_HOSTNAME") ?? "localhost", port: env("POSTGRES_PORT").flatMap(Int.init(_:)) ?? 5432)
}
@available(*, deprecated, message: "Test deprecated functionality")
static func testUnauthenticated(on eventLoop: EventLoop, logLevel: Logger.Level = .info) -> EventLoopFuture<PostgresConnection> {
var logger = Logger(label: "postgres.connection.test")
logger.logLevel = logLevel
do {
return connect(to: try address(), logger: logger, on: eventLoop)
} catch {
return eventLoop.makeFailedFuture(error)
}
}
static func test(on eventLoop: EventLoop, options: Configuration.Options? = nil) -> EventLoopFuture<PostgresConnection> {
let logger = Logger(label: "postgres.connection.test")
var config = PostgresConnection.Configuration(
host: env("POSTGRES_HOSTNAME") ?? "localhost",
port: env("POSTGRES_PORT").flatMap(Int.init(_:)) ?? 5432,
username: env("POSTGRES_USER") ?? "test_username",
password: env("POSTGRES_PASSWORD") ?? "test_password",
database: env("POSTGRES_DB") ?? "test_database",
tls: .disable
)
if let options {
config.options = options
}
return PostgresConnection.connect(on: eventLoop, configuration: config, id: 0, logger: logger)
}
static func testUDS(on eventLoop: EventLoop) -> EventLoopFuture<PostgresConnection> {
let logger = Logger(label: "postgres.connection.test")
let config = PostgresConnection.Configuration(
unixSocketPath: env("POSTGRES_SOCKET") ?? "/tmp/.s.PGSQL.\(env("POSTGRES_PORT").flatMap(Int.init(_:)) ?? 5432)",
username: env("POSTGRES_USER") ?? "test_username",
password: env("POSTGRES_PASSWORD") ?? "test_password",
database: env("POSTGRES_DB") ?? "test_database"
)
return PostgresConnection.connect(on: eventLoop, configuration: config, id: 0, logger: logger)
}
static func testChannel(_ channel: Channel, on eventLoop: EventLoop) -> EventLoopFuture<PostgresConnection> {
let logger = Logger(label: "postgres.connection.test")
let config = PostgresConnection.Configuration(
establishedChannel: channel,
username: env("POSTGRES_USER") ?? "test_username",
password: env("POSTGRES_PASSWORD") ?? "test_password",
database: env("POSTGRES_DB") ?? "test_database"
)
return PostgresConnection.connect(on: eventLoop, configuration: config, id: 0, logger: logger)
}
}
extension Logger {
static var psqlTest: Logger {
.init(label: "psql.test")
}
}
func env(_ name: String) -> String? {
getenv(name).flatMap { String(cString: $0) }
}
extension XCTestCase {
public static var shouldRunLongRunningTests: Bool {
// The env var must be set and have the value `"true"`, `"1"`, or `"yes"` (case-insensitive).
// For the sake of sheer annoying pedantry, values like `"2"` are treated as false.
guard let rawValue = env("POSTGRES_LONG_RUNNING_TESTS") else { return false }
if let boolValue = Bool(rawValue) { return boolValue }
if let intValue = Int(rawValue) { return intValue == 1 }
return rawValue.lowercased() == "yes"
}
public static var shouldRunPerformanceTests: Bool {
// Same semantics as above. Any present non-truthy value will explicitly disable performance
// tests even if they would've overwise run in the current configuration.
let defaultValue = !_isDebugAssertConfiguration() // default to not running in debug builds
guard let rawValue = env("POSTGRES_PERFORMANCE_TESTS") else { return defaultValue }
if let boolValue = Bool(rawValue) { return boolValue }
if let intValue = Int(rawValue) { return intValue == 1 }
return rawValue.lowercased() == "yes"
}
}