[pitivi] tests_: Cleanup tests and enhance common.py



commit 1a475da35e599c0ba13f5e78f09c2f831eda55c0
Author: Thibault Saunier <tsaunier gnome org>
Date:   Wed Jan 6 14:51:35 2016 +0100

    tests_: Cleanup tests and enhance common.py
    
    Reviewed-by: Alex Băluț <alexandru balut gmail com>
    Differential Revision: https://phabricator.freedesktop.org/D622

 tests/__init__.py               |    3 +-
 tests/common.py                 |   44 ++++++++++++++++++++++++++++++++------
 tests/runtests.py               |    5 +++-
 tests/test_project.py           |   23 ++++++++++---------
 tests/test_timeline_layer.py    |    2 +-
 tests/test_timeline_timeline.py |    7 +++--
 tests/test_undo_timeline.py     |    2 +-
 7 files changed, 61 insertions(+), 25 deletions(-)
---
diff --git a/tests/__init__.py b/tests/__init__.py
index 7ec5f82..5c16f03 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
 
 from . import runtests
-runtests.setup()
+if not runtests.setup():
+    raise ImportError("Could not setup testsuite")
diff --git a/tests/common.py b/tests/common.py
index de0c81f..9cfdedd 100644
--- a/tests/common.py
+++ b/tests/common.py
@@ -2,19 +2,49 @@
 A collection of objects to use for testing
 """
 
-from gi.repository import Gst
 import os
 import gc
 import unittest
+import tempfile
+
+from gi.repository import Gst
+
+from unittest import mock
+from pitivi import check
 
+from pitivi.application import Pitivi
 from pitivi.utils.timeline import Selected
+from pitivi.utils.loggable import Loggable
+
+detect_leaks = os.environ.get("PITIVI_TEST_DETECT_LEAKS", "0") not in ("0", "")
+os.environ["PITIVI_USER_CACHE_DIR"] = tempfile.mkdtemp("pitiviTestsuite")
+
+
+def cleanPitiviMock(ptv):
+    ptv.settings = None
 
-detect_leaks = os.environ.get("PITIVI_TEST_DETECT_LEAKS", "1") not in ("0", "")
 
+def getPitiviMock(settings=None):
+    ptv = mock.MagicMock()
 
-class TestCase(unittest.TestCase):
+    ptv.write_action = mock.MagicMock(spec=Pitivi.write_action)
+    check.check_requirements()
+
+    if not settings:
+        settings = mock.MagicMock()
+
+    ptv.settings = settings
+
+    return ptv
+
+
+class TestCase(unittest.TestCase, Loggable):
     _tracked_types = (Gst.MiniObject, Gst.Element, Gst.Pad, Gst.Caps)
 
+    def __init__(self, *args):
+        Loggable.__init__(self)
+        unittest.TestCase.__init__(self, *args)
+
     def gctrack(self):
         self.gccollect()
         self._tracked = []
@@ -77,10 +107,10 @@ class TestCase(unittest.TestCase):
         self._result = result
         unittest.TestCase.run(self, result)
 
-    @staticmethod
-    def getSampleUri(sample):
-        dir = os.path.dirname(os.path.abspath(__file__))
-        return "file://%s/samples/%s" % (dir, sample)
+
+def getSampleUri(sample):
+    assets_dir = os.path.dirname(os.path.abspath(__file__))
+    return "file://%s/samples/%s" % (assets_dir, sample)
 
 
 class SignalMonitor(object):
diff --git a/tests/runtests.py b/tests/runtests.py
index a50f93b..e7c5aba 100644
--- a/tests/runtests.py
+++ b/tests/runtests.py
@@ -46,6 +46,7 @@ def get_build_dir():
 
 
 def setup():
+    res = True
     # Make available to configure.py the top level dir.
     pitivi_dir = get_pitivi_dir()
     os.environ.setdefault('PITIVI_TOP_LEVEL_DIR', pitivi_dir)
@@ -58,11 +59,13 @@ def setup():
     # Make sure the modules are initialized correctly.
     from pitivi import check
     check.initialize_modules()
-    assert(check.check_requirements())
+    res = check.check_requirements()
 
     from pitivi.utils import loggable as log
     log.init('PITIVI_DEBUG')
 
+    return res
+
 if __name__ == "__main__":
     setup()
 
diff --git a/tests/test_project.py b/tests/test_project.py
index 229d84e..efa6448 100644
--- a/tests/test_project.py
+++ b/tests/test_project.py
@@ -22,21 +22,20 @@ import os
 import tempfile
 import time
 
-from unittest import TestCase
-
+from unittest import TestCase, mock
 from gi.repository import GES
 from gi.repository import GLib
 from gi.repository import Gst
 
-from tests import common
-
 from pitivi.application import Pitivi
 from pitivi.project import ProjectManager, Project
 from pitivi.utils.misc import uri_is_reachable
 
+from tests import common
+
 
 def _createRealProject(name=None):
-    app = Pitivi()
+    app = common.getPitiviMock()
     project_manager = ProjectManager(app)
     project_manager.newBlankProject()
     project = project_manager.current_project
@@ -88,7 +87,8 @@ class ProjectManagerListener(object):
 class TestProjectManager(TestCase):
 
     def setUp(self):
-        self.manager = ProjectManager(None)
+        app = mock.MagicMock()
+        self.manager = ProjectManager(app)
         self.listener = ProjectManagerListener(self.manager)
         self.signals = self.listener.signals
 
@@ -380,7 +380,7 @@ class TestProjectLoading(common.TestCase):
         # Create a blank project and add an asset.
         project = _createRealProject()
         result = [False, False, False]
-        uris = [self.getSampleUri("tears_of_steel.webm")]
+        uris = [common.getSampleUri("tears_of_steel.webm")]
         project.connect("loaded", loaded, self.mainloop, result, uris)
         project.connect("done-importing", added, self.mainloop, result, uris)
 
@@ -435,9 +435,9 @@ class TestProjectSettings(common.TestCase):
         self.assertTrue(project._has_default_video_settings)
         self.assertTrue(project._has_default_audio_settings)
 
-        uris = [self.getSampleUri("flat_colour1_640x480.png"),
-                self.getSampleUri("tears_of_steel.webm"),
-                self.getSampleUri("1sec_simpsons_trailer.mp4")]
+        uris = [common.getSampleUri("flat_colour1_640x480.png"),
+                common.getSampleUri("tears_of_steel.webm"),
+                common.getSampleUri("1sec_simpsons_trailer.mp4")]
         project.connect("loaded", loaded, self.mainloop, uris)
         project.connect("done-importing", added, self.mainloop)
 
@@ -462,7 +462,8 @@ class TestProjectSettings(common.TestCase):
         self.assertEqual(Gst.Fraction(1, 1), project.videopar)
 
     def testLoad(self):
-        project = Project(uri="fake.xges", app=None)
+        ptv = common.getPitiviMock()
+        project = Project(uri="fake.xges", app=ptv)
         self.assertFalse(project._has_default_video_settings)
         self.assertFalse(project._has_default_audio_settings)
 
diff --git a/tests/test_timeline_layer.py b/tests/test_timeline_layer.py
index aeb4817..7e560c1 100644
--- a/tests/test_timeline_layer.py
+++ b/tests/test_timeline_layer.py
@@ -46,7 +46,7 @@ class TestLayer(common.TestCase):
 
     def testCheckMediaTypesWhenNoUI(self):
         bLayer = GES.Layer()
-        png = self.getSampleUri("flat_colour1_640x480.png")
+        png = common.getSampleUri("flat_colour1_640x480.png")
         video_clip = GES.UriClipAsset.request_sync(png).extract()
         self.assertTrue(bLayer.add_clip(video_clip))
         self.assertEqual(1, len(bLayer.get_clips()))
diff --git a/tests/test_timeline_timeline.py b/tests/test_timeline_timeline.py
index aac5834..883add8 100644
--- a/tests/test_timeline_timeline.py
+++ b/tests/test_timeline_timeline.py
@@ -18,7 +18,7 @@
 # Boston, MA 02110-1301, USA.
 
 from unittest import TestCase, mock
-
+from tests.common import getPitiviMock
 from gi.repository import Gdk
 
 from pitivi.project import Project, ProjectManager
@@ -34,10 +34,11 @@ THICK = ui.LAYER_HEIGHT
 class TestLayers(TestCase):
 
     def createTimeline(self, layers_heights):
-        project_manager = ProjectManager(app=None)
+        app = getPitiviMock()
+        project_manager = ProjectManager(app)
         project_manager.newBlankProject()
         project = project_manager.current_project
-        timeline = Timeline(container=None, app=None)
+        timeline = Timeline(container=mock.MagicMock(), app=app)
         timeline.get_parent = mock.MagicMock()
         timeline.setProject(project)
         y = 0
diff --git a/tests/test_undo_timeline.py b/tests/test_undo_timeline.py
index 900b601..6358462 100644
--- a/tests/test_undo_timeline.py
+++ b/tests/test_undo_timeline.py
@@ -315,7 +315,7 @@ class TestTimelineUndo(TestCase):
         self.assertEqual(20, clip1.get_priority())
 
     def testUngroup(self):
-        uri = common.TestCase.getSampleUri("tears_of_steel.webm")
+        uri = common.getSampleUri("tears_of_steel.webm")
         asset = GES.UriClipAsset.request_sync(uri)
         clip1 = asset.extract()
         self.layer.add_clip(clip1)


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