~dobey/ubuntu/natty/ubuntuone-control-panel/release-0-9-1

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Natalia Bidart (nessita)
  • Date: 2010-12-06 12:27:11 UTC
  • Revision ID: james.westby@ubuntu.com-20101206122711-0wvvlliao34bjztf
Tags: upstream-0.0.9
ImportĀ upstreamĀ versionĀ 0.0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Authors: Natalia B Bidart <natalia.bidart@canonical.com>
 
4
#
 
5
# Copyright 2010 Canonical Ltd.
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 3, as published
 
9
# by the Free Software Foundation.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
# PURPOSE.  See the GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along
 
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
"""The test suite for the control panel utilities."""
 
20
 
 
21
import logging
 
22
import sys
 
23
 
 
24
from ubuntuone.devtools.handlers import MementoHandler
 
25
 
 
26
from ubuntuone.controlpanel import utils
 
27
from ubuntuone.controlpanel.tests import TestCase
 
28
 
 
29
 
 
30
CONSTANTS_MODULE = 'ubuntuone.controlpanel.constants'
 
31
NOT_DEFINED = object()
 
32
 
 
33
 
 
34
class FakedConstantsModule(object):
 
35
    """Fake the 'ubuntuone.controlpanel.constants' module."""
 
36
 
 
37
    PROJECT_DIR = '/tmp/foo/bar'
 
38
 
 
39
 
 
40
class FakedFailure(object):
 
41
    """Fake a twisted Failure."""
 
42
 
 
43
    def __init__(self, value):
 
44
        self.value = value
 
45
 
 
46
 
 
47
class GetProjectDirTestCase(TestCase):
 
48
    """Test case for get_project_dir when constants module is not defined."""
 
49
 
 
50
    # pylint: disable=E1101
 
51
 
 
52
    def setUp(self):
 
53
        self._constants = sys.modules.get(CONSTANTS_MODULE, NOT_DEFINED)
 
54
        sys.modules[CONSTANTS_MODULE] = None  # force ImportError
 
55
 
 
56
        self.memento = MementoHandler()
 
57
        self.memento.setLevel(logging.DEBUG)
 
58
        utils.logger.addHandler(self.memento)
 
59
 
 
60
    def tearDown(self):
 
61
        if self._constants is not NOT_DEFINED:
 
62
            sys.modules[CONSTANTS_MODULE] = self._constants
 
63
        else:
 
64
            sys.modules.pop(CONSTANTS_MODULE)
 
65
 
 
66
    def test_get_project_dir_relative(self):
 
67
        """The relative path for the data directory is correctly retrieved."""
 
68
        module = utils.os.path.dirname(utils.__file__)
 
69
        rel_data = utils.os.path.join(module, utils.os.path.pardir,
 
70
                                      utils.os.path.pardir, utils.DATA_SUFFIX)
 
71
        expected_dir = utils.os.path.abspath(rel_data)
 
72
 
 
73
        # ensure expected_path exists at os level
 
74
        self.patch(utils.os.path, 'exists', lambda path: path == expected_dir)
 
75
 
 
76
        result = utils.get_project_dir()
 
77
        self.assertEqual(expected_dir, result)
 
78
 
 
79
    def test_get_project_dir_none_exists(self):
 
80
        """No data directory exists, return None and log as error."""
 
81
        self.patch(utils.os.path, 'exists', lambda path: False)
 
82
        sys.modules[CONSTANTS_MODULE] = None
 
83
 
 
84
        result = utils.get_project_dir()
 
85
        self.assertTrue(result is None)
 
86
        msg = 'get_project_dir: can not build a valid path.'
 
87
        self.assertTrue(self.memento.check_error(msg))
 
88
 
 
89
 
 
90
class GetProjectDirWithConstantsTestCase(GetProjectDirTestCase):
 
91
    """Test case for get_project_dir when constants module is defined."""
 
92
 
 
93
    def setUp(self):
 
94
        super(GetProjectDirWithConstantsTestCase, self).setUp()
 
95
        self.patch(utils.os.path, 'exists', lambda path: False)
 
96
        self._constants = sys.modules.get(CONSTANTS_MODULE, NOT_DEFINED)
 
97
        sys.modules[CONSTANTS_MODULE] = FakedConstantsModule()
 
98
 
 
99
    def test_get_project_dir(self):
 
100
        """If the constants.py module exists, use PROJECT_DIR from it."""
 
101
        result = utils.get_project_dir()
 
102
        self.assertEqual(sys.modules[CONSTANTS_MODULE].PROJECT_DIR, result)
 
103
 
 
104
 
 
105
class GetDataFileTestCase(TestCase):
 
106
    """Test cases for get_data_file."""
 
107
 
 
108
    def test_get_data_file(self):
 
109
        """The path for a data file is correctly retrieved."""
 
110
        dummy_dir = '/yadda/yadda'
 
111
        dummy_file = 'test.png'
 
112
        self.patch(utils, 'get_project_dir', lambda: dummy_dir)
 
113
        result = utils.get_data_file(dummy_file)
 
114
        expected = utils.os.path.join(dummy_dir, dummy_file)
 
115
        self.assertEqual(expected, result)
 
116
 
 
117
 
 
118
class ExceptionHandligTestCase(TestCase):
 
119
    """Test cases for exception handling."""
 
120
 
 
121
    def test_is_dbus_no_reply(self):
 
122
        """The failure is a dbus no_reply error."""
 
123
        exc = utils.dbus.exceptions.DBusException()
 
124
        exc._dbus_error_name = utils.DBUS_NO_REPLY
 
125
 
 
126
        result = utils.is_dbus_no_reply(FakedFailure(value=exc))
 
127
 
 
128
        self.assertTrue(result)
 
129
 
 
130
    def test_other_dbus_error_is_not_dbus_no_reply(self):
 
131
        """Another dbus exception is not a dbus no_reply error."""
 
132
        exc = utils.dbus.exceptions.DBusException()
 
133
        exc._dbus_error_name = utils.DBUS_SERVICE_UNKNOWN
 
134
 
 
135
        result = utils.is_dbus_no_reply(FakedFailure(value=exc))
 
136
 
 
137
        self.assertFalse(result)
 
138
 
 
139
    def test_no_dbus_exception_is_not_dbus_no_reply(self):
 
140
        """A non dbus exception is not a dbus no_reply error."""
 
141
        exc = AssertionError(utils.DBUS_NO_REPLY)
 
142
 
 
143
        result = utils.is_dbus_no_reply(FakedFailure(value=exc))
 
144
 
 
145
        self.assertFalse(result)
 
146
 
 
147
    def test_exception_to_error_dict(self):
 
148
        """Transform a regular Exception into a string-string dictionary."""
 
149
        msg = 'Something went wrong.'
 
150
        exc = AssertionError(msg)
 
151
 
 
152
        result = utils.exception_to_error_dict(exc)
 
153
        expected = {utils.ERROR_TYPE: exc.__class__.__name__,
 
154
                    utils.ERROR_MESSAGE: unicode(exc)}
 
155
 
 
156
        self.assertEqual(expected, result)
 
157
 
 
158
    def test_failure_to_error_dict(self):
 
159
        """Transform a Failure into a string-string dictionary."""
 
160
        msg = 'Something went wrong.'
 
161
        exc = AssertionError(msg)
 
162
        failure = FakedFailure(value=exc)
 
163
 
 
164
        result = utils.failure_to_error_dict(failure)
 
165
        expected = {utils.ERROR_TYPE: exc.__class__.__name__,
 
166
                    utils.ERROR_MESSAGE: unicode(exc)}
 
167
 
 
168
        self.assertEqual(expected, result)