[pitivi] tests_: Use mocks to test recovery



commit 9c2a48c21db42f9bb8115ef091d0fd99c07da32a
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Wed Dec 7 12:16:00 2016 +0100

    tests_: Use mocks to test recovery
    
    Reviewed-by: Thibault Saunier <tsaunier gnome org>
    Differential Revision: https://phabricator.freedesktop.org/D1547

 tests/test_pipeline.py |   80 ++++++++++++++++++++---------------------------
 1 files changed, 34 insertions(+), 46 deletions(-)
---
diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py
index 9ac8767..299f260 100644
--- a/tests/test_pipeline.py
+++ b/tests/test_pipeline.py
@@ -18,63 +18,51 @@
 # Boston, MA 02110-1301, USA.
 from unittest import mock
 
+from gi.repository import GES
 from gi.repository import GLib
 from gi.repository import Gst
 
-from pitivi.utils import pipeline
+from pitivi.utils.pipeline import MAX_RECOVERIES
+from pitivi.utils.pipeline import Pipeline
 from tests import common
 
 
-class MockedPipeline(pipeline.Pipeline):
-
-    def __init__(self):
-        pipeline.Pipeline.__init__(self, common.create_pitivi_mock())
-        self.state_calls = {}
-        self._timeline = mock.MagicMock()
-
-    def set_state(self, state):
-        self.state_calls[state] = self.state_calls.get(state, 0) + 1
+class TestPipeline(common.TestCase):
 
-    def post_fake_error_message(self):
+    @staticmethod
+    def post_fake_error_message(pipeline):
         message = mock.Mock()
         message.type = Gst.MessageType.ERROR
         error = GLib.Error.new_literal(Gst.core_error_quark(),
                                        "fake", Gst.CoreError.TOO_LAZY)
         message.parse_error = mock.MagicMock(return_value=(error, "fake"))
-        self._busMessageCb(None, message)
-
-
-class TestPipeline(common.TestCase):
-
-    def pipeline_died_cb(self, pipeline):
-        self.pipeline_died = True
+        pipeline._busMessageCb(None, message)
 
     def test_recovery(self):
-        pipe = MockedPipeline()
-        pipe.pause()
-
-        self.pipeline_died = False
-        pipe.connect("died", self.pipeline_died_cb)
-
-        states = {Gst.State.PAUSED: 1}
-        self.assertEqual(pipe.state_calls, states)
-        self.assertFalse(self.pipeline_died)
-
-        for i in range(1, pipeline.MAX_RECOVERIES + 2):
-            pipe.post_fake_error_message()
-            states = {Gst.State.PAUSED: i + 1, Gst.State.NULL: i}
-            self.assertEqual(pipe.state_calls, states)
-            self.assertEqual(pipe._attempted_recoveries, i)
-            self.assertFalse(self.pipeline_died)
-
-        states = {Gst.State.PAUSED: pipeline.MAX_RECOVERIES + 2,
-                  Gst.State.NULL: pipeline.MAX_RECOVERIES + 1}
-
-        pipe.post_fake_error_message()
-        self.assertTrue(self.pipeline_died)
-
-        states = {Gst.State.PAUSED: pipeline.MAX_RECOVERIES + 2,
-                  Gst.State.NULL: pipeline.MAX_RECOVERIES + 1}
-        self.assertEqual(pipe.state_calls, states)
-        self.assertEqual(pipe._attempted_recoveries,
-                         pipeline.MAX_RECOVERIES + 1)
+        pipe = Pipeline(common.create_pitivi_mock())
+        pipe.set_timeline(GES.Timeline())
+
+        pipeline_died_cb = mock.Mock()
+        pipe.connect("died", pipeline_died_cb)
+
+        with mock.patch.object(pipe, "set_state") as set_state:
+            pipe.pause()
+            set_state.assert_called_once_with(Gst.State.PAUSED)
+        self.assertEqual(pipe._attempted_recoveries, 0)
+        self.assertFalse(pipeline_died_cb.called)
+
+        for i in range(MAX_RECOVERIES + 1):
+            with mock.patch.object(pipe, "set_state") as set_state:
+                set_state.return_value = Gst.StateChangeReturn.SUCCESS
+                self.post_fake_error_message(pipe)
+                set_state.assert_has_calls([mock.call(Gst.State.NULL),
+                                            mock.call(Gst.State.PAUSED)])
+                self.assertEqual(set_state.call_count, 2)
+            self.assertEqual(pipe._attempted_recoveries, i + 1)
+            self.assertFalse(pipeline_died_cb.called)
+
+        with mock.patch.object(pipe, "set_state") as set_state:
+            self.post_fake_error_message(pipe)
+            set_state.assert_not_called()
+        self.assertTrue(pipeline_died_cb.called)
+        self.assertEqual(pipe._attempted_recoveries, MAX_RECOVERIES + 1)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]