2
Functional tests to test the AppLocation class and related methods.
4
from unittest import TestCase
8
from openlp.core.utils import AppLocation
10
class TestAppLocation(TestCase):
12
A test suite to test out various methods around the AppLocation class.
14
def get_data_path_test(self):
16
Test the AppLocation.get_data_path() method
18
with patch(u'openlp.core.utils.Settings') as mocked_class, \
19
patch(u'openlp.core.utils.AppLocation.get_directory') as mocked_get_directory, \
20
patch(u'openlp.core.utils.check_directory_exists') as mocked_check_directory_exists, \
21
patch(u'openlp.core.utils.os') as mocked_os:
22
# GIVEN: A mocked out Settings class and a mocked out AppLocation.get_directory()
23
mocked_settings = mocked_class.return_value
24
mocked_settings.contains.return_value = False
25
mocked_get_directory.return_value = u'test/dir'
26
mocked_check_directory_exists.return_value = True
27
mocked_os.path.normpath.return_value = u'test/dir'
28
# WHEN: we call AppLocation.get_data_path()
29
data_path = AppLocation.get_data_path()
30
# THEN: check that all the correct methods were called, and the result is correct
31
mocked_settings.contains.assert_called_with(u'advanced/data path')
32
mocked_get_directory.assert_called_with(AppLocation.DataDir)
33
mocked_check_directory_exists.assert_called_with(u'test/dir')
34
assert data_path == u'test/dir', u'Result should be "test/dir"'
36
def get_data_path_with_custom_location_test(self):
38
Test the AppLocation.get_data_path() method when a custom location is set in the settings
40
with patch(u'openlp.core.utils.Settings') as mocked_class,\
41
patch(u'openlp.core.utils.os') as mocked_os:
42
# GIVEN: A mocked out Settings class which returns a custom data location
43
mocked_settings = mocked_class.return_value
44
mocked_settings.contains.return_value = True
45
mocked_settings.value.return_value.toString.return_value = u'custom/dir'
46
mocked_os.path.normpath.return_value = u'custom/dir'
47
# WHEN: we call AppLocation.get_data_path()
48
data_path = AppLocation.get_data_path()
49
# THEN: the mocked Settings methods were called and the value returned was our set up value
50
mocked_settings.contains.assert_called_with(u'advanced/data path')
51
mocked_settings.value.assert_called_with(u'advanced/data path')
52
mocked_settings.value.return_value.toString.assert_called_with()
53
assert data_path == u'custom/dir', u'Result should be "custom/dir"'
55
def get_section_data_path_test(self):
57
Test the AppLocation.get_section_data_path() method
59
with patch(u'openlp.core.utils.AppLocation.get_data_path') as mocked_get_data_path, \
60
patch(u'openlp.core.utils.check_directory_exists') as mocked_check_directory_exists:
61
# GIVEN: A mocked out AppLocation.get_data_path()
62
mocked_get_data_path.return_value = u'test/dir'
63
mocked_check_directory_exists.return_value = True
64
# WHEN: we call AppLocation.get_data_path()
65
data_path = AppLocation.get_section_data_path(u'section')
66
# THEN: check that all the correct methods were called, and the result is correct
67
mocked_check_directory_exists.assert_called_with(u'test/dir/section')
68
assert data_path == u'test/dir/section', u'Result should be "test/dir/section"'
70
def get_directory_for_app_dir_test(self):
72
Test the AppLocation.get_directory() method for AppLocation.AppDir
74
with patch(u'openlp.core.utils._get_frozen_path') as mocked_get_frozen_path:
75
mocked_get_frozen_path.return_value = u'app/dir'
76
# WHEN: We call AppLocation.get_directory
77
directory = AppLocation.get_directory(AppLocation.AppDir)
79
assert directory == u'app/dir', u'Directory should be "app/dir"'
81
def get_directory_for_plugins_dir_test(self):
83
Test the AppLocation.get_directory() method for AppLocation.PluginsDir
85
with patch(u'openlp.core.utils._get_frozen_path') as mocked_get_frozen_path, \
86
patch(u'openlp.core.utils.os.path.abspath') as mocked_abspath, \
87
patch(u'openlp.core.utils.os.path.split') as mocked_split, \
88
patch(u'openlp.core.utils.sys') as mocked_sys:
89
mocked_abspath.return_value = u'plugins/dir'
90
mocked_split.return_value = [u'openlp']
91
mocked_get_frozen_path.return_value = u'plugins/dir'
93
mocked_sys.argv = ['openlp']
94
# WHEN: We call AppLocation.get_directory
95
directory = AppLocation.get_directory(AppLocation.PluginsDir)
97
assert directory == u'plugins/dir', u'Directory should be "plugins/dir"'