예제 #1
0
    def test_globally(self):
        """Testing dictobj
        """
        d = dictobj()
        assert str(d) == 'dictobj({})'
        d.test = 'test'
        d.items = 'items'
        self.assertEqual(sorted(d.items()), [('items', 'items'),
                                             ('test', 'test')])

        assert d['items'] == 'items'
        assert isinstance(d.items, collections.Callable)

        d = dictobj({'test': 'newtest', 'items': 'theitems'})
        assert 'test' in d
        assert d['test'] == 'newtest'
        assert 'items' in d

        d = dictobj([('test', 'another')], items=1, b3=34)
        assert 'b3' in d
        assert d.test == 'another'
        assert d['items'] == 1

        print(dict(d))
        print(getattr(d, 'b3'))
예제 #2
0
 def test_pickle(self):
     """Testing dictobj pickle
     """
     d = dictobj({'a': 1, 'b': "c", 'd': dictobj(x="y")})
     ret = pickle.dumps(d)
     assert ret
     d2 = pickle.loads(ret)
     assert d2 == d
     assert d2.__dict__ == d.__dict__
예제 #3
0
    def test___delitem___(self):
        """Testing __delitem__
        """
        t = dictobj({'a': 'a', 'b': 'b'})

        # delitem on 'a' key
        del t['a']
        self.assertEqual(t, {'b': 'b'})

        # changed values is updated if needed
        t = dictobj()
        t.a = 'a'
        t.b = 'b'
        self.assertEqual(t, {'a': 'a', 'b': 'b'})
        del t['a']
        self.assertTrue('a' not in t)
예제 #4
0
 def test_setdefault(self):
     """Testing setdefault
     """
     t = dictobj()
     t.setdefault('a', 'a')
     self.assertEqual(t.a, 'a')
     self.assertEqual(t, {'a': 'a'})
예제 #5
0
    def test___setitem__(self):
        """Testing __setitem__
        """
        t = dictobj()

        t = dictobj({'a': 'a', 'b': 'b', 'c': 'c'})

        t.e = 'e'
        t['f'] = 'f'

        t.f = "new f"
        self.assertEqual(t, {
            'a': 'a',
            'b': 'b',
            'c': 'c',
            'e': 'e',
            'f': "new f"
        })
예제 #6
0
    def test_copy(self):
        """Testing copy
        """
        d1 = dictobj({'a': 'a', 'b': 'b'})
        d2 = d1.copy()
        assert d1 == d2
        assert d1 is not d2

        assert copy.copy(d1) == d2
        assert copy.deepcopy(d1) == d2

        d3 = dictobj(a=1, b="2", c=['a', 'b'], d=object())
        d4 = d3.copy()
        assert d4.c is d3.c
        assert d4.d is d3.d
        d4 = copy.copy(d3)
        assert d4.c is d3.c
        assert d4.d is d3.d
        d4 = copy.deepcopy(d3)
        assert d4.c is not d3.c
        assert d4.d is not d3.d
예제 #7
0
    def test___init__(self):
        """Testing __init__
        """
        # all ways to init a dict work
        # keyword arguments
        t = dictobj(a='a', b='b')
        self.assertEqual(t, {'a': 'a', 'b': 'b'})
        # a dict
        t = dictobj({'a': 'a', 'b': 'b'})
        self.assertEqual(t, {'a': 'a', 'b': 'b'})
        # an iterable
        t = dictobj([('a', 'a'), ('b', 'b')])
        self.assertEqual(t, {'a': 'a', 'b': 'b'})
        # several dicts
        t = dictobj({'a': 'a'}, {'b': 'b'})
        self.assertEqual(t, {'a': 'a', 'b': 'b'})

        # if some values are dict, they're changed to dictobj too
        t = dictobj({'a': {'b': 'b'}})
        self.assertTrue(isinstance(t.a, dictobj))

        # and it's recursive
        t = dictobj({'a': {'b': {'c': 'c'}}})
        self.assertTrue(isinstance(t.a.b, dictobj))

        # circular-referencing dicts are not supported
        orig = {}
        orig['orig'] = orig
        self.assertRaises(RuntimeError, dictobj, orig)
예제 #8
0
    def test___delattr__(self):
        """Testing __delattr__
        """
        t = dictobj({'a': 'a', 'b': 'b'})

        # delitem on 'a' key
        del t.a
        self.assertEqual(t, {'b': 'b'})

        class T(dictobj):
            def __init__(self, *args, **kwargs):
                self.test = 'test'
                super(T, self).__init__(*args, **kwargs)

        t = T()
        self.assertEqual(t.test, "test")
        self.assertEqual(t, {})

        # delattr on 'test' attribute
        del t.test
        self.assertFalse(hasattr(t, 'test'))
        self.assertEqual(t, {})
예제 #9
0
 def test___repr__(self):
     """Testing __repr__
     """
     d = {'a': 1, 'b': 'b'}
     t = dictobj(d)
     self.assertEqual(repr(t), "dictobj(%s)" % dict.__repr__(d))
예제 #10
0
 def test_json(self):
     """Testing dictobj json serialization
     """
     data = json.dumps(dictobj(a=1, b=2, c="d"), sort_keys=True)
     self.assertEqual(data, u'{"a": 1, "b": 2, "c": "d"}')
예제 #11
0
 def test_clear(self):
     """Testing clear
     """
     t = dictobj({'a': 'a', 'b': 'b'})
     t.clear()
     self.assertTrue('a' not in t)
예제 #12
0
 def test_update(self):
     """Testing update
     """
     t = dictobj()
     t.update({'a': 'a', 'b': 'b', 'c': 'c'})
     self.assertEqual(t, {'a': 'a', 'b': 'b', 'c': 'c'})