From c2aeb3c2489d4b4c9282250955c929c9cc0b874a Mon Sep 17 00:00:00 2001 From: pjechris Date: Tue, 19 Dec 2023 17:49:05 +0100 Subject: [PATCH] fix(store): Fixed multiple store not working when alias is a collection (#62) --- Sources/CohesionKit/EntityStore.swift | 14 +++---- Tests/CohesionKitTests/EntityStoreTests.swift | 41 +++++++++++++------ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/Sources/CohesionKit/EntityStore.swift b/Sources/CohesionKit/EntityStore.swift index 7147478..48bc659 100644 --- a/Sources/CohesionKit/EntityStore.swift +++ b/Sources/CohesionKit/EntityStore.swift @@ -77,12 +77,12 @@ public class IdentityMap { update?(&entity) } - let node = nodeStore(entity: entity, modifiedAt: modifiedAt) - if let key = named { storeAlias(content: entity, key: key, modifiedAt: modifiedAt) } + let node = nodeStore(entity: entity, modifiedAt: modifiedAt) + return EntityObserver(node: node, registry: registry) } } @@ -91,12 +91,12 @@ public class IdentityMap { public func store(entities: C, named: AliasKey? = nil, modifiedAt: Stamp? = nil) -> EntityObserver<[C.Element]> where C.Element: Identifiable { transaction { - let nodes = entities.map { nodeStore(entity: $0, modifiedAt: modifiedAt) } - if let key = named { storeAlias(content: entities, key: key, modifiedAt: modifiedAt) } + let nodes = entities.map { nodeStore(entity: $0, modifiedAt: modifiedAt) } + return EntityObserver(nodes: nodes, registry: registry) } } @@ -105,12 +105,13 @@ public class IdentityMap { public func store(entities: C, named: AliasKey? = nil, modifiedAt: Stamp? = nil) -> EntityObserver<[C.Element]> where C.Element: Aggregate { transaction { - let nodes = entities.map { nodeStore(entity: $0, modifiedAt: modifiedAt) } - if let key = named { storeAlias(content: entities, key: key, modifiedAt: modifiedAt) } + let nodes = entities.map { nodeStore(entity: $0, modifiedAt: modifiedAt) } + + return EntityObserver(nodes: nodes, registry: registry) } } @@ -212,7 +213,6 @@ public class IdentityMap { return returnValue } } - } // MARK: Update diff --git a/Tests/CohesionKitTests/EntityStoreTests.swift b/Tests/CohesionKitTests/EntityStoreTests.swift index 07299be..386df45 100644 --- a/Tests/CohesionKitTests/EntityStoreTests.swift +++ b/Tests/CohesionKitTests/EntityStoreTests.swift @@ -1,8 +1,8 @@ import XCTest @testable import CohesionKit -// MARK: Store class EntityStoreTests: XCTestCase { + // MARK: Store Aggregate func test_storeAggregate_nestedEntitiesAreStored() { let entity = RootFixture( id: 1, @@ -95,6 +95,20 @@ class EntityStoreTests: XCTestCase { } } + func test_storeAggregate_named_itEnqueuesAliasInRegistry() { + let root = SingleNodeFixture(id: 1) + let registry = ObserverRegistryStub() + let identityMap = IdentityMap(registry: registry) + + withExtendedLifetime(identityMap.store(entity: root, named: .test)) { + XCTAssertTrue(registry.hasPendingChange(for: AliasContainer.self)) + XCTAssertTrue(registry.hasPendingChange(for: SingleNodeFixture.self)) + } + } +} + +// MARK: Store Entities +extension EntityStoreTests { /// make sure when inserting multiple time the same entity that it actually gets inserted only once func test_storeEntities_sameEntityPresentMultipleTimes_itIsInsertedOnce() { let registry = ObserverRegistryStub(queue: .main) @@ -108,6 +122,20 @@ class EntityStoreTests: XCTestCase { XCTAssertEqual(registry.pendingChangeCount(for: commonEntity), 1) } + func test_storeEntities_named_calledMultipleTimes_lastValueIsStored() { + let identityMap = IdentityMap() + let root = SingleNodeFixture(id: 1) + let root2 = SingleNodeFixture(id: 2) + + _ = identityMap.store(entities: [root], named: .listOfNodes) + _ = identityMap.store(entities: [root, root2], named: .listOfNodes) + + XCTAssertEqual(identityMap.find(named: .listOfNodes).value, [root, root2]) + } +} + +// MARK: Store Identifiable +extension EntityStoreTests { func test_storeIdentifiable_entityIsInsertedForThe1stTime_loggerIsCalled() { let logger = LoggerMock() let identityMap = IdentityMap(logger: logger) @@ -140,17 +168,6 @@ class EntityStoreTests: XCTestCase { wait(for: [expectation], timeout: 0) } - - func test_storeAggregate_named_itEnqueuesAliasInRegistry() { - let root = SingleNodeFixture(id: 1) - let registry = ObserverRegistryStub() - let identityMap = IdentityMap(registry: registry) - - withExtendedLifetime(identityMap.store(entity: root, named: .test)) { - XCTAssertTrue(registry.hasPendingChange(for: AliasContainer.self)) - XCTAssertTrue(registry.hasPendingChange(for: SingleNodeFixture.self)) - } - } } // MARK: Find