File: test_base.py

package info (click to toggle)
thuban 1.2.2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,872 kB
  • ctags: 5,853
  • sloc: python: 30,410; ansic: 6,181; xml: 4,234; cpp: 1,595; makefile: 141
file content (160 lines) | stat: -rw-r--r-- 4,983 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright (c) 2002, 2003 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with Thuban for details.

"""
Test the TitledObject and Modifiable
"""

__version__ = "$Revision: 1123 $"
# $Source$
# $Id: test_base.py 1123 2003-06-02 14:10:00Z bh $

import unittest

import support
support.initthuban()

from Thuban.Model.base import TitledObject, Modifiable
from Thuban.Model.messages import TITLE_CHANGED, CHANGED

SOMETHING_ELSE = "SOMETHING_ELSE"

class SomeTitledObject(TitledObject, Modifiable):

    """TitledObject for test purposes.

    TitledObject is not used directly. It's a mixin to be used together
    with Modifiable.
    """

class SomeModifiable(Modifiable):

    """Modifiable for test purposes.

    Modifiable is intended to be used as a base class for classes that
    need to inform other classes when they change and that have to keep
    track of whether they have changed. Thus we use a derived class for
    testing that provides some methods that simulate modificatios.
    """

    def do_something(self):
        """Call self.changed without parameters"""
        self.changed()

    def do_something_else(self):
        """Call self.changed with a message parameter"""
        self.changed(SOMETHING_ELSE)


class SubscriberTest(unittest.TestCase, support.SubscriberMixin):

    """Base class for test cases that use SubscriberMixin.

    Just provide the default implementations of setUp and tearDown.
    """

    def setUp(self):
        """Clear the list of received messages"""
        self.clear_messages()

    def tearDown(self):
        """Clear the list of received messages"""
        self.clear_messages()


class TestTitledObject(SubscriberTest):

    """Test cases for TitledObject"""

    def test_titled_object(self):
        """Test TitledObject"""
        titled = SomeTitledObject("Some Title")
        titled.Subscribe(TITLE_CHANGED,
                         self.subscribe_with_params, TITLE_CHANGED)

        self.assertEquals(titled.Title(), "Some Title")

        titled.SetTitle("New Title")
        self.assertEquals(titled.Title(), "New Title")

        self.check_messages([(titled, TITLE_CHANGED)])


class TestModifiable(SubscriberTest):

    """Test cases for Modifiable"""

    def setUp(self):
        """Extend the inherited method to create a Modifiable instance.

        Bind the Modifiable instance (actually an instance of
        SomeModifiable) to self.modifiable and subscribe to its CHANGED
        and SOMETHING_ELSE channels.
        """
        SubscriberTest.setUp(self)
        self.modifiable = SomeModifiable()
        self.modifiable.Subscribe(CHANGED, self.subscribe_with_params, CHANGED)
        self.modifiable.Subscribe(SOMETHING_ELSE,
                                  self.subscribe_with_params, SOMETHING_ELSE)

    def tearDown(self):
        """Extend the inherited method to explicitly destroy self.modifiable.
        """
        self.modifiable.Destroy()
        SubscriberTest.tearDown(self)

    def test_initial_state(self):
        """Test Modifiable initial state"""
        # initially a modifiable is unmodified
        self.failIf(self.modifiable.WasModified())
        # this test should not have resulted in any messages
        self.check_messages([])

    def test_silent_change(self):
        """Test Modifiable method which doesn't issue messages"""
        self.modifiable.do_something()
        # Now it should be modified and we should not have received any
        # message
        self.assert_(self.modifiable.WasModified())
        self.check_messages([])

    def test_unset_modified(self):
        """Test Modifiable.UnsetModified.

        Test whether the UnsetModified does in fact clear the modified
        flag and whether it issues a CHANGED message if it changes the
        modified flag from true to false.
        """
        # We start with an unmodified object, so when we call
        # UnsetModified now it shouldn't result in any message.
        self.modifiable.UnsetModified()
        self.check_messages([])
        self.failIf(self.modifiable.WasModified())

        # Call a method that modifies the object silently.
        self.modifiable.do_something()
        self.check_messages([])
        self.assert_(self.modifiable.WasModified())

        # when we now call UnsetModified it should result in a CHANGED
        # message
        self.modifiable.UnsetModified()
        self.check_messages([(CHANGED,)])
        self.failIf(self.modifiable.WasModified())

    def test_issuing_change(self):
        """Test Modifiable method that issues messages"""
        self.modifiable.do_something_else()
        # Now it should be modified and we should have received a
        # CHANGED message
        self.check_messages([(SOMETHING_ELSE,)])
        self.assert_(self.modifiable.WasModified())



if __name__ == "__main__":
    unittest.main()