Skip to content

Commit 4988607

Browse files
authored
Merge pull request #62 from vapor/remove-assert
remove executing code from asserts
2 parents 88592be + f446e14 commit 4988607

File tree

7 files changed

+50
-30
lines changed

7 files changed

+50
-30
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
/Packages
44
/*.xcodeproj
55
Package.resolved
6+
DerivedData
67

Sources/PostgreSQL/Data/PostgreSQLData+Point.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ extension PostgreSQLPoint: PostgreSQLDataConvertible {
4949
let parts = string.split(separator: ",")
5050
var x = parts[0]
5151
var y = parts[1]
52-
assert(x.popFirst()! == "(")
53-
assert(y.popLast()! == ")")
52+
let leftParen = x.popFirst()
53+
assert(leftParen == "(")
54+
let rightParen = y.popLast()
55+
assert(rightParen == ")")
5456
return .init(x: Double(x)!, y: Double(y)!)
5557
case .binary:
5658
let x = value[0..<8]

Sources/PostgreSQL/Database/PostgreSQLTransportConfig.swift

+8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public struct PostgreSQLTransportConfig {
3636
public static func customTLS(_ tlsConfiguration: TLSConfiguration)-> PostgreSQLTransportConfig {
3737
return .init(method: .tls(tlsConfiguration))
3838
}
39+
40+
/// Returns `true` if this configuration uses TLS.
41+
public var isTLS: Bool {
42+
switch method {
43+
case .cleartext: return false
44+
case .tls: return true
45+
}
46+
}
3947

4048
internal enum Method {
4149
case cleartext

Sources/PostgreSQL/Utilities.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ extension Data {
3737
return
3838
}
3939
for _ in 0..<n {
40-
assert(popFirst() != nil)
40+
let first = popFirst()
41+
assert(first != nil)
4142
}
4243
}
4344

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

+14-26
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Core
88
class PostgreSQLConnectionTests: XCTestCase {
99
let defaultTimeout = 5.0
1010
func testVersion() throws {
11-
let client = try PostgreSQLConnection.makeTest()
11+
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
1212
let results = try client.simpleQuery("SELECT version();").wait()
1313
try XCTAssert(results[0].firstValue(forColumn: "version")?.decode(String.self).contains("10.") == true)
1414
}
@@ -20,7 +20,7 @@ class PostgreSQLConnectionTests: XCTestCase {
2020
}
2121

2222
func testSelectTypes() throws {
23-
let client = try PostgreSQLConnection.makeTest()
23+
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
2424
let results = try client.query("select * from pg_type;").wait()
2525
if results.count > 350 {
2626
let name = try results[128].firstValue(forColumn: "typname")?.decode(String.self)
@@ -31,7 +31,7 @@ class PostgreSQLConnectionTests: XCTestCase {
3131
}
3232

3333
func testParse() throws {
34-
let client = try PostgreSQLConnection.makeTest()
34+
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
3535
let query = """
3636
select * from "pg_type" where "typlen" = $1 or "typlen" = $2
3737
"""
@@ -46,7 +46,7 @@ class PostgreSQLConnectionTests: XCTestCase {
4646
}
4747

4848
func testTypes() throws {
49-
let client = try PostgreSQLConnection.makeTest()
49+
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
5050
let createQuery = """
5151
create table kitchen_sink (
5252
"smallint" smallint,
@@ -139,7 +139,7 @@ class PostgreSQLConnectionTests: XCTestCase {
139139
}
140140

141141
func testParameterizedTypes() throws {
142-
let client = try PostgreSQLConnection.makeTest()
142+
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
143143
let createQuery = """
144144
create table kitchen_sink (
145145
"smallint" smallint,
@@ -262,7 +262,7 @@ class PostgreSQLConnectionTests: XCTestCase {
262262
var message: String
263263
}
264264

265-
let client = try PostgreSQLConnection.makeTest()
265+
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
266266
_ = try client.query("drop table if exists foo;").wait()
267267
let createResult = try client.query("create table foo (id integer, dict jsonb);").wait()
268268
XCTAssertEqual(createResult.count, 0)
@@ -282,7 +282,7 @@ class PostgreSQLConnectionTests: XCTestCase {
282282
}
283283

284284
func testNull() throws {
285-
let client = try PostgreSQLConnection.makeTest()
285+
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
286286
_ = try client.query("drop table if exists nulltest;").wait()
287287
let createResult = try client.query("create table nulltest (i integer not null, d timestamp);").wait()
288288
XCTAssertEqual(createResult.count, 0)
@@ -297,7 +297,7 @@ class PostgreSQLConnectionTests: XCTestCase {
297297

298298
func testGH24() throws {
299299
/// PREPARE
300-
let client = try PostgreSQLConnection.makeTest()
300+
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
301301
_ = try client.query("""
302302
DROP TABLE IF EXISTS "acronym+category"
303303
""").wait()
@@ -448,7 +448,7 @@ class PostgreSQLConnectionTests: XCTestCase {
448448
var count: Int
449449
}
450450

451-
let connection = try PostgreSQLConnection.makeTest()
451+
let connection = try PostgreSQLConnection.makeTest(transport: .cleartext)
452452
_ = try connection.simpleQuery("DROP TABLE IF EXISTS apps").wait()
453453
_ = try connection.simpleQuery("CREATE TABLE apps (id INT, platform TEXT, identifier TEXT)").wait()
454454
_ = try connection.simpleQuery("INSERT INTO apps VALUES (1, 'a', 'b')").wait()
@@ -486,24 +486,16 @@ class PostgreSQLConnectionTests: XCTestCase {
486486
}
487487

488488
extension PostgreSQLConnection {
489-
/// Creates a test event loop and psql client.
490-
static func makeTest() throws -> PostgreSQLConnection {
491-
#if Xcode
492-
return try _makeTest(hostname: self.dockerMachineHostname)
493-
#else
494-
return try _makeTest(hostname: "localhost")
495-
#endif
496-
}
497-
498489
/// Creates a test event loop and psql client over ssl.
499490
static func makeTest(transport: PostgreSQLTransportConfig) throws -> PostgreSQLConnection {
500-
#if Xcode
501-
return try _makeTest(hostname: self.dockerMachineHostname, port: 5433, transport: transport)
491+
#if os(macOS)
492+
return try _makeTest(hostname: "192.168.99.100", password: "vapor_password", port: transport.isTLS ? 5433 : 5432, transport: transport)
502493
#else
503-
return try _makeTest(hostname: "localhost-ssl", password: "vapor_password", transport: transport)
494+
return try _makeTest(hostname: transport.isTLS ? "tls" : "cleartext", password: "vapor_password", transport: transport)
504495
#endif
505496
}
506-
497+
498+
/// Creates a test connection.
507499
private static func _makeTest(hostname: String, password: String? = nil, port: Int = 5432, transport: PostgreSQLTransportConfig = .cleartext) throws -> PostgreSQLConnection {
508500
let group = MultiThreadedEventLoopGroup(numThreads: 1)
509501
let client = try PostgreSQLConnection.connect(hostname: hostname, port: port, transport: transport, on: group) { error in
@@ -512,10 +504,6 @@ extension PostgreSQLConnection {
512504
_ = try client.authenticate(username: "vapor_username", database: "vapor_database", password: password).wait()
513505
return client
514506
}
515-
516-
private static var dockerMachineHostname: String {
517-
return (try? Process.execute("docker-machine", "ip")) ?? "192.168.99.100"
518-
}
519507
}
520508

521509
func +=<T>(lhs: inout [T], rhs: T) {

circle.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ jobs:
1414
docker:
1515
- image: codevapor/swift:4.1
1616
- image: circleci/postgres:latest
17+
name: cleartext
1718
environment:
1819
POSTGRES_USER: vapor_username
1920
POSTGRES_DB: vapor_database
2021
POSTGRES_PASSWORD: vapor_password
2122
- image: scenecheck/postgres-ssl:latest
22-
name: localhost-ssl
23+
name: tls
2324
environment:
2425
POSTGRES_USER: vapor_username
2526
POSTGRES_DB: vapor_database

docker-compose.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: '2'
2+
3+
services:
4+
cleartext:
5+
image: postgres:latest
6+
environment:
7+
POSTGRES_USER: vapor_username
8+
POSTGRES_DB: vapor_database
9+
POSTGRES_PASSWORD: vapor_password
10+
ports:
11+
- 5432:5432
12+
tls:
13+
image: scenecheck/postgres-ssl:latest
14+
environment:
15+
POSTGRES_USER: vapor_username
16+
POSTGRES_DB: vapor_database
17+
POSTGRES_PASSWORD: vapor_password
18+
ports:
19+
- 5433:5432

0 commit comments

Comments
 (0)