1
# -*- coding: utf-8 -*-
3
# Authors: Natalia B Bidart <natalia.bidart@canonical.com>
5
# Copyright 2010 Canonical Ltd.
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.
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.
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/>.
19
"""The test suite for the control panel utilities."""
24
from ubuntuone.devtools.handlers import MementoHandler
26
from ubuntuone.controlpanel import utils
27
from ubuntuone.controlpanel.tests import TestCase
30
CONSTANTS_MODULE = 'ubuntuone.controlpanel.constants'
31
NOT_DEFINED = object()
34
class FakedConstantsModule(object):
35
"""Fake the 'ubuntuone.controlpanel.constants' module."""
37
PROJECT_DIR = '/tmp/foo/bar'
40
class FakedFailure(object):
41
"""Fake a twisted Failure."""
43
def __init__(self, value):
47
class GetProjectDirTestCase(TestCase):
48
"""Test case for get_project_dir when constants module is not defined."""
50
# pylint: disable=E1101
53
self._constants = sys.modules.get(CONSTANTS_MODULE, NOT_DEFINED)
54
sys.modules[CONSTANTS_MODULE] = None # force ImportError
56
self.memento = MementoHandler()
57
self.memento.setLevel(logging.DEBUG)
58
utils.logger.addHandler(self.memento)
61
if self._constants is not NOT_DEFINED:
62
sys.modules[CONSTANTS_MODULE] = self._constants
64
sys.modules.pop(CONSTANTS_MODULE)
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)
73
# ensure expected_path exists at os level
74
self.patch(utils.os.path, 'exists', lambda path: path == expected_dir)
76
result = utils.get_project_dir()
77
self.assertEqual(expected_dir, result)
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
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))
90
class GetProjectDirWithConstantsTestCase(GetProjectDirTestCase):
91
"""Test case for get_project_dir when constants module is defined."""
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()
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)
105
class GetDataFileTestCase(TestCase):
106
"""Test cases for get_data_file."""
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)
118
class ExceptionHandligTestCase(TestCase):
119
"""Test cases for exception handling."""
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
126
result = utils.is_dbus_no_reply(FakedFailure(value=exc))
128
self.assertTrue(result)
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
135
result = utils.is_dbus_no_reply(FakedFailure(value=exc))
137
self.assertFalse(result)
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)
143
result = utils.is_dbus_no_reply(FakedFailure(value=exc))
145
self.assertFalse(result)
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)
152
result = utils.exception_to_error_dict(exc)
153
expected = {utils.ERROR_TYPE: exc.__class__.__name__,
154
utils.ERROR_MESSAGE: unicode(exc)}
156
self.assertEqual(expected, result)
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)
164
result = utils.failure_to_error_dict(failure)
165
expected = {utils.ERROR_TYPE: exc.__class__.__name__,
166
utils.ERROR_MESSAGE: unicode(exc)}
168
self.assertEqual(expected, result)