openlp/tests/functional/openlp_core_utils/test_applocation.py

148 lines
6.8 KiB
Python
Raw Normal View History

"""
Functional tests to test the AppLocation class and related methods.
"""
2013-02-27 12:03:17 +00:00
import copy
from unittest import TestCase
from mock import patch
2012-12-07 21:15:10 +00:00
from openlp.core.utils import AppLocation
2013-02-27 12:03:17 +00:00
FILE_LIST = [u'file1', u'file2', u'file3.txt', u'file4.txt', u'file5.mp3', u'file6.mp3']
class TestAppLocation(TestCase):
"""
A test suite to test out various methods around the AppLocation class.
"""
2012-12-05 20:44:42 +00:00
def get_data_path_test(self):
"""
Test the AppLocation.get_data_path() method
"""
with patch(u'openlp.core.utils.applocation.Settings') as mocked_class, \
2013-02-27 12:03:17 +00:00
patch(u'openlp.core.utils.AppLocation.get_directory') as mocked_get_directory, \
patch(u'openlp.core.utils.applocation.check_directory_exists') as mocked_check_directory_exists, \
patch(u'openlp.core.utils.applocation.os') as mocked_os:
2012-12-05 20:44:42 +00:00
# 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'
2013-04-05 19:37:15 +00:00
2012-12-05 20:44:42 +00:00
# WHEN: we call AppLocation.get_data_path()
data_path = AppLocation.get_data_path()
2013-04-05 19:37:15 +00:00
2012-12-05 20:44:42 +00:00
# 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.applocation.Settings') as mocked_class,\
2013-02-27 12:03:17 +00:00
patch(u'openlp.core.utils.applocation.os') as mocked_os:
2012-12-05 20:44:42 +00:00
# GIVEN: A mocked out Settings class which returns a custom data location
mocked_settings = mocked_class.return_value
mocked_settings.contains.return_value = True
2012-12-05 20:44:42 +00:00
mocked_settings.value.return_value.toString.return_value = u'custom/dir'
2012-12-05 20:55:28 +00:00
mocked_os.path.normpath.return_value = u'custom/dir'
2013-04-05 19:37:15 +00:00
2012-12-05 20:44:42 +00:00
# WHEN: we call AppLocation.get_data_path()
data_path = AppLocation.get_data_path()
2013-04-05 19:37:15 +00:00
2012-12-05 20:44:42 +00:00
# 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')
2013-01-30 18:42:22 +00:00
mocked_settings.value.assert_called_with(u'advanced/data path')
2012-12-05 20:44:42 +00:00
assert data_path == u'custom/dir', u'Result should be "custom/dir"'
2012-12-06 10:55:12 +00:00
2013-02-27 12:03:17 +00:00
def get_files_no_section_no_extension_test(self):
"""
Test the AppLocation.get_files() method with no parameters passed.
"""
with patch(u'openlp.core.utils.AppLocation.get_data_path') as mocked_get_data_path, \
patch(u'openlp.core.utils.applocation.os.listdir') as mocked_listdir:
# GIVEN: Our mocked modules/methods.
mocked_get_data_path.return_value = u'test/dir'
mocked_listdir.return_value = copy.deepcopy(FILE_LIST)
# When: Get the list of files.
result = AppLocation.get_files()
2013-02-27 21:40:55 +00:00
# Then: check if the file lists are identical.
assert result == FILE_LIST, u'The file lists should be identical.'
2013-02-27 12:03:17 +00:00
def get_files_test(self):
"""
Test the AppLocation.get_files() method with all parameters passed.
"""
with patch(u'openlp.core.utils.AppLocation.get_data_path') as mocked_get_data_path, \
patch(u'openlp.core.utils.applocation.os.listdir') as mocked_listdir:
# GIVEN: Our mocked modules/methods.
mocked_get_data_path.return_value = u'test/dir'
mocked_listdir.return_value = copy.deepcopy(FILE_LIST)
# When: Get the list of files.
2013-03-01 16:30:13 +00:00
result = AppLocation.get_files(u'section', u'.mp3')
2013-03-01 16:18:50 +00:00
# Then: Check if the section parameter was used correctly.
mocked_listdir.assert_called_with(u'test/dir/section')
2013-02-27 12:03:17 +00:00
2013-02-27 21:40:55 +00:00
# Then: check if the file lists are identical.
assert result == [u'file5.mp3', u'file6.mp3'], u'The file lists should be identical.'
2013-02-27 12:03:17 +00:00
2012-12-06 10:55:12 +00:00
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, \
2013-02-27 12:03:17 +00:00
patch(u'openlp.core.utils.applocation.check_directory_exists') as mocked_check_directory_exists:
2012-12-06 10:55:12 +00:00
# GIVEN: A mocked out AppLocation.get_data_path()
mocked_get_data_path.return_value = u'test/dir'
mocked_check_directory_exists.return_value = True
2013-04-05 19:37:15 +00:00
2012-12-06 10:55:12 +00:00
# WHEN: we call AppLocation.get_data_path()
data_path = AppLocation.get_section_data_path(u'section')
2013-04-05 19:37:15 +00:00
2012-12-06 10:55:12 +00:00
# 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.applocation._get_frozen_path') as mocked_get_frozen_path:
2012-12-06 10:55:12 +00:00
mocked_get_frozen_path.return_value = u'app/dir'
2013-04-05 19:37:15 +00:00
2012-12-06 10:55:12 +00:00
# WHEN: We call AppLocation.get_directory
directory = AppLocation.get_directory(AppLocation.AppDir)
2013-04-05 19:37:15 +00:00
2012-12-06 10:55:12 +00:00
# THEN:
assert directory == u'app/dir', u'Directory should be "app/dir"'
2013-01-30 18:42:22 +00:00
2012-12-06 10:55:12 +00:00
def get_directory_for_plugins_dir_test(self):
"""
Test the AppLocation.get_directory() method for AppLocation.PluginsDir
"""
with patch(u'openlp.core.utils.applocation._get_frozen_path') as mocked_get_frozen_path, \
2013-02-27 12:03:17 +00:00
patch(u'openlp.core.utils.applocation.os.path.abspath') as mocked_abspath, \
patch(u'openlp.core.utils.applocation.os.path.split') as mocked_split, \
patch(u'openlp.core.utils.applocation.sys') as mocked_sys:
2012-12-06 10:55:12 +00:00
mocked_abspath.return_value = u'plugins/dir'
mocked_split.return_value = [u'openlp']
2012-12-06 10:55:12 +00:00
mocked_get_frozen_path.return_value = u'plugins/dir'
mocked_sys.frozen = 1
mocked_sys.argv = ['openlp']
2013-04-05 19:37:15 +00:00
2012-12-06 10:55:12 +00:00
# WHEN: We call AppLocation.get_directory
directory = AppLocation.get_directory(AppLocation.PluginsDir)
2013-04-05 19:37:15 +00:00
2012-12-06 10:55:12 +00:00
# THEN:
assert directory == u'plugins/dir', u'Directory should be "plugins/dir"'
2013-01-30 18:42:22 +00:00