Skip to content

Commit

Permalink
fix(store): Fixed multiple store not working when alias is a collecti…
Browse files Browse the repository at this point in the history
…on (#62)
  • Loading branch information
pjechris authored Dec 19, 2023
1 parent 41c846b commit c2aeb3c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
14 changes: 7 additions & 7 deletions Sources/CohesionKit/EntityStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -91,12 +91,12 @@ public class IdentityMap {
public func store<C: Collection>(entities: C, named: AliasKey<C>? = 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)
}
}
Expand All @@ -105,12 +105,13 @@ public class IdentityMap {
public func store<C: Collection>(entities: C, named: AliasKey<C>? = 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)
}
}
Expand Down Expand Up @@ -212,7 +213,6 @@ public class IdentityMap {
return returnValue
}
}

}

// MARK: Update
Expand Down
41 changes: 29 additions & 12 deletions Tests/CohesionKitTests/EntityStoreTests.swift
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<SingleNodeFixture>.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)
Expand All @@ -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)
Expand Down Expand Up @@ -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<SingleNodeFixture>.self))
XCTAssertTrue(registry.hasPendingChange(for: SingleNodeFixture.self))
}
}
}

// MARK: Find
Expand Down

0 comments on commit c2aeb3c

Please sign in to comment.