~ubuntu-branches/ubuntu/utopic/python-apptools/utopic

« back to all changes in this revision

Viewing changes to apptools/sweet_pickle/tests/class_mapping_test_case.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-08 23:55:50 UTC
  • mfrom: (2.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110708235550-yz5u79ubeo4dhyfx
Tags: 4.0.0-1
* New upstream release
* Update debian/watch file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#-----------------------------------------------------------------------------
 
2
#
 
3
#  Copyright (c) 2006 by Enthought, Inc.
 
4
#  All rights reserved.
 
5
#
 
6
#  Author: Dave Peterson <dpeterson@enthought.com>
 
7
#
 
8
#-----------------------------------------------------------------------------
 
9
 
 
10
""" Tests the class mapping functionality of the enthought.pickle
 
11
    framework.
 
12
"""
 
13
 
 
14
# Standard library imports.
 
15
import unittest
 
16
 
 
17
# Enthought library imports
 
18
import apptools.sweet_pickle as sweet_pickle
 
19
from apptools.sweet_pickle.global_registry import _clear_global_registry
 
20
 
 
21
 
 
22
##############################################################################
 
23
# Classes to use within the tests
 
24
##############################################################################
 
25
 
 
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
 
30
 
 
31
##############################################################################
 
32
# class 'ClassMappingTestCase'
 
33
##############################################################################
 
34
 
 
35
class ClassMappingTestCase(unittest.TestCase):
 
36
    """ Tests the class mapping functionality of the apptools.sweet_pickle
 
37
        framework.
 
38
    """
 
39
 
 
40
    ##########################################################################
 
41
    # 'TestCase' interface
 
42
    ##########################################################################
 
43
 
 
44
    ### public interface #####################################################
 
45
 
 
46
    def setUp(self):
 
47
        """ Creates the test fixture.
 
48
 
 
49
            Overridden here to ensure each test starts with an empty global
 
50
            registry.
 
51
        """
 
52
        # Clear the global registry
 
53
        _clear_global_registry()
 
54
 
 
55
        # Cache a reference to the new global registry
 
56
        self.registry = sweet_pickle.get_global_registry()
 
57
 
 
58
 
 
59
    ##########################################################################
 
60
    # 'ClassMappingTestCase' interface
 
61
    ##########################################################################
 
62
 
 
63
    ### public interface #####################################################
 
64
 
 
65
    def test_infinite_loop_detection(self):
 
66
        """ Validates that the class mapping framework detects infinite
 
67
            loops of class mappings.
 
68
        """
 
69
        # Add mappings to the registry
 
70
        self.registry.add_mapping_to_class(Foo.__module__, Foo.__name__,
 
71
            Bar)
 
72
        self.registry.add_mapping_to_class(Bar.__module__, Bar.__name__,
 
73
            Baz)
 
74
        self.registry.add_mapping_to_class(Baz.__module__, Baz.__name__,
 
75
            Foo)
 
76
 
 
77
        # Validate that an exception is raised when trying to unpickle an
 
78
        # instance anywhere within the circular definition.
 
79
        def fn(o):
 
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())
 
84
 
 
85
 
 
86
    def test_unpickled_class_mapping(self):
 
87
 
 
88
        # Add the mappings to the registry
 
89
        self.registry.add_mapping_to_class(Foo.__module__, Foo.__name__,
 
90
            Bar)
 
91
        self.registry.add_mapping_to_class(Bar.__module__, Bar.__name__,
 
92
            Baz)
 
93
 
 
94
        # Validate that unpickling the first class gives us an instance of
 
95
        # the third class.
 
96
        start = Foo()
 
97
        end = sweet_pickle.loads(sweet_pickle.dumps(start))
 
98
        self.assertEqual(True, isinstance(end, Baz))
 
99
 
 
100
        # Validate that unpickling the second class gives us an instance of
 
101
        # the third class.
 
102
        start = Bar()
 
103
        end = sweet_pickle.loads(sweet_pickle.dumps(start))
 
104
        self.assertEqual(True, isinstance(end, Baz))
 
105
 
 
106
 
 
107
if __name__ == "__main__":
 
108
    unittest.main()
 
109
 
 
110
 
 
111
### EOF ######################################################################