← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~raoul-snyman/openlp/tests into lp:openlp

 

Raoul Snyman has proposed merging lp:~raoul-snyman/openlp/tests into lp:openlp.

Requested reviews:
  OpenLP Core (openlp-core)

For more details, see:
https://code.launchpad.net/~raoul-snyman/openlp/tests/+merge/144036

Written some more tests.

I have written some more tests, bringing our total number of tests up to a whopping 25. Some of the tests are not brilliant, thanks to the nature of what we're doing inside the functions, but they're better than nothing.

I also moved the resources directory to the top level tests directory, so that resources can be shared across tests, and updated the tests affected by that move.
-- 
https://code.launchpad.net/~raoul-snyman/openlp/tests/+merge/144036
Your team OpenLP Core is requested to review the proposed merge of lp:~raoul-snyman/openlp/tests into lp:openlp.
=== modified file '.bzrignore'
--- .bzrignore	2012-03-23 20:52:07 +0000
+++ .bzrignore	2013-01-20 21:30:26 +0000
@@ -23,3 +23,5 @@
 openlp.cfg
 .idea
 openlp.pro
+.kdev4
+tests.kdev4

=== removed directory 'tests/functional/openlp_core_lib/resources'
=== removed file 'tests/functional/openlp_core_lib/resources/church.jpg'
Binary files tests/functional/openlp_core_lib/resources/church.jpg	2013-01-02 21:50:20 +0000 and tests/functional/openlp_core_lib/resources/church.jpg	1970-01-01 00:00:00 +0000 differ
=== modified file 'tests/functional/openlp_core_lib/test_lib.py'
--- tests/functional/openlp_core_lib/test_lib.py	2013-01-08 06:20:49 +0000
+++ tests/functional/openlp_core_lib/test_lib.py	2013-01-20 21:30:26 +0000
@@ -5,7 +5,8 @@
 
 from mock import MagicMock, patch
 
-from openlp.core.lib import str_to_bool, translate, check_directory_exists, get_text_file_string
+from openlp.core.lib import str_to_bool, translate, check_directory_exists, get_text_file_string, build_icon, \
+    image_to_byte, check_item_selected
 
 class TestLib(TestCase):
 
@@ -197,3 +198,104 @@
         """
         assert True, u'Impossible to test due to conflicts when mocking out the "open" function'
 
+    def build_icon_with_qicon_test(self):
+        """
+        Test the build_icon() function with a QIcon instance
+        """
+        with patch(u'openlp.core.lib.QtGui') as MockedQtGui:
+            # GIVEN: A mocked QIcon
+            MockedQtGui.QIcon = MagicMock
+            mocked_icon = MockedQtGui.QIcon()
+
+            # WHEN: We pass a QIcon instance in
+            result = build_icon(mocked_icon)
+
+            # THEN: The result should be our mocked QIcon
+            assert result is mocked_icon, u'The result should be the mocked QIcon'
+
+    def build_icon_with_resource_test(self):
+        """
+        Test the build_icon() function with a resource URI
+        """
+        with patch(u'openlp.core.lib.QtGui') as MockedQtGui, \
+             patch(u'openlp.core.lib.QtGui.QPixmap') as MockedQPixmap:
+            # GIVEN: A mocked QIcon and a mocked QPixmap
+            MockedQtGui.QIcon = MagicMock
+            MockedQtGui.QIcon.Normal = 1
+            MockedQtGui.QIcon.Off = 2
+            MockedQPixmap.return_value = u'mocked_pixmap'
+            resource_uri = u':/resource/uri'
+
+            # WHEN: We pass a QIcon instance in
+            result = build_icon(resource_uri)
+
+            # THEN: The result should be our mocked QIcon
+            MockedQPixmap.assert_called_with(resource_uri)
+            # There really should be more assert statements here but due to type checking and things they all break. The
+            # best we can do is to assert that we get back a MagicMock object.
+            assert isinstance(result, MagicMock), u'The result should be a MagicMock, because we mocked it out'
+
+    def image_to_byte_test(self):
+        """
+        Test the image_to_byte() function
+        """
+        with patch(u'openlp.core.lib.QtCore') as MockedQtCore:
+            # GIVEN: A set of mocked-out Qt classes
+            mocked_byte_array = MagicMock()
+            MockedQtCore.QByteArray.return_value = mocked_byte_array
+            mocked_byte_array.toBase64.return_value = u'base64mock'
+            mocked_buffer = MagicMock()
+            MockedQtCore.QBuffer.return_value = mocked_buffer
+            MockedQtCore.QIODevice.WriteOnly = u'writeonly'
+            mocked_image = MagicMock()
+
+            # WHEN: We convert an image to a byte array
+            result = image_to_byte(mocked_image)
+
+            # THEN: We should receive a value of u'base64mock'
+            MockedQtCore.QByteArray.assert_called_with()
+            MockedQtCore.QBuffer.assert_called_with(mocked_byte_array)
+            mocked_buffer.open.assert_called_with(u'writeonly')
+            mocked_image.save.assert_called_with(mocked_buffer, "PNG")
+            mocked_byte_array.toBase64.assert_called_with()
+            assert result == u'base64mock', u'The result should be the return value of the mocked out base64 method'
+
+    def check_item_selected_true_test(self):
+        """
+        Test that the check_item_selected() function returns True when there are selected indexes.
+        """
+        # GIVEN: A mocked out QtGui module and a list widget with selected indexes
+        MockedQtGui = patch(u'openlp.core.lib.QtGui')
+        mocked_list_widget = MagicMock()
+        mocked_list_widget.selectedIndexes.return_value = True
+        message = u'message'
+
+        # WHEN: We check if there are selected items
+        result = check_item_selected(mocked_list_widget, message)
+
+        # THEN: The selectedIndexes function should have been called and the result should be true
+        mocked_list_widget.selectedIndexes.assert_called_with()
+        assert result, u'The result should be True'
+
+    def check_item_selected_false_test(self):
+        """
+        Test that the check_item_selected() function returns False when there are no selected indexes.
+        """
+        # GIVEN: A mocked out QtGui module and a list widget with selected indexes
+        with patch(u'openlp.core.lib.QtGui') as MockedQtGui, \
+             patch(u'openlp.core.lib.translate') as mocked_translate:
+            mocked_translate.return_value = u'mocked translate'
+            mocked_list_widget = MagicMock()
+            mocked_list_widget.selectedIndexes.return_value = False
+            mocked_list_widget.parent.return_value = u'parent'
+            message = u'message'
+
+            # WHEN: We check if there are selected items
+            result = check_item_selected(mocked_list_widget, message)
+
+            # THEN: The selectedIndexes function should have been called and the result should be true
+            mocked_list_widget.selectedIndexes.assert_called_with()
+            MockedQtGui.QMessageBox.information.assert_called_with(u'parent', u'mocked translate', 'message')
+            assert not result, u'The result should be False'
+
+

=== modified file 'tests/functional/openlp_core_lib/test_serviceitem.py'
--- tests/functional/openlp_core_lib/test_serviceitem.py	2013-01-19 21:34:16 +0000
+++ tests/functional/openlp_core_lib/test_serviceitem.py	2013-01-20 21:30:26 +0000
@@ -16,7 +16,7 @@
         'r{/pk}{o}e{/o}{pp}n{/pp} of the Lord\n'
 FOOTER = [u'Arky Arky (Unknown)', u'Public Domain', u'CCLI 123456']
 
-TESTPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'resources'))
+TESTPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..', u'resources'))
 
 class TestServiceItem(TestCase):
 
@@ -160,4 +160,4 @@
         service_item.validate_item([u'png'])
 
         # THEN the service item should not be valid
-        assert service_item.is_valid is False, u'The service item is not valid'
\ No newline at end of file
+        assert service_item.is_valid is False, u'The service item is not valid'

=== removed file 'tests/functional/openlp_core_ui/starttimedialog.py'
--- tests/functional/openlp_core_ui/starttimedialog.py	2013-01-02 21:50:20 +0000
+++ tests/functional/openlp_core_ui/starttimedialog.py	1970-01-01 00:00:00 +0000
@@ -1,48 +0,0 @@
-"""
-    Package to test the openlp.core.ui package.
-"""
-import sys
-
-from unittest import TestCase
-from mock import MagicMock
-from openlp.core.ui import starttimeform
-from PyQt4 import QtCore, QtGui, QtTest
-
-class TestStartTimeDialog(TestCase):
-
-    def setUp(self):
-        """
-        Create the UI
-        """
-        self.app = QtGui.QApplication(sys.argv)
-        self.window = QtGui.QMainWindow()
-        self.form = starttimeform.StartTimeForm(self.window)
-
-    def ui_defaults_test(self):
-        """
-        Test StartTimeDialog defaults
-        """
-        self.assertEqual(self.form.hourSpinBox.minimum(), 0)
-        self.assertEqual(self.form.hourSpinBox.maximum(), 4)
-        self.assertEqual(self.form.minuteSpinBox.minimum(), 0)
-        self.assertEqual(self.form.minuteSpinBox.maximum(), 59)
-        self.assertEqual(self.form.secondSpinBox.minimum(), 0)
-        self.assertEqual(self.form.secondSpinBox.maximum(), 59)
-        self.assertEqual(self.form.hourFinishSpinBox.minimum(), 0)
-        self.assertEqual(self.form.hourFinishSpinBox.maximum(), 4)
-        self.assertEqual(self.form.minuteFinishSpinBox.minimum(), 0)
-        self.assertEqual(self.form.minuteFinishSpinBox.maximum(), 59)
-        self.assertEqual(self.form.secondFinishSpinBox.minimum(), 0)
-        self.assertEqual(self.form.secondFinishSpinBox.maximum(), 59)
-
-    def time_display_test(self):
-        """
-        Test StartTimeDialog display initialisation
-        """
-        #GIVEN: A service item with with time
-        mocked_serviceitem =  MagicMock()
-        mocked_serviceitem.start_time = 61
-        mocked_serviceitem.end_time = 3701
-
-        self.form.item = mocked_serviceitem
-        #self.form.exec_()
\ No newline at end of file

=== added file 'tests/functional/openlp_core_ui/test_starttimedialog.py'
--- tests/functional/openlp_core_ui/test_starttimedialog.py	1970-01-01 00:00:00 +0000
+++ tests/functional/openlp_core_ui/test_starttimedialog.py	2013-01-20 21:30:26 +0000
@@ -0,0 +1,48 @@
+"""
+Package to test the openlp.core.ui package.
+"""
+import sys
+
+from unittest import TestCase
+from mock import MagicMock
+from openlp.core.ui import starttimeform
+from PyQt4 import QtCore, QtGui, QtTest
+
+class TestStartTimeDialog(TestCase):
+
+    def setUp(self):
+        """
+        Create the UI
+        """
+        self.app = QtGui.QApplication(sys.argv)
+        self.window = QtGui.QMainWindow()
+        self.form = starttimeform.StartTimeForm(self.window)
+
+    def ui_defaults_test(self):
+        """
+        Test StartTimeDialog defaults
+        """
+        self.assertEqual(self.form.hourSpinBox.minimum(), 0)
+        self.assertEqual(self.form.hourSpinBox.maximum(), 4)
+        self.assertEqual(self.form.minuteSpinBox.minimum(), 0)
+        self.assertEqual(self.form.minuteSpinBox.maximum(), 59)
+        self.assertEqual(self.form.secondSpinBox.minimum(), 0)
+        self.assertEqual(self.form.secondSpinBox.maximum(), 59)
+        self.assertEqual(self.form.hourFinishSpinBox.minimum(), 0)
+        self.assertEqual(self.form.hourFinishSpinBox.maximum(), 4)
+        self.assertEqual(self.form.minuteFinishSpinBox.minimum(), 0)
+        self.assertEqual(self.form.minuteFinishSpinBox.maximum(), 59)
+        self.assertEqual(self.form.secondFinishSpinBox.minimum(), 0)
+        self.assertEqual(self.form.secondFinishSpinBox.maximum(), 59)
+
+    def time_display_test(self):
+        """
+        Test StartTimeDialog display initialisation
+        """
+        #GIVEN: A service item with with time
+        mocked_serviceitem =  MagicMock()
+        mocked_serviceitem.start_time = 61
+        mocked_serviceitem.end_time = 3701
+
+        self.form.item = mocked_serviceitem
+        #self.form.exec_()

=== removed directory 'tests/functional/resources'
=== removed file 'tests/functional/resources/church.jpg'
Binary files tests/functional/resources/church.jpg	2012-12-30 22:10:51 +0000 and tests/functional/resources/church.jpg	1970-01-01 00:00:00 +0000 differ
=== added directory 'tests/resources'
=== added file 'tests/resources/church.jpg'
Binary files tests/resources/church.jpg	1970-01-01 00:00:00 +0000 and tests/resources/church.jpg	2013-01-20 21:30:26 +0000 differ

Follow ups