Javier Cicchelli 4210df9eb6 [Libraries] Persistence (#5)
This PR contains the work that implements the Persistence service, which is used to store and serve the data of the application.

To give further details on what was done:
- [x] created the `Persistence`library into the **Libraries** package;
- [x] defined the `Location` model into the **Model** core data model;
- [x] implemented the `PersistenceService` service;
- [x] removed the core data stack boilerplate code from the `AppDelegate` and `SceneDelegate` delegates in the *Locations* target.

Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Reviewed-on: rock-n-code/deep-linking-assignment#5
2023-04-10 22:59:58 +00:00

105 lines
3.6 KiB
Swift

//
// PersistenceServiceTests.swift
// PersistenceTests
//
// Created by Javier Cicchelli on 11/04/2023.
// Copyright © 2023 Röck+Cöde. All rights reserved.
//
import CoreData
import XCTest
@testable import Persistence
final class PersistenceServiceTests: XCTestCase {
// MARK: Properties
private var persistence: PersistenceService!
// MARK: Initialiser tests
func test_initByDefault() {
// GIVEN
// WHEN
persistence = .init()
// THEN
XCTAssertEqual(persistence.container.persistentStoreDescriptions.count, 1)
XCTAssertEqual(persistence.container.persistentStoreDescriptions.first?.type, NSSQLiteStoreType)
XCTAssertEqual(persistence.container.persistentStoreDescriptions.first?.url?.lastPathComponent, "Model.sqlite")
XCTAssertNotNil(persistence.container.viewContext)
XCTAssertTrue(persistence.container.viewContext.automaticallyMergesChangesFromParent)
}
func test_initWithInMemory() {
// GIVEN
// WHEN
persistence = .init(inMemory: true)
// THEN
XCTAssertEqual(persistence.container.persistentStoreDescriptions.count, 1)
XCTAssertEqual(persistence.container.persistentStoreDescriptions.first?.type, NSSQLiteStoreType)
XCTAssertEqual(persistence.container.persistentStoreDescriptions.first?.url?.absoluteString, "file:///dev/null")
XCTAssertNotNil(persistence.container.viewContext)
XCTAssertTrue(persistence.container.viewContext.automaticallyMergesChangesFromParent)
}
// MARK: Static properties tests
func test_shared() {
// GIVEN
persistence = .shared
// WHEN
// THEN
XCTAssertEqual(persistence.container.persistentStoreDescriptions.count, 1)
XCTAssertEqual(persistence.container.persistentStoreDescriptions.first?.type, NSSQLiteStoreType)
XCTAssertEqual(persistence.container.persistentStoreDescriptions.first?.url?.lastPathComponent, "Model.sqlite")
XCTAssertNotNil(persistence.container.viewContext)
XCTAssertTrue(persistence.container.viewContext.automaticallyMergesChangesFromParent)
}
func test_inMemory() {
// GIVEN
persistence = .inMemory
// WHEN
// THEN
XCTAssertEqual(persistence.container.persistentStoreDescriptions.count, 1)
XCTAssertEqual(persistence.container.persistentStoreDescriptions.first?.type, NSSQLiteStoreType)
XCTAssertEqual(persistence.container.persistentStoreDescriptions.first?.url?.absoluteString, "file:///dev/null")
XCTAssertNotNil(persistence.container.viewContext)
XCTAssertTrue(persistence.container.viewContext.automaticallyMergesChangesFromParent)
}
// MARK: Functions tests
func test_makeTaskContext() {
// GIVEN
persistence = .inMemory
// WHEN
let context = persistence.makeTaskContext()
// THEN
XCTAssertTrue(context.automaticallyMergesChangesFromParent)
XCTAssertTrue(context.mergePolicy as AnyObject === NSMergeByPropertyObjectTrumpMergePolicy)
XCTAssertNil(context.parent)
}
func test_makeChildContext() {
// GIVEN
persistence = .inMemory
// WHEN
let context = persistence.makeChildContext()
// THEN
XCTAssertTrue(context.automaticallyMergesChangesFromParent)
XCTAssertTrue(context.mergePolicy as AnyObject === NSMergeByPropertyObjectTrumpMergePolicy)
XCTAssertEqual(context.parent, persistence.container.viewContext)
}
}