diff options
Diffstat (limited to 'tests/test_mappings.py')
-rw-r--r-- | tests/test_mappings.py | 208 |
1 files changed, 109 insertions, 99 deletions
diff --git a/tests/test_mappings.py b/tests/test_mappings.py index b1aef254..1ffe7801 100644 --- a/tests/test_mappings.py +++ b/tests/test_mappings.py @@ -10,7 +10,6 @@ def a_dozen(): class BasicDict(mappings.DictMixin): - def __init__(self, i=None, **kwargs): self._d = {} mappings.DictMixin.__init__(self, i, **kwargs) @@ -20,7 +19,6 @@ class BasicDict(mappings.DictMixin): class MutableDict(BasicDict): - def __setitem__(self, key, val): self._d[key] = val @@ -36,7 +34,6 @@ class ImmutableDict(BasicDict): class TestDictMixin: - def test_immutability(self): d = ImmutableDict() pytest.raises(AttributeError, d.__setitem__, "spork", "foon") @@ -59,12 +56,12 @@ class TestDictMixin: pytest.raises(KeyError, d.pop, "spork") assert d.pop("spork", "bat") == "bat" assert d.pop("foo") == "bar" - assert d.popitem(), ("baz" == "cat") + assert d.popitem(), "baz" == "cat" pytest.raises(KeyError, d.popitem) assert d.pop("nonexistent", None) == None def test_init(self): - d = MutableDict((('foo', 'bar'), ('spork', 'foon')), baz="cat") + d = MutableDict((("foo", "bar"), ("spork", "foon")), baz="cat") assert d["foo"] == "bar" assert d["baz"] == "cat" d.clear() @@ -73,19 +70,20 @@ class TestDictMixin: def test_bool(self): d = MutableDict() assert not d - d['x'] = 1 + d["x"] = 1 assert d - del d['x'] + del d["x"] assert not d class RememberingNegateMixin: - def setup_method(self, method): self.negate_calls = [] + def negate(i): self.negate_calls.append(i) return -i + self.negate = negate def teardown_method(self, method): @@ -94,7 +92,6 @@ class RememberingNegateMixin: class LazyValDictTestMixin: - def test_invalid_operations(self): pytest.raises(AttributeError, operator.setitem, self.dict, 7, 7) pytest.raises(AttributeError, operator.delitem, self.dict, 7) @@ -118,6 +115,7 @@ class LazyValDictTestMixin: # missing key def get(): return self.dict[42] + pytest.raises(KeyError, get) def test_caching(self): @@ -129,7 +127,6 @@ class LazyValDictTestMixin: class TestLazyValDictWithList(LazyValDictTestMixin, RememberingNegateMixin): - def setup_method(self, method): super().setup_method(method) self.dict = mappings.LazyValDict(list(range(12)), self.negate) @@ -148,14 +145,12 @@ class TestLazyValDictWithList(LazyValDictTestMixin, RememberingNegateMixin): class TestLazyValDictWithFunc(LazyValDictTestMixin, RememberingNegateMixin): - def setup_method(self, method): super().setup_method(method) self.dict = mappings.LazyValDict(a_dozen, self.negate) class TestLazyValDict: - def test_invalid_init_args(self): pytest.raises(TypeError, mappings.LazyValDict, [1], 42) pytest.raises(TypeError, mappings.LazyValDict, 42, a_dozen) @@ -164,36 +159,43 @@ class TestLazyValDict: # TODO check for valid values for dict.new, since that seems to be # part of the interface? class TestProtectedDict: - def setup_method(self, method): self.orig = {1: -1, 2: -2} self.dict = mappings.ProtectedDict(self.orig) def test_basic_operations(self): assert self.dict[1] == -1 + def get(i): return self.dict[i] + pytest.raises(KeyError, get, 3) assert sorted(self.dict.keys()) == [1, 2] assert -1 not in self.dict assert 2 in self.dict + def remove(i): del self.dict[i] + pytest.raises(KeyError, remove, 50) def test_basic_mutating(self): # add something self.dict[7] = -7 + def check_after_adding(): assert self.dict[7] == -7 assert 7 in self.dict assert sorted(self.dict.keys()) == [1, 2, 7] + check_after_adding() # remove it again del self.dict[7] assert 7 not in self.dict + def get(i): return self.dict[i] + pytest.raises(KeyError, get, 7) assert sorted(self.dict.keys()) == [1, 2] # add it back @@ -214,7 +216,6 @@ class TestProtectedDict: class TestImmutableDict: - def test_init_iterator(self): d = mappings.ImmutableDict((x, x) for x in range(3)) assert dict(d) == {0: 0, 1: 1, 2: 2} @@ -239,7 +240,7 @@ class TestImmutableDict: def test_init_dictmixin(self): d = MutableDict(baz="cat") e = mappings.ImmutableDict(d) - assert dict(d) == {'baz': 'cat'} + assert dict(d) == {"baz": "cat"} def test_init_bad_data(self): for data in (range(10), list(range(10)), [([], 1)]): @@ -288,7 +289,6 @@ class TestImmutableDict: class TestOrderedFrozenSet: - def test_magic_methods(self): s = mappings.OrderedFrozenSet(range(9)) for x in range(9): @@ -299,7 +299,7 @@ class TestOrderedFrozenSet: for i in range(9): assert s[i] == i assert list(s[1:]) == list(range(1, 9)) - with pytest.raises(IndexError, match='index out of range'): + with pytest.raises(IndexError, match="index out of range"): s[9] assert s == set(range(9)) @@ -308,12 +308,12 @@ class TestOrderedFrozenSet: assert hash(s) def test_ordering(self): - s = mappings.OrderedFrozenSet('set') - assert 'set' == ''.join(s) - assert 'tes' == ''.join(reversed(s)) - s = mappings.OrderedFrozenSet('setordered') - assert 'setord' == ''.join(s) - assert 'drotes' == ''.join(reversed(s)) + s = mappings.OrderedFrozenSet("set") + assert "set" == "".join(s) + assert "tes" == "".join(reversed(s)) + s = mappings.OrderedFrozenSet("setordered") + assert "setord" == "".join(s) + assert "drotes" == "".join(reversed(s)) def test_immmutability(self): s = mappings.OrderedFrozenSet(range(9)) @@ -355,41 +355,40 @@ class TestOrderedFrozenSet: class TestOrderedSet(TestOrderedFrozenSet): - def test_hash(self): with pytest.raises(TypeError): - assert hash(mappings.OrderedSet('set')) + assert hash(mappings.OrderedSet("set")) def test_add(self): s = mappings.OrderedSet() - s.add('a') - assert 'a' in s + s.add("a") + assert "a" in s s.add(1) assert 1 in s - assert list(s) == ['a', 1] + assert list(s) == ["a", 1] def test_discard(self): s = mappings.OrderedSet() - s.discard('a') - s.add('a') + s.discard("a") + s.add("a") assert s - s.discard('a') + s.discard("a") assert not s def test_remove(self): s = mappings.OrderedSet() with pytest.raises(KeyError): - s.remove('a') - s.add('a') - assert 'a' in s - s.remove('a') - assert 'a' not in s + s.remove("a") + s.add("a") + assert "a" in s + s.remove("a") + assert "a" not in s def test_clear(self): s = mappings.OrderedSet() s.clear() assert len(s) == 0 - s.add('a') + s.add("a") assert len(s) == 1 s.clear() assert len(s) == 0 @@ -425,8 +424,9 @@ class TestStackedDict: assert x in std def test_len(self): - assert sum(map(len, (self.orig_dict, self.new_dict))) == \ - len(mappings.StackedDict(self.orig_dict, self.new_dict)) + assert sum(map(len, (self.orig_dict, self.new_dict))) == len( + mappings.StackedDict(self.orig_dict, self.new_dict) + ) def test_setattr(self): pytest.raises(TypeError, mappings.StackedDict().__setitem__, (1, 2)) @@ -447,24 +447,28 @@ class TestStackedDict: assert len(s) == 0 def test_keys(self): - assert sorted(mappings.StackedDict(self.orig_dict, self.new_dict)) == \ - sorted(list(self.orig_dict.keys()) + list(self.new_dict.keys())) + assert sorted(mappings.StackedDict(self.orig_dict, self.new_dict)) == sorted( + list(self.orig_dict.keys()) + list(self.new_dict.keys()) + ) class TestIndeterminantDict: - def test_disabled_methods(self): d = mappings.IndeterminantDict(lambda *a: None) for x in ( - "clear", - ("update", {}), - ("setdefault", 1), - "__iter__", "__len__", "__hash__", - ("__delitem__", 1), - ("__setitem__", 2), - ("popitem", 2), - "keys", "items", "values", - ): + "clear", + ("update", {}), + ("setdefault", 1), + "__iter__", + "__len__", + "__hash__", + ("__delitem__", 1), + ("__setitem__", 2), + ("popitem", 2), + "keys", + "items", + "values", + ): if isinstance(x, tuple): pytest.raises(TypeError, getattr(d, x[0]), x[1]) else: @@ -472,7 +476,8 @@ class TestIndeterminantDict: def test_starter_dict(self): d = mappings.IndeterminantDict( - lambda key: False, starter_dict={}.fromkeys(range(100), True)) + lambda key: False, starter_dict={}.fromkeys(range(100), True) + ) for x in range(100): assert d[x] == True for x in range(100, 110): @@ -481,21 +486,24 @@ class TestIndeterminantDict: def test_behaviour(self): val = [] d = mappings.IndeterminantDict( - lambda key: val.append(key), {}.fromkeys(range(10), True)) + lambda key: val.append(key), {}.fromkeys(range(10), True) + ) assert d[0] == True assert d[11] == None assert val == [11] + def func(*a): raise KeyError + with pytest.raises(KeyError): mappings.IndeterminantDict(func).__getitem__(1) - def test_get(self): def func(key): if key == 2: raise KeyError return True + d = mappings.IndeterminantDict(func, {1: 1}) assert d.get(1, 1) == 1 assert d.get(1, 2) == 1 @@ -505,41 +513,42 @@ class TestIndeterminantDict: class TestFoldingDict: - def test_preserve(self): dct = mappings.PreservingFoldingDict( - str.lower, list({'Foo': 'bar', 'fnz': 'donkey'}.items())) - assert dct['fnz'] == 'donkey' - assert dct['foo'] == 'bar' - assert sorted(['bar' == 'donkey']), sorted(dct.values()) + str.lower, list({"Foo": "bar", "fnz": "donkey"}.items()) + ) + assert dct["fnz"] == "donkey" + assert dct["foo"] == "bar" + assert sorted(["bar" == "donkey"]), sorted(dct.values()) assert dct.copy() == dct - assert dct['foo'] == dct.get('Foo') - assert 'foo' in dct - keys = ['Foo', 'fnz'] + assert dct["foo"] == dct.get("Foo") + assert "foo" in dct + keys = ["Foo", "fnz"] keysList = list(dct) for key in keys: assert key in list(dct.keys()) assert key in keysList assert (key, dct[key]) in list(dct.items()) assert len(keys) == len(dct) - assert dct.pop('foo') == 'bar' - assert 'foo' not in dct - del dct['fnz'] - assert 'fnz' not in dct - dct['Foo'] = 'bar' + assert dct.pop("foo") == "bar" + assert "foo" not in dct + del dct["fnz"] + assert "fnz" not in dct + dct["Foo"] = "bar" dct.refold(lambda _: _) - assert 'foo' not in dct - assert 'Foo' in dct - assert list(dct.items()) == [('Foo', 'bar')] + assert "foo" not in dct + assert "Foo" in dct + assert list(dct.items()) == [("Foo", "bar")] dct.clear() assert {} == dict(dct) def test_no_preserve(self): dct = mappings.NonPreservingFoldingDict( - str.lower, list({'Foo': 'bar', 'fnz': 'monkey'}.items())) - assert sorted(['bar', 'monkey']) == sorted(dct.values()) + str.lower, list({"Foo": "bar", "fnz": "monkey"}.items()) + ) + assert sorted(["bar", "monkey"]) == sorted(dct.values()) assert dct.copy() == dct - keys = ['foo', 'fnz'] + keys = ["foo", "fnz"] keysList = [key for key in dct] for key in keys: assert key in list(dct.keys()) @@ -547,8 +556,8 @@ class TestFoldingDict: assert key in keysList assert (key, dct[key]) in list(dct.items()) assert len(keys) == len(dct) - assert dct.pop('foo') == 'bar' - del dct['fnz'] + assert dct.pop("foo") == "bar" + del dct["fnz"] assert list(dct.keys()) == [] dct.clear() assert {} == dict(dct) @@ -580,20 +589,20 @@ class Test_attr_to_item_mapping: if kls is None: kls = self.kls o = kls(f=2, g=3) - assert ['f', 'g'] == sorted(o) - self.assertBoth(o, 'g', 3) + assert ["f", "g"] == sorted(o) + self.assertBoth(o, "g", 3) o.g = 4 - self.assertBoth(o, 'g', 4) + self.assertBoth(o, "g", 4) del o.g with pytest.raises(KeyError): - operator.__getitem__(o, 'g') + operator.__getitem__(o, "g") with pytest.raises(AttributeError): - getattr(o, 'g') - del o['f'] + getattr(o, "g") + del o["f"] with pytest.raises(KeyError): - operator.__getitem__(o, 'f') + operator.__getitem__(o, "f") with pytest.raises(AttributeError): - getattr(o, 'f') + getattr(o, "f") def test_inject(self): class foon(dict): @@ -611,30 +620,31 @@ class Test_ProxiedAttrs: def __init__(self, **kwargs): for attr, val in kwargs.items(): setattr(self, attr, val) + obj = foo() d = self.kls(obj) with pytest.raises(KeyError): - operator.__getitem__(d, 'x') + operator.__getitem__(d, "x") with pytest.raises(KeyError): - operator.__delitem__(d, 'x') - assert 'x' not in d - d['x'] = 1 - assert d['x'] == 1 - assert 'x' in d - assert ['x'] == list(x for x in d if not x.startswith("__")) - del d['x'] - assert 'x' not in d + operator.__delitem__(d, "x") + assert "x" not in d + d["x"] = 1 + assert d["x"] == 1 + assert "x" in d + assert ["x"] == list(x for x in d if not x.startswith("__")) + del d["x"] + assert "x" not in d with pytest.raises(KeyError): - operator.__delitem__(d, 'x') + operator.__delitem__(d, "x") with pytest.raises(KeyError): - operator.__getitem__(d, 'x') + operator.__getitem__(d, "x") # Finally, verify that immutable attribute errors are handled correctly. d = self.kls(object()) with pytest.raises(KeyError): - operator.__setitem__(d, 'x', 1) + operator.__setitem__(d, "x", 1) with pytest.raises(KeyError): - operator.__delitem__(d, 'x') + operator.__delitem__(d, "x") class TestSlottedDict: @@ -642,9 +652,9 @@ class TestSlottedDict: kls = staticmethod(mappings.make_SlottedDict_kls) def test_exceptions(self): - d = self.kls(['spork'])() + d = self.kls(["spork"])() for op in (operator.getitem, operator.delitem): with pytest.raises(KeyError): - op(d, 'spork') + op(d, "spork") with pytest.raises(KeyError): - op(d, 'foon') + op(d, "foon") |