5 from cola
.models
.observable
import ObservableModel
7 class ConcreteModel(ObservableModel
):
8 """A concrete model for testing."""
10 def copy(self
, *args
, **opts
):
11 """Send notification from the model."""
12 self
.notify_message_observers(self
.message_copy
, *args
, **opts
)
14 class ConcreteObserver(object):
15 """A concrete observer for testing."""
16 def __init__(self
, model
):
19 model
.add_message_observer(ConcreteModel
.message_copy
,
23 """Reset test data back to a known good state."""
27 def receive_message(self
, *args
, **opts
):
28 """Receive notification from the model."""
33 class ObservableModelTestCase(unittest
.TestCase
):
34 """Tests the ObservableModel class."""
36 self
.model
= ConcreteModel()
37 self
.observer
= ConcreteObserver(self
.model
)
39 def test_notify_message_observers(self
):
40 """Test that notifications are delivered."""
41 self
.model
.copy(hello
='world')
42 self
.assertEqual(self
.observer
.args
, (),)
43 self
.assertEqual(self
.observer
.opts
, {'hello': 'world'})
45 def test_observing_multiple_models(self
):
46 """Test that we can attach to multiple models."""
47 self
.model
.copy(self
.model
, foo
='bar')
48 self
.assertEqual(self
.observer
.args
, (self
.model
,))
49 self
.assertEqual(self
.observer
.opts
, {'foo': 'bar'})
51 model
= ConcreteModel()
52 model
.add_message_observer(model
.message_copy
,
53 self
.observer
.receive_message
)
54 model
.copy(model
, baz
='quux')
55 self
.assertEqual(self
.observer
.args
, (model
,))
56 self
.assertEqual(self
.observer
.opts
, {'baz': 'quux'})
58 self
.model
.copy(foo
='bar')
59 self
.assertEqual(self
.observer
.args
, (),)
60 self
.assertEqual(self
.observer
.opts
, {'foo': 'bar'})
63 if __name__
== '__main__':