1
#-----------------------------------------------------------------------------
3
# Copyright (c) 2006 by Enthought, Inc.
6
# Author: Dave Peterson <dpeterson@enthought.com>
8
#-----------------------------------------------------------------------------
10
""" Tests the class mapping functionality of the enthought.pickle
14
# Standard library imports.
17
# Enthought library imports
18
import apptools.sweet_pickle as sweet_pickle
19
from apptools.sweet_pickle.global_registry import _clear_global_registry
22
##############################################################################
23
# Classes to use within the tests
24
##############################################################################
26
# Need complete package name so that mapping matches correctly.
27
# The problem here is the Python loader that will load the same module with
28
# multiple names in sys.modules due to relative naming. Nice.
29
from apptools.sweet_pickle.tests.class_mapping_classes import Foo, Bar, Baz
31
##############################################################################
32
# class 'ClassMappingTestCase'
33
##############################################################################
35
class ClassMappingTestCase(unittest.TestCase):
36
""" Tests the class mapping functionality of the apptools.sweet_pickle
40
##########################################################################
41
# 'TestCase' interface
42
##########################################################################
44
### public interface #####################################################
47
""" Creates the test fixture.
49
Overridden here to ensure each test starts with an empty global
52
# Clear the global registry
53
_clear_global_registry()
55
# Cache a reference to the new global registry
56
self.registry = sweet_pickle.get_global_registry()
59
##########################################################################
60
# 'ClassMappingTestCase' interface
61
##########################################################################
63
### public interface #####################################################
65
def test_infinite_loop_detection(self):
66
""" Validates that the class mapping framework detects infinite
67
loops of class mappings.
69
# Add mappings to the registry
70
self.registry.add_mapping_to_class(Foo.__module__, Foo.__name__,
72
self.registry.add_mapping_to_class(Bar.__module__, Bar.__name__,
74
self.registry.add_mapping_to_class(Baz.__module__, Baz.__name__,
77
# Validate that an exception is raised when trying to unpickle an
78
# instance anywhere within the circular definition.
80
sweet_pickle.loads(sweet_pickle.dumps(o))
81
self.assertRaises(sweet_pickle.UnpicklingError, fn, Foo())
82
self.assertRaises(sweet_pickle.UnpicklingError, fn, Bar())
83
self.assertRaises(sweet_pickle.UnpicklingError, fn, Baz())
86
def test_unpickled_class_mapping(self):
88
# Add the mappings to the registry
89
self.registry.add_mapping_to_class(Foo.__module__, Foo.__name__,
91
self.registry.add_mapping_to_class(Bar.__module__, Bar.__name__,
94
# Validate that unpickling the first class gives us an instance of
97
end = sweet_pickle.loads(sweet_pickle.dumps(start))
98
self.assertEqual(True, isinstance(end, Baz))
100
# Validate that unpickling the second class gives us an instance of
103
end = sweet_pickle.loads(sweet_pickle.dumps(start))
104
self.assertEqual(True, isinstance(end, Baz))
107
if __name__ == "__main__":
111
### EOF ######################################################################