WIP: documentation and unit tests

This commit is contained in:
John Rommel Estropia
2016-03-12 20:13:38 +09:00
parent 8a1144b1be
commit 603dffffb0
21 changed files with 760 additions and 65 deletions

View File

@@ -42,42 +42,6 @@ class CoreStoreTests: XCTestCase {
super.tearDown()
}
func testMigrationChains() {
let emptyChain: MigrationChain = nil
XCTAssertTrue(emptyChain.valid, "emptyChain.valid")
XCTAssertTrue(emptyChain.empty, "emptyChain.empty")
let normalChain: MigrationChain = "version1"
XCTAssertTrue(normalChain.valid, "normalChain.valid")
XCTAssertTrue(normalChain.empty, "normalChain.empty")
let linearChain: MigrationChain = ["version1", "version2", "version3", "version4"]
XCTAssertTrue(linearChain.valid, "linearChain.valid")
XCTAssertFalse(linearChain.empty, "linearChain.empty")
let treeChain: MigrationChain = [
"version1": "version4",
"version2": "version3",
"version3": "version4"
]
XCTAssertTrue(treeChain.valid, "treeChain.valid")
XCTAssertFalse(treeChain.empty, "treeChain.empty")
// The cases below will trigger assertion failures internally
// let linearLoopChain: MigrationChain = ["version1", "version2", "version1", "version3", "version4"]
// XCTAssertFalse(linearLoopChain.valid, "linearLoopChain.valid")
//
// let treeAmbiguousChain: MigrationChain = [
// "version1": "version4",
// "version2": "version3",
// "version1": "version2",
// "version3": "version4"
// ]
// XCTAssertFalse(treeAmbiguousChain.valid, "treeAmbiguousChain.valid")
}
func testExample() {
let stack = DataStack(modelName: "Model", bundle: NSBundle(forClass: self.dynamicType))

View File

@@ -0,0 +1,111 @@
//
// MigrationChainTests.swift
// CoreStore
//
// Copyright © 2016 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import XCTest
@testable
import CoreStore
class MigrationChainTests: XCTestCase {
@objc dynamic func testEmptyChain() {
let chain: MigrationChain = nil
expect(chain.valid).to(beTrue())
expect(chain.empty).to(beTrue())
expect(chain.contains("version1")).to(beFalse())
expect(chain.nextVersionFrom("version1")).to(beNil())
}
@objc dynamic func testSingleChain() {
let chain: MigrationChain = "version1"
expect(chain.valid).to(beTrue())
expect(chain.empty).to(beTrue())
expect(chain.contains("version1")).to(beTrue())
expect(chain.contains("version2")).to(beFalse())
expect(chain.nextVersionFrom("version1")).to(beNil())
expect(chain.nextVersionFrom("version2")).to(beNil())
}
@objc dynamic func testLinearChain() {
let chain: MigrationChain = ["version1", "version2", "version3", "version4"]
expect(chain.valid).to(beTrue())
expect(chain.empty).to(beFalse())
expect(chain.contains("version1")).to(beTrue())
expect(chain.contains("version2")).to(beTrue())
expect(chain.contains("version3")).to(beTrue())
expect(chain.contains("version4")).to(beTrue())
expect(chain.contains("version5")).to(beFalse())
expect(chain.nextVersionFrom("version1")).to(equal("version2"))
expect(chain.nextVersionFrom("version2")).to(equal("version3"))
expect(chain.nextVersionFrom("version3")).to(equal("version4"))
expect(chain.nextVersionFrom("version4")).to(beNil())
expect(chain.nextVersionFrom("version5")).to(beNil())
}
@objc dynamic func testTreeChain() {
let chain: MigrationChain = [
"version1": "version4",
"version2": "version3",
"version3": "version4"
]
expect(chain.valid).to(beTrue())
expect(chain.empty).to(beFalse())
expect(chain.contains("version1")).to(beTrue())
expect(chain.contains("version2")).to(beTrue())
expect(chain.contains("version3")).to(beTrue())
expect(chain.contains("version4")).to(beTrue())
expect(chain.contains("version5")).to(beFalse())
expect(chain.nextVersionFrom("version1")).to(equal("version4"))
expect(chain.nextVersionFrom("version2")).to(equal("version3"))
expect(chain.nextVersionFrom("version3")).to(equal("version4"))
expect(chain.nextVersionFrom("version4")).to(beNil())
expect(chain.nextVersionFrom("version5")).to(beNil())
// The cases below will trigger assertion failures internally
// let linearLoopChain: MigrationChain = ["version1", "version2", "version1", "version3", "version4"]
// XCTAssertFalse(linearLoopChain.valid, "linearLoopChain.valid")
//
// let treeAmbiguousChain: MigrationChain = [
// "version1": "version4",
// "version2": "version3",
// "version1": "version2",
// "version3": "version4"
// ]
// XCTAssertFalse(treeAmbiguousChain.valid, "treeAmbiguousChain.valid")
}
}

View File

@@ -0,0 +1,205 @@
//
// StorageInterfaceTests.swift
// CoreStore
//
// Copyright © 2016 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import XCTest
@testable
import CoreStore
class StorageInterfaceTests: XCTestCase {
func testDefaultInMemoryStore() {
let store = InMemoryStore()
expect(store.dynamicType.storeType).to(equal(NSInMemoryStoreType))
expect(store.configuration).to(beNil())
expect(store.storeOptions).to(beNil())
}
func testInMemoryStoreConfiguration() {
let store = InMemoryStore(configuration: "config1")
expect(store.dynamicType.storeType).to(equal(NSInMemoryStoreType))
expect(store.configuration).to(equal("config1"))
expect(store.storeOptions).to(beNil())
}
func testSQLiteStoreDefaultDirectories() {
#if os(tvOS)
let systemDirectorySearchPath = NSSearchPathDirectory.CachesDirectory
#else
let systemDirectorySearchPath = NSSearchPathDirectory.ApplicationSupportDirectory
#endif
let defaultSystemDirectory = NSFileManager
.defaultManager()
.URLsForDirectory(systemDirectorySearchPath, inDomains: .UserDomainMask).first!
let defaultRootDirectory = defaultSystemDirectory.URLByAppendingPathComponent(
NSBundle.mainBundle().bundleIdentifier ?? "com.CoreStore.DataStack",
isDirectory: true
)
let applicationName = (NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as? String) ?? "CoreData"
let defaultFileURL = defaultRootDirectory
.URLByAppendingPathComponent(applicationName, isDirectory: false)
.URLByAppendingPathExtension("sqlite")
expect(SQLiteStore.defaultRootDirectory).to(equal(defaultRootDirectory))
expect(SQLiteStore.defaultFileURL).to(equal(defaultFileURL))
}
func testDefaultSQLiteStore() {
let store = SQLiteStore()
expect(store.dynamicType.storeType).to(equal(NSSQLiteStoreType))
expect(store.configuration).to(beNil())
expect(store.storeOptions).to(equal([NSSQLitePragmasOption: ["journal_mode": "WAL"]] as NSDictionary))
expect(store.fileURL).to(equal(SQLiteStore.defaultFileURL))
expect(store.mappingModelBundles).to(equal(NSBundle.allBundles()))
expect(store.resetStoreOnModelMismatch).to(beFalse())
}
func testSQLiteStoreFileURL() {
let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory())
.URLByAppendingPathComponent(NSUUID().UUIDString, isDirectory: false)
.URLByAppendingPathExtension("db")
let bundles = [NSBundle(forClass: self.dynamicType)]
let store = SQLiteStore(
fileURL: fileURL,
configuration: "config1",
mappingModelBundles: bundles,
resetStoreOnModelMismatch: true
)
expect(store.dynamicType.storeType).to(equal(NSSQLiteStoreType))
expect(store.configuration).to(equal("config1"))
expect(store.storeOptions).to(equal([NSSQLitePragmasOption: ["journal_mode": "WAL"]] as NSDictionary))
expect(store.fileURL).to(equal(fileURL))
expect(store.mappingModelBundles).to(equal(bundles))
expect(store.resetStoreOnModelMismatch).to(beTrue())
}
func testSQLiteStoreFileName() {
let fileName = NSUUID().UUIDString + ".db"
let bundles = [NSBundle(forClass: self.dynamicType)]
let store = SQLiteStore(
fileName: fileName,
configuration: "config1",
mappingModelBundles: bundles,
resetStoreOnModelMismatch: true
)
expect(store.dynamicType.storeType).to(equal(NSSQLiteStoreType))
expect(store.configuration).to(equal("config1"))
expect(store.storeOptions).to(equal([NSSQLitePragmasOption: ["journal_mode": "WAL"]] as NSDictionary))
expect(store.fileURL.URLByDeletingLastPathComponent).to(equal(SQLiteStore.defaultRootDirectory))
expect(store.fileURL.lastPathComponent).to(equal(fileName))
expect(store.mappingModelBundles).to(equal(bundles))
expect(store.resetStoreOnModelMismatch).to(beTrue())
}
func testLegacySQLiteStoreDefaultDirectories() {
#if os(tvOS)
let systemDirectorySearchPath = NSSearchPathDirectory.CachesDirectory
#else
let systemDirectorySearchPath = NSSearchPathDirectory.ApplicationSupportDirectory
#endif
let legacyDefaultRootDirectory = NSFileManager.defaultManager().URLsForDirectory(
systemDirectorySearchPath,
inDomains: .UserDomainMask
).first!
let legacyDefaultFileURL = legacyDefaultRootDirectory
.URLByAppendingPathComponent(DataStack.applicationName, isDirectory: false)
.URLByAppendingPathExtension("sqlite")
expect(LegacySQLiteStore.legacyDefaultRootDirectory).to(equal(legacyDefaultRootDirectory))
expect(LegacySQLiteStore.legacyDefaultFileURL).to(equal(legacyDefaultFileURL))
}
func testDefaultLegacySQLiteStore() {
let store = LegacySQLiteStore()
expect(store.dynamicType.storeType).to(equal(NSSQLiteStoreType))
expect(store.configuration).to(beNil())
expect(store.storeOptions).to(equal([NSSQLitePragmasOption: ["journal_mode": "WAL"]] as NSDictionary))
expect(store.fileURL).to(equal(LegacySQLiteStore.legacyDefaultFileURL))
expect(store.mappingModelBundles).to(equal(NSBundle.allBundles()))
expect(store.resetStoreOnModelMismatch).to(beFalse())
}
func testLegacySQLiteStoreFileURL() {
let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory())
.URLByAppendingPathComponent(NSUUID().UUIDString, isDirectory: false)
.URLByAppendingPathExtension("db")
let bundles = [NSBundle(forClass: self.dynamicType)]
let store = LegacySQLiteStore(
fileURL: fileURL,
configuration: "config1",
mappingModelBundles: bundles,
resetStoreOnModelMismatch: true
)
expect(store.dynamicType.storeType).to(equal(NSSQLiteStoreType))
expect(store.configuration).to(equal("config1"))
expect(store.storeOptions).to(equal([NSSQLitePragmasOption: ["journal_mode": "WAL"]] as NSDictionary))
expect(store.fileURL).to(equal(fileURL))
expect(store.mappingModelBundles).to(equal(bundles))
expect(store.resetStoreOnModelMismatch).to(beTrue())
}
func testLegacySQLiteStoreFileName() {
let fileName = NSUUID().UUIDString + ".db"
let bundles = [NSBundle(forClass: self.dynamicType)]
let store = LegacySQLiteStore(
fileName: fileName,
configuration: "config1",
mappingModelBundles: bundles,
resetStoreOnModelMismatch: true
)
expect(store.dynamicType.storeType).to(equal(NSSQLiteStoreType))
expect(store.configuration).to(equal("config1"))
expect(store.storeOptions).to(equal([NSSQLitePragmasOption: ["journal_mode": "WAL"]] as NSDictionary))
expect(store.fileURL.URLByDeletingLastPathComponent).to(equal(LegacySQLiteStore.legacyDefaultRootDirectory))
expect(store.fileURL.lastPathComponent).to(equal(fileName))
expect(store.mappingModelBundles).to(equal(bundles))
expect(store.resetStoreOnModelMismatch).to(beTrue())
}
}