def test_pickle(self):
     lazy = LazyObject()
     lazy._wrapped = (11, 22, 33)
     s = pickle.dumps(lazy)
     pickled = pickle.loads(s)
     self.assertTupleEqual(pickled._wrapped, lazy._wrapped)
     self.assertIsNot(pickled._wrapped, lazy._wrapped)
示例#2
0
    def test_lazy_iter(self):
        lazy = LazyObject()
        data = [11, 33, 22]
        lazy._wrapped = data

        # Shrugs
        for x, y in zip(iter(lazy), iter(data)):
            self.assertEqual(x, y)
示例#3
0
    def test_lazy_set_attribute(self):
        lazy = LazyObject()
        lazy._wrapped = type("NewDict", (dict, ), {})()

        lazy.test = 15
        with self.assertRaises(AttributeError):
            object.__getattribute__(lazy, 'test')
        self.assertEqual(object.__getattribute__(lazy._wrapped, 'test'), 15)
        self.assertIn('test', dir(lazy))
示例#4
0
    def test_lazy_set_item(self):
        lazy = LazyObject()
        lazy._wrapped = type("NewDict", (dict, ), {})()

        # set item
        lazy['foo'] = 'bar'
        # verify it is set right
        self.assertEqual(lazy['foo'], 'bar')
        self.assertEqual(lazy._wrapped['foo'], 'bar')
示例#5
0
    def test_lazy_del_attribute(self):
        lazy = LazyObject()
        lazy._wrapped = type("NewDict", (dict, ), {})()

        lazy.test = 17
        self.assertTrue(hasattr(lazy, 'test'))
        self.assertTrue(hasattr(lazy._wrapped, 'test'))
        del (lazy.test)
        self.assertFalse(hasattr(lazy, 'test'))
        self.assertFalse(hasattr(lazy._wrapped, 'test'))
示例#6
0
    def test_lazy_dir(self):
        lazy = LazyObject()

        self.assertNotIn('values', dir(lazy))
        # Make a dict that can take attribute assignment (aka not built in)
        lazy._wrapped = type("NewDict", (dict, ), {'quack': 'duck'})()
        # Values is there because _wrapped is a dict
        self.assertIn('values', dir(lazy))
        # quack is there because it's a class level variable
        self.assertIn('quack', dir(lazy))
    def test_lazy_items(self):
        lazy = LazyObject()
        lazy._wrapped = type("NewDict", (list, ), {})((11, 22, 33))

        lazy[1] = 17
        self.assertEqual(lazy[0], 11)
        self.assertEqual(lazy[1], 17)
        self.assertEqual(lazy[2], 33)
        self.assertEqual(len(lazy), 3)
        del (lazy[0])
        self.assertEqual(lazy[0], 17)
        self.assertEqual(lazy[1], 33)
        self.assertEqual(len(lazy), 2)
示例#8
0
    def test_setup_called(self):
        lazy = LazyObject()

        # Test auto loading, but since they aren't implemented, the error means
        # _setup is successfully getting called
        with self.assertRaises(NotImplementedError):
            lazy.test
        with self.assertRaises(NotImplementedError):
            lazy['test']
        with self.assertRaises(NotImplementedError):
            lazy.test = 13
        with self.assertRaises(NotImplementedError):
            'test' in lazy
        with self.assertRaises(NotImplementedError):
            lazy['test'] = 12
        with self.assertRaises(NotImplementedError):
            del (lazy.test)
        with self.assertRaises(NotImplementedError):
            del (lazy['test'])
        with self.assertRaises(NotImplementedError):
            iter(lazy)
示例#9
0
 def test_lazy_del_attribute_protections(self):
     lazy = LazyObject()
     lazy._wrapped = type("NewDict", (dict, ), {})()
     with self.assertRaises(TypeError):
         del (lazy._wrapped)
示例#10
0
    def test_lazy_contains(self):
        lazy = LazyObject()
        lazy._wrapped = type("NewDict", (dict, ), {})(foo='bar')

        self.assertIn('foo', lazy)