mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Experimental ExternalDataStorageTests
This commit is contained in:
1 parent
feca8625ef
commit
aa1d3e89b8
2 files changed
+289
No files matched your search
@@ -0,0 +1,283 @@
|
||||
import XCTest
|
||||
import GRDB
|
||||
|
||||
class ExternalDataStorage : TransactionObserverType {
|
||||
let path: String
|
||||
let forbiddenData: NSData?
|
||||
private var data: NSData?? = nil
|
||||
private var dataForRowID: [Int64: NSData?] = [:]
|
||||
var movedRowIDs: [Int64] = []
|
||||
var storedRowIDs: [Int64] = []
|
||||
var restoreOnRollback: Bool = false
|
||||
|
||||
init(path: String, forbiddenData: NSData?) {
|
||||
self.path = path
|
||||
self.forbiddenData = forbiddenData
|
||||
setupDirectories()
|
||||
}
|
||||
|
||||
func willSaveData(data: NSData?) {
|
||||
self.data = data
|
||||
}
|
||||
|
||||
func databaseDidChangeWithEvent(event: DatabaseEvent) {
|
||||
guard let data = data else {
|
||||
return
|
||||
}
|
||||
|
||||
switch event.kind {
|
||||
case .Insert, .Update:
|
||||
dataForRowID[event.rowID] = data
|
||||
default:
|
||||
break
|
||||
}
|
||||
self.data = nil
|
||||
}
|
||||
|
||||
func databaseWillCommit() throws {
|
||||
do {
|
||||
let fm = NSFileManager.defaultManager()
|
||||
print(dataForRowID)
|
||||
for (rowID, data) in dataForRowID.sort({ $0.0 < $1.0 }) {
|
||||
if let forbiddenData = forbiddenData where forbiddenData == data {
|
||||
throw NSError(domain: "ExternalDataStorage", code: 0, userInfo: nil)
|
||||
}
|
||||
|
||||
let storagePath = storageDataPath(rowID)
|
||||
let tempPath = temporaryDataPath(rowID)
|
||||
if fm.fileExistsAtPath(storagePath) {
|
||||
if fm.fileExistsAtPath(tempPath) {
|
||||
try! fm.removeItemAtPath(tempPath)
|
||||
}
|
||||
try fm.moveItemAtPath(storagePath, toPath: tempPath)
|
||||
}
|
||||
movedRowIDs.append(rowID)
|
||||
if let data = data {
|
||||
try data.writeToFile(storagePath, options: [])
|
||||
}
|
||||
storedRowIDs.append(rowID)
|
||||
}
|
||||
} catch {
|
||||
restoreOnRollback = true
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
func databaseDidCommit(db: Database) {
|
||||
// TODO: clean up tmp directory
|
||||
cleanup()
|
||||
}
|
||||
|
||||
func databaseDidRollback(db: Database) {
|
||||
if restoreOnRollback {
|
||||
let fm = NSFileManager.defaultManager()
|
||||
for rowID in storedRowIDs {
|
||||
if fm.fileExistsAtPath(storageDataPath(rowID)) {
|
||||
try! fm.removeItemAtPath(storageDataPath(rowID))
|
||||
}
|
||||
}
|
||||
for rowID in movedRowIDs {
|
||||
if fm.fileExistsAtPath(temporaryDataPath(rowID)) {
|
||||
try! fm.moveItemAtPath(temporaryDataPath(rowID), toPath: storageDataPath(rowID))
|
||||
}
|
||||
}
|
||||
}
|
||||
cleanup()
|
||||
}
|
||||
|
||||
func cleanup() {
|
||||
restoreOnRollback = false
|
||||
movedRowIDs = []
|
||||
storedRowIDs = []
|
||||
dataForRowID = [:]
|
||||
}
|
||||
|
||||
func loadData(rowID: Int64) -> NSData? {
|
||||
let fm = NSFileManager.defaultManager()
|
||||
if fm.fileExistsAtPath(storageDataPath(rowID)) {
|
||||
return NSData(contentsOfFile: storageDataPath(rowID))!
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
private var temporaryDirectoryPath: String {
|
||||
return (path as NSString).stringByAppendingPathComponent("tmp")
|
||||
}
|
||||
|
||||
private func storageDataPath(rowID: Int64) -> String {
|
||||
return (path as NSString).stringByAppendingPathComponent(String(rowID))
|
||||
}
|
||||
|
||||
private func temporaryDataPath(rowID: Int64) -> String {
|
||||
return (temporaryDirectoryPath as NSString).stringByAppendingPathComponent(String(rowID))
|
||||
}
|
||||
|
||||
private func setupDirectories() {
|
||||
let fm = NSFileManager.defaultManager()
|
||||
try! fm.createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil)
|
||||
try! fm.createDirectoryAtPath(temporaryDirectoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
}
|
||||
}
|
||||
|
||||
class ExternalDataProxy {
|
||||
let externalDataStorage: ExternalDataStorage
|
||||
var rowID: Int64?
|
||||
var data: NSData? {
|
||||
get {
|
||||
if let _data = _data {
|
||||
return _data
|
||||
} else {
|
||||
_data = externalDataStorage.loadData(rowID!)
|
||||
return _data!
|
||||
}
|
||||
}
|
||||
set {
|
||||
_data = newValue
|
||||
}
|
||||
}
|
||||
var _data: NSData??
|
||||
|
||||
init(externalDataStorage: ExternalDataStorage) {
|
||||
self.externalDataStorage = externalDataStorage
|
||||
}
|
||||
|
||||
func willSave() {
|
||||
externalDataStorage.willSaveData(data)
|
||||
}
|
||||
}
|
||||
|
||||
class RecordWithExternalData : Record {
|
||||
var id: Int64? {
|
||||
didSet {
|
||||
if let externalDataProxy = externalDataProxy {
|
||||
externalDataProxy.rowID = id
|
||||
}
|
||||
}
|
||||
}
|
||||
var title: String?
|
||||
var data: NSData? {
|
||||
get { return externalDataProxy!.data }
|
||||
set { externalDataProxy!.data = newValue }
|
||||
}
|
||||
|
||||
override static func databaseTableName() -> String {
|
||||
return "datas"
|
||||
}
|
||||
|
||||
override var storedDatabaseDictionary: [String: DatabaseValueConvertible?] {
|
||||
return ["id": id, "title": title]
|
||||
}
|
||||
|
||||
override func updateFromRow(row: Row) {
|
||||
if let dbv = row["id"] { id = dbv.value() }
|
||||
if let dbv = row["title"] { title = dbv.value() }
|
||||
super.updateFromRow(row)
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
private var externalDataProxy: ExternalDataProxy? {
|
||||
didSet {
|
||||
if let externalDataProxy = externalDataProxy {
|
||||
externalDataProxy.rowID = id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override func insert(db: Database) throws {
|
||||
externalDataProxy!.willSave()
|
||||
try super.insert(db)
|
||||
}
|
||||
|
||||
override func update(db: Database) throws {
|
||||
externalDataProxy!.willSave()
|
||||
try super.update(db)
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
static func setupInDatabase(db: Database) throws {
|
||||
// TODO: make tests run with a single "id INTEGER PRIMARY KEY" column.
|
||||
// The "update" method doing nothing in this case, we expect troubles.
|
||||
try db.execute(
|
||||
"CREATE TABLE datas (id INTEGER PRIMARY KEY, title TEXT)")
|
||||
}
|
||||
}
|
||||
|
||||
class ExperimentalTransactionObserverTests : GRDBTestCase {
|
||||
var externalDataStorage: ExternalDataStorage!
|
||||
|
||||
override var dbConfiguration: Configuration {
|
||||
externalDataStorage = ExternalDataStorage(path: "/tmp/ExternalDataStorage", forbiddenData: "Bunny".dataUsingEncoding(NSUTF8StringEncoding))
|
||||
var c = super.dbConfiguration
|
||||
c.transactionObserver = externalDataStorage
|
||||
return c
|
||||
}
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try RecordWithExternalData.setupInDatabase(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testBlah() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = RecordWithExternalData()
|
||||
// TODO: this explicit line is a problem
|
||||
record.externalDataProxy = ExternalDataProxy(externalDataStorage: self.externalDataStorage)
|
||||
record.data = "foo".dataUsingEncoding(NSUTF8StringEncoding)
|
||||
try record.save(db)
|
||||
}
|
||||
|
||||
dbQueue.inDatabase { db in
|
||||
let record = RecordWithExternalData.fetchOne(db, "SELECT * FROM datas")!
|
||||
// TODO: this explicit line is a problem
|
||||
record.externalDataProxy = ExternalDataProxy(externalDataStorage: self.externalDataStorage)
|
||||
XCTAssertEqual(record.data, "foo".dataUsingEncoding(NSUTF8StringEncoding))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testError() {
|
||||
assertNoError {
|
||||
let record = RecordWithExternalData()
|
||||
record.externalDataProxy = ExternalDataProxy(externalDataStorage: self.externalDataStorage)
|
||||
try dbQueue.inDatabase { db in
|
||||
record.data = "foo".dataUsingEncoding(NSUTF8StringEncoding)
|
||||
try record.save(db)
|
||||
}
|
||||
|
||||
do {
|
||||
try dbQueue.inTransaction { db in
|
||||
record.data = "bar".dataUsingEncoding(NSUTF8StringEncoding)
|
||||
try record.save(db)
|
||||
|
||||
record.data = "baz".dataUsingEncoding(NSUTF8StringEncoding)
|
||||
try record.save(db)
|
||||
|
||||
let forbiddenRecord = RecordWithExternalData()
|
||||
forbiddenRecord.externalDataProxy = ExternalDataProxy(externalDataStorage: self.externalDataStorage)
|
||||
forbiddenRecord.data = "Bunny".dataUsingEncoding(NSUTF8StringEncoding)
|
||||
try forbiddenRecord.save(db)
|
||||
return .Commit
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch let error as NSError {
|
||||
XCTAssertEqual(error.domain, "ExternalDataStorage")
|
||||
}
|
||||
|
||||
let data = dbQueue.inDatabase { db -> NSData? in
|
||||
let record = RecordWithExternalData.fetchOne(db, "SELECT * FROM datas")!
|
||||
record.externalDataProxy = ExternalDataProxy(externalDataStorage: self.externalDataStorage)
|
||||
return record.data
|
||||
}
|
||||
XCTAssertEqual(data, "foo".dataUsingEncoding(NSUTF8StringEncoding))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -176,6 +176,8 @@
|
||||
56F0B9901B6001C600A2F135 /* NSDateComponentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98D1B6001C600A2F135 /* NSDateComponentsTests.swift */; };
|
||||
56F0B9911B6001C600A2F135 /* NSDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98E1B6001C600A2F135 /* NSDateTests.swift */; };
|
||||
56F0B9921B6001C600A2F135 /* NSDateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56F0B98E1B6001C600A2F135 /* NSDateTests.swift */; };
|
||||
56FC78581BBAA57800CA1285 /* ExternalDataStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FC78571BBAA57800CA1285 /* ExternalDataStorageTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56FC78591BBAA57800CA1285 /* ExternalDataStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FC78571BBAA57800CA1285 /* ExternalDataStorageTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56FDECE21BB32DFD009AD709 /* MetalRowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FDECE11BB32DFD009AD709 /* MetalRowTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
56FDECE31BB32DFD009AD709 /* MetalRowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56FDECE11BB32DFD009AD709 /* MetalRowTests.swift */; settings = {ASSET_TAGS = (); }; };
|
||||
DC2393C81ABE35F8003FF113 /* GRDB-Bridging.h in Headers */ = {isa = PBXBuildFile; fileRef = DC2393C61ABE35F8003FF113 /* GRDB-Bridging.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
@@ -369,6 +371,7 @@
|
||||
56EE573C1BB317B7007A6A95 /* SQLiteStatementConversionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteStatementConversionTests.swift; sourceTree = "<group>"; };
|
||||
56F0B98D1B6001C600A2F135 /* NSDateComponentsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDateComponentsTests.swift; sourceTree = "<group>"; };
|
||||
56F0B98E1B6001C600A2F135 /* NSDateTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSDateTests.swift; sourceTree = "<group>"; };
|
||||
56FC78571BBAA57800CA1285 /* ExternalDataStorageTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExternalDataStorageTests.swift; sourceTree = "<group>"; };
|
||||
56FDECE11BB32DFD009AD709 /* MetalRowTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetalRowTests.swift; sourceTree = "<group>"; };
|
||||
DC2393C61ABE35F8003FF113 /* GRDB-Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GRDB-Bridging.h"; sourceTree = "<group>"; };
|
||||
DC3773F319C8CBB3004FCF85 /* GRDB.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = GRDB.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
@@ -423,6 +426,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
5607EFD21BB8254800605DE3 /* TransactionObserverTests.swift */,
|
||||
56FC78571BBAA57800CA1285 /* ExternalDataStorageTests.swift */,
|
||||
);
|
||||
path = TransactionObserver;
|
||||
sourceTree = "<group>";
|
||||
@@ -1109,6 +1113,7 @@
|
||||
56E8CE111BB4FE5B00828BEC /* SQLiteStatementConvertibleFetchTests.swift in Sources */,
|
||||
56A2384A1B9C74A90082EB20 /* SelectStatementTests.swift in Sources */,
|
||||
56A2384C1B9C74A90082EB20 /* UpdateStatementTests.swift in Sources */,
|
||||
56FC78591BBAA57800CA1285 /* ExternalDataStorageTests.swift in Sources */,
|
||||
56A2384E1B9C74A90082EB20 /* DatabaseMigratorTests.swift in Sources */,
|
||||
56A238641B9C74A90082EB20 /* RecordAwakeFromFetchTests.swift in Sources */,
|
||||
56A2385A1B9C74A90082EB20 /* PrimaryKeySingleTests.swift in Sources */,
|
||||
@@ -1167,6 +1172,7 @@
|
||||
56A2384B1B9C74A90082EB20 /* UpdateStatementTests.swift in Sources */,
|
||||
56A2384D1B9C74A90082EB20 /* DatabaseMigratorTests.swift in Sources */,
|
||||
56A238631B9C74A90082EB20 /* RecordAwakeFromFetchTests.swift in Sources */,
|
||||
56FC78581BBAA57800CA1285 /* ExternalDataStorageTests.swift in Sources */,
|
||||
56A238591B9C74A90082EB20 /* PrimaryKeySingleTests.swift in Sources */,
|
||||
56EE573D1BB317B7007A6A95 /* SQLiteStatementConversionTests.swift in Sources */,
|
||||
56A238651B9C74A90082EB20 /* RecordFetchTests.swift in Sources */,
|
||||
|
||||
Reference in new issue
Block a user