Skip to content

Commit

Permalink
one to many relationship unit test
Browse files Browse the repository at this point in the history
add a test suite to check a relationship implementation works
this adds a constraint on esper because this implementation use a same component instance (called group) for many entities
  • Loading branch information
Felecarpp committed Jan 23, 2024
1 parent 923533e commit 4830919
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions tests/test_relationships.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
check a one-to-many relationship implementation is working
this test suite implementation uses the theme of ownership
"""

import esper


class Inventory:
"""the entity belongs to the inventory
every entity in the same group must use the same instance of Inventory"""


class Hold:
"""the inventory belongs to this entity"""

def __init__(self, invent: Inventory) -> None:
self.invent = invent


def test_query_owned() -> None:
invent = Inventory()

owner = esper.create_entity(Hold(invent))
expected = [esper.create_entity(invent), esper.create_entity(invent)]

esper.create_entity(Inventory())

# we want to get all owned entities by owner
# step 1 : get Hold component of $owner
h = esper.try_component(owner, Hold)
assert h is not None
# step 2 : iterates entities with an Inventory component
result = []
for owned, invent in esper.get_component(Inventory):
# step 3 : skip the entities with another Inventory
if invent is not h.invent:
continue
# we would get entities owned by $owner
result.append(owned)

# check result
assert result == expected


def test_get_owner() -> None:
invent = Inventory()

expected = [esper.create_entity(Hold(invent))]
owned = esper.create_entity(invent)

esper.create_entity(Hold(Inventory()))

# we want to get the owner of $owned
# step 1 : get Inventory component of $owned
invent_ = esper.try_component(owned, Inventory)
assert invent_ is not None
# step 2 : iterates entities with a Hold component
result = []
for owner, h in esper.get_component(Hold):
# step 3 : skip entities that own an other Inventory
if h.invent is not invent_:
continue
# we would get the entity that owns $owned
result.append(owner)

# check result
assert result == expected

0 comments on commit 4830919

Please sign in to comment.