1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
from hdmf.testing import TestCase
from hdmf.utils import LabelledDict
class MyTestClass:
def __init__(self, prop1, prop2):
self._prop1 = prop1
self._prop2 = prop2
@property
def prop1(self):
return self._prop1
@property
def prop2(self):
return self._prop2
class TestLabelledDict(TestCase):
def test_constructor(self):
"""Test that constructor sets arguments properly."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
self.assertEqual(ld.label, 'all_objects')
self.assertEqual(ld.key_attr, 'prop1')
def test_constructor_default(self):
"""Test that constructor sets default key attribute."""
ld = LabelledDict(label='all_objects')
self.assertEqual(ld.key_attr, 'name')
def test_set_key_attr(self):
"""Test that the key attribute cannot be set after initialization."""
ld = LabelledDict(label='all_objects')
with self.assertRaises(AttributeError):
ld.key_attr = 'another_name'
def test_getitem_unknown_val(self):
"""Test that dict[unknown_key] where the key unknown_key is not in the dict raises an error."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
with self.assertRaises(KeyError):
ld['unknown_key']
def test_getitem_eqeq_unknown_val(self):
"""Test that dict[unknown_attr == val] where there are no query matches returns an empty set."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
self.assertSetEqual(ld['unknown_attr == val'], set())
def test_getitem_eqeq_other_key(self):
"""Test that dict[other_attr == val] where there are no query matches returns an empty set."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
self.assertSetEqual(ld['prop2 == val'], set())
def test_getitem_eqeq_no_key_attr(self):
"""Test that dict[key_attr == val] raises an error if key_attr is not given."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
with self.assertRaisesWith(ValueError, "An attribute name is required before '=='."):
ld[' == unknown_key']
def test_getitem_eqeq_no_val(self):
"""Test that dict[key_attr == val] raises an error if val is not given."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
with self.assertRaisesWith(ValueError, "A value is required after '=='."):
ld['prop1 == ']
def test_getitem_eqeq_no_key_attr_no_val(self):
"""Test that dict[key_attr == val] raises an error if key_attr is not given and val is not given."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
with self.assertRaisesWith(ValueError, "An attribute name is required before '=='."):
ld[' == ']
def test_add_basic(self):
"""Test add method on object with correct key_attr."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
obj1 = MyTestClass('a', 'b')
ld.add(obj1)
self.assertIs(ld['a'], obj1)
def test_add_value_missing_key(self):
"""Test that add raises an error if the value being set does not have the attribute key_attr."""
ld = LabelledDict(label='all_objects', key_attr='unknown_key')
obj1 = MyTestClass('a', 'b')
err_msg = r"Cannot set value '<.*>' in LabelledDict\. Value must have attribute 'unknown_key'\."
with self.assertRaisesRegex(ValueError, err_msg):
ld.add(obj1)
def test_setitem_getitem_basic(self):
"""Test that setitem and getitem properly set and get the object."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
obj1 = MyTestClass('a', 'b')
ld.add(obj1)
self.assertIs(ld['a'], obj1)
def test_setitem_value_missing_key(self):
"""Test that setitem raises an error if the value being set does not have the attribute key_attr."""
ld = LabelledDict(label='all_objects', key_attr='unknown_key')
obj1 = MyTestClass('a', 'b')
err_msg = r"Cannot set value '<.*>' in LabelledDict\. Value must have attribute 'unknown_key'\."
with self.assertRaisesRegex(ValueError, err_msg):
ld['a'] = obj1
def test_setitem_value_inconsistent_key(self):
"""Test that setitem raises an error if the value being set has an inconsistent key."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
obj1 = MyTestClass('a', 'b')
err_msg = r"Key 'b' must equal attribute 'prop1' of '<.*>'\."
with self.assertRaisesRegex(KeyError, err_msg):
ld['b'] = obj1
def test_setitem_value_duplicate_key(self):
"""Test that setitem raises an error if the key already exists in the dict."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
obj1 = MyTestClass('a', 'b')
obj2 = MyTestClass('a', 'c')
ld['a'] = obj1
err_msg = "Key 'a' is already in this dict. Cannot reset items in a LabelledDict."
with self.assertRaisesWith(TypeError, err_msg):
ld['a'] = obj2
def test_add_callable(self):
"""Test that add properly adds the object and calls the add_callable function."""
self.signal = None
def func(v):
self.signal = v
ld = LabelledDict(label='all_objects', key_attr='prop1', add_callable=func)
obj1 = MyTestClass('a', 'b')
ld.add(obj1)
self.assertIs(ld['a'], obj1)
self.assertIs(self.signal, obj1)
def test_setitem_callable(self):
"""Test that setitem properly sets the object and calls the add_callable function."""
self.signal = None
def func(v):
self.signal = v
ld = LabelledDict(label='all_objects', key_attr='prop1', add_callable=func)
obj1 = MyTestClass('a', 'b')
ld['a'] = obj1
self.assertIs(ld['a'], obj1)
self.assertIs(self.signal, obj1)
def test_getitem_eqeq_nonempty(self):
"""Test that dict[key_attr == val] returns the single matching object."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
obj1 = MyTestClass('a', 'b')
ld.add(obj1)
self.assertIs(ld['prop1 == a'], obj1)
def test_getitem_eqeq_nonempty_key_attr_no_match(self):
"""Test that dict[key_attr == unknown_val] where a matching value is not found raises a KeyError."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
obj1 = MyTestClass('a', 'b')
ld.add(obj1)
with self.assertRaises(KeyError):
ld['prop1 == unknown_val'] # same as ld['unknown_val']
def test_getitem_eqeq_nonempty_unknown_attr(self):
"""Test that dict[unknown_attr == val] where unknown_attr is not a field on the values raises an error."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
obj1 = MyTestClass('a', 'b')
ld['a'] = obj1
self.assertSetEqual(ld['unknown_attr == unknown_val'], set())
def test_getitem_nonempty_other_key(self):
"""Test that dict[other_key == val] returns a set of matching objects."""
ld = LabelledDict(label='all_objects', key_attr='prop1')
obj1 = MyTestClass('a', 'b')
obj2 = MyTestClass('d', 'b')
obj3 = MyTestClass('f', 'e')
ld.add(obj1)
ld.add(obj2)
ld.add(obj3)
self.assertSetEqual(ld['prop2 == b'], {obj1, obj2})
def test_pop_nocallback(self):
ld = LabelledDict(label='all_objects', key_attr='prop1')
obj1 = MyTestClass('a', 'b')
ld.add(obj1)
ret = ld.pop('a')
self.assertEqual(ret, obj1)
self.assertEqual(ld, dict())
def test_pop_callback(self):
self.signal = None
def func(v):
self.signal = v
ld = LabelledDict(label='all_objects', key_attr='prop1', remove_callable=func)
obj1 = MyTestClass('a', 'b')
ld.add(obj1)
ret = ld.pop('a')
self.assertEqual(ret, obj1)
self.assertEqual(self.signal, obj1)
self.assertEqual(ld, dict())
def test_popitem_nocallback(self):
ld = LabelledDict(label='all_objects', key_attr='prop1')
obj1 = MyTestClass('a', 'b')
ld.add(obj1)
ret = ld.popitem()
self.assertEqual(ret, ('a', obj1))
self.assertEqual(ld, dict())
def test_popitem_callback(self):
self.signal = None
def func(v):
self.signal = v
ld = LabelledDict(label='all_objects', key_attr='prop1', remove_callable=func)
obj1 = MyTestClass('a', 'b')
ld.add(obj1)
ret = ld.popitem()
self.assertEqual(ret, ('a', obj1))
self.assertEqual(self.signal, obj1)
self.assertEqual(ld, dict())
def test_clear_nocallback(self):
ld = LabelledDict(label='all_objects', key_attr='prop1')
obj1 = MyTestClass('a', 'b')
obj2 = MyTestClass('d', 'b')
ld.add(obj1)
ld.add(obj2)
ld.clear()
self.assertEqual(ld, dict())
def test_clear_callback(self):
self.signal = set()
def func(v):
self.signal.add(v)
ld = LabelledDict(label='all_objects', key_attr='prop1', remove_callable=func)
obj1 = MyTestClass('a', 'b')
obj2 = MyTestClass('d', 'b')
ld.add(obj1)
ld.add(obj2)
ld.clear()
self.assertSetEqual(self.signal, {obj2, obj1})
self.assertEqual(ld, dict())
def test_delitem_nocallback(self):
ld = LabelledDict(label='all_objects', key_attr='prop1')
obj1 = MyTestClass('a', 'b')
ld.add(obj1)
del ld['a']
self.assertEqual(ld, dict())
def test_delitem_callback(self):
self.signal = None
def func(v):
self.signal = v
ld = LabelledDict(label='all_objects', key_attr='prop1', remove_callable=func)
obj1 = MyTestClass('a', 'b')
ld.add(obj1)
del ld['a']
self.assertEqual(self.signal, obj1)
self.assertEqual(ld, dict())
def test_update_callback(self):
ld = LabelledDict(label='all_objects', key_attr='prop1')
with self.assertRaisesWith(TypeError, "update is not supported for LabelledDict"):
ld.update(object())
def test_setdefault_callback(self):
ld = LabelledDict(label='all_objects', key_attr='prop1')
with self.assertRaisesWith(TypeError, "setdefault is not supported for LabelledDict"):
ld.setdefault(object())
|