33// Set environment variables: HOST, USERNAME, PASSWORD, DATABASE (optional)
44
55import XCTest
6+
67@testable import SQLClientSwift
78
89final class SQLClientSwiftTests : XCTestCase {
910
1011 private func env( _ key: String ) -> String { ProcessInfo . processInfo. environment [ key] ?? " " }
11- private var host : String { env ( " HOST " ) }
12+ private var host : String { env ( " HOST " ) }
1213 private var username : String { env ( " USERNAME " ) }
1314 private var password : String { env ( " PASSWORD " ) }
1415 private var database : String { env ( " DATABASE " ) }
1516 private var canConnect : Bool { !host. isEmpty && !username. isEmpty && !password. isEmpty }
16- private var client : SQLClient ! // global client
17+ private var client : SQLClient ! // global client
1718
1819 private func makeClient( ) async throws -> SQLClient {
1920 let c = SQLClient ( )
20- try await c. connect ( server: host, username: username, password: password,
21- database: database. isEmpty ? nil : database)
21+ try await c. connect (
22+ server: host, username: username, password: password,
23+ database: database. isEmpty ? nil : database)
2224 return c
2325 }
24-
26+
2527 /// Called before each XCTest method is run. Able to throw errors on setup.
26- /// Centralises boilerplate setup making
27- override func setupWithError( ) throws {
28- try await super. setupWithError ( )
29- guard canConnect else {
30- throw XCTSkip ( " Set HOST, USERNAME, PASSWORD environment variables to run tests. " )
31- }
28+ /// Centralises boilerplate setup making
29+ override func setUp( ) async throws {
30+ try super. setUpWithError ( )
31+
3232 client = try await makeClient ( )
3333 }
3434
3535 /// Called after each XCTest method is run. Able to throw errors on cleanup.
3636 /// Ensures cleanup from each test is completed after the test is run. Before
3737 /// the next test is run.
38- override func tearDownWithError ( ) throws {
38+ override func tearDown ( ) async throws {
3939 try await client. disconnect ( )
40- try await super. tearDownWithError ( )
40+ try await super. tearDown ( )
4141 }
4242
43-
4443 func testConnect( ) async throws {
4544 // Use a local client, as the global client is already connected
4645 let localClient = SQLClient ( )
47- try await localClient. connect ( server: host, username: username, password: password,
48- database: database. isEmpty ? nil : database)
46+ try await localClient. connect (
47+ server: host, username: username, password: password,
48+ database: database. isEmpty ? nil : database)
4949
5050 let connected = await localClient. isConnected
5151 XCTAssertTrue ( connected)
@@ -58,15 +58,16 @@ final class SQLClientSwiftTests: XCTestCase {
5858 func testDoubleConnectThrows( ) async throws {
5959 // Use a local client as the global client is already connected
6060 let localClient = SQLClient ( )
61- try await localClient. connect ( server: host, username: username, password: password,
62- database: database. isEmpty ? nil : database)
61+ try await localClient. connect (
62+ server: host, username: username, password: password,
63+ database: database. isEmpty ? nil : database)
6364 // Defer is used here as race condition on cleanup is inconsequential
6465 defer { Task { await localClient. disconnect ( ) } }
65-
66+
6667 do {
6768 try await localClient. connect ( server: host, username: username, password: password)
6869 XCTFail ( " Expected alreadyConnected " )
69- } catch SQLClientError . alreadyConnected { }
70+ } catch SQLClientError . alreadyConnected { }
7071 }
7172
7273 func testSelectScalar( ) async throws {
@@ -114,11 +115,12 @@ final class SQLClientSwiftTests: XCTestCase {
114115 }
115116
116117 func testRowsAffected( ) async throws {
117- try await client. run ( """
118- IF OBJECT_ID('tempdb..#T') IS NOT NULL DROP TABLE #T;
119- CREATE TABLE #T (id INT);
120- INSERT INTO #T VALUES (1),(2),(3);
121- """ )
118+ try await client. run (
119+ """
120+ IF OBJECT_ID('tempdb..#T') IS NOT NULL DROP TABLE #T;
121+ CREATE TABLE #T (id INT);
122+ INSERT INTO #T VALUES (1),(2),(3);
123+ """ )
122124 let affected = try await client. run ( " UPDATE #T SET id = id + 10 " )
123125 XCTAssertEqual ( affected, 3 )
124126 try await client. run ( " DROP TABLE #T " )
@@ -139,42 +141,48 @@ final class SQLClientSwiftTests: XCTestCase {
139141 do {
140142 _ = try await client. execute ( " SELECT ? AS A " , parameters: [ 1 , 2 ] )
141143 XCTFail ( " Expected parameterCountMismatch " )
142- } catch SQLClientError . parameterCountMismatch { }
144+ } catch SQLClientError . parameterCountMismatch { }
143145 }
144146
145147 func testDecodableStruct( ) async throws {
146- struct Point : Decodable { let x : Int ; let y : Int }
148+ struct Point : Decodable {
149+ let x : Int
150+ let y : Int
151+ }
147152 let points : [ Point ] = try await client. query ( " SELECT 10 AS x, 20 AS y " )
148153 XCTAssertEqual ( points [ 0 ] . x, 10 )
149154 XCTAssertEqual ( points [ 0 ] . y, 20 )
150155 }
151156
152157 func testDecodableSnakeCase( ) async throws {
153- struct Item : Decodable { let itemId : Int ; let itemName : String }
158+ struct Item : Decodable {
159+ let itemId : Int
160+ let itemName : String
161+ }
154162 let items : [ Item ] = try await client. query ( " SELECT 7 AS item_id, 'Widget' AS item_name " )
155- XCTAssertEqual ( items [ 0 ] . itemId, 7 )
163+ XCTAssertEqual ( items [ 0 ] . itemId, 7 )
156164 XCTAssertEqual ( items [ 0 ] . itemName, " Widget " )
157165 }
158166
159167 func testBadSQLThrows( ) async throws {
160168 do {
161169 _ = try await client. execute ( " THIS IS NOT VALID SQL " )
162170 XCTFail ( " Expected executionFailed " )
163- } catch SQLClientError . executionFailed { }
171+ } catch SQLClientError . executionFailed { }
164172 }
165173
166174 func testEmptySQLThrows( ) async throws {
167175 do {
168176 _ = try await client. execute ( " " )
169177 XCTFail ( " Expected noCommandText " )
170- } catch SQLClientError . noCommandText { }
178+ } catch SQLClientError . noCommandText { }
171179 }
172180
173181 func testQueryBeforeConnectThrows( ) async throws {
174182 let client = SQLClient ( )
175183 do {
176184 _ = try await client. query ( " SELECT 1 " )
177185 XCTFail ( " Expected notConnected " )
178- } catch SQLClientError . notConnected { }
186+ } catch SQLClientError . notConnected { }
179187 }
180188}
0 commit comments