~mbernis/openlp/2.2_remote_app_enhancement

« back to all changes in this revision

Viewing changes to tests/functional/openlp_core_utils/test_applocation.py

  • Committer: Tim Bentley
  • Date: 2012-12-08 11:27:22 UTC
  • mfrom: (2127 openlp)
  • mto: This revision was merged to the branch mainline in revision 2149.
  • Revision ID: tim.bentley@gmail.com-20121208112722-sy5wud6xz1xsmrty
Head

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Functional tests to test the AppLocation class and related methods.
 
3
"""
 
4
from unittest import TestCase
 
5
 
 
6
from mock import patch
 
7
 
 
8
from openlp.core.utils import AppLocation
 
9
 
 
10
class TestAppLocation(TestCase):
 
11
    """
 
12
    A test suite to test out various methods around the AppLocation class.
 
13
    """
 
14
    def get_data_path_test(self):
 
15
        """
 
16
        Test the AppLocation.get_data_path() method
 
17
        """
 
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"'
 
35
 
 
36
    def get_data_path_with_custom_location_test(self):
 
37
        """
 
38
        Test the AppLocation.get_data_path() method when a custom location is set in the settings
 
39
        """
 
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"'
 
54
 
 
55
    def get_section_data_path_test(self):
 
56
        """
 
57
        Test the AppLocation.get_section_data_path() method
 
58
        """
 
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"'
 
69
 
 
70
    def get_directory_for_app_dir_test(self):
 
71
        """
 
72
        Test the AppLocation.get_directory() method for AppLocation.AppDir
 
73
        """
 
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)
 
78
            # THEN:
 
79
            assert directory == u'app/dir', u'Directory should be "app/dir"'
 
80
            
 
81
    def get_directory_for_plugins_dir_test(self):
 
82
        """
 
83
        Test the AppLocation.get_directory() method for AppLocation.PluginsDir
 
84
        """
 
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'
 
92
            mocked_sys.frozen = 1
 
93
            mocked_sys.argv = ['openlp']
 
94
            # WHEN: We call AppLocation.get_directory
 
95
            directory = AppLocation.get_directory(AppLocation.PluginsDir)
 
96
            # THEN:
 
97
            assert directory == u'plugins/dir', u'Directory should be "plugins/dir"'
 
98