| 1 |
import inspect |
| 2 |
|
| 3 |
from hdmf.container import Container, Data, MultiContainerInterface |
| 4 |
from hdmf.testing import TestCase |
| 5 |
from hdmf.utils import LabelledDict, get_docval |
| 6 |
|
| 7 |
|
| 8 |
class OData(Data): |
| 9 |
|
| 10 |
pass |
| 11 |
|
| 12 |
|
| 13 |
class Foo(MultiContainerInterface): |
| 14 |
|
| 15 |
__clsconf__ = [ |
| 16 |
{ |
| 17 |
'attr': 'containers', |
| 18 |
'add': 'add_container', |
| 19 |
'type': (Container, ), |
| 20 |
'get': 'get_container', |
| 21 |
}, |
| 22 |
{ |
| 23 |
'attr': 'data', |
| 24 |
'add': 'add_data', |
| 25 |
'type': (Data, OData), |
| 26 |
}, |
| 27 |
{ |
| 28 |
'attr': 'foo_data', |
| 29 |
'add': 'add_foo_data', |
| 30 |
'type': OData, |
| 31 |
'create': 'create_foo_data', |
| 32 |
}, |
| 33 |
{ |
| 34 |
'attr': 'things', |
| 35 |
'add': 'add_thing', |
| 36 |
'type': (Container, Data, OData), |
| 37 |
}, |
| 38 |
] |
| 39 |
|
| 40 |
|
| 41 |
class FooSingle(MultiContainerInterface): |
| 42 |
|
| 43 |
__clsconf__ = { |
| 44 |
'attr': 'containers', |
| 45 |
'add': 'add_container', |
| 46 |
'type': (Container, ), |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
class Baz(MultiContainerInterface): |
| 51 |
|
| 52 |
__containers = dict() |
| 53 |
|
| 54 |
__clsconf__ = [ |
| 55 |
{ |
| 56 |
'attr': 'containers', |
| 57 |
'add': 'add_container', |
| 58 |
'type': Container, |
| 59 |
'get': 'get_container', |
| 60 |
}, |
| 61 |
] |
| 62 |
|
| 63 |
# use custom keyword arguments |
| 64 |
def __init__(self, name, other_arg, my_containers): |
| 65 |
super().__init__(name=name) |
| 66 |
self.other_arg = other_arg |
| 67 |
self.containers = {'my ' + v.name: v for v in my_containers} |
| 68 |
|
| 69 |
@property |
| 70 |
def containers(self): |
| 71 |
return self.__containers |
| 72 |
|
| 73 |
@containers.setter |
| 74 |
def containers(self, value): |
| 75 |
self.__containers = value |
| 76 |
|
| 77 |
|
| 78 |
class TestBasic(TestCase): |
| 79 |
|
| 80 |
def test_init_docval(self): |
| 81 |
"""Test that the docval for the __init__ method is set correctly.""" |
| 82 |
dv = get_docval(Foo.__init__) |
| 83 |
self.assertEqual(dv[0]['name'], 'containers') |
| 84 |
self.assertEqual(dv[1]['name'], 'data') |
| 85 |
self.assertEqual(dv[2]['name'], 'foo_data') |
| 86 |
self.assertEqual(dv[3]['name'], 'things') |
| 87 |
self.assertTupleEqual(dv[0]['type'], (list, tuple, dict, Container)) |
| 88 |
self.assertTupleEqual(dv[1]['type'], (list, tuple, dict, Data, OData)) |
| 89 |
self.assertTupleEqual(dv[2]['type'], (list, tuple, dict, OData)) |
| 90 |
self.assertTupleEqual(dv[3]['type'], (list, tuple, dict, Container, Data, OData)) |
| 91 |
self.assertEqual(dv[0]['doc'], 'Container to store in this interface') |
| 92 |
self.assertEqual(dv[1]['doc'], 'Data or OData to store in this interface') |
| 93 |
self.assertEqual(dv[2]['doc'], 'OData to store in this interface') |
| 94 |
self.assertEqual(dv[3]['doc'], 'Container, Data, or OData to store in this interface') |
| 95 |
for i in range(4): |
| 96 |
self.assertDictEqual(dv[i]['default'], {}) |
| 97 |
self.assertEqual(dv[4]['name'], 'name') |
| 98 |
self.assertEqual(dv[4]['type'], str) |
| 99 |
self.assertEqual(dv[4]['doc'], 'the name of this container') |
| 100 |
self.assertEqual(dv[4]['default'], 'Foo') |
| 101 |
|
| 102 |
def test_add_docval(self): |
| 103 |
"""Test that the docval for the add method is set correctly.""" |
| 104 |
expected_doc = "add_container(containers)\n\nAdd one or multiple Container objects to this Foo" |
| 105 |
self.assertTrue(Foo.add_container.__doc__.startswith(expected_doc)) |
| 106 |
dv = get_docval(Foo.add_container) |
| 107 |
self.assertEqual(dv[0]['name'], 'containers') |
| 108 |
self.assertTupleEqual(dv[0]['type'], (list, tuple, dict, Container)) |
| 109 |
self.assertEqual(dv[0]['doc'], 'one or multiple Container objects to add to this Foo') |
| 110 |
self.assertFalse('default' in dv[0]) |
| 111 |
|
| 112 |
def test_create_docval(self): |
| 113 |
"""Test that the docval for the create method is set correctly.""" |
| 114 |
dv = get_docval(Foo.create_foo_data) |
| 115 |
self.assertEqual(dv[0]['name'], 'name') |
| 116 |
self.assertEqual(dv[1]['name'], 'data') |
| 117 |
|
| 118 |
def test_getter_docval(self): |
| 119 |
"""Test that the docval for the get method is set correctly.""" |
| 120 |
dv = get_docval(Foo.get_container) |
| 121 |
self.assertEqual(dv[0]['doc'], 'the name of the Container') |
| 122 |
self.assertIsNone(dv[0]['default']) |
| 123 |
|
| 124 |
def test_getitem_docval(self): |
| 125 |
"""Test that the docval for __getitem__ is set correctly.""" |
| 126 |
dv = get_docval(Baz.__getitem__) |
| 127 |
self.assertEqual(dv[0]['doc'], 'the name of the Container') |
| 128 |
self.assertIsNone(dv[0]['default']) |
| 129 |
|
| 130 |
def test_attr_property(self): |
| 131 |
"""Test that a property is created for the attribute.""" |
| 132 |
properties = inspect.getmembers(Foo, lambda o: isinstance(o, property)) |
| 133 |
match = [p for p in properties if p[0] == 'containers'] |
| 134 |
self.assertEqual(len(match), 1) |
| 135 |
|
| 136 |
def test_attr_getter(self): |
| 137 |
"""Test that the getter for the attribute dict returns a LabelledDict.""" |
| 138 |
foo = Foo() |
| 139 |
self.assertTrue(isinstance(foo.containers, LabelledDict)) |
| 140 |
|
| 141 |
def test_init_empty(self): |
| 142 |
"""Test that initializing the MCI with no arguments initializes the attribute dict empty.""" |
| 143 |
foo = Foo() |
| 144 |
self.assertDictEqual(foo.containers, {}) |
| 145 |
self.assertEqual(foo.name, 'Foo') |
| 146 |
|
| 147 |
def test_init_multi(self): |
| 148 |
"""Test that initializing the MCI with no arguments initializes the attribute dict empty.""" |
| 149 |
obj1 = Container('obj1') |
| 150 |
data1 = Data('data1', [1, 2, 3]) |
| 151 |
foo = Foo(containers=obj1, data=data1) |
| 152 |
self.assertDictEqual(foo.containers, {'obj1': obj1}) |
| 153 |
self.assertDictEqual(foo.data, {'data1': data1}) |
| 154 |
|
| 155 |
def test_init_custom_name(self): |
| 156 |
"""Test that initializing the MCI with a custom name works.""" |
| 157 |
foo = Foo(name='test_foo') |
| 158 |
self.assertEqual(foo.name, 'test_foo') |
| 159 |
|
| 160 |
# init, create, and setter calls add, so just test add |
| 161 |
def test_add_single(self): |
| 162 |
"""Test that adding a container to the attribute dict correctly adds the container.""" |
| 163 |
obj1 = Container('obj1') |
| 164 |
foo = Foo() |
| 165 |
foo.add_container(obj1) |
| 166 |
self.assertDictEqual(foo.containers, {'obj1': obj1}) |
| 167 |
self.assertIs(obj1.parent, foo) |
| 168 |
|
| 169 |
def test_add_single_modified(self): |
| 170 |
"""Test that adding a container to the attribute dict correctly makes the MCI as modified.""" |
| 171 |
obj1 = Container('obj1') |
| 172 |
foo = Foo() |
| 173 |
foo.set_modified(False) # set to False so that we can test whether add_container makes it True |
| 174 |
foo.add_container(obj1) |
| 175 |
self.assertTrue(foo.modified) |
| 176 |
|
| 177 |
def test_add_single_not_parent(self): |
| 178 |
"""Test that adding a container with a parent to the attribute dict correctly adds the container.""" |
| 179 |
obj1 = Container('obj1') |
| 180 |
obj2 = Container('obj2') |
| 181 |
obj1.parent = obj2 |
| 182 |
foo = Foo() |
| 183 |
foo.add_container(obj1) |
| 184 |
self.assertDictEqual(foo.containers, {'obj1': obj1}) |
| 185 |
self.assertIs(obj1.parent, obj2) |
| 186 |
|
| 187 |
def test_add_single_not_parent_modified(self): |
| 188 |
"""Test that adding a container with a parent to the attribute dict correctly marks the MCI as modified.""" |
| 189 |
obj1 = Container('obj1') |
| 190 |
obj2 = Container('obj2') |
| 191 |
obj1.parent = obj2 |
| 192 |
foo = Foo() |
| 193 |
foo.set_modified(False) # set to False so that we can test whether add_container makes it True |
| 194 |
foo.add_container(obj1) |
| 195 |
self.assertTrue(foo.modified) |
| 196 |
|
| 197 |
def test_add_single_dup(self): |
| 198 |
"""Test that adding a container to the attribute dict correctly adds the container.""" |
| 199 |
obj1 = Container('obj1') |
| 200 |
foo = Foo(obj1) |
| 201 |
msg = (f"Cannot add <class 'hdmf.container.Container'> 'obj1' at 0x{id(obj1)} to dict attribute " |
| 202 |
"'containers' in <class 'tests.unit.test_multicontainerinterface.Foo'> 'Foo'. " |
| 203 |
f"<class 'hdmf.container.Container'> 'obj1' at 0x{id(obj1)} already exists in 'containers' " |
| 204 |
"and has the same name.") |
| 205 |
with self.assertRaisesWith(ValueError, msg): |
| 206 |
foo.add_container(obj1) |
| 207 |
|
| 208 |
def test_add_list(self): |
| 209 |
"""Test that adding a list to the attribute dict correctly adds the items.""" |
| 210 |
obj1 = Container('obj1') |
| 211 |
obj2 = Container('obj2') |
| 212 |
foo = Foo() |
| 213 |
foo.add_container([obj1, obj2]) |
| 214 |
self.assertDictEqual(foo.containers, {'obj1': obj1, 'obj2': obj2}) |
| 215 |
|
| 216 |
def test_add_dict(self): |
| 217 |
"""Test that adding a dict to the attribute dict correctly adds the input dict values.""" |
| 218 |
obj1 = Container('obj1') |
| 219 |
obj2 = Container('obj2') |
| 220 |
foo = Foo() |
| 221 |
foo.add_container({'a': obj1, 'b': obj2}) |
| 222 |
self.assertDictEqual(foo.containers, {'obj1': obj1, 'obj2': obj2}) |
| 223 |
|
| 224 |
def test_attr_setter_none(self): |
| 225 |
"""Test that setting the attribute dict to None does not alter the dict.""" |
| 226 |
obj1 = Container('obj1') |
| 227 |
foo = Foo(obj1) |
| 228 |
foo.containers = None |
| 229 |
self.assertDictEqual(foo.containers, {'obj1': obj1}) |
| 230 |
|
| 231 |
def test_remove_child(self): |
| 232 |
"""Test that removing a child container from the attribute dict resets the parent to None.""" |
| 233 |
obj1 = Container('obj1') |
| 234 |
foo = Foo(obj1) |
| 235 |
del foo.containers['obj1'] |
| 236 |
self.assertDictEqual(foo.containers, {}) |
| 237 |
self.assertIsNone(obj1.parent) |
| 238 |
|
| 239 |
def test_remove_non_child(self): |
| 240 |
"""Test that removing a non-child container from the attribute dict resets the parent to None.""" |
| 241 |
obj1 = Container('obj1') |
| 242 |
obj2 = Container('obj2') |
| 243 |
obj1.parent = obj2 |
| 244 |
foo = Foo(obj1) |
| 245 |
del foo.containers['obj1'] |
| 246 |
self.assertDictEqual(foo.containers, {}) |
| 247 |
self.assertIs(obj1.parent, obj2) |
| 248 |
|
| 249 |
def test_getter_empty(self): |
| 250 |
"""Test that calling the getter with no args and no items in the attribute dict raises an error.""" |
| 251 |
foo = Foo() |
| 252 |
msg = "containers of Foo 'Foo' is empty." |
| 253 |
with self.assertRaisesWith(ValueError, msg): |
| 254 |
foo.get_container() |
| 255 |
|
| 256 |
def test_getter_none(self): |
| 257 |
"""Test that calling the getter with no args and one item in the attribute returns the item.""" |
| 258 |
obj1 = Container('obj1') |
| 259 |
foo = Foo(obj1) |
| 260 |
self.assertIs(foo.get_container(), obj1) |
| 261 |
|
| 262 |
def test_getter_none_multiple(self): |
| 263 |
"""Test that calling the getter with no args and multiple items in the attribute dict raises an error.""" |
| 264 |
obj1 = Container('obj1') |
| 265 |
obj2 = Container('obj2') |
| 266 |
foo = Foo([obj1, obj2]) |
| 267 |
msg = "More than one element in containers of Foo 'Foo' -- must specify a name." |
| 268 |
with self.assertRaisesWith(ValueError, msg): |
| 269 |
foo.get_container() |
| 270 |
|
| 271 |
def test_getter_name(self): |
| 272 |
"""Test that calling the getter with a correct key works.""" |
| 273 |
obj1 = Container('obj1') |
| 274 |
foo = Foo(obj1) |
| 275 |
self.assertIs(foo.get_container('obj1'), obj1) |
| 276 |
|
| 277 |
def test_getter_name_not_found(self): |
| 278 |
"""Test that calling the getter with a key not in the attribute dict raises a KeyError.""" |
| 279 |
foo = Foo() |
| 280 |
msg = "\"'obj1' not found in containers of Foo 'Foo'.\"" |
| 281 |
with self.assertRaisesWith(KeyError, msg): |
| 282 |
foo.get_container('obj1') |
| 283 |
|
| 284 |
def test_getitem_multiconf(self): |
| 285 |
"""Test that classes with multiple attribute configurations cannot use getitem.""" |
| 286 |
foo = Foo() |
| 287 |
msg = "'Foo' object is not subscriptable" |
| 288 |
with self.assertRaisesWith(TypeError, msg): |
| 289 |
foo['aa'] |
| 290 |
|
| 291 |
def test_getitem(self): |
| 292 |
"""Test that getitem works.""" |
| 293 |
obj1 = Container('obj1') |
| 294 |
foo = FooSingle(obj1) |
| 295 |
self.assertIs(foo['obj1'], obj1) |
| 296 |
|
| 297 |
def test_getitem_single_none(self): |
| 298 |
"""Test that getitem works wwhen there is a single item and no name is given to getitem.""" |
| 299 |
obj1 = Container('obj1') |
| 300 |
foo = FooSingle(obj1) |
| 301 |
self.assertIs(foo[None], obj1) |
| 302 |
|
| 303 |
def test_getitem_empty(self): |
| 304 |
"""Test that an error is raised if the attribute dict is empty and no name is given to getitem.""" |
| 305 |
foo = FooSingle() |
| 306 |
msg = "FooSingle 'FooSingle' is empty." |
| 307 |
with self.assertRaisesWith(ValueError, msg): |
| 308 |
foo[None] |
| 309 |
|
| 310 |
def test_getitem_multiple(self): |
| 311 |
"""Test that an error is raised if the attribute dict has multiple values and no name is given to getitem.""" |
| 312 |
obj1 = Container('obj1') |
| 313 |
obj2 = Container('obj2') |
| 314 |
foo = FooSingle([obj1, obj2]) |
| 315 |
msg = "More than one Container in FooSingle 'FooSingle' -- must specify a name." |
| 316 |
with self.assertRaisesWith(ValueError, msg): |
| 317 |
foo[None] |
| 318 |
|
| 319 |
def test_getitem_not_found(self): |
| 320 |
"""Test that a KeyError is raised if the key is not found using getitem.""" |
| 321 |
obj1 = Container('obj1') |
| 322 |
foo = FooSingle(obj1) |
| 323 |
msg = "\"'obj2' not found in FooSingle 'FooSingle'.\"" |
| 324 |
with self.assertRaisesWith(KeyError, msg): |
| 325 |
foo['obj2'] |
| 326 |
|
| 327 |
def test_repr_html_(self): |
| 328 |
obj1 = Container('obj1') |
| 329 |
obj2 = Container('obj2') |
| 330 |
foo = FooSingle() |
| 331 |
foo.add_container([obj1, obj2]) |
| 332 |
|
| 333 |
self.assertEqual( |
| 334 |
foo._repr_html_(), |
| 335 |
( |
| 336 |
'\n <style>\n .container-fields {\n font-family: "Open Sans", Arial, ' |
| 337 |
'sans-serif;\n }\n .container-fields .field-value {\n color: ' |
| 338 |
'#00788E;\n }\n .container-fields details > summary {\n cursor: ' |
| 339 |
'pointer;\n display: list-item;\n }\n .container-fields details ' |
| 340 |
'> summary:hover {\n color: #0A6EAA;\n }\n </style>\n \n ' |
| 341 |
' <script>\n function copyToClipboard(text) {\n ' |
| 342 |
'navigator.clipboard.writeText(text).then(function() {\n console.log(\'Copied to ' |
| 343 |
'clipboard: \' + text);\n }, function(err) {\n console.error(' |
| 344 |
'\'Could not copy text: \', err);\n });\n }\n\n ' |
| 345 |
'document.addEventListener(\'DOMContentLoaded\', function() {\n let fieldKeys = ' |
| 346 |
'document.querySelectorAll(\'.container-fields .field-key\');\n fieldKeys.forEach(' |
| 347 |
'function(fieldKey) {\n fieldKey.addEventListener(\'click\', function() {\n ' |
| 348 |
' let accessCode = fieldKey.getAttribute(\'title\').replace(\'Access code: \', ' |
| 349 |
'\'\');\n copyToClipboard(accessCode);\n });\n ' |
| 350 |
' });\n });\n </script>\n <div class=\'container-wrap\'><div ' |
| 351 |
'class=\'container-header\'><div ' |
| 352 |
'class=\'xr-obj-type\'><h3>FooSingle</h3></div></div><details><summary style="display: list-item; ' |
| 353 |
'margin-left: 0px;" class="container-fields field-key" ' |
| 354 |
'title=".containers"><b>containers</b></summary><details><summary style="display: list-item; ' |
| 355 |
'margin-left: 20px;" class="container-fields field-key" title=".containers[' |
| 356 |
'\'obj1\']"><b>obj1</b></summary></details><details><summary style="display: list-item; margin-left: ' |
| 357 |
'20px;" class="container-fields field-key" title=".containers[' |
| 358 |
'\'obj2\']"><b>obj2</b></summary></details></details></div>' |
| 359 |
) |
| 360 |
) |
| 361 |
|
| 362 |
|
| 363 |
class TestOverrideInit(TestCase): |
| 364 |
|
| 365 |
def test_override_init(self): |
| 366 |
"""Test that overriding __init__ works.""" |
| 367 |
obj1 = Container('obj1') |
| 368 |
obj2 = Container('obj2') |
| 369 |
containers = [obj1, obj2] |
| 370 |
|
| 371 |
baz = Baz(name='test_baz', other_arg=1, my_containers=containers) |
| 372 |
self.assertEqual(baz.name, 'test_baz') |
| 373 |
self.assertEqual(baz.other_arg, 1) |
| 374 |
|
| 375 |
def test_override_property(self): |
| 376 |
"""Test that overriding the attribute property works.""" |
| 377 |
obj1 = Container('obj1') |
| 378 |
obj2 = Container('obj2') |
| 379 |
containers = [obj1, obj2] |
| 380 |
baz = Baz(name='test_baz', other_arg=1, my_containers=containers) |
| 381 |
self.assertDictEqual(baz.containers, {'my obj1': obj1, 'my obj2': obj2}) |
| 382 |
self.assertFalse(isinstance(baz.containers, LabelledDict)) |
| 383 |
self.assertIs(baz.get_container('my obj1'), obj1) |
| 384 |
baz.containers = {} |
| 385 |
self.assertDictEqual(baz.containers, {}) |
| 386 |
|
| 387 |
|
| 388 |
class TestNoClsConf(TestCase): |
| 389 |
|
| 390 |
def test_mci_init(self): |
| 391 |
"""Test that MultiContainerInterface cannot be instantiated.""" |
| 392 |
msg = "Can't instantiate class MultiContainerInterface." |
| 393 |
with self.assertRaisesWith(TypeError, msg): |
| 394 |
MultiContainerInterface(name='a') |
| 395 |
|
| 396 |
def test_init_no_cls_conf(self): |
| 397 |
"""Test that defining an MCI subclass without __clsconf__ raises an error.""" |
| 398 |
|
| 399 |
class Bar(MultiContainerInterface): |
| 400 |
pass |
| 401 |
|
| 402 |
msg = ("MultiContainerInterface subclass Bar is missing __clsconf__ attribute. Please check that " |
| 403 |
"the class is properly defined.") |
| 404 |
with self.assertRaisesWith(TypeError, msg): |
| 405 |
Bar(name='a') |
| 406 |
|
| 407 |
def test_init_superclass_no_cls_conf(self): |
| 408 |
"""Test that a subclass of an MCI class without a __clsconf__ can be initialized.""" |
| 409 |
|
| 410 |
class Bar(MultiContainerInterface): |
| 411 |
pass |
| 412 |
|
| 413 |
class Qux(Bar): |
| 414 |
|
| 415 |
__clsconf__ = { |
| 416 |
'attr': 'containers', |
| 417 |
'add': 'add_container', |
| 418 |
'type': Container, |
| 419 |
} |
| 420 |
|
| 421 |
obj1 = Container('obj1') |
| 422 |
qux = Qux(obj1) |
| 423 |
self.assertDictEqual(qux.containers, {'obj1': obj1}) |
| 424 |
|
| 425 |
|
| 426 |
class TestBadClsConf(TestCase): |
| 427 |
|
| 428 |
def test_wrong_type(self): |
| 429 |
"""Test that an error is raised if __clsconf__ is missing the add key.""" |
| 430 |
|
| 431 |
msg = "'__clsconf__' for MultiContainerInterface subclass Bar must be a dict or a list of dicts." |
| 432 |
with self.assertRaisesWith(TypeError, msg): |
| 433 |
|
| 434 |
class Bar(MultiContainerInterface): |
| 435 |
|
| 436 |
__clsconf__ = ( |
| 437 |
{ |
| 438 |
'attr': 'data', |
| 439 |
'add': 'add_data', |
| 440 |
'type': (Data, ), |
| 441 |
}, |
| 442 |
) |
| 443 |
|
| 444 |
def test_missing_add(self): |
| 445 |
"""Test that an error is raised if __clsconf__ is missing the add key.""" |
| 446 |
|
| 447 |
msg = "MultiContainerInterface subclass Bar is missing 'add' key in __clsconf__" |
| 448 |
with self.assertRaisesWith(ValueError, msg): |
| 449 |
|
| 450 |
class Bar(MultiContainerInterface): |
| 451 |
|
| 452 |
__clsconf__ = {} |
| 453 |
|
| 454 |
def test_missing_attr(self): |
| 455 |
"""Test that an error is raised if __clsconf__ is missing the attr key.""" |
| 456 |
|
| 457 |
msg = "MultiContainerInterface subclass Bar is missing 'attr' key in __clsconf__" |
| 458 |
with self.assertRaisesWith(ValueError, msg): |
| 459 |
|
| 460 |
class Bar(MultiContainerInterface): |
| 461 |
|
| 462 |
__clsconf__ = { |
| 463 |
'add': 'add_container', |
| 464 |
} |
| 465 |
|
| 466 |
def test_missing_type(self): |
| 467 |
"""Test that an error is raised if __clsconf__ is missing the type key.""" |
| 468 |
|
| 469 |
msg = "MultiContainerInterface subclass Bar is missing 'type' key in __clsconf__" |
| 470 |
with self.assertRaisesWith(ValueError, msg): |
| 471 |
|
| 472 |
class Bar(MultiContainerInterface): |
| 473 |
|
| 474 |
__clsconf__ = { |
| 475 |
'add': 'add_container', |
| 476 |
'attr': 'containers', |
| 477 |
} |
| 478 |
|
| 479 |
def test_create_multiple_types(self): |
| 480 |
"""Test that an error is raised if __clsconf__ specifies 'create' key with multiple types.""" |
| 481 |
|
| 482 |
msg = ("Cannot specify 'create' key in __clsconf__ for MultiContainerInterface subclass Bar " |
| 483 |
"when 'type' key is not a single type") |
| 484 |
with self.assertRaisesWith(ValueError, msg): |
| 485 |
|
| 486 |
class Bar(MultiContainerInterface): |
| 487 |
|
| 488 |
__clsconf__ = { |
| 489 |
'attr': 'data', |
| 490 |
'add': 'add_data', |
| 491 |
'type': (Data, ), |
| 492 |
'create': 'create_data', |
| 493 |
} |
| 494 |
|
| 495 |
def test_missing_add_multi(self): |
| 496 |
"""Test that an error is raised if one item of a __clsconf__ list is missing the add key.""" |
| 497 |
|
| 498 |
msg = "MultiContainerInterface subclass Bar is missing 'add' key in __clsconf__ at index 1" |
| 499 |
with self.assertRaisesWith(ValueError, msg): |
| 500 |
|
| 501 |
class Bar(MultiContainerInterface): |
| 502 |
|
| 503 |
__clsconf__ = [ |
| 504 |
{ |
| 505 |
'attr': 'data', |
| 506 |
'add': 'add_data', |
| 507 |
'type': (Data, ), |
| 508 |
}, |
| 509 |
{} |
| 510 |
] |
| 511 |
|
| 512 |
def test_missing_attr_multi(self): |
| 513 |
"""Test that an error is raised if one item of a __clsconf__ list is missing the attr key.""" |
| 514 |
|
| 515 |
msg = "MultiContainerInterface subclass Bar is missing 'attr' key in __clsconf__ at index 1" |
| 516 |
with self.assertRaisesWith(ValueError, msg): |
| 517 |
|
| 518 |
class Bar(MultiContainerInterface): |
| 519 |
|
| 520 |
__clsconf__ = [ |
| 521 |
{ |
| 522 |
'attr': 'data', |
| 523 |
'add': 'add_data', |
| 524 |
'type': (Data, ), |
| 525 |
}, |
| 526 |
{ |
| 527 |
'add': 'add_container', |
| 528 |
} |
| 529 |
] |
| 530 |
|
| 531 |
def test_missing_type_multi(self): |
| 532 |
"""Test that an error is raised if one item of a __clsconf__ list is missing the type key.""" |
| 533 |
|
| 534 |
msg = "MultiContainerInterface subclass Bar is missing 'type' key in __clsconf__ at index 1" |
| 535 |
with self.assertRaisesWith(ValueError, msg): |
| 536 |
|
| 537 |
class Bar(MultiContainerInterface): |
| 538 |
|
| 539 |
__clsconf__ = [ |
| 540 |
{ |
| 541 |
'attr': 'data', |
| 542 |
'add': 'add_data', |
| 543 |
'type': (Data, ), |
| 544 |
}, |
| 545 |
{ |
| 546 |
'add': 'add_container', |
| 547 |
'attr': 'containers', |
| 548 |
} |
| 549 |
] |
| 550 |
|
| 551 |
def test_create_multiple_types_multi(self): |
| 552 |
"""Test that an error is raised if one item of a __clsconf__ list specifies 'create' key with multiple types.""" |
| 553 |
|
| 554 |
msg = ("Cannot specify 'create' key in __clsconf__ for MultiContainerInterface subclass Bar " |
| 555 |
"when 'type' key is not a single type at index 1") |
| 556 |
with self.assertRaisesWith(ValueError, msg): |
| 557 |
|
| 558 |
class Bar(MultiContainerInterface): |
| 559 |
|
| 560 |
__clsconf__ = [ |
| 561 |
{ |
| 562 |
'attr': 'data', |
| 563 |
'add': 'add_data', |
| 564 |
'type': (Data, ), |
| 565 |
}, |
| 566 |
{ |
| 567 |
'add': 'add_container', |
| 568 |
'attr': 'containers', |
| 569 |
'type': (Container, ), |
| 570 |
'create': 'create_container', |
| 571 |
} |
| 572 |
] |