Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 committed Apr 10, 2024
1 parent c14d37a commit 1417d3a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/hdmf/build/classgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def set_init(cls, classdict, bases, docval_args, not_inherited_fields, name):
fixed_value_attrs_to_set.append(attr_name)
elif attr_name not in attrs_not_to_set:
attrs_to_set.append(attr_name)

@docval(*docval_args, allow_positional=AllowPositional.WARNING)
def __init__(self, **kwargs):
original_kwargs = dict(kwargs)
Expand Down
56 changes: 47 additions & 9 deletions tests/unit/build_tests/test_classgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ def test_no_generators(self):

class TestPostInitGetClass(TestCase):
def setUp(self):
def post_init_method(self, **kwargs):
attr1 = kwargs['attr1']
if attr1<10:
msg = "attr1 should be >=10"
raise ValueError(msg)
self.post_init=post_init_method

def test_post_init(self):
spec = GroupSpec(
doc='A test group specification with a data type',
data_type_def='Baz',
Expand All @@ -103,21 +111,51 @@ def setUp(self):
)
namespace_catalog = NamespaceCatalog()
namespace_catalog.add_namespace(CORE_NAMESPACE, namespace)
self.type_map = TypeMap(namespace_catalog)

type_map = TypeMap(namespace_catalog)

def test_post_init(self):
def post_init_method(self, **kwargs):
attr1 = kwargs['attr1']
if attr1<10:
msg = "attr1 should be >=10"
raise ValueError(msg)

cls = self.type_map.get_dt_container_cls('Baz', CORE_NAMESPACE, post_init_method)
cls = type_map.get_dt_container_cls('Baz', CORE_NAMESPACE, self.post_init)

with self.assertRaises(ValueError):
cls(name='instance', attr1=9)

def test_multi_container_post_init(self):
bar_spec = GroupSpec(
doc='A test group specification with a data type',
data_type_def='Bar',
datasets=[
DatasetSpec(
doc='a dataset',
dtype='int',
name='data',
attributes=[AttributeSpec(name='attr2', doc='an integer attribute', dtype='int')]
)
],
attributes=[AttributeSpec(name='attr1', doc='a string attribute', dtype='text')])

multi_spec = GroupSpec(doc='A test extension that contains a multi',
data_type_def='Multi',
groups=[GroupSpec(data_type_inc=bar_spec, doc='test multi', quantity='*')],
attributes=[AttributeSpec(name='attr1', doc='a float attribute', dtype='float')])

spec_catalog = SpecCatalog()
spec_catalog.register_spec(bar_spec, 'test.yaml')
spec_catalog.register_spec(multi_spec, 'test.yaml')
namespace = SpecNamespace(
doc='a test namespace',
name=CORE_NAMESPACE,
schema=[{'source': 'test.yaml'}],
version='0.1.0',
catalog=spec_catalog
)
namespace_catalog = NamespaceCatalog()
namespace_catalog.add_namespace(CORE_NAMESPACE, namespace)
type_map = TypeMap(namespace_catalog)
Multi = type_map.get_dt_container_cls('Multi', CORE_NAMESPACE, self.post_init)

with self.assertRaises(ValueError):
Multi(name='instance', attr1=9.1)

class TestDynamicContainer(TestCase):

def setUp(self):
Expand Down

0 comments on commit 1417d3a

Please sign in to comment.