From ee6495e08f3416e28df57471f6d850854c6c9043 Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Wed, 5 Dec 2012 22:44:42 +0200 Subject: [PATCH] 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"'