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.assertRaisesWith(KeyError, "'unknown_val'"): ld['prop1 == unknown_val'] # same as ld['unknown_val']
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_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='prop3') obj1 = MyTestClass('a', 'b') err_msg = r"Cannot set value '<.*>' in LabelledDict\. Value must have key 'prop3'\." with self.assertRaisesRegex(ValueError, err_msg): ld.add(obj1)
def test_addval_getitem_eqeq_unknown_val(self): """Test that dict[key_attr == val] with an unknown val raises an error even if there are other objects in dict""" ld = LabelledDict(label='all_objects', key_attr='prop1') obj1 = MyTestClass('a', 'b') ld.add(obj1) with self.assertRaisesWith(KeyError, "'unknown_val'"): ld['prop1 == unknown_val']
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_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_addval_getitem_other_key_multi(self): """Test that dict[other_key == val] returns a list 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'], set([obj1, obj2]))
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_addval_getitem_other_key_none(self): """Test that dict[other_key == val] raises an error if val does not equal any of the other_key attribute values in the dict, even when the other_key attribute exists""" 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) with self.assertRaisesWith(KeyError, "'c'"): ld['prop2 == c']
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_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_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_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_add_basic(self): """Test add function 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_addval_getitem_other_key(self): """Test that dict[other_key == val] returns a list of matching objects""" ld = LabelledDict(label='all_objects', key_attr='prop1') obj1 = MyTestClass('a', 'b') ld.add(obj1) self.assertSetEqual(ld['prop2 == b'], {obj1})
def test_addval_getitem_eqeq(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)