From 13f43e689e9a9017b870f2129beb9d6263ec9a01 Mon Sep 17 00:00:00 2001 From: pjechris Date: Sun, 25 Aug 2024 20:09:36 +0200 Subject: [PATCH] tech(unit tests): Don't use EntityStore internal properties (#71) --- Sources/CohesionKit/EntityStore.swift | 4 +-- Tests/CohesionKitTests/EntityStoreTests.swift | 36 ++++++++++--------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Sources/CohesionKit/EntityStore.swift b/Sources/CohesionKit/EntityStore.swift index f6398c0..c6fbe12 100644 --- a/Sources/CohesionKit/EntityStore.swift +++ b/Sources/CohesionKit/EntityStore.swift @@ -12,8 +12,8 @@ public class EntityStore { private let logger: Logger? private let registry: ObserverRegistry - private(set) var storage: EntitiesStorage = EntitiesStorage() - private(set) var refAliases: AliasStorage = [:] + private var storage: EntitiesStorage = EntitiesStorage() + private var refAliases: AliasStorage = [:] private lazy var storeVisitor = EntityStoreStoreVisitor(entityStore: self) /// Create a new EntityStore instance optionally with a queue and a logger diff --git a/Tests/CohesionKitTests/EntityStoreTests.swift b/Tests/CohesionKitTests/EntityStoreTests.swift index cdf00c5..d2c0661 100644 --- a/Tests/CohesionKitTests/EntityStoreTests.swift +++ b/Tests/CohesionKitTests/EntityStoreTests.swift @@ -14,18 +14,18 @@ class EntityStoreTests: XCTestCase { let entityStore = EntityStore() withExtendedLifetime(entityStore.store(entity: entity)) { _ in - XCTAssertNotNil(entityStore.storage[SingleNodeFixture.self, id: 1]) - XCTAssertNotNil(entityStore.storage[OptionalNodeFixture.self, id: 1]) - XCTAssertNotNil(entityStore.storage[ListNodeFixture.self, id: 1]) + XCTAssertNotNil(entityStore.find(SingleNodeFixture.self, id: 1)) + XCTAssertNotNil(entityStore.find(OptionalNodeFixture.self, id: 1)) + XCTAssertNotNil(entityStore.find(ListNodeFixture.self, id: 1)) } } - func test_storeAggregate_nestedEntityReplacedByNil_entityIsUpdated_aggregateEntityRemainsNil() { + func test_storeAggregate_nestedEntitySetToNil_entityIsUpdated_aggregateNestedEntityRemainsNil() throws { let entityStore = EntityStore() let nestedOptional = OptionalNodeFixture(id: 1) var root = RootFixture(id: 1, primitive: "", singleNode: SingleNodeFixture(id: 1), optional: nestedOptional, listNodes: []) - withExtendedLifetime(entityStore.store(entity: root)) { + try withExtendedLifetime(entityStore.store(entity: root)) { root.optional = nil _ = entityStore.store(entity: root) @@ -33,52 +33,56 @@ class EntityStoreTests: XCTestCase { XCTAssertNotNil(entityStore.find(RootFixture.self, id: 1)) XCTAssertNil(entityStore.find(RootFixture.self, id: 1)!.value.optional) + let root = try XCTUnwrap(entityStore.find(RootFixture.self, id: 1)) + XCTAssertNil(root.value.optional) } } /// check that removed relations do not trigger an update - func test_storeAggregate_removeEntityFromNestedArray_removedEntityChange_aggregateArrayNotChanged() { + func test_storeAggregate_removeEntityFromNestedArray_removedEntityChange_aggregateArrayNotChanged() throws { let entityStore = EntityStore() var entityToRemove = ListNodeFixture(id: 2) let nestedArray: [ListNodeFixture] = [entityToRemove, ListNodeFixture(id: 1)] var root = RootFixture(id: 1, primitive: "", singleNode: SingleNodeFixture(id: 1), optional: OptionalNodeFixture(id: 1), listNodes: nestedArray) - withExtendedLifetime(entityStore.store(entity: root)) { + try withExtendedLifetime(entityStore.store(entity: root)) { root.listNodes = Array(nestedArray[1...]) entityToRemove.key = "changed" _ = entityStore.store(entity: root) _ = entityStore.store(entity: entityToRemove) - let storedRoot = entityStore.find(RootFixture.self, id: 1)!.value + let root = try XCTUnwrap(entityStore.find(RootFixture.self, id: 1)) - XCTAssertFalse(storedRoot.listNodes.contains(entityToRemove)) - XCTAssertFalse(storedRoot.listNodes.map(\.id).contains(entityToRemove.id)) + XCTAssertFalse(root.value.listNodes.contains(entityToRemove)) + XCTAssertFalse(root.value.listNodes.map(\.id).contains(entityToRemove.id)) } } - func test_storeAggregate_nestedWrapperChanged_aggregateIsUpdated() { + func test_storeAggregate_nestedWrapperChanged_aggregateIsUpdated() throws { let entityStore = EntityStore() let root = RootFixture(id: 1, primitive: "", singleNode: SingleNodeFixture(id: 1), optional: OptionalNodeFixture(id: 1), listNodes: [], enumWrapper: .single(SingleNodeFixture(id: 2))) let updatedValue = SingleNodeFixture(id: 2, primitive: "updated") - withExtendedLifetime(entityStore.store(entity: root)) { + try withExtendedLifetime(entityStore.store(entity: root)) { _ = entityStore.store(entity: updatedValue) - XCTAssertEqual(entityStore.find(RootFixture.self, id: 1)!.value.enumWrapper, .single(updatedValue)) + let root = try XCTUnwrap(entityStore.find(RootFixture.self, id: 1)) + XCTAssertEqual(root.value.enumWrapper, .single(updatedValue)) } } - func test_storeAggregate_nestedOptionalWrapperNullified_aggregateIsNullified() { + func test_storeAggregate_nestedOptionalWrapperNullified_aggregateIsNullified() throws { let entityStore = EntityStore() var root = RootFixture(id: 1, primitive: "", singleNode: SingleNodeFixture(id: 1), optional: OptionalNodeFixture(id: 1), listNodes: [], enumWrapper: .single(SingleNodeFixture(id: 2))) - withExtendedLifetime(entityStore.store(entity: root)) { + try withExtendedLifetime(entityStore.store(entity: root)) { root.enumWrapper = nil _ = entityStore.store(entity: root) _ = entityStore.store(entity: SingleNodeFixture(id: 2, primitive: "deleted")) - XCTAssertNil(entityStore.find(RootFixture.self, id: 1)!.value.enumWrapper) + let root = try XCTUnwrap(entityStore.find(RootFixture.self, id: 1)) + XCTAssertNil(root.value.enumWrapper) } }