add general CO3 tests to test suite

This commit is contained in:
Sam G. 2024-04-18 13:31:19 -07:00
parent 1f89f0dee8
commit 2335001a80
4 changed files with 29 additions and 47 deletions

View File

@ -113,6 +113,7 @@ class CO3(metaclass=FormatRegistryMeta):
logger.debug(f'Collation for {action_key} not supported') logger.debug(f'Collation for {action_key} not supported')
return None return None
else: else:
return self.action_registry[action_key][0](self) action_method = self.action_registry[action_key][0]
return action_method(self, *action_args, **action_kwargs)

View File

@ -6,3 +6,5 @@ myst-parser
sqlalchemy sqlalchemy
numpy numpy
pytest

View File

@ -24,7 +24,7 @@ class Tomato(Vegetable):
def attributes(self): def attributes(self):
return vars(self) return vars(self)
def collation_attributes(self, action_key, action_grounp): def collation_attributes(self, action_key, action_group):
return { return {
'name': self.name, 'name': self.name,
'state': action_key, 'state': action_key,

View File

@ -1,57 +1,36 @@
from collections import defaultdict
from co3.components import Relation from co3.components import Relation
from setups import vegetables as veg from setups import vegetables as veg
def test_mapper_getters(): tomato = veg.Tomato('t1', 10)
veg_comp = veg.vegetable_schema.get_component('vegetable')
tom_comp = veg.vegetable_schema.get_component('tomato')
assert veg.vegetable_mapper.get_attr_comp(veg.Vegetable) is veg_comp def test_co3_registry():
assert veg.vegetable_mapper.get_attr_comp(veg.Tomato) is tom_comp keys_to_groups = defaultdict(list)
tom_aging = veg.vegetable_schema.get_component('tomato_aging_states') # collect groups each key is associated
tom_cooking = veg.vegetable_schema.get_component('tomato_cooking_states') for action_group, action_keys in tomato.group_registry.items():
for action_key in action_keys:
keys_to_groups[action_key].append(action_group)
assert veg.vegetable_mapper.get_coll_comp(veg.Tomato, 'aging') is tom_aging # check against `action_registry`, should map keys to all groups
assert veg.vegetable_mapper.get_coll_comp(veg.Tomato, 'cooking') is tom_cooking for action_key, (_, action_groups) in tomato.action_registry.items():
assert keys_to_groups.get(action_key) == action_groups
def test_mapper_attach(): def test_co3_attributes():
assert veg.vegetable_mapper.attach( assert tomato.attributes is not None
veg.Tomato,
'tomato',
coll_groups={
'aging': 'tomato_aging_states',
'cooking': 'tomato_cooking_states',
},
) is None
def test_mapper_attach_many(): def test_co3_components():
assert veg.vegetable_mapper.attach_many( assert tomato.components is not None
[veg.Vegetable, veg.Tomato],
lambda t: f'{t.__name__.lower()}'
) is None
def test_mapper_collect():
tomato = veg.Tomato('t1', 10)
receipts = veg.vegetable_mapper.collect(tomato)
assert len(receipts) == 2
# attempt to retrieve receipts one at a time
res1 = veg.vegetable_mapper.collector.collect_inserts([receipts[0]])
assert len(res1) == 1 # should be just one match
assert len(res1[next(iter(res1.keys()))]) == 1 # and one dict for matching comp
# try again, check no persistent match
res1 = veg.vegetable_mapper.collector.collect_inserts([receipts[0]])
assert len(res1) == 0 # should be no matches for the same receipt
res2 = veg.vegetable_mapper.collector.collect_inserts([receipts[1]])
assert len(res2) == 1
assert len(res2[next(iter(res2.keys()))]) == 1
def test_co3_collation_attributes():
for action_group, action_keys in tomato.group_registry.items():
for action_key in action_keys:
assert tomato.collation_attributes(action_key, action_group) is not None
def test_co3_collate():
for action_group, action_keys in tomato.group_registry.items():
for action_key in action_keys:
assert tomato.collate(action_key) is not None