[Feature] Persistence (#5)
This PR contains the work done to implement some useful protocols, classes and extensions to use to setup a persistence layer in an application. To provide further details about the work done: - [x] declared the `Persistence` target in the Package file; - [x] forgot to declare the `Communications` and `Persistence` target to the `SwiftLibs` library in the `Package` file; - [x] defined the `Service` public protocol; - [x] implemented the `Fetcher` generic class; - [x] implemented the `bitBucket` static property in the `URL+Devices` public extension; - [x] updated the `README` file. Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Reviewed-on: #5
This commit was merged in pull request #5.
This commit is contained in:
@@ -0,0 +1,319 @@
|
||||
//
|
||||
// FetcherTests.swift
|
||||
// PersistenceTests
|
||||
//
|
||||
// Created by Javier Cicchelli on 17/04/2023.
|
||||
// Copyright © 2023 Röck+Cöde. All rights reserved.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import Persistence
|
||||
import XCTest
|
||||
|
||||
final class FetcherTests: XCTestCase {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private lazy var persistence: TestPersistenceService = .shared
|
||||
private lazy var fetcher: Fetcher<TestEntity> = .init(
|
||||
fetchRequest: .allTestEntities(),
|
||||
managedObjectContext: persistence.viewContext
|
||||
)
|
||||
|
||||
// MARK: Setup
|
||||
|
||||
override func tearDownWithError() throws {
|
||||
try persistence.clean()
|
||||
}
|
||||
|
||||
// MARK: Number of sections tests
|
||||
|
||||
func test_numberOfSections_whenModelIsEmpty() throws {
|
||||
// GIVEN
|
||||
try fetcher.fetch()
|
||||
|
||||
// WHEN
|
||||
let numberOfSections = fetcher.numberOfSections
|
||||
|
||||
// THEN
|
||||
XCTAssertEqual(numberOfSections, 1)
|
||||
}
|
||||
|
||||
func test_numberOfSections_whenModelIsFilled() throws {
|
||||
// GIVEN
|
||||
let context = persistence.makeChildContext()
|
||||
let _ = [
|
||||
TestEntity(context: context),
|
||||
TestEntity(context: context),
|
||||
TestEntity(context: context)
|
||||
]
|
||||
|
||||
try persistence.save(childContext: context)
|
||||
try fetcher.fetch()
|
||||
|
||||
// WHEN
|
||||
let numberOfSections = fetcher.numberOfSections
|
||||
|
||||
// THEN
|
||||
XCTAssertEqual(numberOfSections, 1)
|
||||
}
|
||||
|
||||
func test_numberOfSections_whenNoFetch() async throws {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
let numberOfSections = fetcher.numberOfSections
|
||||
|
||||
// THEN
|
||||
XCTAssertEqual(numberOfSections, 0)
|
||||
}
|
||||
|
||||
// MARK: Number of objects tests
|
||||
|
||||
func test_numberOfObjects_inFirstSection_whenModelIsEmpty() throws {
|
||||
// GIVEN
|
||||
try fetcher.fetch()
|
||||
|
||||
// WHEN
|
||||
let section = fetcher.numberOfSections - 1
|
||||
let numberOfObjects = try fetcher.numberOfObjects(in: section)
|
||||
|
||||
// THEN
|
||||
XCTAssertEqual(numberOfObjects, 0)
|
||||
}
|
||||
|
||||
func test_numberOfObjects_inFirstSection_whenModelIsFilled() throws {
|
||||
// GIVEN
|
||||
let context = persistence.makeChildContext()
|
||||
let entities = [
|
||||
TestEntity(context: context),
|
||||
TestEntity(context: context),
|
||||
TestEntity(context: context)
|
||||
]
|
||||
|
||||
try persistence.save(childContext: context)
|
||||
try fetcher.fetch()
|
||||
|
||||
// WHEN
|
||||
let section = fetcher.numberOfSections - 1
|
||||
let numberOfObjects = try fetcher.numberOfObjects(in: section)
|
||||
|
||||
// THEN
|
||||
XCTAssertEqual(numberOfObjects, entities.count)
|
||||
}
|
||||
|
||||
func test_numberOfObjects_inNonExistingSection() throws {
|
||||
// GIVEN
|
||||
try fetcher.fetch()
|
||||
|
||||
// WHEN & THEN
|
||||
let section = fetcher.numberOfSections
|
||||
|
||||
XCTAssertThrowsError(try fetcher.numberOfObjects(in: section)) { error in
|
||||
XCTAssertEqual(error as? FetcherError, .sectionNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func test_numberOfObjects_whenNoFetch() throws {
|
||||
// GIVEN
|
||||
// WHEN & THEN
|
||||
XCTAssertThrowsError(try fetcher.numberOfObjects(in: 1)) { error in
|
||||
XCTAssertEqual(error as? FetcherError, .fetchNotExecuted)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Object at tests
|
||||
|
||||
func test_objectAt_whenModelIsEmpty() throws {
|
||||
// GIVEN
|
||||
try fetcher.fetch()
|
||||
|
||||
// WHEN & THEN
|
||||
let _ = IndexPath(
|
||||
item: 0,
|
||||
section: fetcher.numberOfSections - 1
|
||||
)
|
||||
|
||||
// TODO: Need to find out how to handle NSInvalidArgumentException in this test.
|
||||
// let object = try fetcher.object(at: indexPath)
|
||||
}
|
||||
|
||||
func test_objectAt_whenModelIsFilled() throws {
|
||||
// GIVEN
|
||||
let context = persistence.makeChildContext()
|
||||
let entities = [TestEntity(context: context)]
|
||||
|
||||
try persistence.save(childContext: context)
|
||||
try fetcher.fetch()
|
||||
|
||||
// WHEN & THEN
|
||||
let indexPath = IndexPath(
|
||||
item: entities.count - 1,
|
||||
section: fetcher.numberOfSections - 1
|
||||
)
|
||||
|
||||
let object = try fetcher.object(at: indexPath)
|
||||
|
||||
XCTAssertNotNil(object)
|
||||
}
|
||||
|
||||
func test_objectAt_withOutOfBoundsIndexPath() throws {
|
||||
// GIVEN
|
||||
let context = persistence.makeChildContext()
|
||||
let entities = [TestEntity(context: context)]
|
||||
|
||||
try persistence.save(childContext: context)
|
||||
try fetcher.fetch()
|
||||
|
||||
// WHEN & THEN
|
||||
let _ = IndexPath(
|
||||
item: entities.count,
|
||||
section: fetcher.numberOfSections
|
||||
)
|
||||
|
||||
// TODO: Need to find out how to handle NSInvalidArgumentException in this test.
|
||||
// let object = try fetcher.object(at: indexPath)
|
||||
}
|
||||
|
||||
func test_objectAt_whenNoFetch() throws {
|
||||
// GIVEN
|
||||
// WHEN & THEN
|
||||
let indexPath = IndexPath(
|
||||
item: 0,
|
||||
section: 0
|
||||
)
|
||||
|
||||
XCTAssertThrowsError(try fetcher.object(at: indexPath)) { error in
|
||||
XCTAssertEqual(error as? FetcherError, .fetchNotExecuted)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Did change publisher tests
|
||||
|
||||
func test_didChangePublisher_whenModelIsEmpty() throws {
|
||||
let expectation = self.expectation(description: "didChangePublisher when model is filled.")
|
||||
|
||||
var result: [Change]?
|
||||
|
||||
// GIVEN
|
||||
let cancellable = fetcher
|
||||
.didChangePublisher
|
||||
.sink(receiveValue: { value in
|
||||
result = value
|
||||
|
||||
expectation.fulfill()
|
||||
})
|
||||
|
||||
// WHEN
|
||||
try fetcher.fetch()
|
||||
|
||||
// THEN
|
||||
let waiter = XCTWaiter.wait(for: [expectation], timeout: 1.0)
|
||||
|
||||
guard waiter == .timedOut else {
|
||||
XCTFail("Waiter expected to time out.")
|
||||
return
|
||||
}
|
||||
|
||||
cancellable.cancel()
|
||||
|
||||
XCTAssertNil(result)
|
||||
}
|
||||
|
||||
func test_didChangePublisher_whenModelIsFilled() throws {
|
||||
let expectation = self.expectation(description: "didChangePublisher when model is filled.")
|
||||
|
||||
var result: [Change]?
|
||||
|
||||
// GIVEN
|
||||
let context = persistence.makeChildContext()
|
||||
let _ = [
|
||||
TestEntity(context: context),
|
||||
TestEntity(context: context),
|
||||
TestEntity(context: context)
|
||||
]
|
||||
|
||||
let cancellable = fetcher
|
||||
.didChangePublisher
|
||||
.sink(receiveValue: { value in
|
||||
result = value
|
||||
|
||||
expectation.fulfill()
|
||||
})
|
||||
|
||||
// WHEN
|
||||
try persistence.save(childContext: context)
|
||||
try fetcher.fetch()
|
||||
|
||||
// THEN
|
||||
let waiter = XCTWaiter.wait(for: [expectation], timeout: 1.0)
|
||||
|
||||
guard waiter == .timedOut else {
|
||||
XCTFail("Waiter expected to time out.")
|
||||
return
|
||||
}
|
||||
|
||||
cancellable.cancel()
|
||||
|
||||
XCTAssertNil(result)
|
||||
}
|
||||
|
||||
func test_didChangePublisher_whenModelIsUpdated() throws {
|
||||
let expectation = self.expectation(description: "didChangePublisher when model is updated.")
|
||||
|
||||
var result: [Change]?
|
||||
|
||||
// GIVEN
|
||||
let context = persistence.makeChildContext()
|
||||
let entities = [
|
||||
TestEntity(context: context),
|
||||
TestEntity(context: context),
|
||||
TestEntity(context: context)
|
||||
]
|
||||
|
||||
let cancellable = fetcher
|
||||
.didChangePublisher
|
||||
.sink(receiveValue: { value in
|
||||
result = value
|
||||
|
||||
expectation.fulfill()
|
||||
})
|
||||
|
||||
// WHEN
|
||||
try fetcher.fetch()
|
||||
try persistence.save(childContext: context)
|
||||
|
||||
// THEN
|
||||
waitForExpectations(timeout: 1.0)
|
||||
|
||||
cancellable.cancel()
|
||||
|
||||
XCTAssertNotNil(result)
|
||||
XCTAssertEqual(result?.count, entities.count)
|
||||
XCTAssertEqual(result, [
|
||||
.object(.inserted(at: .init(item: 2, section: 0))),
|
||||
.object(.inserted(at: .init(item: 1, section: 0))),
|
||||
.object(.inserted(at: .init(item: 0, section: 0))),
|
||||
])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - TestPersistenceService+Functions
|
||||
|
||||
private extension TestPersistenceService {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func clean() throws {
|
||||
let context = makeChildContext()
|
||||
|
||||
try context.performAndWait {
|
||||
try context
|
||||
.fetch(.allTestEntities())
|
||||
.forEach(context.delete)
|
||||
}
|
||||
|
||||
try save(childContext: context)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// URL+DevicesTests.swift
|
||||
// PersistenceTests
|
||||
//
|
||||
// Created by Javier Cicchelli on 17/04/2023.
|
||||
// Copyright © 2023 Röck+Cöde. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Persistence
|
||||
import XCTest
|
||||
|
||||
final class URL_DevicesTests: XCTestCase {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private var url: URL!
|
||||
|
||||
// MARK: - Tests
|
||||
|
||||
func test_bitBucket() {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
url = .bitBucket
|
||||
|
||||
// THEN
|
||||
XCTAssertEqual(url.absoluteString, "file:///dev/null")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// NSFetchRequest+TestEntity.swift
|
||||
// PersistenceTests
|
||||
//
|
||||
// Created by Javier Cicchelli on 17/04/2023.
|
||||
// Copyright © 2023 Röck+Cöde. All rights reserved.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
|
||||
extension NSFetchRequest where ResultType == TestEntity {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
static func allTestEntities() -> NSFetchRequest<TestEntity> {
|
||||
let request = TestEntity.fetchRequest()
|
||||
|
||||
request.sortDescriptors = []
|
||||
request.resultType = .managedObjectResultType
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="21754" systemVersion="22E261" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
|
||||
<entity name="TestEntity" representedClassName="TestEntity" syncable="YES" codeGenerationType="class"/>
|
||||
</model>
|
||||
@@ -0,0 +1,127 @@
|
||||
//
|
||||
// TestPersistenceService.swift
|
||||
// PersistenceTests
|
||||
//
|
||||
// Created by Javier Cicchelli on 17/04/2023.
|
||||
// Copyright © 2023 Röck+Cöde. All rights reserved.
|
||||
//
|
||||
|
||||
import CoreData
|
||||
import Persistence
|
||||
|
||||
struct TestPersistenceService {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
static let shared = TestPersistenceService()
|
||||
|
||||
private let container: NSPersistentContainer
|
||||
|
||||
var viewContext: NSManagedObjectContext {
|
||||
container.viewContext
|
||||
}
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
init() {
|
||||
guard
|
||||
let modelURL = Bundle.module.url(forResource: .Model.name, withExtension: .Model.extension),
|
||||
let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL)
|
||||
else {
|
||||
fatalError("Could not load the Core Data model from the module.")
|
||||
}
|
||||
|
||||
self.container = .init(
|
||||
name: .Model.name,
|
||||
managedObjectModel: managedObjectModel
|
||||
)
|
||||
|
||||
setContainer()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Service
|
||||
|
||||
extension TestPersistenceService: Service {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func makeTaskContext() -> NSManagedObjectContext {
|
||||
let taskContext = container.newBackgroundContext()
|
||||
|
||||
taskContext.automaticallyMergesChangesFromParent = true
|
||||
taskContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
|
||||
|
||||
return taskContext
|
||||
}
|
||||
|
||||
func makeChildContext() -> NSManagedObjectContext {
|
||||
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
||||
|
||||
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
|
||||
context.parent = container.viewContext
|
||||
context.automaticallyMergesChangesFromParent = true
|
||||
|
||||
return context
|
||||
}
|
||||
|
||||
func save(context: NSManagedObjectContext) throws {
|
||||
guard context.hasChanges else {
|
||||
return
|
||||
}
|
||||
|
||||
try context.save()
|
||||
}
|
||||
|
||||
func save(childContext context: NSManagedObjectContext) throws {
|
||||
guard context.hasChanges else {
|
||||
return
|
||||
}
|
||||
|
||||
try context.save()
|
||||
|
||||
guard
|
||||
let parent = context.parent,
|
||||
parent == container.viewContext
|
||||
else {
|
||||
return
|
||||
}
|
||||
|
||||
try parent.performAndWait {
|
||||
try parent.save()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension TestPersistenceService {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
func setContainer() {
|
||||
container.persistentStoreDescriptions = [
|
||||
.init(url: .bitBucket)
|
||||
]
|
||||
|
||||
container.loadPersistentStores { _, error in
|
||||
if let error = error as NSError? {
|
||||
fatalError("Unresolved error \(error), \(error.userInfo)")
|
||||
}
|
||||
}
|
||||
|
||||
container.viewContext.automaticallyMergesChangesFromParent = false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
enum Model {
|
||||
static let name = "TestModel"
|
||||
static let `extension` = "momd"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user