Skip to content

Commit

Permalink
Merge pull request #62 from vapor/remove-assert
Browse files Browse the repository at this point in the history
remove executing code from asserts
  • Loading branch information
tanner0101 authored May 14, 2018
2 parents 88592be + f446e14 commit 4988607
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/Packages
/*.xcodeproj
Package.resolved
DerivedData

6 changes: 4 additions & 2 deletions Sources/PostgreSQL/Data/PostgreSQLData+Point.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ extension PostgreSQLPoint: PostgreSQLDataConvertible {
let parts = string.split(separator: ",")
var x = parts[0]
var y = parts[1]
assert(x.popFirst()! == "(")
assert(y.popLast()! == ")")
let leftParen = x.popFirst()
assert(leftParen == "(")
let rightParen = y.popLast()
assert(rightParen == ")")
return .init(x: Double(x)!, y: Double(y)!)
case .binary:
let x = value[0..<8]
Expand Down
8 changes: 8 additions & 0 deletions Sources/PostgreSQL/Database/PostgreSQLTransportConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public struct PostgreSQLTransportConfig {
public static func customTLS(_ tlsConfiguration: TLSConfiguration)-> PostgreSQLTransportConfig {
return .init(method: .tls(tlsConfiguration))
}

/// Returns `true` if this configuration uses TLS.
public var isTLS: Bool {
switch method {
case .cleartext: return false
case .tls: return true
}
}

internal enum Method {
case cleartext
Expand Down
3 changes: 2 additions & 1 deletion Sources/PostgreSQL/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ extension Data {
return
}
for _ in 0..<n {
assert(popFirst() != nil)
let first = popFirst()
assert(first != nil)
}
}

Expand Down
40 changes: 14 additions & 26 deletions Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Core
class PostgreSQLConnectionTests: XCTestCase {
let defaultTimeout = 5.0
func testVersion() throws {
let client = try PostgreSQLConnection.makeTest()
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
let results = try client.simpleQuery("SELECT version();").wait()
try XCTAssert(results[0].firstValue(forColumn: "version")?.decode(String.self).contains("10.") == true)
}
Expand All @@ -20,7 +20,7 @@ class PostgreSQLConnectionTests: XCTestCase {
}

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

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

func testTypes() throws {
let client = try PostgreSQLConnection.makeTest()
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
let createQuery = """
create table kitchen_sink (
"smallint" smallint,
Expand Down Expand Up @@ -139,7 +139,7 @@ class PostgreSQLConnectionTests: XCTestCase {
}

func testParameterizedTypes() throws {
let client = try PostgreSQLConnection.makeTest()
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
let createQuery = """
create table kitchen_sink (
"smallint" smallint,
Expand Down Expand Up @@ -262,7 +262,7 @@ class PostgreSQLConnectionTests: XCTestCase {
var message: String
}

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

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

func testGH24() throws {
/// PREPARE
let client = try PostgreSQLConnection.makeTest()
let client = try PostgreSQLConnection.makeTest(transport: .cleartext)
_ = try client.query("""
DROP TABLE IF EXISTS "acronym+category"
""").wait()
Expand Down Expand Up @@ -448,7 +448,7 @@ class PostgreSQLConnectionTests: XCTestCase {
var count: Int
}

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

extension PostgreSQLConnection {
/// Creates a test event loop and psql client.
static func makeTest() throws -> PostgreSQLConnection {
#if Xcode
return try _makeTest(hostname: self.dockerMachineHostname)
#else
return try _makeTest(hostname: "localhost")
#endif
}

/// Creates a test event loop and psql client over ssl.
static func makeTest(transport: PostgreSQLTransportConfig) throws -> PostgreSQLConnection {
#if Xcode
return try _makeTest(hostname: self.dockerMachineHostname, port: 5433, transport: transport)
#if os(macOS)
return try _makeTest(hostname: "192.168.99.100", password: "vapor_password", port: transport.isTLS ? 5433 : 5432, transport: transport)
#else
return try _makeTest(hostname: "localhost-ssl", password: "vapor_password", transport: transport)
return try _makeTest(hostname: transport.isTLS ? "tls" : "cleartext", password: "vapor_password", transport: transport)
#endif
}


/// Creates a test connection.
private static func _makeTest(hostname: String, password: String? = nil, port: Int = 5432, transport: PostgreSQLTransportConfig = .cleartext) throws -> PostgreSQLConnection {
let group = MultiThreadedEventLoopGroup(numThreads: 1)
let client = try PostgreSQLConnection.connect(hostname: hostname, port: port, transport: transport, on: group) { error in
Expand All @@ -512,10 +504,6 @@ extension PostgreSQLConnection {
_ = try client.authenticate(username: "vapor_username", database: "vapor_database", password: password).wait()
return client
}

private static var dockerMachineHostname: String {
return (try? Process.execute("docker-machine", "ip")) ?? "192.168.99.100"
}
}

func +=<T>(lhs: inout [T], rhs: T) {
Expand Down
3 changes: 2 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ jobs:
docker:
- image: codevapor/swift:4.1
- image: circleci/postgres:latest
name: cleartext
environment:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
POSTGRES_PASSWORD: vapor_password
- image: scenecheck/postgres-ssl:latest
name: localhost-ssl
name: tls
environment:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
Expand Down
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '2'

services:
cleartext:
image: postgres:latest
environment:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
POSTGRES_PASSWORD: vapor_password
ports:
- 5432:5432
tls:
image: scenecheck/postgres-ssl:latest
environment:
POSTGRES_USER: vapor_username
POSTGRES_DB: vapor_database
POSTGRES_PASSWORD: vapor_password
ports:
- 5433:5432

0 comments on commit 4988607

Please sign in to comment.