Add Observer tests
authorTim Van Steenburgh <tvansteenburgh@gmail.com>
Sun, 6 Nov 2016 00:32:25 +0000 (20:32 -0400)
committerTim Van Steenburgh <tvansteenburgh@gmail.com>
Sun, 6 Nov 2016 00:32:25 +0000 (20:32 -0400)
tests/unit/test_model.py [new file with mode: 0644]

diff --git a/tests/unit/test_model.py b/tests/unit/test_model.py
new file mode 100644 (file)
index 0000000..97966da
--- /dev/null
@@ -0,0 +1,56 @@
+import unittest
+
+
+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(
+            'application', 'change', dict(name=id_))
+
+        self.assertTrue(o.cares_about(delta))
+
+    def test_cares_about_type(self):
+        type_ = 'application'
+
+        o = self._make_observer(
+            None, type_, None, None, None)
+
+        delta = self._make_delta(
+            type_, 'change', dict(name='foo'))
+
+        self.assertTrue(o.cares_about(delta))
+
+    def test_cares_about_action(self):
+        action = 'change'
+
+        o = self._make_observer(
+            None, None, action, None, None)
+
+        delta = self._make_delta(
+            'application', action, dict(name='foo'))
+
+        self.assertTrue(o.cares_about(delta))
+
+    def test_cares_about_predicate(self):
+        def predicate(delta):
+            return delta.data.get('fizz') == 'bang'
+
+        o = self._make_observer(
+            None, None, None, None, predicate)
+
+        delta = self._make_delta(
+            'application', 'change', dict(fizz='bang'))
+
+        self.assertTrue(o.cares_about(delta))