6 def _make_delta(entity
, type_
, data
=None):
7 from juju
.client
.client
import Delta
8 from juju
.delta
import get_entity_delta
10 delta
= Delta([entity
, type_
, data
])
11 return get_entity_delta(delta
)
14 class TestObserver(unittest
.TestCase
):
15 def _make_observer(self
, *args
):
16 from juju
.model
import _Observer
17 return _Observer(*args
)
19 def test_cares_about_id(self
):
22 o
= self
._make
_observer
(
23 None, None, None, id_
, None)
26 'application', 'change', dict(name
=id_
))
28 self
.assertTrue(o
.cares_about(delta
))
30 def test_cares_about_type(self
):
33 o
= self
._make
_observer
(
34 None, type_
, None, None, None)
37 type_
, 'change', dict(name
='foo'))
39 self
.assertTrue(o
.cares_about(delta
))
41 def test_cares_about_action(self
):
44 o
= self
._make
_observer
(
45 None, None, action
, None, None)
48 'application', action
, dict(name
='foo'))
50 self
.assertTrue(o
.cares_about(delta
))
52 def test_cares_about_predicate(self
):
54 return delta
.data
.get('fizz') == 'bang'
56 o
= self
._make
_observer
(
57 None, None, None, None, predicate
)
60 'application', 'change', dict(fizz
='bang'))
62 self
.assertTrue(o
.cares_about(delta
))
65 class TestModelState(unittest
.TestCase
):
66 def test_apply_delta(self
):
67 from juju
.model
import Model
68 from juju
.application
import Application
70 loop
= mock
.MagicMock()
71 model
= Model(loop
=loop
)
72 delta
= _make_delta('application', 'add', dict(name
='foo'))
75 prev
, new
= model
.state
.apply_delta(delta
)
77 len(model
.state
.state
[delta
.entity
][delta
.get_id()]), 1)
78 self
.assertIsNone(prev
)
79 self
.assertIsInstance(new
, Application
)
83 prev
, new
= model
.state
.apply_delta(delta
)
84 # length of the entity history deque is now 3:
85 # - 1 for the first delta
86 # - 1 for the second delta
87 # - 1 for the None sentinel appended after the 'remove'
89 len(model
.state
.state
[delta
.entity
][delta
.get_id()]), 3)
90 self
.assertIsInstance(new
, Application
)
91 # new object is falsy because its data is None
93 self
.assertIsInstance(prev
, Application
)
97 def test_get_series():
98 from juju
.model
import Model
102 'supported-series': {
110 assert model
._get
_series
('cs:trusty/ubuntu', entity
) == 'trusty'
111 assert model
._get
_series
('xenial/ubuntu', entity
) == 'xenial'
112 assert model
._get
_series
('~foo/xenial/ubuntu', entity
) == 'xenial'
113 assert model
._get
_series
('~foo/ubuntu', entity
) == 'xenial'
114 assert model
._get
_series
('ubuntu', entity
) == 'xenial'
115 assert model
._get
_series
('cs:ubuntu', entity
) == 'xenial'