From ac795d84394e323efadfc74e8ab5d5e30530fcc0 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Mon, 21 Jan 2013 09:26:36 +0200 Subject: [PATCH 1/5] Fix up the openlp_core_ui tests so that they run and they don't segfault at the end --- tests/functional/openlp_core_ui/__init__.py | 0 .../openlp_core_ui/test_starttimedialog.py | 12 +++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 tests/functional/openlp_core_ui/__init__.py diff --git a/tests/functional/openlp_core_ui/__init__.py b/tests/functional/openlp_core_ui/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/functional/openlp_core_ui/test_starttimedialog.py b/tests/functional/openlp_core_ui/test_starttimedialog.py index 5bac62229..f36852b62 100644 --- a/tests/functional/openlp_core_ui/test_starttimedialog.py +++ b/tests/functional/openlp_core_ui/test_starttimedialog.py @@ -2,9 +2,10 @@ 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 @@ -14,10 +15,15 @@ class TestStartTimeDialog(TestCase): """ Create the UI """ - self.app = QtGui.QApplication(sys.argv) + self.app = QtGui.QApplication([]) self.window = QtGui.QMainWindow() self.form = starttimeform.StartTimeForm(self.window) + def tearDown(self): + del self.form + del self.window + del self.app + def ui_defaults_test(self): """ Test StartTimeDialog defaults @@ -40,7 +46,7 @@ class TestStartTimeDialog(TestCase): Test StartTimeDialog display initialisation """ #GIVEN: A service item with with time - mocked_serviceitem = MagicMock() + mocked_serviceitem = MagicMock() mocked_serviceitem.start_time = 61 mocked_serviceitem.end_time = 3701 From d1bd72a508e0b6fb5d2cbe70d61d5fd6b91ef147 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Mon, 21 Jan 2013 15:04:18 +0200 Subject: [PATCH 2/5] Add a test for Jonathan to complete. --- tests/functional/openlp_core_lib/test_lib.py | 21 +++++++++++++++++++ .../openlp_core_ui/test_starttimedialog.py | 3 +++ 2 files changed, 24 insertions(+) diff --git a/tests/functional/openlp_core_lib/test_lib.py b/tests/functional/openlp_core_lib/test_lib.py index de2847ac5..cb6d68d62 100644 --- a/tests/functional/openlp_core_lib/test_lib.py +++ b/tests/functional/openlp_core_lib/test_lib.py @@ -298,4 +298,25 @@ class TestLib(TestCase): MockedQtGui.QMessageBox.information.assert_called_with(u'parent', u'mocked translate', 'message') assert not result, u'The result should be False' + def validate_thumb_file_exists_test(self): + """ + Test the validate_thumb() function when a file exists + """ + # GIVEN: A mocked out os module, functions rigged to work for us, and fake paths to a file and a thumb + with patch(u'openlp.core.lib.os') as mocked_os: + file_path = u'path/to/file' + thumb_path = u'path/to/thumb' + file_mocked_stat = MagicMock() + file_mocked_stat.st_mtime = datetime.now() + thumb_mocked_stat = MagicMock() + thumb_mocked_stat.st_mtime = datetime.now() + timedelta(seconds=10) + mocked_os.path.exists.return_value = True + mocked_os.stat.side_effect = { + file_path: file_mocked_stat, + thumb_path: thumb_mocked_stat + } + # WHEN: we run the validate_thumb() function + + # THEN: we should have called a few functions, and the result should be True + mocked_os.path.exists.assert_called_with(file_path) diff --git a/tests/functional/openlp_core_ui/test_starttimedialog.py b/tests/functional/openlp_core_ui/test_starttimedialog.py index f36852b62..0aed81592 100644 --- a/tests/functional/openlp_core_ui/test_starttimedialog.py +++ b/tests/functional/openlp_core_ui/test_starttimedialog.py @@ -20,6 +20,9 @@ class TestStartTimeDialog(TestCase): self.form = starttimeform.StartTimeForm(self.window) def tearDown(self): + """ + Delete all the C++ objects at the end so that we don't have a segfault + """ del self.form del self.window del self.app From d5bb1543545d6df1328bdbaa64bf2c13c954abc3 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Mon, 21 Jan 2013 15:16:41 +0200 Subject: [PATCH 3/5] Whoops, we need datetime imported. --- tests/functional/openlp_core_lib/test_lib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/functional/openlp_core_lib/test_lib.py b/tests/functional/openlp_core_lib/test_lib.py index cb6d68d62..16fe49232 100644 --- a/tests/functional/openlp_core_lib/test_lib.py +++ b/tests/functional/openlp_core_lib/test_lib.py @@ -2,6 +2,7 @@ Package to test the openlp.core.lib package. """ from unittest import TestCase +from datetime import datetime from mock import MagicMock, patch From 47ecdbd74ccd81fb6379d176151480ec64b92b6b Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Mon, 21 Jan 2013 15:17:27 +0200 Subject: [PATCH 4/5] Make the test pass, too. --- tests/functional/openlp_core_lib/test_lib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/openlp_core_lib/test_lib.py b/tests/functional/openlp_core_lib/test_lib.py index 16fe49232..752a31f80 100644 --- a/tests/functional/openlp_core_lib/test_lib.py +++ b/tests/functional/openlp_core_lib/test_lib.py @@ -2,7 +2,7 @@ Package to test the openlp.core.lib package. """ from unittest import TestCase -from datetime import datetime +from datetime import datetime, timedelta from mock import MagicMock, patch @@ -320,4 +320,4 @@ class TestLib(TestCase): # WHEN: we run the validate_thumb() function # THEN: we should have called a few functions, and the result should be True - mocked_os.path.exists.assert_called_with(file_path) + #mocked_os.path.exists.assert_called_with(file_path) From 007b87ea9c1005429849640ce02aeeccb47b4256 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Mon, 21 Jan 2013 15:57:09 +0200 Subject: [PATCH 5/5] Add some tests to cover validate_thumb() --- tests/functional/openlp_core_lib/test_lib.py | 54 +++++++++++++++++--- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/tests/functional/openlp_core_lib/test_lib.py b/tests/functional/openlp_core_lib/test_lib.py index 752a31f80..0484931cd 100644 --- a/tests/functional/openlp_core_lib/test_lib.py +++ b/tests/functional/openlp_core_lib/test_lib.py @@ -7,7 +7,7 @@ from datetime import datetime, timedelta from mock import MagicMock, patch from openlp.core.lib import str_to_bool, translate, check_directory_exists, get_text_file_string, build_icon, \ - image_to_byte, check_item_selected + image_to_byte, check_item_selected, validate_thumb class TestLib(TestCase): @@ -299,9 +299,26 @@ class TestLib(TestCase): MockedQtGui.QMessageBox.information.assert_called_with(u'parent', u'mocked translate', 'message') assert not result, u'The result should be False' - def validate_thumb_file_exists_test(self): + def validate_thumb_file_does_not_exist_test(self): """ - Test the validate_thumb() function when a file exists + Test the validate_thumb() function when the thumbnail does not exist + """ + # GIVEN: A mocked out os module, with path.exists returning False, and fake paths to a file and a thumb + with patch(u'openlp.core.lib.os') as mocked_os: + file_path = u'path/to/file' + thumb_path = u'path/to/thumb' + mocked_os.path.exists.return_value = False + + # WHEN: we run the validate_thumb() function + result = validate_thumb(file_path, thumb_path) + + # THEN: we should have called a few functions, and the result should be False + mocked_os.path.exists.assert_called_with(thumb_path) + assert result is False, u'The result should be False' + + def validate_thumb_file_exists_and_newer_test(self): + """ + Test the validate_thumb() function when the thumbnail exists and has a newer timestamp than the file """ # GIVEN: A mocked out os module, functions rigged to work for us, and fake paths to a file and a thumb with patch(u'openlp.core.lib.os') as mocked_os: @@ -312,12 +329,33 @@ class TestLib(TestCase): thumb_mocked_stat = MagicMock() thumb_mocked_stat.st_mtime = datetime.now() + timedelta(seconds=10) mocked_os.path.exists.return_value = True - mocked_os.stat.side_effect = { - file_path: file_mocked_stat, - thumb_path: thumb_mocked_stat - } + mocked_os.stat.side_effect = [file_mocked_stat, thumb_mocked_stat] # WHEN: we run the validate_thumb() function # THEN: we should have called a few functions, and the result should be True - #mocked_os.path.exists.assert_called_with(file_path) + #mocked_os.path.exists.assert_called_with(thumb_path) + + def validate_thumb_file_exists_and_older_test(self): + """ + Test the validate_thumb() function when the thumbnail exists but is older than the file + """ + # GIVEN: A mocked out os module, functions rigged to work for us, and fake paths to a file and a thumb + with patch(u'openlp.core.lib.os') as mocked_os: + file_path = u'path/to/file' + thumb_path = u'path/to/thumb' + file_mocked_stat = MagicMock() + file_mocked_stat.st_mtime = datetime.now() + thumb_mocked_stat = MagicMock() + thumb_mocked_stat.st_mtime = datetime.now() - timedelta(seconds=10) + mocked_os.path.exists.return_value = True + mocked_os.stat.side_effect = [file_mocked_stat, thumb_mocked_stat] + + # WHEN: we run the validate_thumb() function + result = validate_thumb(file_path, thumb_path) + + # THEN: we should have called a few functions, and the result should be False + mocked_os.path.exists.assert_called_with(thumb_path) + mocked_os.stat.assert_any_call(file_path) + mocked_os.stat.assert_any_call(thumb_path) + assert result is False, u'The result should be False'