From c8d7bcea7e1fd4b174e3be2a29e382df66e88b66 Mon Sep 17 00:00:00 2001 From: Tim Van Steenburgh Date: Sat, 5 Nov 2016 21:47:29 -0400 Subject: [PATCH] Add ModelState tests --- juju/model.py | 12 ++++----- tests/unit/test_model.py | 54 ++++++++++++++++++++++++++++++++++------ tox.ini | 3 ++- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/juju/model.py b/juju/model.py index 953aa8f..db65b7d 100644 --- a/juju/model.py +++ b/juju/model.py @@ -159,14 +159,12 @@ class ModelState(object): def get_entity( self, entity_type, entity_id, history_index=-1, connected=True): - """Return an object instance representing the entity created or - updated by ``delta`` + """Return an object instance for the given entity_type and id. + + By default the object state matches the most recent state from + Juju. To get an instance of the object in an older state, pass + history_index, an index into the history deque for the entity. - """ - """ - log.debug( - 'Getting %s:%s at index %s', - entity_type, entity_id, history_index) """ if history_index < 0 and history_index != -1: diff --git a/tests/unit/test_model.py b/tests/unit/test_model.py index 97966da..f8cced3 100644 --- a/tests/unit/test_model.py +++ b/tests/unit/test_model.py @@ -1,22 +1,28 @@ import unittest +import mock + + +def _make_delta(entity, type_, data=None): + from juju.client.client import Delta + from juju.delta import get_entity_delta + + delta = Delta([entity, type_, data]) + return get_entity_delta(delta) + class TestObserver(unittest.TestCase): def _make_observer(self, *args): from juju.model import _Observer return _Observer(*args) - def _make_delta(self, entity, type_, data=None): - from juju.delta import ApplicationDelta - return ApplicationDelta([entity, type_, data]) - def test_cares_about_id(self): id_ = 'foo' o = self._make_observer( None, None, None, id_, None) - delta = self._make_delta( + delta = _make_delta( 'application', 'change', dict(name=id_)) self.assertTrue(o.cares_about(delta)) @@ -27,7 +33,7 @@ class TestObserver(unittest.TestCase): o = self._make_observer( None, type_, None, None, None) - delta = self._make_delta( + delta = _make_delta( type_, 'change', dict(name='foo')) self.assertTrue(o.cares_about(delta)) @@ -38,7 +44,7 @@ class TestObserver(unittest.TestCase): o = self._make_observer( None, None, action, None, None) - delta = self._make_delta( + delta = _make_delta( 'application', action, dict(name='foo')) self.assertTrue(o.cares_about(delta)) @@ -50,7 +56,39 @@ class TestObserver(unittest.TestCase): o = self._make_observer( None, None, None, None, predicate) - delta = self._make_delta( + delta = _make_delta( 'application', 'change', dict(fizz='bang')) self.assertTrue(o.cares_about(delta)) + + +class TestModelState(unittest.TestCase): + def test_apply_delta(self): + from juju.model import Model + from juju.application import Application + + loop = mock.MagicMock() + model = Model(loop=loop) + delta = _make_delta('application', 'add', dict(name='foo')) + + # test add + prev, new = model.state.apply_delta(delta) + self.assertEqual( + len(model.state.state[delta.entity][delta.get_id()]), 1) + self.assertIsNone(prev) + self.assertIsInstance(new, Application) + + # test remove + delta.type = 'remove' + prev, new = model.state.apply_delta(delta) + # length of the entity history deque is now 3: + # - 1 for the first delta + # - 1 for the second delta + # - 1 for the None sentinel appended after the 'remove' + self.assertEqual( + len(model.state.state[delta.entity][delta.get_id()]), 3) + self.assertIsInstance(new, Application) + # new object is falsy because its data is None + self.assertFalse(new) + self.assertIsInstance(prev, Application) + self.assertTrue(prev) diff --git a/tox.ini b/tox.ini index f3cbbf8..5babec0 100644 --- a/tox.ini +++ b/tox.ini @@ -11,6 +11,7 @@ skipsdist=True usedevelop=True passenv = HOME -commands = py.test -rsx -s +commands = py.test -rsx deps = pytest + mock -- 2.25.1