WIP: Unit tests

This commit is contained in:
John Rommel Estropia
2016-06-04 14:29:10 +09:00
parent b199f38b0c
commit fcca6b205e
14 changed files with 1686 additions and 191 deletions

View File

@@ -75,6 +75,10 @@ public struct From<T: NSManagedObject> {
*/
public init(_ entityClass: AnyClass) {
CoreStore.assert(
entityClass is T.Type,
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
)
self.init(entityClass: entityClass)
}
@@ -146,6 +150,10 @@ public struct From<T: NSManagedObject> {
*/
public init(_ entityClass: AnyClass, _ configuration: String?, _ otherConfigurations: String?...) {
CoreStore.assert(
entityClass is T.Type,
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
)
self.init(entityClass: entityClass, configurations: [configuration] + otherConfigurations)
}
@@ -160,143 +168,13 @@ public struct From<T: NSManagedObject> {
*/
public init(_ entityClass: AnyClass, _ configurations: [String?]) {
CoreStore.assert(
entityClass is T.Type,
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
)
self.init(entityClass: entityClass, configurations: configurations)
}
/**
Initializes a `From` clause with the specified store URLs.
- parameter storeURL: the persistent store URL to associate objects from.
- parameter otherStoreURLs: an optional list of other persistent store URLs to associate objects from (see `storeURL` parameter)
*/
public init(_ storeURL: NSURL, _ otherStoreURLs: NSURL...) {
self.init(entityClass: T.self, storeURLs: [storeURL] + otherStoreURLs)
}
/**
Initializes a `From` clause with the specified store URLs.
- parameter storeURLs: the persistent store URLs to associate objects from.
*/
public init(_ storeURLs: [NSURL]) {
self.init(entityClass: T.self, storeURLs: storeURLs)
}
/**
Initializes a `From` clause with the specified store URLs.
- parameter entity: the associated `NSManagedObject` type
- parameter storeURL: the persistent store URL to associate objects from.
- parameter otherStoreURLs: an optional list of other persistent store URLs to associate objects from (see `storeURL` parameter)
*/
public init(_ entity: T.Type, _ storeURL: NSURL, _ otherStoreURLs: NSURL...) {
self.init(entityClass: entity, storeURLs: [storeURL] + otherStoreURLs)
}
/**
Initializes a `From` clause with the specified store URLs.
- parameter entity: the associated `NSManagedObject` type
- parameter storeURLs: the persistent store URLs to associate objects from.
*/
public init(_ entity: T.Type, _ storeURLs: [NSURL]) {
self.init(entityClass: entity, storeURLs: storeURLs)
}
/**
Initializes a `From` clause with the specified store URLs.
- parameter entity: the associated `NSManagedObject` entity class
- parameter storeURL: the persistent store URL to associate objects from.
- parameter otherStoreURLs: an optional list of other persistent store URLs to associate objects from (see `storeURL` parameter)
*/
public init(_ entityClass: AnyClass, _ storeURL: NSURL, _ otherStoreURLs: NSURL...) {
self.init(entityClass: entityClass, storeURLs: [storeURL] + otherStoreURLs)
}
/**
Initializes a `From` clause with the specified store URLs.
- parameter entity: the associated `NSManagedObject` entity class
- parameter storeURLs: the persistent store URLs to associate objects from.
*/
public init(_ entityClass: AnyClass, _ storeURLs: [NSURL]) {
self.init(entityClass: entityClass, storeURLs: storeURLs)
}
/**
Initializes a `From` clause with the specified `NSPersistentStore`s.
- parameter persistentStore: the `NSPersistentStore` to associate objects from.
- parameter otherPersistentStores: an optional list of other `NSPersistentStore`s to associate objects from (see `persistentStore` parameter)
*/
public init(_ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) {
self.init(entityClass: T.self, persistentStores: [persistentStore] + otherPersistentStores)
}
/**
Initializes a `From` clause with the specified `NSPersistentStore`s.
- parameter persistentStores: the `NSPersistentStore`s to associate objects from.
*/
public init(_ persistentStores: [NSPersistentStore]) {
self.init(entityClass: T.self, persistentStores: persistentStores)
}
/**
Initializes a `From` clause with the specified `NSPersistentStore`s.
- parameter entity: the associated `NSManagedObject` type
- parameter persistentStore: the `NSPersistentStore` to associate objects from.
- parameter otherPersistentStores: an optional list of other `NSPersistentStore`s to associate objects from (see `persistentStore` parameter)
*/
public init(_ entity: T.Type, _ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) {
self.init(entityClass: entity, persistentStores: [persistentStore] + otherPersistentStores)
}
/**
Initializes a `From` clause with the specified `NSPersistentStore`s.
- parameter entity: the associated `NSManagedObject` type
- parameter persistentStores: the `NSPersistentStore`s to associate objects from.
*/
public init(_ entity: T.Type, _ persistentStores: [NSPersistentStore]) {
self.init(entityClass: entity, persistentStores: persistentStores)
}
/**
Initializes a `From` clause with the specified `NSPersistentStore`s.
- parameter entity: the associated `NSManagedObject` entity class
- parameter persistentStore: the `NSPersistentStore` to associate objects from.
- parameter otherPersistentStores: an optional list of other `NSPersistentStore`s to associate objects from (see `persistentStore` parameter)
*/
public init(_ entityClass: AnyClass, _ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) {
self.init(entityClass: entityClass, persistentStores: [persistentStore] + otherPersistentStores)
}
/**
Initializes a `From` clause with the specified `NSPersistentStore`s.
- parameter entity: the associated `NSManagedObject` entity class
- parameter persistentStores: the `NSPersistentStore`s to associate objects from.
*/
public init(_ entityClass: AnyClass, _ persistentStores: [NSPersistentStore]) {
self.init(entityClass: entityClass, persistentStores: persistentStores)
}
// MARK: Internal
@@ -399,4 +277,131 @@ public struct From<T: NSManagedObject> {
self.dumpInfo = dumpInfo
self.findPersistentStores = findPersistentStores
}
// MARK: Deprecated
/**
Deprecated. Use initializers that accept configuration names.
*/
@available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.")
public init(_ storeURL: NSURL, _ otherStoreURLs: NSURL...) {
self.init(entityClass: T.self, storeURLs: [storeURL] + otherStoreURLs)
}
/**
Deprecated. Use initializers that accept configuration names.
*/
@available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.")
public init(_ storeURLs: [NSURL]) {
self.init(entityClass: T.self, storeURLs: storeURLs)
}
/**
Deprecated. Use initializers that accept configuration names.
*/
@available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.")
public init(_ entity: T.Type, _ storeURL: NSURL, _ otherStoreURLs: NSURL...) {
self.init(entityClass: entity, storeURLs: [storeURL] + otherStoreURLs)
}
/**
Deprecated. Use initializers that accept configuration names.
*/
@available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.")
public init(_ entity: T.Type, _ storeURLs: [NSURL]) {
self.init(entityClass: entity, storeURLs: storeURLs)
}
/**
Deprecated. Use initializers that accept configuration names.
*/
@available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.")
public init(_ entityClass: AnyClass, _ storeURL: NSURL, _ otherStoreURLs: NSURL...) {
CoreStore.assert(
entityClass is T.Type,
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
)
self.init(entityClass: entityClass, storeURLs: [storeURL] + otherStoreURLs)
}
/**
Deprecated. Use initializers that accept configuration names.
*/
@available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.")
public init(_ entityClass: AnyClass, _ storeURLs: [NSURL]) {
CoreStore.assert(
entityClass is T.Type,
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
)
self.init(entityClass: entityClass, storeURLs: storeURLs)
}
/**
Deprecated. Use initializers that accept configuration names.
*/
@available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.")
public init(_ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) {
self.init(entityClass: T.self, persistentStores: [persistentStore] + otherPersistentStores)
}
/**
Deprecated. Use initializers that accept configuration names.
*/
@available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.")
public init(_ persistentStores: [NSPersistentStore]) {
self.init(entityClass: T.self, persistentStores: persistentStores)
}
/**
Deprecated. Use initializers that accept configuration names.
*/
@available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.")
public init(_ entity: T.Type, _ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) {
self.init(entityClass: entity, persistentStores: [persistentStore] + otherPersistentStores)
}
/**
Deprecated. Use initializers that accept configuration names.
*/
@available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.")
public init(_ entity: T.Type, _ persistentStores: [NSPersistentStore]) {
self.init(entityClass: entity, persistentStores: persistentStores)
}
/**
Deprecated. Use initializers that accept configuration names.
*/
@available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.")
public init(_ entityClass: AnyClass, _ persistentStore: NSPersistentStore, _ otherPersistentStores: NSPersistentStore...) {
CoreStore.assert(
entityClass is T.Type,
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
)
self.init(entityClass: entityClass, persistentStores: [persistentStore] + otherPersistentStores)
}
/**
Deprecated. Use initializers that accept configuration names.
*/
@available(*, deprecated=2.0.0, message="Use initializers that accept configuration names.")
public init(_ entityClass: AnyClass, _ persistentStores: [NSPersistentStore]) {
CoreStore.assert(
entityClass is T.Type,
"Attempted to create generic type \(cs_typeName(From<T>)) with entity class \(cs_typeName(entityClass))"
)
self.init(entityClass: entityClass, persistentStores: persistentStores)
}
}

View File

@@ -52,6 +52,10 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll
from?.applyToFetchRequest(fetchRequest, context: context, applyAffectedStores: false)
applyFetchClauses(fetchRequest: fetchRequest)
CoreStore.assert(
fetchRequest.sortDescriptors?.isEmpty == false,
"An \(cs_typeName(NSFetchedResultsController)) requires a sort information. Specify from a \(cs_typeName(OrderBy)) clause or any custom \(cs_typeName(FetchClause)) that provides a sort descriptor."
)
if let from = from {
self.reapplyAffectedStores = { fetchRequest, context in
@@ -63,7 +67,7 @@ internal final class CoreStoreFetchedResultsController: NSFetchedResultsControll
guard let from = (fetchRequest.entity.flatMap { $0.managedObjectClassName }).flatMap(NSClassFromString).flatMap(From.init) else {
fatalError("Attempted to create an \(cs_typeName(NSFetchedResultsController)) without a From clause or an NSEntityDescription.")
fatalError("Attempted to create an \(cs_typeName(NSFetchedResultsController)) without a \(cs_typeName(From)) clause or an \(cs_typeName(NSEntityDescription)).")
}
self.reapplyAffectedStores = { fetchRequest, context in

View File

@@ -96,31 +96,25 @@ public final class CSUnsafeDataTransaction: CSBaseDataTransaction {
Immediately flushes all pending changes to the transaction's observers. This is useful in conjunction with `ListMonitor`s and `ObjectMonitor`s created from `UnsafeDataTransaction`s used to manage temporary "scratch" data.
- Important: Note that unlike `commit()`, `flush()` does not propagate/save updates to the `DataStack` and the persistent store. However, the flushed changes will be seen by children transactions created further from the current transaction (i.e. through `transaction.beginUnsafe()`)
- parameter error: the `NSError` pointer that indicates the reason in case of an failure
*/
@objc
public func flush(error error: NSErrorPointer) {
public func flush() {
bridge(error) {
try self.bridgeToSwift.flush()
}
self.bridgeToSwift.flush()
}
/**
Flushes all pending changes to the transaction's observers at the end of the `closure`'s execution. This is useful in conjunction with `ListMonitor`s and `ObjectMonitor`s created from `UnsafeDataTransaction`s used to manage temporary "scratch" data.
- Important: Note that unlike `commit()`, `flush()` does not propagate/save updates to the `DataStack` and the persistent store. However, the flushed changes will be seen by children transactions created further from the current transaction (i.e. through `transaction.beginUnsafe()`)
- parameter error: the `NSError` pointer that indicates the reason in case of an failure
- parameter closure: the closure where changes can be made prior to the flush
*/
@objc
public func flush(error error: NSErrorPointer, block: () -> Void) throws {
public func flush(block: () -> Void) {
bridge(error) {
self.bridgeToSwift.flush {
block()
try self.bridgeToSwift.context.save()
}
}

View File

@@ -78,10 +78,6 @@ public extension DataStack {
NSThread.isMainThread(),
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
)
CoreStore.assert(
fetchClauses.contains { $0 is OrderBy },
"A ListMonitor requires an OrderBy clause."
)
return ListMonitor(
dataStack: self,
from: from,
@@ -118,10 +114,6 @@ public extension DataStack {
NSThread.isMainThread(),
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
)
CoreStore.assert(
fetchClauses.contains { $0 is OrderBy },
"A ListMonitor requires an OrderBy clause."
)
_ = ListMonitor(
dataStack: self,
from: from,
@@ -163,10 +155,6 @@ public extension DataStack {
NSThread.isMainThread(),
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
)
CoreStore.assert(
fetchClauses.contains { $0 is OrderBy },
"A ListMonitor requires an OrderBy clause."
)
return ListMonitor(
dataStack: self,
@@ -206,10 +194,6 @@ public extension DataStack {
NSThread.isMainThread(),
"Attempted to observe objects from \(cs_typeName(self)) outside the main thread."
)
CoreStore.assert(
fetchClauses.contains { $0 is OrderBy },
"A ListMonitor requires an OrderBy clause."
)
_ = ListMonitor(
dataStack: self,

View File

@@ -75,6 +75,10 @@ public struct Into<T: NSManagedObject>: Hashable {
*/
public init(_ entityClass: AnyClass) {
CoreStore.assert(
entityClass is T.Type,
"Attempted to create generic type \(cs_typeName(Into<T>)) with entity class \(cs_typeName(entityClass))"
)
self.init(entityClass: entityClass, configuration: nil, inferStoreIfPossible: true)
}
@@ -116,6 +120,10 @@ public struct Into<T: NSManagedObject>: Hashable {
*/
public init(_ entityClass: AnyClass, _ configuration: String?) {
CoreStore.assert(
entityClass is T.Type,
"Attempted to create generic type \(cs_typeName(Into<T>)) with entity class \(cs_typeName(entityClass))"
)
self.init(entityClass: entityClass, configuration: configuration, inferStoreIfPossible: false)
}
@@ -171,3 +179,11 @@ public func == <T: NSManagedObject, U: NSManagedObject>(lhs: Into<T>, rhs: Into<
&& lhs.configuration == rhs.configuration
&& lhs.inferStoreIfPossible == rhs.inferStoreIfPossible
}
@warn_unused_result
public func != <T: NSManagedObject, U: NSManagedObject>(lhs: Into<T>, rhs: Into<U>) -> Bool {
return lhs.entityClass == rhs.entityClass
&& lhs.configuration == rhs.configuration
&& lhs.inferStoreIfPossible == rhs.inferStoreIfPossible
}