Implemented some test cases for the Fetcher class and the URL+Device extension.
This commit is contained in:
@@ -62,21 +62,25 @@ public class Fetcher<Model: NSManagedObject>: NSObject, NSFetchedResultsControll
|
||||
/// Retrieve the number of objects in a given section number.
|
||||
/// - Parameter section: The section number to inquiry about.
|
||||
/// - Returns: A number of objects in the given section number.
|
||||
public func numberOfObjectsInSection(_ section: Int) -> Int {
|
||||
guard
|
||||
let sections = fetchedResultsController.sections,
|
||||
sections.endIndex > section
|
||||
else {
|
||||
return 0
|
||||
public func numberOfObjects(in section: Int) throws -> Int {
|
||||
guard let sections = fetchedResultsController.sections else {
|
||||
throw FetcherError.fetchNotExecuted
|
||||
}
|
||||
|
||||
guard sections.endIndex > section else {
|
||||
throw FetcherError.sectionNotFound
|
||||
}
|
||||
|
||||
return sections[section].numberOfObjects
|
||||
}
|
||||
|
||||
/// Retrieve an object out of a given index path.
|
||||
/// - Parameter indexPath: The index path to use to retrieve an object.
|
||||
/// - Returns: A `NSManagedObject` entity positioned in the given index path.
|
||||
public func object(at indexPath: IndexPath) -> Model {
|
||||
public func object(at indexPath: IndexPath) throws -> Model {
|
||||
guard fetchedResultsController.sections != nil else {
|
||||
throw FetcherError.fetchNotExecuted
|
||||
}
|
||||
|
||||
return fetchedResultsController.object(at: indexPath)
|
||||
}
|
||||
|
||||
@@ -134,6 +138,13 @@ public class Fetcher<Model: NSManagedObject>: NSObject, NSFetchedResultsControll
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Errors
|
||||
|
||||
public enum FetcherError: Error {
|
||||
case fetchNotExecuted
|
||||
case sectionNotFound
|
||||
}
|
||||
|
||||
// MARK: - Enumerations
|
||||
|
||||
public enum Change: Hashable {
|
||||
|
||||
@@ -27,10 +27,10 @@ public protocol Service {
|
||||
|
||||
/// Save a given context.
|
||||
/// - Parameter context: A `NSManagedObjectContext` context instance to save.
|
||||
func save(context: NSManagedObjectContext)
|
||||
func save(context: NSManagedObjectContext) throws
|
||||
|
||||
/// Save a given child context as well as its respective parent context.
|
||||
/// - Parameter context: A child `NSManagedObjectContext` context instance to save.
|
||||
func save(childContext context: NSManagedObjectContext)
|
||||
func save(childContext context: NSManagedObjectContext) throws
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user