def test_copy(self): """ConstrainedDict copy should retain constraint""" a = ConstrainedDict(dict.fromkeys("12"), "123") b = a.copy() self.assertEqual(a.constraint, b.constraint) self.assertRaises(ConstraintError, a.__setitem__, 1, "3") self.assertRaises(ConstraintError, b.__setitem__, 1, "3")
def test_setdefault(self): """ConstrainedDict setdefault shouldn't allow bad keys""" a = ConstrainedDict({"1": None, "2": "xyz"}, "123") self.assertEqual(a.setdefault("2", None), "xyz") self.assertEqual(a.setdefault("1", None), None) self.assertRaises(ConstraintError, a.setdefault, "x", 3) a.setdefault("3", 12345) self.assertEqual(a, {"1": None, "2": "xyz", "3": 12345})
def test_fromkeys(self): """ConstrainedDict instance fromkeys should retain constraint""" a = ConstrainedDict(dict.fromkeys("12"), "123") b = a.fromkeys("23") self.assertEqual(a.constraint, b.constraint) self.assertRaises(ConstraintError, a.__setitem__, 1, "3") self.assertRaises(ConstraintError, b.__setitem__, 1, "3") b["2"] = 5 self.assertEqual(b, {"2": 5, "3": None})
def test_init_sequence(self): """ConstrainedDict should init from sequence, unlike normal dict""" self.assertEqual(ConstrainedDict("abcda"), { "a": 2, "b": 1, "c": 1, "d": 1 })
def test_setitem_masks(self): """ConstrainedDict setitem should work only if key in constraint""" key_mask = str def val_mask(x): return int(x) + 3 d = ConstrainedDict({1: 4, 2: 6}, "123", key_mask, val_mask) d[1] = "456" self.assertEqual(d, {"1": 459, "2": 9}) d["1"] = 234 self.assertEqual(d, {"1": 237, "2": 9}) self.assertRaises(ConstraintError, d.__setitem__, 4, "3") e = d.copy() assert e.mask is d.mask assert "1" in d assert not 1 in d
def test_init_good_data(self): """ConstrainedDict should init OK if list matches constraint""" self.assertEqual(ConstrainedDict(dict.fromkeys("abc"), "abcd"), dict.fromkeys("abc")) self.assertEqual(ConstrainedDict("", "abcd"), dict("")) items = [1, 2, 3.2234, tuple("xyz")] # should accept anything dict() does if no constraint is passed self.assertEqual(ConstrainedDict(dict.fromkeys(items)), dict.fromkeys(items)) self.assertEqual(ConstrainedDict(dict.fromkeys(items), None), dict.fromkeys(items)) self.assertEqual(ConstrainedDict([(x, 1) for x in "12345"]), dict.fromkeys("12345", 1)) # check that list is formatted correctly and chars are all there test_dict = dict.fromkeys("12345") self.assertEqual(ConstrainedDict(test_dict, "12345"), test_dict)
def test_setitem(self): """ConstrainedDict setitem should work only if key in constraint""" a = ConstrainedDict(dict.fromkeys("12"), "123") a["1"] = "3" self.assertEqual(a, {"1": "3", "2": None}) self.assertRaises(ConstraintError, a.__setitem__, 1, "3")
def test_update(self): """ConstrainedDict should allow update only of compliant data""" a = ConstrainedDict(dict.fromkeys("123"), "12345") b = ConstrainedDict(dict.fromkeys("444"), "4") c = ConstrainedDict(dict.fromkeys("45"), "12345") d = ConstrainedDict([["x", "y"]]) a.update(b) self.assertEqual(a, dict.fromkeys("1234")) a.update(c) self.assertEqual(a, dict.fromkeys("12345")) self.assertRaises(ConstraintError, b.update, c) self.assertRaises(ConstraintError, c.update, d) # should be OK if constraint removed b.constraint = None b.update(c) self.assertEqual(b, dict.fromkeys("45")) b.update(d) self.assertEqual(b, {"4": None, "5": None, "x": "y"}) # should fail if we add the constraint back b.constraint = {"4": 1, 5: 2, "5": 1, "x": 1} self.assertRaises(ConstraintError, b.update, {4: 1}) b.update({5: 1}) self.assertEqual(b, {"4": None, "5": None, "x": "y", 5: 1})