~mandel/ubuntuone-control-panel/auto-update-looping-call

« back to all changes in this revision

Viewing changes to ubuntuone/controlpanel/tests/test_utils.py

  • Committer: Natalia B. Bidart
  • Date: 2010-11-08 20:32:42 UTC
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: natalia.bidart@canonical.com-20101108203242-3015oxw1ogekdz9k
Modified setup.py so ui files are automatically grabbed by distutils-extra.

Added .in files to define at installation time the paths for the dbus service
and the data dir.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
"""The test suite for the control panel utilities."""
20
20
 
21
 
import os
 
21
import logging
 
22
import sys
22
23
 
23
24
from twisted.trial.unittest import TestCase
24
25
 
25
26
from ubuntuone.controlpanel import utils
26
 
 
27
 
 
28
 
class GetDataTestCase(TestCase):
29
 
    """Test case for get_data_dir and get_data_file."""
 
27
from ubuntuone.controlpanel.tests import testcase
 
28
# XXX: to be replaced when MementoHandler is there
 
29
#from ubuntuone.devtools import testcase
 
30
 
 
31
 
 
32
CONSTANTS_MODULE = 'ubuntuone.controlpanel.constants'
 
33
NOT_DEFINED = object()
 
34
 
 
35
 
 
36
class FakedConstantsModule(object):
 
37
    """FAke the 'ubuntuone.controlpanel.constants' module."""
 
38
 
 
39
    DATA_DIR = '/tmp/foo/bar'
 
40
 
 
41
 
 
42
class GetDataDirNoConstantsTestCase(TestCase):
 
43
    """Test case for get_data_dir when constants module is not defined."""
 
44
 
 
45
    # pylint: disable=E1101
 
46
 
 
47
    def setUp(self):
 
48
        self._constants = sys.modules.get(CONSTANTS_MODULE, NOT_DEFINED)
 
49
        sys.modules[CONSTANTS_MODULE] = None  # force ImportError
 
50
 
 
51
        module = utils.os.path.dirname(utils.__file__)
 
52
        rel_data = utils.os.path.join(module, utils.os.path.pardir,
 
53
                                      utils.os.path.pardir, utils.DATA_SUFFIX)
 
54
        self.rel_data_path = utils.os.path.abspath(rel_data)
 
55
 
 
56
        dirs = []
 
57
        for i in xrange(3):
 
58
            ddir = '/tmp/%i' % i
 
59
            fdir = utils.os.path.join(ddir, 'ubuntuone-control-panel')
 
60
            setattr(self, 'data_dir_%i' % i, fdir)
 
61
            dirs.append(ddir)
 
62
 
 
63
        self.patch(utils.xdg.BaseDirectory, 'xdg_data_dirs', dirs)
 
64
 
 
65
        self.memento = testcase.MementoHandler()
 
66
        self.memento.setLevel(logging.DEBUG)
 
67
        utils.logger.addHandler(self.memento)
 
68
 
 
69
    def tearDown(self):
 
70
        if self._constants is not NOT_DEFINED:
 
71
            sys.modules[CONSTANTS_MODULE] = self._constants
 
72
        else:
 
73
            sys.modules.pop(CONSTANTS_MODULE)
 
74
 
 
75
    def assert_correct_dir(self, expected_dir):
 
76
        """Check that utils.get_data_dir returned expected_dir."""
 
77
        # ensure expected_path exists at os level
 
78
        self.patch(utils.os.path, 'exists', lambda path: path == expected_dir)
 
79
 
 
80
        result = utils.get_data_dir()
 
81
        self.assertEqual(expected_dir, result)
30
82
 
31
83
    def test_get_data_dir_relative(self):
32
84
        """The relative path for the data directory is correctly retrieved."""
33
 
        module = os.path.dirname(utils.__file__)
34
 
        expected = os.path.abspath(os.path.join(module, os.path.pardir,
35
 
                                                os.path.pardir, 'data'))
36
 
        result = utils.get_data_dir()
37
 
        self.assertEqual(expected, result)
 
85
        self.assert_correct_dir(self.rel_data_path)
 
86
 
 
87
    def test_get_data_dir_from_xdg_first_in_the_list(self):
 
88
        """The system data directory is correctly retrieved."""
 
89
        self.assert_correct_dir(self.data_dir_0)
 
90
 
 
91
    def test_get_data_dir_from_xdg_middle_in_the_list(self):
 
92
        """The system data directory is correctly retrieved."""
 
93
        self.assert_correct_dir(self.data_dir_1)
 
94
 
 
95
    def test_get_data_dir_from_xdg_last_in_the_list(self):
 
96
        """The system data directory is correctly retrieved."""
 
97
        self.assert_correct_dir(self.data_dir_2)
 
98
 
 
99
    def test_get_data_dir_none_exists(self):
 
100
        """No data directory exists, return None and log as error."""
 
101
        self.patch(utils.os.path, 'exists', lambda path: False)
 
102
        sys.modules[CONSTANTS_MODULE] = None
 
103
 
 
104
        result = utils.get_data_dir()
 
105
        self.assertTrue(result is None)
 
106
        msg = 'get_data_dir: can not build a valid data path'
 
107
        self.assertTrue(self.memento.check_error(msg))
 
108
 
 
109
 
 
110
class GetDataDirWithConstantsTestCase(GetDataDirNoConstantsTestCase):
 
111
    """Test case for get_data_dir when constants module is defined."""
 
112
 
 
113
    def setUp(self):
 
114
        super(GetDataDirWithConstantsTestCase, self).setUp()
 
115
        self.patch(utils.os.path, 'exists', lambda path: False)
 
116
        self._constants = sys.modules.get(CONSTANTS_MODULE, NOT_DEFINED)
 
117
        sys.modules[CONSTANTS_MODULE] = FakedConstantsModule()
 
118
 
 
119
    def test_get_data_dir(self):
 
120
        """If the constants.py module exists, use DATA_DIR from it."""
 
121
        result = utils.get_data_dir()
 
122
        self.assertEqual(sys.modules[CONSTANTS_MODULE].DATA_DIR, result)
 
123
 
 
124
 
 
125
class GetDataFileTestCase(TestCase):
 
126
    """Test cases for get_data_file."""
38
127
 
39
128
    def test_get_data_file(self):
40
129
        """The path for a data file is correctly retrieved."""
42
131
        dummy_file = 'test.png'
43
132
        self.patch(utils, 'get_data_dir', lambda: dummy_dir)
44
133
        result = utils.get_data_file(dummy_file)
45
 
        self.assertEqual(os.path.join(dummy_dir, dummy_file), result)
 
134
        self.assertEqual(utils.os.path.join(dummy_dir, dummy_file), result)