From 0c61f79f2ba3ce961e88b1424231653bf2b0f982 Mon Sep 17 00:00:00 2001 From: "Sam G." Date: Thu, 18 Apr 2024 13:31:19 -0700 Subject: [PATCH] add general CO3 tests to test suite --- co3/co3.py | 3 +- requirements.txt | 2 ++ tests/setups/vegetables.py | 2 +- tests/test_co3.py | 69 +++++++++++++------------------------- 4 files changed, 29 insertions(+), 47 deletions(-) diff --git a/co3/co3.py b/co3/co3.py index cc0ff4f..c599fd5 100644 --- a/co3/co3.py +++ b/co3/co3.py @@ -113,6 +113,7 @@ class CO3(metaclass=FormatRegistryMeta): logger.debug(f'Collation for {action_key} not supported') return None 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) diff --git a/requirements.txt b/requirements.txt index f05ce5b..65f68c4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,5 @@ myst-parser sqlalchemy numpy + +pytest diff --git a/tests/setups/vegetables.py b/tests/setups/vegetables.py index ed09698..6b4863f 100644 --- a/tests/setups/vegetables.py +++ b/tests/setups/vegetables.py @@ -24,7 +24,7 @@ class Tomato(Vegetable): def attributes(self): return vars(self) - def collation_attributes(self, action_key, action_grounp): + def collation_attributes(self, action_key, action_group): return { 'name': self.name, 'state': action_key, diff --git a/tests/test_co3.py b/tests/test_co3.py index 65538ff..863d865 100644 --- a/tests/test_co3.py +++ b/tests/test_co3.py @@ -1,57 +1,36 @@ +from collections import defaultdict + from co3.components import Relation from setups import vegetables as veg -def test_mapper_getters(): - veg_comp = veg.vegetable_schema.get_component('vegetable') - tom_comp = veg.vegetable_schema.get_component('tomato') +tomato = veg.Tomato('t1', 10) - assert veg.vegetable_mapper.get_attr_comp(veg.Vegetable) is veg_comp - assert veg.vegetable_mapper.get_attr_comp(veg.Tomato) is tom_comp +def test_co3_registry(): + keys_to_groups = defaultdict(list) - tom_aging = veg.vegetable_schema.get_component('tomato_aging_states') - tom_cooking = veg.vegetable_schema.get_component('tomato_cooking_states') + # collect groups each key is associated + 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 - assert veg.vegetable_mapper.get_coll_comp(veg.Tomato, 'cooking') is tom_cooking + # check against `action_registry`, should map keys to all groups + for action_key, (_, action_groups) in tomato.action_registry.items(): + assert keys_to_groups.get(action_key) == action_groups -def test_mapper_attach(): - assert veg.vegetable_mapper.attach( - veg.Tomato, - 'tomato', - coll_groups={ - 'aging': 'tomato_aging_states', - 'cooking': 'tomato_cooking_states', - }, - ) is None +def test_co3_attributes(): + assert tomato.attributes is not None -def test_mapper_attach_many(): - assert veg.vegetable_mapper.attach_many( - [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_components(): + assert tomato.components is not None +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