Unit test for series logic
[osm/N2VC.git] / tests / unit / test_model.py
1 import unittest
2
3 import mock
4
5
6 def _make_delta(entity, type_, data=None):
7 from juju.client.client import Delta
8 from juju.delta import get_entity_delta
9
10 delta = Delta([entity, type_, data])
11 return get_entity_delta(delta)
12
13
14 class TestObserver(unittest.TestCase):
15 def _make_observer(self, *args):
16 from juju.model import _Observer
17 return _Observer(*args)
18
19 def test_cares_about_id(self):
20 id_ = 'foo'
21
22 o = self._make_observer(
23 None, None, None, id_, None)
24
25 delta = _make_delta(
26 'application', 'change', dict(name=id_))
27
28 self.assertTrue(o.cares_about(delta))
29
30 def test_cares_about_type(self):
31 type_ = 'application'
32
33 o = self._make_observer(
34 None, type_, None, None, None)
35
36 delta = _make_delta(
37 type_, 'change', dict(name='foo'))
38
39 self.assertTrue(o.cares_about(delta))
40
41 def test_cares_about_action(self):
42 action = 'change'
43
44 o = self._make_observer(
45 None, None, action, None, None)
46
47 delta = _make_delta(
48 'application', action, dict(name='foo'))
49
50 self.assertTrue(o.cares_about(delta))
51
52 def test_cares_about_predicate(self):
53 def predicate(delta):
54 return delta.data.get('fizz') == 'bang'
55
56 o = self._make_observer(
57 None, None, None, None, predicate)
58
59 delta = _make_delta(
60 'application', 'change', dict(fizz='bang'))
61
62 self.assertTrue(o.cares_about(delta))
63
64
65 class TestModelState(unittest.TestCase):
66 def test_apply_delta(self):
67 from juju.model import Model
68 from juju.application import Application
69
70 loop = mock.MagicMock()
71 model = Model(loop=loop)
72 delta = _make_delta('application', 'add', dict(name='foo'))
73
74 # test add
75 prev, new = model.state.apply_delta(delta)
76 self.assertEqual(
77 len(model.state.state[delta.entity][delta.get_id()]), 1)
78 self.assertIsNone(prev)
79 self.assertIsInstance(new, Application)
80
81 # test remove
82 delta.type = 'remove'
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'
88 self.assertEqual(
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
92 self.assertFalse(new)
93 self.assertIsInstance(prev, Application)
94 self.assertTrue(prev)
95
96
97 def test_get_series():
98 from juju.model import Model
99 model = Model()
100 entity = {
101 'Meta': {
102 'supported-series': {
103 'SupportedSeries': [
104 'xenial',
105 'trusty',
106 ],
107 },
108 },
109 }
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'