Fixed broken tests after the API refactor

This commit is contained in:
Pedro Piñera 2015-12-13 21:17:27 +01:00
parent 3899bdfcde
commit 67a29bfcaa
7 changed files with 27 additions and 65 deletions

View File

@ -26,13 +26,13 @@ extension NSManagedObjectContext: Context {
}
/**
Adds the entity to the Storage without saving it.
Inserts the entity to the Storage without saving it.
- parameter entity: Entity to be added.
- throws: Throws an Error.InvalidType or Internal Storage error in case the object couldn't be added.
*/
public func add<T: Entity>(entity: T) throws {}
public func insert<T: Entity>(entity: T) throws {}
/**
Initializes an instance of type T and returns it.

View File

@ -18,13 +18,13 @@ public protocol Context: Requestable, ReactiveContext {
/**
Adds the entity to the Storage without saving it.
Inserts the entity to the Storage without saving it.
- parameter entity: Entity to be added.
- throws: Throws an Error.InvalidType or Internal Storage error in case the object couldn't be added.
*/
func add<T: Entity>(entity: T) throws
func insert<T: Entity>(entity: T) throws
/**
Initializes an instance of type T and returns it.
@ -76,7 +76,7 @@ public extension Context {
*/
public func create<T: Entity>() throws -> T {
let instance: T = try self.new()
try self.add(instance)
try self.insert(instance)
return instance
}

View File

@ -25,15 +25,15 @@ extension Realm: Context {
}
/**
Adds the entity to the Storage without saving it.
Inserts the entity to the Storage without saving it.
- parameter entity: Entity to be added.
- throws: Throws an Error.InvalidType or Internal Storage error in case the object couldn't be added.
*/
public func add<T: Entity>(entity: T) throws {
public func insert<T: Entity>(entity: T) throws {
guard let _ = T.self as? Object.Type else { throw Error.InvalidType }
try self.add(entity)
self.add(entity as! Object)
}
/**

View File

@ -94,26 +94,25 @@ class CoreDataDefaultStorageTests: QuickSpec {
it("shouldn't persist changes if we save the memory context") {
waitUntil(action: { (done) -> Void in
let memoryContext = defaultStorage!.memoryContext as! NSManagedObjectContext!
let _: Track = memoryContext.insert().value!
let _: Track = try! memoryContext.create()
try! memoryContext.save()
defaultStorage?.operation({ (context, save) -> Void in
let resultsCount = context.request(Track.self).fetch().value!.count
let resultsCount = try! context.request(Track.self).fetch().count
expect(resultsCount) == 0
done()
}, completed: {})
})
})
}
it("should persist the changes if it's save context") {
waitUntil(action: { (done) -> Void in
defaultStorage?.operation({ (context, save) -> Void in
let _: Track = context.insert().value!
let _: Track = try! context.create()
save()
}, completed: {
let tracksCount: Int = (defaultStorage?.mainContext.request(Track.self).fetch().value!.count)!
expect(tracksCount) == 1
done()
})
let tracksCount: Int? = try! defaultStorage?.mainContext.request(Track.self).fetch().count
expect(tracksCount) == 1
done()
})
}
}

View File

@ -10,17 +10,6 @@ class RequestTests: QuickSpec {
override func spec() {
describe("fetching") {
it("should use the context for fetching passing itself") {
let context = MockContext()
let request: Request<Issue> = Request(context)
_ = request.fetch()
expect(context.fetched) == true
}
}
describe("builders") {
it("-filteredWithPredicate") {
@ -60,25 +49,4 @@ class RequestTests: QuickSpec {
}
}
// MARK: - FetchContext
private class MockContext: Context {
var fetched: Bool = false
private func insert<T : Entity>() -> Result<T, Error> {
return Result(error: Error.Nothing)
}
private func remove<T : Entity>(objects: [T]) -> Result<Void, Error> {
return Result(error: Error.Nothing)
}
private func fetch<T : Entity>(request: Request<T>) -> Result<[T], Error> {
self.fetched = true
return Result(error: Error.Nothing)
}
}

View File

@ -28,18 +28,15 @@ class RealmTests: QuickSpec {
subject?.beginWrite()
subject?.add(issue)
_ = try? subject?.commitWrite()
// let fetched = storage?.mainContext.request(Issue.self).fetch().value
let fetched: Result<[Issue], Error> = subject!.request(Issue.self).fetch()
expect(fetched.value?.count) == 1
let fetched: [Issue] = try! subject!.request(Issue.self).fetch()
expect(fetched.count) == 1
}
}
describe("insert") {
it("should return the object inserted in the Realm") {
subject!.beginWrite()
let inserted: Result<Issue, Error> = subject!.insert()
let inserted: Issue = try! subject!.create()
_ = try? subject!.commitWrite()
_ = inserted
expect(subject!.objects(Issue.self).count) == 1
@ -57,7 +54,7 @@ class RealmTests: QuickSpec {
// Fetching
let _issue = subject!.objects(Issue.self).filter("name == %@", "test").first!
subject!.remove([_issue])
try! subject!.remove([_issue])
// Testing
expect(subject!.objects(Issue.self).count) == 0

View File

@ -64,27 +64,25 @@ class RealmDefaultStorageTests: QuickSpec {
it("should save the changes if the save closure is called") {
waitUntil(action: { (done) -> Void in
storage?.operation({ (context, save) -> Void in
let issue: Issue = context.insert().value!
let issue: Issue = try! context.create()
issue.name = "test"
save()
}, completed: { () -> Void in
let fetched = storage?.mainContext.request(Issue.self).fetch().value
expect(fetched?.count) == 1
done()
})
let fetched = try! storage?.mainContext.request(Issue.self).fetch()
expect(fetched?.count) == 1
done()
})
}
it("shouldn't persist the changes if the save closure is not called") {
waitUntil(action: { (done) -> Void in
storage?.operation({ (context, save) -> Void in
let issue: Issue = context.insert().value!
let issue: Issue = try! context.create()
issue.name = "test"
}, completed: { () -> Void in
let fetched = storage?.mainContext.request(Issue.self).fetch().value
expect(fetched?.count) == 0
done()
})
let fetched = try! storage?.mainContext.request(Issue.self).fetch()
expect(fetched?.count) == 0
done()
})
}