Source

remember / remember / test / test_dicts.py

Full commit
import copy
import json
import inspect
from random import shuffle

try:
    import unittest2 as unittest
except ImportError:
    import unittest

from remember import dicts
from remember.dicts import OrderedDict

class TestLRUDict(unittest.TestCase):
    def setUp(self):
        self.num = 14
        self.d = dicts.LRUDict(self.num)
        
    def test_numitems(self):
        for i in range(30):
            # We'll fill it with C objects as keys so we can use the same 
            # tests for LRUIDDict.
            self.d[i] = i
            self.assertGreaterEqual(self.num, len(self.d))
            self.assertGreaterEqual(i + 1, len(self.d))
            self.assertEqual(len(self.d), len(dict(self.d)))
        self.assertEqual(self.num, len(self.d))
        self.assertEqual(len(self.d), sum(1 for k in self.d))
            
    def test_set_and_get_and_contain_items(self):
        key = 100
        self.d[key] = 5
        self.assertEqual(self.d[key], 5)
        with self.assertRaises(KeyError):
            self.d[0]
        self.assertIn(key, self.d)
        
    def test_hashable_keys(self):
        with self.assertRaises(TypeError):
            self.d[[1]]
        
    def test_compare_to_dict(self):
        real_dict = {}
        self.d[6] = real_dict[6] = 1
        self.d[9] = real_dict[9] = 2
        self.assertEqual(self.d, real_dict)
        del real_dict[9]
        self.assertNotEqual(self.d, real_dict)
        
        
class TestFrozenDict(unittest.TestCase):
    def setUp(self):
        # Since we are going to be using JSON, we want strings as keys. =/
        # XXX: I would like to make sure we can't make this roundtrip without
        # losing correctness. The real intent of this class (storing kwargs)
        # will only have string keys, but still.
        self.real = dict((str(i), i) for i in xrange(100))
        self.frozen = dicts.FrozenDict(self.real)
        
        self.real_mutable_values = dict((i, []) for i in xrange(100))
        
    def test_compare_to_dict(self):
        self.assertEqual(self.real, self.frozen)
        for method in ["keys", "values", "items"]:
            self.assertItemsEqual(getattr(self.real, method)(), 
                                  getattr(self.frozen, method)())
        
    def test_mutate(self):
        for attr in ['pop', 'frozen', 'update', 'clear', 'setdefault']:
            with self.assertRaises(AttributeError):
               getattr(self.frozen, attr)

        with self.assertRaises(TypeError):
            del self.frozen['1']
            
        self.assertIn('1', self.frozen)
            
        with self.assertRaises(TypeError):
            self.frozen['1'] = 4
            
        self.assertNotEqual(self.frozen['1'], 4)
               
    def test_hash(self):
        new_frozen = dicts.FrozenDict(self.real)
        self.assertEqual(hash(self.frozen), hash(new_frozen))
        
        frozen_mutable_values = dicts.FrozenDict(self.real_mutable_values)
        with self.assertRaises(TypeError):
            hash(frozen_mutable_values)
        
    def test_json(self):
        encoder = dicts.FrozenDictJSONEncoder()
        s = encoder.encode(self.frozen)
        d = json.loads(s)
        new_frozen = dicts.FrozenDict(d)
        
        self.assertEqual(d, new_frozen)
        self.assertEqual(self.frozen, new_frozen)
        self.assertEqual(hash(self.frozen), hash(new_frozen))
        for method in ["keys", "values", "items"]:
            self.assertItemsEqual(getattr(self.frozen, method)(), 
                                  getattr(new_frozen, method)())
                                

# The rest of the code here was copied from the 2.7 stdlib, slighlty modified. 

class TestOrderedDict(unittest.TestCase):

    def test_init(self):
        with self.assertRaises(TypeError):
            OrderedDict([('a', 1), ('b', 2)], None)                                 # too many args
        pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
        self.assertEqual(sorted(OrderedDict(dict(pairs)).items()), pairs)           # dict input
        self.assertEqual(sorted(OrderedDict(**dict(pairs)).items()), pairs)         # kwds input
        self.assertEqual(list(OrderedDict(pairs).items()), pairs)                   # pairs input
        self.assertEqual(list(OrderedDict([('a', 1), ('b', 2), ('c', 9), ('d', 4)],
                                          c=3, e=5).items()), pairs)                # mixed input

        # make sure no positional args conflict with possible kwdargs
        self.assertEqual(inspect.getargspec(OrderedDict.__dict__['__init__']).args,
                         ['self'])

        # XXX Not currently supported. Should it be?
        # @unittest.expectedFailure
        # Make sure that direct calls to __init__ do not clear previous contents
        #d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
        #d.__init__([('e', 5), ('f', 6)], g=7, d=4)
        #self.assertEqual(list(d.items()),
        #    [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])

    def test_update(self):
        with self.assertRaises(TypeError):
            OrderedDict().update([('a', 1), ('b', 2)], None)                        # too many args
        pairs = [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
        od = OrderedDict()
        od.update(dict(pairs))
        self.assertEqual(sorted(od.items()), pairs)                                 # dict input
        od = OrderedDict()
        od.update(**dict(pairs))
        self.assertEqual(sorted(od.items()), pairs)                                 # kwds input
        od = OrderedDict()
        od.update(pairs)
        self.assertEqual(list(od.items()), pairs)                                   # pairs input
        od = OrderedDict()
        od.update([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5)
        self.assertEqual(list(od.items()), pairs)                                   # mixed input

        # Make sure that direct calls to update do not clear previous contents
        # add that updates items are not moved to the end
        d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
        d.update([('e', 5), ('f', 6)], g=7, d=4)
        self.assertEqual(list(d.items()),
            [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])

    def test_clear(self):
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        shuffle(pairs)
        od = OrderedDict(pairs)
        self.assertEqual(len(od), len(pairs))
        od.clear()
        self.assertEqual(len(od), 0)

    def test_delitem(self):
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        od = OrderedDict(pairs)
        del od['a']
        self.assertNotIn('a', od)
        with self.assertRaises(KeyError):
            del od['a']
        self.assertEqual(list(od.items()), pairs[:2] + pairs[3:])

    def test_setitem(self):
        od = OrderedDict([('d', 1), ('b', 2), ('c', 3), ('a', 4), ('e', 5)])
        od['c'] = 10           # existing element
        od['f'] = 20           # new element
        self.assertEqual(list(od.items()),
                         [('d', 1), ('b', 2), ('c', 10), ('a', 4), ('e', 5), ('f', 20)])

    def test_iterators(self):
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        shuffle(pairs)
        od = OrderedDict(pairs)
        self.assertEqual(list(od), [t[0] for t in pairs])
        self.assertEqual(od.keys()[:], [t[0] for t in pairs])
        self.assertEqual(od.values()[:], [t[1] for t in pairs])
        self.assertEqual(od.items()[:], pairs)
        self.assertEqual(list(od.iterkeys()), [t[0] for t in pairs])
        self.assertEqual(list(od.itervalues()), [t[1] for t in pairs])
        self.assertEqual(list(od.iteritems()), pairs)
        self.assertEqual(list(reversed(od)),
                         [t[0] for t in reversed(pairs)])

    def test_popitem(self):
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        shuffle(pairs)
        od = OrderedDict(pairs)
        while pairs:
            self.assertEqual(od.popitem(), pairs.pop())
        with self.assertRaises(KeyError):
            od.popitem()
        self.assertEqual(len(od), 0)

    def test_pop(self):
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        shuffle(pairs)
        od = OrderedDict(pairs)
        shuffle(pairs)
        while pairs:
            k, v = pairs.pop()
            self.assertEqual(od.pop(k), v)
        with self.assertRaises(KeyError):
            od.pop('xyz')
        self.assertEqual(len(od), 0)
        self.assertEqual(od.pop(k, 12345), 12345)

    def test_equality(self):
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        shuffle(pairs)
        od1 = OrderedDict(pairs)
        od2 = OrderedDict(pairs)
        self.assertEqual(od1, od2)          # same order implies equality
        pairs = pairs[2:] + pairs[:2]
        od2 = OrderedDict(pairs)
        self.assertNotEqual(od1, od2)       # different order implies inequality
        # comparison to regular dict is not order sensitive
        self.assertEqual(od1, dict(od2))
        self.assertEqual(dict(od2), od1)
        # different length implied inequality
        self.assertNotEqual(od1, OrderedDict(pairs[:-1]))

    @unittest.expectedFailure
    def test_copying(self):
        # Check that ordered dicts are copyable, deepcopyable, picklable,
        # and have a repr/eval round-trip
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        od = OrderedDict(pairs)
        update_test = OrderedDict()
        update_test.update(od)
        for i, dup in enumerate([
                    od.copy(),
                    copy.copy(od),
                    copy.deepcopy(od),
                    pickle.loads(pickle.dumps(od, 0)),
                    pickle.loads(pickle.dumps(od, 1)),
                    pickle.loads(pickle.dumps(od, 2)),
                    pickle.loads(pickle.dumps(od, -1)),
                    eval(repr(od)),
                    update_test,
                    OrderedDict(od),
                    ]):
            self.assertTrue(dup is not od)
            self.assertEquals(dup, od)
            self.assertEquals(list(dup.items()), list(od.items()))
            self.assertEquals(len(dup), len(od))
            self.assertEquals(type(dup), type(od))

    @unittest.expectedFailure # __reduce__ not supported
    def test_yaml_linkage(self):
        # Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature.
        # In yaml, lists are native but tuples are not.
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        od = OrderedDict(pairs)
        # yaml.dump(od) -->
        # '!!python/object/apply:__main__.OrderedDict\n- - [a, 1]\n  - [b, 2]\n'
        self.assertTrue(all(type(pair)==list for pair in od.__reduce__()[1]))

    @unittest.expectedFailure
    def test_reduce_not_too_fat(self):
        # do not save instance dictionary if not needed
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        od = OrderedDict(pairs)
        self.assertEqual(len(od.__reduce__()), 2)
        od.x = 10
        self.assertEqual(len(od.__reduce__()), 3)

    def test_repr(self):
        od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])
        self.assertEqual(repr(od),
            "OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])")
        self.assertEqual(eval(repr(od)), od)
        self.assertEqual(repr(OrderedDict()), "OrderedDict()")

    def test_setdefault(self):
        pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
        shuffle(pairs)
        od = OrderedDict(pairs)
        pair_order = list(od.items())
        self.assertEqual(od.setdefault('a', 10), 3)
        # make sure order didn't change
        self.assertEqual(list(od.items()), pair_order)
        self.assertEqual(od.setdefault('x', 10), 10)
        # make sure 'x' is added to the end
        self.assertEqual(list(od.items())[-1], ('x', 10))

    def test_reinsert(self):
        # Given insert a, insert b, delete a, re-insert a,
        # verify that a is now later than b.
        od = OrderedDict()
        od['a'] = 1
        od['b'] = 2
        del od['a']
        od['a'] = 1
        self.assertEqual(list(od.items()), [('b', 2), ('a', 1)])