Skip to content

Commit fade03d

Browse files
authored
Update to Swift 4.1 and Kitura 2.3 (#75)
1 parent 7d652c3 commit fade03d

File tree

9 files changed

+208
-294
lines changed

9 files changed

+208
-294
lines changed

.swift-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0.3
1+
4.1

.travis.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@ branches:
55
- develop
66
- /^issue.*$/
77

8-
notifications:
9-
slack:
10-
secure: "MBh9+nRhthaIG0qqb31MFGLtkcl1KdV3a8ERChoLS9YySlc5TiqAzQyofUhPLaGeuNxmBscTZfTXDBhD30KQfC9nNnQVhfOLtbOCLJFuplM7G39YYRe3dnaHpkvlhO5QjXTvmqnz3eC1XxnaaMe7a+LykuWW1INcWQwyOQh5d4BTSgOoae6xKuF7WORysaEJpF3H76JKfGaaosBvSrI5BZInGTn9WJ4Ocejd0EmO8MUyxmGJOFeTlhNOqhkPmwuGVUOFjH0dsJiqG0uF6MjAAZ0tMLlsxHL0oHCjUFP60rMhmv25tAAT2I+jWdVQM07VBG0NPwgSn9JUf9ezZAE3K8nSF/aG1l5vibQRZjUzVd5ecpehRZhvzCa/EvVYTqT5GDJuWPcdIBsl1fODVkjqkDlAqfUfBTUh9cRma5K8JqtcZFgjegyjxOqxuLA/rCkZGZ8Xudp4qDJOrk1oZkm+m7xbN1NPQUcJKXRApDvOnexJPsXhYx50T+WU6SWOILX4k8d4rGOTQVwTXZMCoL/4lPMRV6xTxrAy/vgYboQItJmYtP85PWZo3hwbqBW7tzc+Vt8ZkTU8Uv6EfJAsJNwQfciNvttZKyIb5uQrDVV+QV6pkjJ4etyl7I04MM28CA5BrGKH7ZUKYHhow0qVXTW+vP/k1moTWnKDEWzJS5KmJkw="
11-
128
matrix:
139
include:
1410
- os: linux
1511
dist: trusty
1612
sudo: required
13+
env: SWIFT_SNAPSHOT=4.0.3
14+
env: TESTDB_NAME=testdb_1
15+
- os: linux
16+
dist: trusty
17+
sudo: required
18+
env: TESTDB_NAME=testdb_2
1719
- os: osx
1820
osx_image: xcode9.2
1921
sudo: required
22+
env: SWIFT_SNAPSHOT=4.0.3
23+
env: TESTDB_NAME=testdb_3
24+
- os: osx
25+
osx_image: xcode9.3
26+
sudo: required
27+
env: TESTDB_NAME=testdb_4
2028

2129
before_install:
2230
- git clone https://github.com/IBM-Swift/Package-Builder.git

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ let package = Package(
3030
dependencies: [
3131
// Dependencies declare other packages that this package depends on.
3232
// .package(url: /* package url */, from: "1.0.0"),
33-
.package(url: "https://github.com/IBM-Swift/HeliumLogger.git", .upToNextMinor(from: "1.7.0")),
34-
.package(url: "https://github.com/IBM-Swift/Kitura-net.git", .upToNextMinor(from: "2.0.0")),
35-
.package(url: "https://github.com/IBM-Swift/SwiftyJSON.git", .upToNextMinor(from: "17.0.0")),
33+
.package(url: "https://github.com/IBM-Swift/HeliumLogger.git", from: "1.7.0"),
34+
.package(url: "https://github.com/IBM-Swift/Kitura-net.git", from: "2.1.0"),
35+
.package(url: "https://github.com/IBM-Swift/SwiftyJSON.git", from: "17.0.0"),
3636
],
3737
targets: [
3838
// Targets are the basic building blocks of a package. A target defines a module or a test suite.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* Copyright IBM Corporation 2016, 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
import XCTest
18+
#if os(Linux)
19+
import Glibc
20+
#else
21+
import Darwin
22+
#endif
23+
import Foundation
24+
25+
@testable import CouchDB
26+
27+
class CouchDBTest: XCTestCase {
28+
29+
// MARK: - Database connection properties
30+
31+
// The database name should be defined in an environment variable TESTDB_NAME
32+
// in Travis, to allow each Travis build to use a separate database.
33+
let dbName = ProcessInfo.processInfo.environment["TESTDB_NAME"] ?? "Error-TESTDB_NAME-not-set"
34+
35+
let couchDBClient: CouchDBClient! = {
36+
let credentials = Utils.readCredentials()
37+
38+
// Connection properties for testing Cloudant or CouchDB instance
39+
let connProperties = ConnectionProperties(host: credentials.host,
40+
port: credentials.port,
41+
secured: true,
42+
username: credentials.username,
43+
password: credentials.password)
44+
45+
// Create couchDBClient instance using connection properties
46+
let client = CouchDBClient(connectionProperties: connProperties)
47+
48+
print("Hostname is: \(client.connProperties.host)")
49+
50+
return client
51+
}()
52+
53+
var database: Database?
54+
55+
// MARK: - Initializers and test set-up and tear-down
56+
57+
override func setUp() {
58+
delay(dropDatabaseIfExists)
59+
}
60+
61+
override func tearDown() {
62+
delay(dropDatabaseIfExists)
63+
}
64+
65+
/// Drop the test database, if it exists.
66+
///
67+
func dropDatabaseIfExists() {
68+
// Check if DB exists
69+
couchDBClient.dbExists(dbName) { exists, error in
70+
if error != nil {
71+
XCTFail("Failed checking existence of database \(self.dbName). Error=\(error!.localizedDescription)")
72+
} else {
73+
if exists {
74+
self.dropDatabase()
75+
}
76+
}
77+
}
78+
}
79+
80+
/// Create the test database, failing the test if it already exists, or if there
81+
/// is a connectivity error.
82+
///
83+
func createDatabase() {
84+
delay(delayedCreateDatabase)
85+
}
86+
87+
/// Drop the test database, failing the test if it does not exist, or if there
88+
/// is a connectivity error.
89+
///
90+
func dropDatabase() {
91+
delay(delayedDropDatabase)
92+
}
93+
94+
/// Delay an action by a specified amount of time (default 1 second). The purpose
95+
/// of delaying actions in CouchDB tests is to avoid exceeding the API limit for
96+
/// the test database provider while running multiple executions via Travis.
97+
///
98+
func delay(time: Double = 1.0, _ work: @escaping () -> Void) {
99+
let start = DispatchSemaphore(value: 0)
100+
let end = DispatchSemaphore(value: 0)
101+
DispatchQueue.global().asyncAfter(deadline: .now() + time) {
102+
start.wait()
103+
work()
104+
end.signal()
105+
}
106+
start.signal()
107+
end.wait()
108+
}
109+
110+
fileprivate func delayedCreateDatabase() {
111+
couchDBClient.createDB(dbName) { database, error in
112+
if let error = error {
113+
XCTFail("DB creation error: \(error.code) \(error.localizedDescription)")
114+
return
115+
}
116+
if database == nil {
117+
XCTFail("Created database is nil")
118+
return
119+
}
120+
print("Database \"\(self.dbName)\" successfully created")
121+
self.database = database
122+
}
123+
}
124+
125+
fileprivate func delayedDropDatabase() {
126+
// Retrieve and delete test database
127+
couchDBClient.deleteDB(dbName) { error in
128+
if let error = error {
129+
XCTFail("DB deletion error: \(error.code) \(error.localizedDescription)")
130+
return
131+
}
132+
print("Database \"\(self.dbName)\" successfully deleted")
133+
}
134+
}
135+
136+
}
137+

Tests/CouchDBTests/DBTests.swift

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,56 +23,38 @@ import XCTest
2323
#endif
2424

2525
import Foundation
26-
import SwiftyJSON
2726

2827
@testable import CouchDB
2928

30-
class DBTests: XCTestCase {
29+
class DBTests: CouchDBTest {
3130

32-
static var allTests: [(String, (DBTests) -> () throws -> Void)] {
33-
return [
34-
("testDB", testDB),
35-
]
36-
}
31+
static var allTests: [(String, (DBTests) -> () throws -> Void)] {
32+
return [
33+
("testDB", testDB),
34+
]
35+
}
3736

38-
// To enable running Linux and OSX tests in parallel
39-
#if os(Linux)
40-
let dbName = "test_db_linux"
41-
#else
42-
let dbName = "test_db_db"
43-
#endif
44-
45-
func testDB() {
46-
let credentials = Utils.readCredentials()
47-
48-
// Connection properties for testing Cloudant or CouchDB instance
49-
let connProperties = ConnectionProperties(host: credentials.host,
50-
port: credentials.port, secured: false,
51-
username: credentials.username,
52-
password: credentials.password)
53-
54-
// Create couchDBClient instance using conn properties
55-
let couchDBClient = CouchDBClient(connectionProperties: connProperties)
56-
print("Hostname is: \(couchDBClient.connProperties.host)")
57-
58-
couchDBClient.createDB(dbName) {(db: Database?, error: NSError?) in
59-
if let error = error {
60-
XCTFail("DB creation error: \(error.code) \(error.localizedDescription)")
61-
}
37+
/// Test that the database can be created and deleted.
38+
func testDB() {
39+
couchDBClient.createDB(dbName) {(db: Database?, error: NSError?) in
40+
if let error = error {
41+
XCTFail("DB creation error: \(error.code) \(error.localizedDescription)")
42+
}
6243

63-
guard let db = db else {
64-
XCTFail("Created database is nil")
65-
return
66-
}
44+
guard let db = db else {
45+
XCTFail("Created database is nil")
46+
return
47+
}
6748

68-
print(">> Database successfully created")
69-
couchDBClient.deleteDB(db) {(error: NSError?) in
70-
if let error = error {
71-
XCTFail("DB deletion error: \(error.code) \(error.localizedDescription)")
49+
print(">> Database successfully created")
50+
51+
self.couchDBClient.deleteDB(db) {(error: NSError?) in
52+
if let error = error {
53+
XCTFail("DB deletion error: \(error.code) \(error.localizedDescription)")
54+
}
55+
print(">> Database successfully deleted")
7256
}
73-
print(">> Database successfully deleted")
7457
}
7558
}
76-
}
7759

78-
}
60+
}

Tests/CouchDBTests/DocumentBulkUpdateTests.swift

Lines changed: 6 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import SwiftyJSON
2727

2828
@testable import CouchDB
2929

30-
class DocumentBulkUpdateTests: XCTestCase {
30+
class DocumentBulkUpdateTests: CouchDBTest {
3131

3232
// Add an additional class variable holding all tests for Linux compatibility
3333
static var allTests: [(String, (DocumentBulkUpdateTests) -> () throws -> Void)] {
@@ -38,37 +38,6 @@ class DocumentBulkUpdateTests: XCTestCase {
3838
]
3939
}
4040

41-
// To enable running Linux and OSX tests in parallel
42-
#if os(Linux)
43-
let dbName = "test_db_linux"
44-
#else
45-
let dbName = "test_db_db"
46-
#endif
47-
48-
// MARK: - Database connection properties
49-
50-
static let dbSecuredConnection = false
51-
52-
let couchDBClient: CouchDBClient! = {
53-
let credentials = Utils.readCredentials()
54-
55-
// Connection properties for testing Cloudant or CouchDB instance
56-
let connProperties = ConnectionProperties(host: credentials.host,
57-
port: credentials.port,
58-
secured: dbSecuredConnection,
59-
username: credentials.username,
60-
password: credentials.password)
61-
62-
// Create couchDBClient instance using connection properties
63-
let client = CouchDBClient(connectionProperties: connProperties)
64-
65-
print("Hostname is: \(client.connProperties.host)")
66-
67-
return client
68-
}()
69-
70-
var database: Database?
71-
7241
// MARK: - Database test objects
7342

7443
let json1 = JSON(["_id": "1234567",
@@ -104,45 +73,10 @@ class DocumentBulkUpdateTests: XCTestCase {
10473
"userId": "8901234",
10574
"addressId": "2345678"])
10675

107-
// MARK: - Initializers and test set-up and tear-down
108-
109-
override func setUp() {
110-
111-
// Create test database
112-
couchDBClient.createDB(dbName) { database, error in
113-
if let error = error {
114-
XCTFail("DB creation error: \(error.code) \(error.localizedDescription)")
115-
return
116-
}
117-
118-
if database == nil {
119-
XCTFail("Created database is nil")
120-
return
121-
}
122-
123-
print("Database \"\(self.dbName)\" successfully created")
124-
125-
// Assign data base
126-
self.database = database
127-
}
128-
}
129-
130-
override func tearDown() {
131-
132-
// Retrieve and delete test database
133-
couchDBClient.deleteDB(dbName) { error in
134-
if let error = error {
135-
XCTFail("DB deletion error: \(error.code) \(error.localizedDescription)")
136-
return
137-
}
138-
139-
print("Database \"\(self.dbName)\" successfully deleted")
140-
}
141-
}
142-
14376
// MARK: - Xcode tests
14477

145-
func testBulkInsert() {
78+
func testBulkInsert() {
79+
createDatabase()
14680
guard let database = database else {
14781
XCTFail("Failed to retrieve database")
14882
return
@@ -181,7 +115,8 @@ class DocumentBulkUpdateTests: XCTestCase {
181115
}
182116
}
183117

184-
func testBulkUpdate() {
118+
func testBulkUpdate() {
119+
createDatabase()
185120
guard let database = database else {
186121
XCTFail("Failed to retrieve database")
187122
return
@@ -273,6 +208,7 @@ class DocumentBulkUpdateTests: XCTestCase {
273208
}
274209

275210
func testBulkDelete() {
211+
createDatabase()
276212
guard let database = database else {
277213
XCTFail("Failed to retrieve database")
278214
return

0 commit comments

Comments
 (0)