swift: Move syncbase test helpers into a separate extension
This allows for other test classes/files to be created yet share
the same basic helpers for creating databases and collections.
Change-Id: If9e9ef7d2f445ac96276df4c963f95baa8ecdf22
diff --git a/SyncbaseCore/SyncbaseCore.xcodeproj/project.pbxproj b/SyncbaseCore/SyncbaseCore.xcodeproj/project.pbxproj
index 7407901..ba7d08e 100644
--- a/SyncbaseCore/SyncbaseCore.xcodeproj/project.pbxproj
+++ b/SyncbaseCore/SyncbaseCore.xcodeproj/project.pbxproj
@@ -40,6 +40,7 @@
930DFCEB1CEED8BD00738DB8 /* Refs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 930DFCEA1CEED8BD00738DB8 /* Refs.swift */; };
930DFCF51CEED96B00738DB8 /* Locking.swift in Sources */ = {isa = PBXBuildFile; fileRef = 930DFCF41CEED96B00738DB8 /* Locking.swift */; };
9351A4941CE46DB9009CC4F4 /* sbcore_amd64.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 9351A4931CE46DB9009CC4F4 /* sbcore_amd64.a */; };
+ 9374F6681D00FFE5004ECE59 /* TestHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9374F6661D00FF68004ECE59 /* TestHelpers.swift */; };
93D3AD5C1CE4392A00A80CDA /* libleveldb_amd64.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 93D3AD581CE4392A00A80CDA /* libleveldb_amd64.a */; };
93D3AD5D1CE4392A00A80CDA /* libleveldb_arm64.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 93D3AD591CE4392A00A80CDA /* libleveldb_arm64.a */; };
93D3AD5E1CE4392A00A80CDA /* libsnappy_amd64.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 93D3AD5A1CE4392A00A80CDA /* libsnappy_amd64.a */; };
@@ -93,6 +94,7 @@
930DFCEA1CEED8BD00738DB8 /* Refs.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Refs.swift; sourceTree = "<group>"; };
930DFCF41CEED96B00738DB8 /* Locking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Locking.swift; sourceTree = "<group>"; };
9351A4931CE46DB9009CC4F4 /* sbcore_amd64.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = sbcore_amd64.a; sourceTree = "<group>"; };
+ 9374F6661D00FF68004ECE59 /* TestHelpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestHelpers.swift; sourceTree = "<group>"; };
93D3AD581CE4392A00A80CDA /* libleveldb_amd64.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libleveldb_amd64.a; sourceTree = "<group>"; };
93D3AD591CE4392A00A80CDA /* libleveldb_arm64.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libleveldb_arm64.a; sourceTree = "<group>"; };
93D3AD5A1CE4392A00A80CDA /* libsnappy_amd64.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libsnappy_amd64.a; sourceTree = "<group>"; };
@@ -177,6 +179,7 @@
children = (
30AD2E481CDD508D00A28A0C /* Info.plist */,
30A1F52D1CE68465008FC205 /* BasicDatabaseTests.swift */,
+ 9374F6661D00FF68004ECE59 /* TestHelpers.swift */,
);
path = Tests;
sourceTree = "<group>";
@@ -362,6 +365,7 @@
buildActionMask = 2147483647;
files = (
30A1F52E1CE68465008FC205 /* BasicDatabaseTests.swift in Sources */,
+ 9374F6681D00FFE5004ECE59 /* TestHelpers.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
diff --git a/SyncbaseCore/Tests/BasicDatabaseTests.swift b/SyncbaseCore/Tests/BasicDatabaseTests.swift
index 4055de6..23962ee 100644
--- a/SyncbaseCore/Tests/BasicDatabaseTests.swift
+++ b/SyncbaseCore/Tests/BasicDatabaseTests.swift
@@ -7,70 +7,7 @@
@testable import SyncbaseCore
class BasicDatabaseTests: XCTestCase {
-
- // MARK: Basic test helpers
-
- func withTestDb(runBlock: Database throws -> Void) {
- withTestDbAsync { (db, cleanup) in
- defer { cleanup() }
- try runBlock(db)
- }
- }
-
- func withTestDbAsync(runBlock: (db: Database, cleanup: Void -> Void) throws -> Void) {
- do {
- // Randomize the name to prevent conflicts between tests.
- let dbName = "test\(NSUUID().UUIDString)".stringByReplacingOccurrencesOfString("-", withString: "")
- let db = try Syncbase.instance.database(dbName)
- let cleanup = {
- do {
- print("Destroying db \(db)")
- try db.destroy()
- XCTAssertFalse(try db.exists(), "Database shouldn't exist after being destroyed")
- } catch let e {
- log.warning("Unable to delete db: \(e)")
- }
- }
- do {
- print("Got db \(db)")
- XCTAssertFalse(try db.exists(), "Database shouldn't exist before being created")
- print("Creating db \(db)")
- try db.create(nil)
- XCTAssertTrue(try db.exists(), "Database should exist after being created")
- // Always delete the db at the end to prevent conflicts between tests.
- try runBlock(db: db, cleanup: cleanup)
- } catch let e {
- XCTFail("Got unexpected exception: \(e)")
- cleanup()
- }
- } catch (let e) {
- XCTFail("Got unexpected exception: \(e)")
- }
- }
-
- func withTestCollection(db: Database? = nil, runBlock: (Database, Collection) throws -> Void) {
- let testBlock: Database throws -> Void = { db in
- let collection = try db.collection("collection1")
- XCTAssertFalse(try collection.exists())
- try collection.create(nil)
- XCTAssertTrue(try collection.exists())
-
- try runBlock(db, collection)
-
- try collection.destroy()
- XCTAssertFalse(try collection.exists())
- }
-
- if let db = db {
- do {
- try testBlock(db)
- } catch (let e) {
- XCTFail("Got unexpected exception: \(e)")
- }
- } else {
- withTestDb(testBlock)
- }
- }
+ // MARK: Database & cllection creation / destroying / listing
func testDbCreateExistsDestroy() {
withTestDb { db in }
diff --git a/SyncbaseCore/Tests/TestHelpers.swift b/SyncbaseCore/Tests/TestHelpers.swift
new file mode 100644
index 0000000..9fdc562
--- /dev/null
+++ b/SyncbaseCore/Tests/TestHelpers.swift
@@ -0,0 +1,61 @@
+// Copyright 2016 The Vanadium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+import Foundation
+import XCTest
+@testable import SyncbaseCore
+
+extension XCTestCase {
+ func withTestDb(runBlock: Database throws -> Void) {
+ withTestDbAsync { (db, cleanup) in
+ defer { cleanup() }
+ try runBlock(db)
+ }
+ }
+
+ func withTestDbAsync(runBlock: (db: Database, cleanup: Void -> Void) throws -> Void) {
+ do {
+ // Randomize the name to prevent conflicts between tests
+ let dbName = "test\(NSUUID().UUIDString)".stringByReplacingOccurrencesOfString("-", withString: "")
+ let db = try Syncbase.instance.database(dbName)
+ let cleanup = {
+ do {
+ print("Destroying db \(db)")
+ try db.destroy()
+ XCTAssertFalse(try db.exists(), "Database shouldn't exist after being destroyed")
+ } catch let e {
+ log.warning("Unable to delete db: \(e)")
+ }
+ }
+ do {
+ print("Got db \(db)")
+ XCTAssertFalse(try db.exists(), "Database shouldn't exist before being created")
+ print("Creating db \(db)")
+ try db.create(nil)
+ XCTAssertTrue(try db.exists(), "Database should exist after being created")
+ // Always delete the db at the end to prevent conflicts between tests
+ try runBlock(db: db, cleanup: cleanup)
+ } catch let e {
+ XCTFail("Got unexpected exception: \(e)")
+ cleanup()
+ }
+ } catch (let e) {
+ XCTFail("Got unexpected exception: \(e)")
+ }
+ }
+
+ func withTestCollection(runBlock: (Database, Collection) throws -> Void) {
+ withTestDb { db in
+ let collection = try db.collection("collection1")
+ XCTAssertFalse(try collection.exists())
+ try collection.create(nil)
+ XCTAssertTrue(try collection.exists())
+
+ try runBlock(db, collection)
+
+ try collection.destroy()
+ XCTAssertFalse(try collection.exists())
+ }
+ }
+}