def test_lazy_update(self): d = LazyDict() d.set_stub(4, lambda x: x) d.update(LazyDict({1: 100})) d.update(LazyDict({2: 20})) d.update({4: 5}) d.update(LazyDict({1: 1, 2: 2, 3: 3})) self.assertEqual(d, {1: 1, 2: 2, 3: 3, 4: 5}) d.update() self.assertEqual(d, {1: 1, 2: 2, 3: 3, 4: 5}) self.assertRaises((TypeError, AttributeError), d.update, None) class SimpleUserDict: def __init__(self): self.d = {1: 1, 2: 2, 3: 3} def keys(self): return self.d.keys() def __getitem__(self, i): return self.d[i] d.clear() d.update(SimpleUserDict()) self.assertEqual(d, {1: 1, 2: 2, 3: 3})
def test_lazy_len(self): d = LazyDict({"1": 1}) self.assertEqual(len(d), 1) d["2"] = 2 self.assertEqual(len(d), 2) d.set_stub("3", lambda x: x) self.assertEqual(len(d), 3)
def test_lazy_len(self): d = LazyDict({'1': 1}) self.assertEqual(len(d), 1) d['2'] = 2 self.assertEqual(len(d), 2) d.set_stub('3', lambda x: x) self.assertEqual(len(d), 3)
def test_lazy_len(self): d = LazyDict({'1':1}) self.assertEqual(len(d), 1) d['2'] = 2 self.assertEqual(len(d), 2) d.set_stub('3', lambda x: x) self.assertEqual(len(d), 3)
def test_lazy_items(self): d = LazyDict() d.set_stub(1, lambda x: x) self.assertEqual(set(d.items()), {(1, 1)}) d = LazyDict({1: 2}) d.set_stub(2, lambda x: x) self.assertEqual(set(d.items()), {(1, 2), (2, 2)})
def test_lazy_items(self): d = LazyDict() d.set_stub(1, lambda x:x) self.assertEqual(set(d.items()), {(1, 1)}) d = LazyDict({1:2}) d.set_stub(2, lambda x:x) self.assertEqual(set(d.items()), {(1, 2), (2, 2)})
def test_lazy_setdefault(self): d = LazyDict() self.assertIs(d.setdefault("key0"), None) d.set_stub("key0", lambda x: "value0") self.assertIs(d.setdefault("key0"), "value0") d.set_stub("key0", lambda x: "value0") self.assertEqual(d.setdefault("key1", "value1"), "value1") self.assertEqual(d["key1"], "value1") self.assertEqual(d.setdefault("key0", "value2"), "value0")
def test_lazy_setdefault(self): d = LazyDict() self.assertIs(d.setdefault('key0'), None) d.set_stub('key0', lambda x: 'value0') self.assertIs(d.setdefault('key0'), 'value0') d.set_stub('key0', lambda x: 'value0') self.assertEqual(d.setdefault('key1', 'value1'), 'value1') self.assertEqual(d['key1'], 'value1') self.assertEqual(d.setdefault('key0', 'value2'), 'value0')
def test_lazy_setdefault(self): d = LazyDict() self.assertIs(d.setdefault('key0'), None) d.set_stub('key0', lambda x:'value0') self.assertIs(d.setdefault('key0'), 'value0') d.set_stub('key0', lambda x:'value0') self.assertEqual(d.setdefault('key1', 'value1'), 'value1') self.assertEqual(d['key1'], 'value1') self.assertEqual(d.setdefault('key0', 'value2'), 'value0')
def test_lazy_clear(self): d = LazyDict({1: 1, 2: 2, 3: 3}) d.set_stub(4, lambda x: x) d.clear() self.assertEqual(d, {}) self.assertEqual(set(d.keys()), set()) self.assertEqual(set(d.values()), set()) self.assertRaises(TypeError, d.clear, None)
def test_lazy_clear(self): d = LazyDict({1:1, 2:2, 3:3}) d.set_stub(4, lambda x:x) d.clear() self.assertEqual(d, {}) self.assertEqual(set(d.keys()), set()) self.assertEqual(set(d.values()), set()) self.assertRaises(TypeError, d.clear, None)
def test_lazy_popitem(self): d = LazyDict({1: 1}) d.set_stub(2, lambda x: x) k, v = d.popitem() self.assertEqual(k, v) k, v = d.popitem() self.assertEqual(k, v) self.assertEqual(k, 2) self.assertEqual(len(d._stubs), 0) self.assertRaises(KeyError, d.popitem)
def test_lazy_popitem(self): d = LazyDict({1: 1}) d.set_stub(2, lambda x:x) k, v = d.popitem() self.assertEqual(k, v) k, v = d.popitem() self.assertEqual(k, v) self.assertEqual(k, 2) self.assertEqual(len(d._stubs), 0) self.assertRaises(KeyError, d.popitem)
def test_lazy_get(self): d = LazyDict({'a': 1, 'b': 2}) d.set_stub('d', lambda x: x) self.assertIs(d.get('c'), None) self.assertEqual(d.get('c', 3), 3) self.assertEqual(d.get('a'), 1) self.assertEqual(d.get('a', 3), 1) self.assertEqual(d.get('d'), 'd') self.assertEqual(d.get('d', 3), 'd') self.assertRaises(TypeError, d.get) self.assertRaises(TypeError, d.get, None, None, None)
def test_lazy_get(self): d = LazyDict({"a": 1, "b": 2}) d.set_stub("d", lambda x: x) self.assertIs(d.get("c"), None) self.assertEqual(d.get("c", 3), 3) self.assertEqual(d.get("a"), 1) self.assertEqual(d.get("a", 3), 1) self.assertEqual(d.get("d"), "d") self.assertEqual(d.get("d", 3), "d") self.assertRaises(TypeError, d.get) self.assertRaises(TypeError, d.get, None, None, None)
def test_lazy_get(self): d = LazyDict({'a': 1, 'b': 2}) d.set_stub('d', lambda x:x) self.assertIs(d.get('c'), None) self.assertEqual(d.get('c', 3), 3) self.assertEqual(d.get('a'), 1) self.assertEqual(d.get('a', 3), 1) self.assertEqual(d.get('d'), 'd') self.assertEqual(d.get('d', 3), 'd') self.assertRaises(TypeError, d.get) self.assertRaises(TypeError, d.get, None, None, None)
def test_lazy_values(self): def resolver(id): p = {'c': 3} return p[id] d = LazyDict() self.assertEqual(set(d.values()), set()) d = LazyDict({1:2}) d.set_stub('c', resolver) v = d.values() self.assertEqual(set(v), {2,3}) self.assertRaises(TypeError, d.values, None)
def test_lazy_values(self): def resolver(id): p = {"c": 3} return p[id] d = LazyDict() self.assertEqual(set(d.values()), set()) d = LazyDict({1: 2}) d.set_stub("c", resolver) v = d.values() self.assertEqual(set(v), {2, 3}) self.assertRaises(TypeError, d.values, None)
def test_lazy_keys(self): def resolver(id): p = {'b': 2, 'c': 3} return p[id] d = LazyDict({'a': 1}) d.set_stub('b', resolver) d.set_stub('c', resolver) d['d'] = 4 k = d.keys() self.assertTrue('a' in d) self.assertTrue('b' in d) self.assertTrue('c' in d) self.assertTrue('d' in d) self.assertEqual(set(k), {'a','b','c','d'})
def test_lazy_keys(self): def resolver(id): p = {"b": 2, "c": 3} return p[id] d = LazyDict({"a": 1}) d.set_stub("b", resolver) d.set_stub("c", resolver) d["d"] = 4 k = d.keys() self.assertTrue("a" in d) self.assertTrue("b" in d) self.assertTrue("c" in d) self.assertTrue("d" in d) self.assertEqual(set(k), {"a", "b", "c", "d"})
def test_lazy_keys(self): def resolver(id): p = {'b': 2, 'c': 3} return p[id] d = LazyDict({'a': 1}) d.set_stub('b', resolver) d.set_stub('c', resolver) d['d'] = 4 k = d.keys() self.assertTrue('a' in d) self.assertTrue('b' in d) self.assertTrue('c' in d) self.assertTrue('d' in d) self.assertEqual(set(k), {'a', 'b', 'c', 'd'})
def test_lazy_pop(self): d = LazyDict() k, v = ('abc', 'def') d.set_stub(k, lambda x:'def') self.assertRaises(KeyError, d.pop, 'ghi') self.assertEqual(d.pop(k), v) self.assertEqual(len(d), 0) self.assertRaises(KeyError, d.pop, k) self.assertEqual(d.pop(k, v), v) d.set_stub(k, lambda x:v) self.assertEqual(d.pop(k, 1), v) self.assertRaises(TypeError, d.pop)
def test_lazy_pop(self): d = LazyDict() k, v = ("abc", "def") d.set_stub(k, lambda x: "def") self.assertRaises(KeyError, d.pop, "ghi") self.assertEqual(d.pop(k), v) self.assertEqual(len(d), 0) self.assertRaises(KeyError, d.pop, k) self.assertEqual(d.pop(k, v), v) d.set_stub(k, lambda x: v) self.assertEqual(d.pop(k, 1), v) self.assertRaises(TypeError, d.pop)
def test_lazy_pop(self): d = LazyDict() k, v = ('abc', 'def') d.set_stub(k, lambda x: 'def') self.assertRaises(KeyError, d.pop, 'ghi') self.assertEqual(d.pop(k), v) self.assertEqual(len(d), 0) self.assertRaises(KeyError, d.pop, k) self.assertEqual(d.pop(k, v), v) d.set_stub(k, lambda x: v) self.assertEqual(d.pop(k, 1), v) self.assertRaises(TypeError, d.pop)
def test_lazy_delitem(self): d = LazyDict() d["1"] = 1 d["2"] = 2 del d["1"] self.assertEqual(len(d), 1) self.assertRaises(KeyError, d.__getitem__, "1") d.set_stub("1", lambda x: x) self.assertEqual(len(d), 2) del d["1"] self.assertEqual(len(d), 1) self.assertRaises(KeyError, d.__getitem__, "1") d.set_stub("1", lambda x: x) self.assertEqual(d["1"], "1") del d["1"] self.assertEqual(len(d), 1) self.assertRaises(KeyError, d.__getitem__, "1")
def test_lazy_delitem(self): d = LazyDict() d['1'] = 1 d['2'] = 2 del d['1'] self.assertEqual(len(d), 1) self.assertRaises(KeyError, d.__getitem__, '1') d.set_stub('1', lambda x: x) self.assertEqual(len(d), 2) del d['1'] self.assertEqual(len(d), 1) self.assertRaises(KeyError, d.__getitem__, '1') d.set_stub('1', lambda x: x) self.assertEqual(d['1'], '1') del d['1'] self.assertEqual(len(d), 1) self.assertRaises(KeyError, d.__getitem__, '1')
def test_lazy_delitem(self): d = LazyDict() d['1'] = 1 d['2'] = 2 del d['1'] self.assertEqual(len(d), 1) self.assertRaises(KeyError, d.__getitem__, '1') d.set_stub('1', lambda x:x) self.assertEqual(len(d), 2) del d['1'] self.assertEqual(len(d), 1) self.assertRaises(KeyError, d.__getitem__, '1') d.set_stub('1', lambda x:x) self.assertEqual(d['1'], '1') del d['1'] self.assertEqual(len(d), 1) self.assertRaises(KeyError, d.__getitem__, '1')
def test_lazy_copy(self): d = LazyDict({'1':1}) d['2'] = 2 x = d.copy() self.assertEqual(len(x), 2) i = {'num': 0} def r(key, i): i['num'] += 1 return i['num'] d.set_stub('3', r, i) x = d.copy() self.assertEqual(d['3'], 1) self.assertEqual(len(d._stubs), 0) self.assertEqual(len(x._stubs), 1) self.assertEqual(x['3'], 2) self.assertEqual(len(x._stubs), 0) x = d.copy() self.assertEqual(d['3'], 1) self.assertEqual(x['3'], 1)
def test_lazy_copy(self): d = LazyDict({'1': 1}) d['2'] = 2 x = d.copy() self.assertEqual(len(x), 2) i = {'num': 0} def r(key, i): i['num'] += 1 return i['num'] d.set_stub('3', r, i) x = d.copy() self.assertEqual(d['3'], 1) self.assertEqual(len(d._stubs), 0) self.assertEqual(len(x._stubs), 1) self.assertEqual(x['3'], 2) self.assertEqual(len(x._stubs), 0) x = d.copy() self.assertEqual(d['3'], 1) self.assertEqual(x['3'], 1)
def test_lazy_copy(self): d = LazyDict({"1": 1}) d["2"] = 2 x = d.copy() self.assertEqual(len(x), 2) i = {"num": 0} def r(key, i): i["num"] += 1 return i["num"] d.set_stub("3", r, i) x = d.copy() self.assertEqual(d["3"], 1) self.assertEqual(len(d._stubs), 0) self.assertEqual(len(x._stubs), 1) self.assertEqual(x["3"], 2) self.assertEqual(len(x._stubs), 0) x = d.copy() self.assertEqual(d["3"], 1) self.assertEqual(x["3"], 1)
def test_lazy_update(self): d = LazyDict() d.set_stub(4, lambda x:x) d.update(LazyDict({1:100})) d.update(LazyDict({2:20})) d.update({4:5}) d.update(LazyDict({1:1, 2:2, 3:3})) self.assertEqual(d, {1:1, 2:2, 3:3, 4:5}) d.update() self.assertEqual(d, {1:1, 2:2, 3:3, 4:5}) self.assertRaises((TypeError, AttributeError), d.update, None) class SimpleUserDict: def __init__(self): self.d = {1:1, 2:2, 3:3} def keys(self): return self.d.keys() def __getitem__(self, i): return self.d[i] d.clear() d.update(SimpleUserDict()) self.assertEqual(d, {1:1, 2:2, 3:3})