From 0450866e732b5247cdba6ed8397a6764bafe1f1b Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Wed, 5 Dec 2012 20:13:10 +0200 Subject: [PATCH 01/17] Renamed testing to tests --- {testing => tests}/conftest.py | 0 {testing => tests}/run.py | 0 {testing => tests}/test_app.py | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename {testing => tests}/conftest.py (100%) rename {testing => tests}/run.py (100%) rename {testing => tests}/test_app.py (100%) diff --git a/testing/conftest.py b/tests/conftest.py similarity index 100% rename from testing/conftest.py rename to tests/conftest.py diff --git a/testing/run.py b/tests/run.py similarity index 100% rename from testing/run.py rename to tests/run.py diff --git a/testing/test_app.py b/tests/test_app.py similarity index 100% rename from testing/test_app.py rename to tests/test_app.py From 8a4f1438784f5a2f62e826c42c108d296b80d20e Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Wed, 5 Dec 2012 20:52:31 +0200 Subject: [PATCH 02/17] First couple of (example) tests and a README file to go with it. --- tests/README.txt | 34 +++++++++++++++++ .../functional/openlp_core_utils/__init__.py | 1 + .../openlp_core_utils/test_applocation.py | 37 +++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 tests/README.txt create mode 100644 tests/functional/openlp_core_utils/__init__.py create mode 100644 tests/functional/openlp_core_utils/test_applocation.py diff --git a/tests/README.txt b/tests/README.txt new file mode 100644 index 000000000..493b5ccb5 --- /dev/null +++ b/tests/README.txt @@ -0,0 +1,34 @@ +Tests for OpenLP +================ + +This directory contains unit tests for OpenLP. The ``functional`` directory contains functional unit tests. + +Prerequisites +------------- + +In order to run the unit tests, you will need the following Python packages/libraries installed: + + - Mock + - Nose + +On Ubuntu you can simple install the python-mock and python-nose packages. Most other distributions will also have these +packages. On Windows and Mac OS X you will need to use ``pip`` or ``easy_install`` to install these packages. + +Running the Tests +----------------- + +To run the tests, navigate to the root directory of the OpenLP project, and then run the following command:: + + nosetests -v tests + +Or, to run only the functional tests, run the following command:: + + nosetests -v tests/functional + +Or, to run only a particular test suite within a file, run the following command:: + + nosetests -v tests/functional/test_applocation.py + +Finally, to only run a particular test, run the following command:: + + nosetests -v tests/functional/test_applocation.py:TestAppLocation.get_frozen_path_test diff --git a/tests/functional/openlp_core_utils/__init__.py b/tests/functional/openlp_core_utils/__init__.py new file mode 100644 index 000000000..33507f08b --- /dev/null +++ b/tests/functional/openlp_core_utils/__init__.py @@ -0,0 +1 @@ +__author__ = 'raoul' diff --git a/tests/functional/openlp_core_utils/test_applocation.py b/tests/functional/openlp_core_utils/test_applocation.py new file mode 100644 index 000000000..fd0a92fe6 --- /dev/null +++ b/tests/functional/openlp_core_utils/test_applocation.py @@ -0,0 +1,37 @@ +""" +Functional tests to test the AppLocation class and related methods. +""" +import sys +from unittest import TestCase + +from mock import patch +from PyQt4 import QtCore + +from openlp.core.utils import AppLocation, _get_frozen_path + +class TestAppLocation(TestCase): + """ + A test suite to test out various methods around the AppLocation class. + """ + def get_frozen_path_test(self): + """ + Test the _get_frozen_path() function + """ + sys.frozen = None + assert _get_frozen_path(u'frozen', u'not frozen') == u'not frozen', u'Should return "not frozen"' + sys.frozen = 1 + assert _get_frozen_path(u'frozen', u'not frozen') == u'frozen', u'Should return "frozen"' + + def get_data_path_with_custom_location_test(self): + """ + Test the AppLocation.get_data_path() method when a custom location is set in the settings + """ + with patch(u'openlp.core.utils.Settings') as mocked_class: + mocked_settings = mocked_class.return_value + mocked_settings.contains.return_value = True + mocked_settings.value.return_value.toString.return_value = u'test/dir' + data_path = AppLocation.get_data_path() + mocked_settings.contains.assert_called_with(u'advanced/data path') + mocked_settings.value.assert_called_with(u'advanced/data path') + mocked_settings.value.return_value.toString.assert_called_with() + assert data_path == u'test/dir', u'Result should be "test/dir"' From ee6495e08f3416e28df57471f6d850854c6c9043 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Wed, 5 Dec 2012 22:44:42 +0200 Subject: [PATCH 03/17] Expanded the tests. --- .../openlp_core_utils/test_applocation.py | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/tests/functional/openlp_core_utils/test_applocation.py b/tests/functional/openlp_core_utils/test_applocation.py index fd0a92fe6..a33c4bb0c 100644 --- a/tests/functional/openlp_core_utils/test_applocation.py +++ b/tests/functional/openlp_core_utils/test_applocation.py @@ -4,7 +4,7 @@ Functional tests to test the AppLocation class and related methods. import sys from unittest import TestCase -from mock import patch +from mock import patch, MagicMock from PyQt4 import QtCore from openlp.core.utils import AppLocation, _get_frozen_path @@ -17,21 +17,52 @@ class TestAppLocation(TestCase): """ Test the _get_frozen_path() function """ + # GIVEN: The sys module "without" a "frozen" attribute sys.frozen = None + # WHEN: We call _get_frozen_path() with two parameters + # THEN: The non-frozen parameter is returned assert _get_frozen_path(u'frozen', u'not frozen') == u'not frozen', u'Should return "not frozen"' + # GIVEN: The sys module *with* a "frozen" attribute sys.frozen = 1 + # WHEN: We call _get_frozen_path() with two parameters + # THEN: The frozen parameter is returned assert _get_frozen_path(u'frozen', u'not frozen') == u'frozen', u'Should return "frozen"' + def get_data_path_test(self): + """ + Test the AppLocation.get_data_path() method + """ + with patch(u'openlp.core.utils.Settings') as mocked_class, \ + patch(u'openlp.core.utils.AppLocation.get_directory') as mocked_get_directory, \ + patch(u'openlp.core.utils.check_directory_exists') as mocked_check_directory_exists,\ + patch(u'openlp.core.utils.os') as mocked_os: + # GIVEN: A mocked out Settings class and a mocked out AppLocation.get_directory() + mocked_settings = mocked_class.return_value + mocked_settings.contains.return_value = False + mocked_get_directory.return_value = u'test/dir' + mocked_check_directory_exists.return_value = True + mocked_os.path.normpath.return_value = u'test/dir' + # WHEN: we call AppLocation.get_data_path() + data_path = AppLocation.get_data_path() + # THEN: check that all the correct methods were called, and the result is correct + mocked_settings.contains.assert_called_with(u'advanced/data path') + mocked_get_directory.assert_called_with(AppLocation.DataDir) + mocked_check_directory_exists.assert_called_with(u'test/dir') + assert data_path == u'test/dir', u'Result should be "test/dir"' + def get_data_path_with_custom_location_test(self): """ Test the AppLocation.get_data_path() method when a custom location is set in the settings """ with patch(u'openlp.core.utils.Settings') as mocked_class: + # GIVEN: A mocked out Settings class which returns a custom data location mocked_settings = mocked_class.return_value mocked_settings.contains.return_value = True - mocked_settings.value.return_value.toString.return_value = u'test/dir' + mocked_settings.value.return_value.toString.return_value = u'custom/dir' + # WHEN: we call AppLocation.get_data_path() data_path = AppLocation.get_data_path() + # THEN: the mocked Settings methods were called and the value returned was our set up value mocked_settings.contains.assert_called_with(u'advanced/data path') mocked_settings.value.assert_called_with(u'advanced/data path') mocked_settings.value.return_value.toString.assert_called_with() - assert data_path == u'test/dir', u'Result should be "test/dir"' + assert data_path == u'custom/dir', u'Result should be "custom/dir"' From 0483fdc8ea02e70f249a504dd00b8f4ce1a59bfa Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Wed, 5 Dec 2012 22:55:28 +0200 Subject: [PATCH 04/17] Forgot to mock out another method. --- tests/functional/openlp_core_utils/test_applocation.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/functional/openlp_core_utils/test_applocation.py b/tests/functional/openlp_core_utils/test_applocation.py index a33c4bb0c..68c2d6f19 100644 --- a/tests/functional/openlp_core_utils/test_applocation.py +++ b/tests/functional/openlp_core_utils/test_applocation.py @@ -34,7 +34,7 @@ class TestAppLocation(TestCase): """ with patch(u'openlp.core.utils.Settings') as mocked_class, \ patch(u'openlp.core.utils.AppLocation.get_directory') as mocked_get_directory, \ - patch(u'openlp.core.utils.check_directory_exists') as mocked_check_directory_exists,\ + patch(u'openlp.core.utils.check_directory_exists') as mocked_check_directory_exists, \ patch(u'openlp.core.utils.os') as mocked_os: # GIVEN: A mocked out Settings class and a mocked out AppLocation.get_directory() mocked_settings = mocked_class.return_value @@ -54,11 +54,13 @@ class TestAppLocation(TestCase): """ Test the AppLocation.get_data_path() method when a custom location is set in the settings """ - with patch(u'openlp.core.utils.Settings') as mocked_class: + with patch(u'openlp.core.utils.Settings') as mocked_class,\ + patch(u'openlp.core.utils.os') as mocked_os: # GIVEN: A mocked out Settings class which returns a custom data location mocked_settings = mocked_class.return_value mocked_settings.contains.return_value = True mocked_settings.value.return_value.toString.return_value = u'custom/dir' + mocked_os.path.normpath.return_value = u'custom/dir' # WHEN: we call AppLocation.get_data_path() data_path = AppLocation.get_data_path() # THEN: the mocked Settings methods were called and the value returned was our set up value From ebdd1d35dc6dfb22a3f3c6493a30d0a76927d481 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Wed, 5 Dec 2012 23:22:29 +0200 Subject: [PATCH 05/17] Removed some unnecessary imports in the tests. --- tests/functional/openlp_core_utils/test_applocation.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/functional/openlp_core_utils/test_applocation.py b/tests/functional/openlp_core_utils/test_applocation.py index 68c2d6f19..2577bf797 100644 --- a/tests/functional/openlp_core_utils/test_applocation.py +++ b/tests/functional/openlp_core_utils/test_applocation.py @@ -4,8 +4,7 @@ Functional tests to test the AppLocation class and related methods. import sys from unittest import TestCase -from mock import patch, MagicMock -from PyQt4 import QtCore +from mock import patch from openlp.core.utils import AppLocation, _get_frozen_path From 745bf0e1ee99927443235837a882e8e1f783a26c Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Thu, 6 Dec 2012 12:55:12 +0200 Subject: [PATCH 06/17] More tests --- .../openlp_core_utils/test_applocation.py | 63 ++++++++++++++++--- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/tests/functional/openlp_core_utils/test_applocation.py b/tests/functional/openlp_core_utils/test_applocation.py index 2577bf797..b5d295c2b 100644 --- a/tests/functional/openlp_core_utils/test_applocation.py +++ b/tests/functional/openlp_core_utils/test_applocation.py @@ -16,16 +16,17 @@ class TestAppLocation(TestCase): """ Test the _get_frozen_path() function """ - # GIVEN: The sys module "without" a "frozen" attribute - sys.frozen = None - # WHEN: We call _get_frozen_path() with two parameters - # THEN: The non-frozen parameter is returned - assert _get_frozen_path(u'frozen', u'not frozen') == u'not frozen', u'Should return "not frozen"' - # GIVEN: The sys module *with* a "frozen" attribute - sys.frozen = 1 - # WHEN: We call _get_frozen_path() with two parameters - # THEN: The frozen parameter is returned - assert _get_frozen_path(u'frozen', u'not frozen') == u'frozen', u'Should return "frozen"' + with patch(u'openlp.core.utils.sys') as mocked_sys: + # GIVEN: The sys module "without" a "frozen" attribute + mocked_sys.frozen = None + # WHEN: We call _get_frozen_path() with two parameters + # THEN: The non-frozen parameter is returned + assert _get_frozen_path(u'frozen', u'not frozen') == u'not frozen', u'Should return "not frozen"' + # GIVEN: The sys module *with* a "frozen" attribute + mocked_sys.frozen = 1 + # WHEN: We call _get_frozen_path() with two parameters + # THEN: The frozen parameter is returned + assert _get_frozen_path(u'frozen', u'not frozen') == u'frozen', u'Should return "frozen"' def get_data_path_test(self): """ @@ -67,3 +68,45 @@ class TestAppLocation(TestCase): mocked_settings.value.assert_called_with(u'advanced/data path') mocked_settings.value.return_value.toString.assert_called_with() assert data_path == u'custom/dir', u'Result should be "custom/dir"' + + def get_section_data_path_test(self): + """ + Test the AppLocation.get_section_data_path() method + """ + with patch(u'openlp.core.utils.AppLocation.get_data_path') as mocked_get_data_path, \ + patch(u'openlp.core.utils.check_directory_exists') as mocked_check_directory_exists: + # GIVEN: A mocked out AppLocation.get_data_path() + mocked_get_data_path.return_value = u'test/dir' + mocked_check_directory_exists.return_value = True + # WHEN: we call AppLocation.get_data_path() + data_path = AppLocation.get_section_data_path(u'section') + # THEN: check that all the correct methods were called, and the result is correct + mocked_check_directory_exists.assert_called_with(u'test/dir/section') + assert data_path == u'test/dir/section', u'Result should be "test/dir/section"' + + def get_directory_for_app_dir_test(self): + """ + Test the AppLocation.get_directory() method for AppLocation.AppDir + """ + with patch(u'openlp.core.utils._get_frozen_path') as mocked_get_frozen_path: + mocked_get_frozen_path.return_value = u'app/dir' + # WHEN: We call AppLocation.get_directory + directory = AppLocation.get_directory(AppLocation.AppDir) + # THEN: + assert directory == u'app/dir', u'Directory should be "app/dir"' + + def get_directory_for_plugins_dir_test(self): + """ + Test the AppLocation.get_directory() method for AppLocation.PluginsDir + """ + with patch(u'openlp.core.utils._get_frozen_path') as mocked_get_frozen_path, \ + patch(u'openlp.core.utils.os.path.abspath') as mocked_abspath, \ + patch(u'openlp.core.utils.sys') as mocked_sys: + mocked_abspath.return_value = u'plugins/dir' + mocked_get_frozen_path.return_value = u'plugins/dir' + mocked_sys.frozen = 1 + # WHEN: We call AppLocation.get_directory + directory = AppLocation.get_directory(AppLocation.PluginsDir) + # THEN: + assert directory == u'plugins/dir', u'Directory should be "plugins/dir"' + From 392dc312232ca3320fbba8807a44871e33765793 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Thu, 6 Dec 2012 14:58:19 +0200 Subject: [PATCH 07/17] Tried to fix a problem with a test on Jenkins. --- tests/functional/openlp_core_utils/test_applocation.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/functional/openlp_core_utils/test_applocation.py b/tests/functional/openlp_core_utils/test_applocation.py index b5d295c2b..048b036d0 100644 --- a/tests/functional/openlp_core_utils/test_applocation.py +++ b/tests/functional/openlp_core_utils/test_applocation.py @@ -101,10 +101,13 @@ class TestAppLocation(TestCase): """ with patch(u'openlp.core.utils._get_frozen_path') as mocked_get_frozen_path, \ patch(u'openlp.core.utils.os.path.abspath') as mocked_abspath, \ + patch(u'openlp.core.utils.os.path.split') as mocked_split, \ patch(u'openlp.core.utils.sys') as mocked_sys: mocked_abspath.return_value = u'plugins/dir' + mocked_split.return_value = [u'openlp'] mocked_get_frozen_path.return_value = u'plugins/dir' mocked_sys.frozen = 1 + mocked_sys.argv = ['openlp'] # WHEN: We call AppLocation.get_directory directory = AppLocation.get_directory(AppLocation.PluginsDir) # THEN: From 9e898d60679eb8b466d6f4d7829164ff569e213e Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 7 Dec 2012 00:19:17 +0200 Subject: [PATCH 08/17] Yay! More tests! --- tests/functional/openlp_core_lib/__init__.py | 0 .../openlp_core_lib/test_lib_module.py | 99 +++++++++++++++++++ .../openlp_core_utils/test_applocation.py | 1 - 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/functional/openlp_core_lib/__init__.py create mode 100644 tests/functional/openlp_core_lib/test_lib_module.py diff --git a/tests/functional/openlp_core_lib/__init__.py b/tests/functional/openlp_core_lib/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/functional/openlp_core_lib/test_lib_module.py b/tests/functional/openlp_core_lib/test_lib_module.py new file mode 100644 index 000000000..f3e63d655 --- /dev/null +++ b/tests/functional/openlp_core_lib/test_lib_module.py @@ -0,0 +1,99 @@ +""" +Package to test the openlp.core.lib package. +""" +from unittest import TestCase + +from openlp.core.lib import str_to_bool + +class TestLibModule(TestCase): + + def str_to_bool_with_bool_test(self): + """ + Test the str_to_bool function with boolean input + """ + #GIVEN: A boolean value set to true + true_boolean = True + + # WHEN: We "convert" it to a bool + true_result = str_to_bool(true_boolean) + + # THEN: We should get back a True bool + assert isinstance(true_result, bool), u'The result should be a boolean' + assert true_result is True, u'The result should be True' + + #GIVEN: A boolean value set to false + false_boolean = False + + # WHEN: We "convert" it to a bool + false_result = str_to_bool(false_boolean) + + # THEN: We should get back a True bool + assert isinstance(false_result, bool), u'The result should be a boolean' + assert false_result is False, u'The result should be True' + + def str_to_bool_with_invalid_test(self): + """ + Test the str_to_bool function with a set of invalid inputs + """ + # GIVEN: An integer value + int_string = 1 + + # WHEN: we convert it to a bool + int_result = str_to_bool(int_string) + + # THEN: we should get back a false + assert int_result is False, u'The result should be False' + + # GIVEN: An string value with completely invalid input + invalid_string = u'my feet are wet' + + # WHEN: we convert it to a bool + str_result = str_to_bool(invalid_string) + + # THEN: we should get back a false + assert str_result is False, u'The result should be False' + + def str_to_bool_with_false_values_test(self): + """ + Test the str_to_bool function with a set of invalid inputs + """ + # GIVEN: A string set to "false" + false_string = u'false' + + # WHEN: we convert it to a bool + false_result = str_to_bool(false_string) + + # THEN: we should get back a false + assert false_result is False, u'The result should be False' + + # GIVEN: An string set to "NO" + no_string = u'NO' + + # WHEN: we convert it to a bool + str_result = str_to_bool(no_string) + + # THEN: we should get back a false + assert str_result is False, u'The result should be False' + + def str_to_bool_with_true_values_test(self): + """ + Test the str_to_bool function with a set of invalid inputs + """ + # GIVEN: A string set to "True" + true_string = u'True' + + # WHEN: we convert it to a bool + true_result = str_to_bool(true_string) + + # THEN: we should get back a true + assert true_result is True, u'The result should be True' + + # GIVEN: An string set to "yes" + yes_string = u'yes' + + # WHEN: we convert it to a bool + str_result = str_to_bool(yes_string) + + # THEN: we should get back a true + assert str_result is True, u'The result should be True' + diff --git a/tests/functional/openlp_core_utils/test_applocation.py b/tests/functional/openlp_core_utils/test_applocation.py index 048b036d0..f558c93f0 100644 --- a/tests/functional/openlp_core_utils/test_applocation.py +++ b/tests/functional/openlp_core_utils/test_applocation.py @@ -1,7 +1,6 @@ """ Functional tests to test the AppLocation class and related methods. """ -import sys from unittest import TestCase from mock import patch From 6a1fac1d5130bf4695b26eb97a4980db8964773d Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 7 Dec 2012 00:21:19 +0200 Subject: [PATCH 09/17] Fixed up some comments. --- tests/functional/openlp_core_lib/test_lib_module.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/openlp_core_lib/test_lib_module.py b/tests/functional/openlp_core_lib/test_lib_module.py index f3e63d655..a8d48ab5f 100644 --- a/tests/functional/openlp_core_lib/test_lib_module.py +++ b/tests/functional/openlp_core_lib/test_lib_module.py @@ -55,7 +55,7 @@ class TestLibModule(TestCase): def str_to_bool_with_false_values_test(self): """ - Test the str_to_bool function with a set of invalid inputs + Test the str_to_bool function with a set of false inputs """ # GIVEN: A string set to "false" false_string = u'false' @@ -77,7 +77,7 @@ class TestLibModule(TestCase): def str_to_bool_with_true_values_test(self): """ - Test the str_to_bool function with a set of invalid inputs + Test the str_to_bool function with a set of true inputs """ # GIVEN: A string set to "True" true_string = u'True' From 23150fefe840d412de363769be6705d1b96ea832 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 7 Dec 2012 19:47:33 +0200 Subject: [PATCH 10/17] Added a test for the translate() function. --- .../openlp_core_lib/test_lib_module.py | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/functional/openlp_core_lib/test_lib_module.py b/tests/functional/openlp_core_lib/test_lib_module.py index a8d48ab5f..d95df6ce5 100644 --- a/tests/functional/openlp_core_lib/test_lib_module.py +++ b/tests/functional/openlp_core_lib/test_lib_module.py @@ -3,7 +3,9 @@ Package to test the openlp.core.lib package. """ from unittest import TestCase -from openlp.core.lib import str_to_bool +from mock import MagicMock + +from openlp.core.lib import str_to_bool, translate class TestLibModule(TestCase): @@ -97,3 +99,22 @@ class TestLibModule(TestCase): # THEN: we should get back a true assert str_result is True, u'The result should be True' + def translate_test(self): + """ + Test the translate() function + """ + # GIVEN: A string to translate and a mocked Qt translate function + context = u'OpenLP.Tests' + text = u'Untranslated string' + comment = u'A comment' + encoding = 1 + n = 1 + mocked_translate = MagicMock(return_value=u'Translated string') + + # WHEN: we call the translate function + result = translate(context, text, comment, encoding, n, mocked_translate) + + # THEN: the translated string should be returned, and the mocked function should have been called + mocked_translate.assert_called_with(context, text, comment, encoding, n) + assert result == u'Translated string', u'The translated string should have been returned' + From 4de4a1823c1f7c0eb3f8ee1363c4c3a4f2e1e0c6 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 7 Dec 2012 20:10:24 +0200 Subject: [PATCH 11/17] Test check_directory_exists. --- .../openlp_core_lib/test_lib_module.py | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/tests/functional/openlp_core_lib/test_lib_module.py b/tests/functional/openlp_core_lib/test_lib_module.py index d95df6ce5..835b36f35 100644 --- a/tests/functional/openlp_core_lib/test_lib_module.py +++ b/tests/functional/openlp_core_lib/test_lib_module.py @@ -3,9 +3,9 @@ Package to test the openlp.core.lib package. """ from unittest import TestCase -from mock import MagicMock +from mock import MagicMock, patch -from openlp.core.lib import str_to_bool, translate +from openlp.core.lib import str_to_bool, translate, check_directory_exists class TestLibModule(TestCase): @@ -118,3 +118,28 @@ class TestLibModule(TestCase): mocked_translate.assert_called_with(context, text, comment, encoding, n) assert result == u'Translated string', u'The translated string should have been returned' + def check_directory_exists_test(self): + """ + Test the check_directory_exists() function + """ + with patch(u'openlp.core.lib.os.path.exists') as mocked_exists, \ + patch(u'openlp.core.lib.os.makedirs') as mocked_makedirs: + # GIVEN: A directory to check and a mocked out os.makedirs and os.path.exists + directory_to_check = u'existing/directory' + + # WHEN: os.path.exists returns Truew and we check to see if the directory exists + mocked_exists.return_value = True + check_directory_exists(directory_to_check) + + # THEN: Only os.path.exists should have been called + mocked_exists.assert_called_with(directory_to_check) + assert not mocked_makedirs.called, u'os.makedirs should not have been called' + + # WHEN: os.path.exists returns False and we check the directory exists + mocked_exists.return_value = False + check_directory_exists(directory_to_check) + + # THEN: Both the mocked functions should have been called + mocked_exists.assert_called_with(directory_to_check) + mocked_makedirs.assert_called_with(directory_to_check) + From 93fd4763837bbeedfea7238457b7ea0c9a023a6b Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 7 Dec 2012 21:08:28 +0200 Subject: [PATCH 12/17] Expanded the check_directory_exists() test. --- tests/functional/openlp_core_lib/test_lib_module.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/functional/openlp_core_lib/test_lib_module.py b/tests/functional/openlp_core_lib/test_lib_module.py index 835b36f35..a2ea57eed 100644 --- a/tests/functional/openlp_core_lib/test_lib_module.py +++ b/tests/functional/openlp_core_lib/test_lib_module.py @@ -143,3 +143,15 @@ class TestLibModule(TestCase): mocked_exists.assert_called_with(directory_to_check) mocked_makedirs.assert_called_with(directory_to_check) + # WHEN: os.path.exists raises an IOError + mocked_exists.side_effect = IOError() + check_directory_exists(directory_to_check) + + # THEN: We shouldn't get an exception though the mocked exists has been called + mocked_exists.assert_called_with(directory_to_check) + + # WHEN: Some other exception is raised + mocked_exists.side_effect = ValueError() + + # THEN: check_directory_exists raises an exception + self.assertRaises(ValueError, check_directory_exists, directory_to_check) From e5b8a1b1ade16426982df35f457d793b39e2e7a5 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 7 Dec 2012 22:50:38 +0200 Subject: [PATCH 13/17] Stubbed out a new test. --- .../openlp_core_lib/test_lib_module.py | 3 ++- .../openlp_core_utils/test_utils.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/functional/openlp_core_utils/test_utils.py diff --git a/tests/functional/openlp_core_lib/test_lib_module.py b/tests/functional/openlp_core_lib/test_lib_module.py index a2ea57eed..89a3301f2 100644 --- a/tests/functional/openlp_core_lib/test_lib_module.py +++ b/tests/functional/openlp_core_lib/test_lib_module.py @@ -127,7 +127,7 @@ class TestLibModule(TestCase): # GIVEN: A directory to check and a mocked out os.makedirs and os.path.exists directory_to_check = u'existing/directory' - # WHEN: os.path.exists returns Truew and we check to see if the directory exists + # WHEN: os.path.exists returns True and we check to see if the directory exists mocked_exists.return_value = True check_directory_exists(directory_to_check) @@ -154,4 +154,5 @@ class TestLibModule(TestCase): mocked_exists.side_effect = ValueError() # THEN: check_directory_exists raises an exception + mocked_exists.assert_called_with(directory_to_check) self.assertRaises(ValueError, check_directory_exists, directory_to_check) diff --git a/tests/functional/openlp_core_utils/test_utils.py b/tests/functional/openlp_core_utils/test_utils.py new file mode 100644 index 000000000..dd5c44e90 --- /dev/null +++ b/tests/functional/openlp_core_utils/test_utils.py @@ -0,0 +1,19 @@ +""" +Functional tests to test the AppLocation class and related methods. +""" +from unittest import TestCase + +from mock import patch + +from openlp.core.utils import get_filesystem_encoding + +class TestUtils(TestCase): + """ + A test suite to test out various methods around the AppLocation class. + """ + def get_filesystem_encoding_test(self): + """ + Test the get_filesystem_encoding() function + """ + assert False, u'This test needs to be written' + From 000b06ffe659afe65b7b81da640e173d79841935 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 7 Dec 2012 22:52:55 +0200 Subject: [PATCH 14/17] Moved the _get_frozen_path() test to the proper test case. --- .../openlp_core_utils/test_applocation.py | 16 ---------------- .../functional/openlp_core_utils/test_utils.py | 18 +++++++++++++++++- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/functional/openlp_core_utils/test_applocation.py b/tests/functional/openlp_core_utils/test_applocation.py index f558c93f0..999fe3d29 100644 --- a/tests/functional/openlp_core_utils/test_applocation.py +++ b/tests/functional/openlp_core_utils/test_applocation.py @@ -11,22 +11,6 @@ class TestAppLocation(TestCase): """ A test suite to test out various methods around the AppLocation class. """ - def get_frozen_path_test(self): - """ - Test the _get_frozen_path() function - """ - with patch(u'openlp.core.utils.sys') as mocked_sys: - # GIVEN: The sys module "without" a "frozen" attribute - mocked_sys.frozen = None - # WHEN: We call _get_frozen_path() with two parameters - # THEN: The non-frozen parameter is returned - assert _get_frozen_path(u'frozen', u'not frozen') == u'not frozen', u'Should return "not frozen"' - # GIVEN: The sys module *with* a "frozen" attribute - mocked_sys.frozen = 1 - # WHEN: We call _get_frozen_path() with two parameters - # THEN: The frozen parameter is returned - assert _get_frozen_path(u'frozen', u'not frozen') == u'frozen', u'Should return "frozen"' - def get_data_path_test(self): """ Test the AppLocation.get_data_path() method diff --git a/tests/functional/openlp_core_utils/test_utils.py b/tests/functional/openlp_core_utils/test_utils.py index dd5c44e90..148380cbe 100644 --- a/tests/functional/openlp_core_utils/test_utils.py +++ b/tests/functional/openlp_core_utils/test_utils.py @@ -5,7 +5,7 @@ from unittest import TestCase from mock import patch -from openlp.core.utils import get_filesystem_encoding +from openlp.core.utils import get_filesystem_encoding, _get_frozen_path class TestUtils(TestCase): """ @@ -17,3 +17,19 @@ class TestUtils(TestCase): """ assert False, u'This test needs to be written' + def get_frozen_path_test(self): + """ + Test the _get_frozen_path() function + """ + with patch(u'openlp.core.utils.sys') as mocked_sys: + # GIVEN: The sys module "without" a "frozen" attribute + mocked_sys.frozen = None + # WHEN: We call _get_frozen_path() with two parameters + # THEN: The non-frozen parameter is returned + assert _get_frozen_path(u'frozen', u'not frozen') == u'not frozen', u'Should return "not frozen"' + # GIVEN: The sys module *with* a "frozen" attribute + mocked_sys.frozen = 1 + # WHEN: We call _get_frozen_path() with two parameters + # THEN: The frozen parameter is returned + assert _get_frozen_path(u'frozen', u'not frozen') == u'frozen', u'Should return "frozen"' + From 9934a41d4816be19da05566bd9ce955341f8021b Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 7 Dec 2012 23:08:38 +0200 Subject: [PATCH 15/17] Fleshed out get_filesystem_encoding_test thanks to Jonathan C --- .../openlp_core_utils/test_utils.py | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/functional/openlp_core_utils/test_utils.py b/tests/functional/openlp_core_utils/test_utils.py index 148380cbe..ae8894661 100644 --- a/tests/functional/openlp_core_utils/test_utils.py +++ b/tests/functional/openlp_core_utils/test_utils.py @@ -15,7 +15,30 @@ class TestUtils(TestCase): """ Test the get_filesystem_encoding() function """ - assert False, u'This test needs to be written' + with patch(u'sys.getfilesystemencoding') as mocked_getfilesystemencoding, \ + patch(u'sys.getdefaultencoding') as mocked_getdefaultencoding: + # GIVEN: sys.getfilesystemencoding returns "cp1252" + mocked_getfilesystemencoding.return_value = u'cp1252' + + # WHEN: get_filesystem_encoding() is called + result = get_filesystem_encoding() + + # THEN: getdefaultencoding should have been called + mocked_getfilesystemencoding.assert_called_with() + assert not mocked_getdefaultencoding.called + assert result == u'cp1252', u'The result should be "cp1252"' + + # GIVEN: sys.getfilesystemencoding returns None and sys.getdefaultencoding returns "utf-8" + mocked_getfilesystemencoding.return_value = None + mocked_getdefaultencoding.return_value = u'utf-8' + + # WHEN: get_filesystem_encoding() is called + result = get_filesystem_encoding() + + # THEN: getdefaultencoding should have been called + mocked_getfilesystemencoding.assert_called_with() + mocked_getdefaultencoding.assert_called_with() + assert result == u'utf-8', u'The result should be "utf-8"' def get_frozen_path_test(self): """ From 25c64d5651be8b4ee4ccfb4f7583fff6d5b04f1f Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 7 Dec 2012 23:15:10 +0200 Subject: [PATCH 16/17] Renamed some tests. --- .../openlp_core_lib/{test_lib_module.py => test_lib.py} | 2 +- tests/functional/openlp_core_utils/test_applocation.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename tests/functional/openlp_core_lib/{test_lib_module.py => test_lib.py} (99%) diff --git a/tests/functional/openlp_core_lib/test_lib_module.py b/tests/functional/openlp_core_lib/test_lib.py similarity index 99% rename from tests/functional/openlp_core_lib/test_lib_module.py rename to tests/functional/openlp_core_lib/test_lib.py index 89a3301f2..2edc9d462 100644 --- a/tests/functional/openlp_core_lib/test_lib_module.py +++ b/tests/functional/openlp_core_lib/test_lib.py @@ -7,7 +7,7 @@ from mock import MagicMock, patch from openlp.core.lib import str_to_bool, translate, check_directory_exists -class TestLibModule(TestCase): +class TestLib(TestCase): def str_to_bool_with_bool_test(self): """ diff --git a/tests/functional/openlp_core_utils/test_applocation.py b/tests/functional/openlp_core_utils/test_applocation.py index 999fe3d29..2d3c83e5a 100644 --- a/tests/functional/openlp_core_utils/test_applocation.py +++ b/tests/functional/openlp_core_utils/test_applocation.py @@ -5,7 +5,7 @@ from unittest import TestCase from mock import patch -from openlp.core.utils import AppLocation, _get_frozen_path +from openlp.core.utils import AppLocation class TestAppLocation(TestCase): """ From 94224beed5298832830ca861fb9fa3d8fab9c320 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 7 Dec 2012 23:38:02 +0200 Subject: [PATCH 17/17] Fully qualified the module path to patch. --- tests/functional/openlp_core_utils/test_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/openlp_core_utils/test_utils.py b/tests/functional/openlp_core_utils/test_utils.py index ae8894661..2e826bc61 100644 --- a/tests/functional/openlp_core_utils/test_utils.py +++ b/tests/functional/openlp_core_utils/test_utils.py @@ -15,8 +15,8 @@ class TestUtils(TestCase): """ Test the get_filesystem_encoding() function """ - with patch(u'sys.getfilesystemencoding') as mocked_getfilesystemencoding, \ - patch(u'sys.getdefaultencoding') as mocked_getdefaultencoding: + with patch(u'openlp.core.utils.sys.getfilesystemencoding') as mocked_getfilesystemencoding, \ + patch(u'openlp.core.utils.sys.getdefaultencoding') as mocked_getdefaultencoding: # GIVEN: sys.getfilesystemencoding returns "cp1252" mocked_getfilesystemencoding.return_value = u'cp1252'