Finished RealmDefaultStorage tests

This commit is contained in:
Pedro Piñera 2015-11-11 18:49:11 +01:00
parent 6ee8aa52aa
commit a87c28bfef
6 changed files with 22 additions and 18 deletions

View File

@ -3,6 +3,7 @@ osx_image: xcode7.1
notifications:
email: false
install:
- brew update
- brew install carthage
- gem install xcpretty --no-rdoc --no-ri --no-document --quiet
- carthage update

View File

@ -39,8 +39,9 @@ public protocol Storage: CustomStringConvertible {
- parameter write: true if the context has to persist the changes
- parameter operation: operation to be executed
- parameter completed: closure called when the execution is completed
*/
func operation(write: Bool, operation: (context: Context) -> Void)
func operation(write: Bool, operation: (context: Context) -> Void, completed: (() -> Void))
/**
Executes the provided operation in a given queue
@ -50,8 +51,9 @@ public protocol Storage: CustomStringConvertible {
- parameter queue: queue where the operation will be executed
- parameter write: true if the context has to persist the changes
- parameter operation: operation to be executed
- parameter completed: closure called when the execution is completed
*/
func operation(queue: dispatch_queue_t, write: Bool, operation: (context: Context) -> Void)
func operation(queue: dispatch_queue_t, write: Bool, operation: (context: Context) -> Void, completed: (() -> Void)?)
}
@ -59,9 +61,9 @@ public protocol Storage: CustomStringConvertible {
public extension Storage {
func operation(write: Bool, operation: (context: Context) -> Void) {
func operation(write: Bool, operation: (context: Context) -> Void, completed: (() -> Void)) {
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)
self.operation(queue, write: write, operation: operation)
self.operation(queue, write: write, operation: operation, completed: completed)
}
}

View File

@ -32,15 +32,8 @@ extension Realm: Context {
public func insert<T: Entity>() -> Result<T, Error> {
guard let E = T.self as? Object.Type else { return Result(error: .InvalidType) }
let inserted = E.init()
self.beginWrite()
self.add(inserted)
do {
try self.commitWrite()
return Result(value: inserted as Any as! T)
}
catch {
return Result(error: .WriteError)
}
return Result(value: inserted as Any as! T)
}
/**

View File

@ -60,12 +60,13 @@ public class RealmDefaultStorage: Storage {
- parameter write: true if the context has to persist the changes
- parameter operation: operation to be executed
*/
public func operation(queue: dispatch_queue_t, write: Bool, operation: (context: Context) -> Void) {
public func operation(queue: dispatch_queue_t, write: Bool, operation: (context: Context) -> Void, completed: (() -> Void)?) {
dispatch_async(queue) { () -> Void in
let _context: Realm = self.saveContext as! Realm
if (write) { _context.beginWrite() }
operation(context: _context)
if (write) { _ = try? _context.commitWrite() } // FIXME: Propagate the exception out of the scope
completed?()
}
}
}

View File

@ -36,7 +36,9 @@ class RealmTests: QuickSpec {
describe("insert") {
it("should return the object inserted in the Realm") {
subject!.beginWrite()
let inserted: Result<Issue, Error> = subject!.insert()
_ = try? subject!.commitWrite()
_ = inserted
expect(subject!.objects(Issue.self).count) == 1
}

View File

@ -62,12 +62,17 @@ class RealmDefaultStorageTests: QuickSpec {
describe("operations") {
it("should save the changes if write is passed as true") {
storage?.operation(dispatch_get_main_queue(), write: true, operation: { (context) -> Void in
let issue: Issue = context.insert().value!
issue.name = "test"
waitUntil(action: { (done) -> Void in
storage?.operation(true, operation: { (context) -> Void in
let issue: Issue = context.insert().value!
issue.name = "test"
}, completed: { () -> Void in
let fetched = storage?.mainContext.fetch(Request<Issue>()).value
expect(fetched?.count) == 1
done()
})
})
let fetched = storage?.mainContext.fetch(Request<Issue>()).value
expect(fetched?.count) == 1
}
}