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)
예제 #2
0
    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_module(self):
        obj = Thing('with-module')
        obj.themodule = os

        flattened = self.pickler.flatten(obj)
        inflated = self.unpickler.restore(flattened)
        self.assertEqual(inflated.themodule, os)
예제 #4
0
    def test_list_item_reference(self):
        thing = Thing('parent')
        thing.child = Thing('child')
        thing.child.refs = [thing]

        encoded = jsonpickle.encode(thing)
        decoded = jsonpickle.decode(encoded)

        self.assertEqual(id(decoded.child.refs[0]), id(decoded))
    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)
예제 #6
0
    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)
예제 #7
0
 def test_references(self):
     obj_a = Thing('foo')
     obj_b = Thing('bar')
     coll = [obj_a, obj_b, obj_b]
     flattened = self.pickler.flatten(coll)
     inflated = self.unpickler.restore(flattened)
     self.assertEqual(len(inflated), len(coll))
     for x in range(len(coll)):
         self.assertEqual(repr(coll[x]), repr(inflated[x]))
예제 #8
0
    def test_list_item_reference(self):
        thing = Thing('parent')
        thing.child = Thing('child')
        thing.child.refs = [thing]

        encoded = jsonpickle.encode(thing)
        decoded = jsonpickle.decode(encoded)

        self.assertEqual(id(decoded.child.refs[0]), id(decoded))
예제 #9
0
 def test_list_of_objects(self):
     """Test that objects in lists are referenced correctly"""
     a = Thing('a')
     b = Thing('b')
     pickled = jsonpickle.encode([a, b, b])
     unpickled = jsonpickle.decode(pickled)
     self.assertEqual(unpickled[1], unpickled[2])
     self.assertEqual(type(unpickled[0]), Thing)
     self.assertEqual(unpickled[0].name, 'a')
     self.assertEqual(unpickled[1].name, 'b')
     self.assertEqual(unpickled[2].name, 'b')
예제 #10
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)
예제 #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: "samples.Thing"})

        inflated = self.unpickler.restore(flattened)
        self.assertEqual(inflated.classref, Thing)
예제 #12
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)
예제 #13
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)
예제 #14
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'])
    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)
예제 #16
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])
예제 #17
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)
예제 #18
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: 'samples.Thing',
        })

        inflated = self.unpickler.restore(flattened)
        self.assertEqual(inflated.classref, Thing)
예제 #19
0
    def test_reference_to_list(self):
        thing = Thing('parent')
        thing.a = [1]
        thing.b = thing.a
        thing.b.append(thing.a)
        thing.b.append([thing.a])

        encoded = jsonpickle.encode(thing)
        decoded = jsonpickle.decode(encoded)

        self.assertEqual(decoded.a[0], 1)
        self.assertEqual(decoded.b[0], 1)
        self.assertEqual(id(decoded.a), id(decoded.b))
        self.assertEqual(id(decoded.a), id(decoded.a[1]))
        self.assertEqual(id(decoded.a), id(decoded.a[2][0]))
예제 #20
0
    def test_reference_to_list(self):
        thing = Thing('parent')
        thing.a = [1]
        thing.b = thing.a
        thing.b.append(thing.a)
        thing.b.append([thing.a])

        encoded = jsonpickle.encode(thing)
        decoded = jsonpickle.decode(encoded)

        self.assertEqual(decoded.a[0], 1)
        self.assertEqual(decoded.b[0], 1)
        self.assertEqual(id(decoded.a), id(decoded.b))
        self.assertEqual(id(decoded.a), id(decoded.a[1]))
        self.assertEqual(id(decoded.a), id(decoded.a[2][0]))
예제 #21
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('samples.Thing("random")'): True})
예제 #22
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)
예제 #23
0
 def test_newstyleslots_with_children(self):
     obj = ThingWithSlots(Thing('a'), Thing('b'))
     jsonstr = jsonpickle.encode(obj)
     newobj = jsonpickle.decode(jsonstr)
     self.assertEqual(newobj.a.name, 'a')
     self.assertEqual(newobj.b.name, 'b')
예제 #24
0
from samples import Thing

from six import u
import jsonpickle
import unittest
import warnings

SAMPLE_DATA = {'things': [Thing('data')]}


class BackendTestCase(unittest.TestCase):
    def _is_installed(self, backend):
        if not jsonpickle.util.is_installed(backend):
            self.fail('%s module not available, please install' % backend)

    def set_backend(self, *args):
        backend = args[0]

        self._is_installed(backend)

        jsonpickle.load_backend(*args)
        jsonpickle.set_preferred_backend(backend)

    def set_preferred_backend(self, backend):
        self._is_installed(backend)

        jsonpickle.set_preferred_backend(backend)

    def tearDown(self):
        # always reset to default backend
        jsonpickle.set_preferred_backend('json')
예제 #25
0
 def test_object(self):
     self.assertFalse(is_primitive(Thing('test')))
예제 #26
0
 def setUp(self):
     self.obj = Thing('A name')
     self.expected_json = ('{"' + tags.OBJECT + '": "samples.Thing",'
                           ' "name": "A name", "child": null}')