Example #1
0
    def test_normal_setstate(self):
        """ Validates that only existing setstate methods are called when
            there are no registered state functions in the class chain.
        """
        # Validate that unpickling the first class gives us an instance of
        # the third class with the appropriate attribute values.  It will have
        # the default Foo values (because there is no state function to move
        # them) and also the default Baz values (since they inherit the
        # trait defaults because nothing overwrote the values.)
        start = Foo()
        end = sweet_pickle.loads(sweet_pickle.dumps(start))
        self.assertEqual(True, isinstance(end, Baz))
        self._assertAttributes(end, 1, (False, 1, 1, 'foo'))
        self._assertAttributes(end, 2, None)
        self._assertAttributes(end, 3, (False, 3, 3, 'baz'))

        # Validate that unpickling the second class gives us an instance of
        # the third class with the appropriate attribute values.  It will have
        # only the Baz attributes with the Bar values (since the __setstate__
        # on Baz converted the Bar attributes to Baz attributes.)
        start = Bar()
        end = sweet_pickle.loads(sweet_pickle.dumps(start))
        self.assertEqual(True, isinstance(end, Baz))
        self._assertAttributes(end, 2, None)
        self._assertAttributes(end, 3, (True, 2, 2, 'bar'))
Example #2
0
    def test_unpickled_class_mapping(self):

        # Add the mappings to the registry
        self.registry.add_mapping_to_class(Foo.__module__, Foo.__name__, Bar)
        self.registry.add_mapping_to_class(Bar.__module__, Bar.__name__, Baz)

        # Validate that unpickling the first class gives us an instance of
        # the third class.
        start = Foo()
        end = sweet_pickle.loads(sweet_pickle.dumps(start))
        self.assertEqual(True, isinstance(end, Baz))

        # Validate that unpickling the second class gives us an instance of
        # the third class.
        start = Bar()
        end = sweet_pickle.loads(sweet_pickle.dumps(start))
        self.assertEqual(True, isinstance(end, Baz))
Example #3
0
    def test_unpickled_class_mapping(self):

        # Add the mappings to the registry
        self.registry.add_mapping_to_class(Foo.__module__, Foo.__name__,
            Bar)
        self.registry.add_mapping_to_class(Bar.__module__, Bar.__name__,
            Baz)

        # Validate that unpickling the first class gives us an instance of
        # the third class.
        start = Foo()
        end = sweet_pickle.loads(sweet_pickle.dumps(start))
        self.assertEqual(True, isinstance(end, Baz))

        # Validate that unpickling the second class gives us an instance of
        # the third class.
        start = Bar()
        end = sweet_pickle.loads(sweet_pickle.dumps(start))
        self.assertEqual(True, isinstance(end, Baz))
Example #4
0
    def test_unpickled_chain_functionality(self):
        """ Validates that the registered state functions are used when
            unpickling.
        """
        # Add the state function to the registry
        self.registry.add_state_function_for_class(Bar, 2,
            bar_state_function)

        # Validate that unpickling the first class gives us an instance of
        # the third class with the appropriate attribute values.
        start = Foo()
        end = sweet_pickle.loads(sweet_pickle.dumps(start))
        self.assertEqual(True, isinstance(end, Baz))
        self._assertAttributes(end, 1, None)
        self._assertAttributes(end, 2, None)
        self._assertAttributes(end, 3, (False, 1, 1, 'foo'))

        # Validate that unpickling the second class gives us an instance of
        # the third class.
        start = Bar()
        end = sweet_pickle.loads(sweet_pickle.dumps(start))
        self.assertEqual(True, isinstance(end, Baz))
        self._assertAttributes(end, 2, None)
        self._assertAttributes(end, 3, (True, 2, 2, 'bar'))
Example #5
0
 def fn(o):
     sweet_pickle.loads(sweet_pickle.dumps(o))
Example #6
0
 def fn(o):
     sweet_pickle.loads(sweet_pickle.dumps(o))