Skip to content

Commit

Permalink
tech(unit tests): Don't use EntityStore internal properties (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjechris authored Aug 25, 2024
1 parent ba0173b commit 13f43e6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Sources/CohesionKit/EntityStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 20 additions & 16 deletions Tests/CohesionKitTests/EntityStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,75 @@ 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)
_ = entityStore.store(entity: nestedOptional)

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)
}
}

Expand Down

0 comments on commit 13f43e6

Please sign in to comment.