Skip to content

Commit

Permalink
Update unit tests for CoreDataManager
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmeichigo committed Oct 3, 2024
1 parent bd20257 commit 7481bd3
Showing 1 changed file with 69 additions and 14 deletions.
83 changes: 69 additions & 14 deletions Storage/StorageTests/CoreData/CoreDataManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,77 @@ final class CoreDataManagerTests: XCTestCase {
XCTAssertEqual(viewContext.countObjects(ofType: ShippingLine.self), 0)
}

func test_saving_derived_storage_while_resetting_CoreData_does_not_save_data() throws {
func test_performAndSave_executes_changes_in_background_then_updates_viewContext() throws {
// Arrange
let manager = CoreDataManager(name: storageIdentifier, crashLogger: MockCrashLogger())
manager.reset()
let modelsInventory = try makeModelsInventory()
let manager = try makeManager(using: modelsInventory, deletingExistingStoreFiles: true)
let viewContext = try XCTUnwrap(manager.viewStorage as? NSManagedObjectContext)
let derivedContext = try XCTUnwrap(manager.writerDerivedStorage as? NSManagedObjectContext)
XCTAssertEqual(viewContext.countObjects(ofType: Account.self), 0)

// Action
waitForExpectation(count: 2) { expectation in
derivedContext.perform {
_ = derivedContext.insertNewObject(ofType: ShippingLine.self)
}
manager.saveDerivedType(derivedStorage: derivedContext, {
XCTAssertEqual(viewContext.countObjects(ofType: ShippingLine.self), 1)
waitForExpectation(count: 1) { expectation in
manager.performAndSave({ storage in
XCTAssertFalse(Thread.current.isMainThread)
self.insertAccount(to: storage)
}, completion: {
XCTAssertTrue(Thread.current.isMainThread)
expectation.fulfill()
})
manager.reset()
expectation.fulfill()
}, on: .main)
}

// Assert
XCTAssertEqual(viewContext.countObjects(ofType: ShippingLine.self), 0)
XCTAssertEqual(viewContext.countObjects(ofType: Account.self), 1)
}

func test_performAndSave_with_result_returns_correct_result_upon_success() throws {
// Arrange
let modelsInventory = try makeModelsInventory()
let manager = try makeManager(using: modelsInventory, deletingExistingStoreFiles: true)
let viewContext = try XCTUnwrap(manager.viewStorage as? NSManagedObjectContext)
XCTAssertEqual(viewContext.countObjects(ofType: Account.self), 0)

// Action
let result: Result<Account, Error> = waitFor { promise in
manager.performAndSave({ storage -> Account in
XCTAssertFalse(Thread.current.isMainThread)
let account = self.insertAccount(to: storage)
return account
}, completion: { result in
promise(result)
}, on: .main)
}

// Assert
let account = try result.get()
XCTAssertEqual(account.userID, 0)
XCTAssertEqual(account.username, "")
XCTAssertEqual(viewContext.countObjects(ofType: Account.self), 1)
}

func test_performAndSave_with_result_returns_correct_result_upon_failure() throws {
// Arrange
let modelsInventory = try makeModelsInventory()
let manager = try makeManager(using: modelsInventory, deletingExistingStoreFiles: true)
let viewContext = try XCTUnwrap(manager.viewStorage as? NSManagedObjectContext)
XCTAssertEqual(viewContext.countObjects(ofType: Account.self), 0)

// Action
let result: Result<Account, Error> = waitFor { promise in
manager.performAndSave({ storage -> Account in
XCTAssertFalse(Thread.current.isMainThread)
throw CoreDataManagerTestsError.unexpectedFailure
}, completion: { result in
promise(result)
}, on: .main)
}

// Assert
switch result {
case .success:
XCTFail("Result should be failure")
case .failure(let error):
XCTAssertTrue(error is CoreDataManagerTestsError)
}
}

func test_when_the_model_is_incompatible_then_it_recovers_and_recreates_the_database() throws {
Expand Down Expand Up @@ -272,3 +321,9 @@ private extension CoreDataManagerTests {
line: line)
}
}

private extension CoreDataManagerTests {
enum CoreDataManagerTestsError: Error {
case unexpectedFailure
}
}

0 comments on commit 7481bd3

Please sign in to comment.