def test_recursive(self):
        """create a recursive structure and test that we can handle it
        """
        parent = Thing('parent')
        child = Thing('child')
        child.sibling = Thing('sibling')

        parent.self = parent
        parent.child = child
        parent.child.twin = child
        parent.child.parent = parent
        parent.child.sibling.parent = parent

        cloned = jsonpickle.decode(jsonpickle.encode(parent))

        self.assertEqual(parent.name,
                         cloned.name)
        self.assertEqual(parent.child.name,
                         cloned.child.name)
        self.assertEqual(parent.child.sibling.name,
                         cloned.child.sibling.name)
        self.assertEqual(cloned,
                         cloned.child.parent)
        self.assertEqual(cloned,
                         cloned.child.sibling.parent)
        self.assertEqual(cloned,
                         cloned.child.twin.parent)
        self.assertEqual(cloned.child,
                         cloned.child.twin)
    def test_thing_with_module(self):
        obj = Thing('with-module')
        obj.themodule = os

        flattened = self.pickler.flatten(obj)
        inflated = self.unpickler.restore(flattened)
        self.assertEqual(inflated.themodule, os)
    def test_thing_with_submodule(self):
        from distutils import sysconfig

        obj = Thing('with-submodule')
        obj.submodule = sysconfig

        flattened = self.pickler.flatten(obj)
        inflated = self.unpickler.restore(flattened)
        self.assertEqual(inflated.submodule, sysconfig)
 def test_class(self):
     inst = Thing('test name')
     inst.child = Thing('child name') 
     
     flattened = self.pickler.flatten(inst)
     self.assertEqual('test name', flattened['name'])
     child = flattened['child']
     self.assertEqual('child name', child['name'])
     
     inflated = self.unpickler.restore(flattened)
     self.assertEqual('test name', inflated.name)
     self.assertTrue(type(inflated) is Thing)
     self.assertEqual('child name', inflated.child.name)
     self.assertTrue(type(inflated.child) is Thing)
Example #5
0
    def test_recursive(self):
        """create a recursive structure and test that we can handle it
        """
        parent = Thing('parent')
        child = Thing('child')
        child.sibling = Thing('sibling')

        parent.self = parent
        parent.child = child
        parent.child.twin = child
        parent.child.parent = parent
        parent.child.sibling.parent = parent

        cloned = jsonpickle.decode(jsonpickle.encode(parent))

        self.assertEqual(parent.name,
                         cloned.name)
        self.assertEqual(parent.child.name,
                         cloned.child.name)
        self.assertEqual(parent.child.sibling.name,
                         cloned.child.sibling.name)
        self.assertEqual(cloned,
                         cloned.child.parent)
        self.assertEqual(cloned,
                         cloned.child.sibling.parent)
        self.assertEqual(cloned,
                         cloned.child.twin.parent)
        self.assertEqual(cloned.child,
                         cloned.child.twin)
Example #6
0
 def test_classdict(self):
     dict = {'k1':Thing('one'), 'k2':Thing('two'), 'k3':3}
     
     flattened = self.pickler.flatten(dict)
     self.assertEqual('one', flattened['k1']['name'])
     self.assertEqual('two', flattened['k2']['name'])
     self.assertEqual(3, flattened['k3'])        
     
     inflated = self.unpickler.restore(flattened)
     self.assertEqual('one', inflated['k1'].name)
     self.assertTrue(type(inflated['k1']) is Thing)
     self.assertEqual('two', inflated['k2'].name)
     self.assertTrue(type(inflated['k2']) is Thing)
     self.assertEqual(3, inflated['k3'])
Example #7
0
 def test_classlist(self):
     array = [Thing('one'), Thing('two'), 'a string']
     
     flattened = self.pickler.flatten(array)
     self.assertEqual('one', flattened[0]['name'])
     self.assertEqual('two', flattened[1]['name'])
     self.assertEqual('a string', flattened[2])        
     
     inflated = self.unpickler.restore(flattened)
     self.assertEqual('one', inflated[0].name)
     self.assertTrue(type(inflated[0]) is Thing)
     self.assertEqual('two', inflated[1].name)
     self.assertTrue(type(inflated[1]) is Thing)
     self.assertEqual('a string', inflated[2])
Example #8
0
 def test_class(self):
     inst = Thing('test name')
     inst.child = Thing('child name') 
     
     flattened = self.pickler.flatten(inst)
     self.assertEqual('test name', flattened['name'])
     child = flattened['child']
     self.assertEqual('child name', child['name'])
     
     inflated = self.unpickler.restore(flattened)
     self.assertEqual('test name', inflated.name)
     self.assertTrue(type(inflated) is Thing)
     self.assertEqual('child name', inflated.child.name)
     self.assertTrue(type(inflated.child) is Thing)
    def test_class_reference(self):
        """This test ensures that users can store references to classes.
        """
        obj = Thing('object-with-class-reference')

        # reference the 'Thing' class (not an instance of the class)
        obj.classref = Thing

        flattened = self.pickler.flatten(obj)
        self.assertEqual(flattened['classref'], {
                            tags.TYPE: 'jsonpickle.tests.classes.Thing',
                         })

        inflated = self.unpickler.restore(flattened)
        self.assertEqual(inflated.classref, Thing)
    def test_type_reference(self):
        """This test ensures that users can store references to types.
        """
        obj = Thing('object-with-type-reference')

        # reference the built-in 'object' type
        obj.typeref = object

        flattened = self.pickler.flatten(obj)
        self.assertEqual(flattened['typeref'], {
                            tags.TYPE: '__builtin__.object',
                         })

        inflated = self.unpickler.restore(flattened)
        self.assertEqual(inflated.typeref, object)
Example #11
0
    def test_class_reference(self):
        """This test ensures that users can store references to classes.
        """
        obj = Thing('object-with-class-reference')

        # reference the 'Thing' class (not an instance of the class)
        obj.classref = Thing

        flattened = self.pickler.flatten(obj)
        self.assertEqual(flattened['classref'], {
                            tags.TYPE: 'jsonpickle.tests.classes.Thing',
                         })

        inflated = self.unpickler.restore(flattened)
        self.assertEqual(inflated.classref, Thing)
Example #12
0
    def test_type_reference(self):
        """This test ensures that users can store references to types.
        """
        obj = Thing('object-with-type-reference')

        # reference the built-in 'object' type
        obj.typeref = object

        flattened = self.pickler.flatten(obj)
        self.assertEqual(flattened['typeref'], {
                            tags.TYPE: '__builtin__.object',
                         })

        inflated = self.unpickler.restore(flattened)
        self.assertEqual(inflated.typeref, object)
Example #13
0
    def test_object_dict_keys(self):
        """Test that we handle random objects as keys.

        """
        pickled = jsonpickle.encode({Thing('random'): True})
        unpickled = jsonpickle.decode(pickled)
        self.assertEqual(unpickled,
                         {u'jsonpickle.tests.classes.Thing("random")': True})
Example #14
0
 def test_object(self):
     self.assertFalse(is_primitive(Thing('test')))
Example #15
0
 def setUp(self):
     self.obj = Thing('A name')
     self.expected_json = ('{"'+tags.OBJECT+'": "jsonpickle.tests.classes.Thing",'
                           ' "name": "A name", "child": null}')