Logo Search packages:      
Sourcecode: ubuntuone-control-panel version File versions  Download package

test_utils.py

# -*- coding: utf-8 -*-

# Authors: Natalia B Bidart <natalia.bidart@canonical.com>
#
# Copyright 2010 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

"""The test suite for the control panel utilities."""

import logging
import sys

from ubuntuone.devtools.handlers import MementoHandler

from ubuntuone.controlpanel import utils
from ubuntuone.controlpanel.tests import TestCase


CONSTANTS_MODULE = 'ubuntuone.controlpanel.constants'
NOT_DEFINED = object()


00034 class FakedConstantsModule(object):
    """Fake the 'ubuntuone.controlpanel.constants' module."""

    PROJECT_DIR = '/tmp/foo/bar'


00040 class FakedFailure(object):
    """Fake a twisted Failure."""

    def __init__(self, value):
        self.value = value


00047 class GetProjectDirTestCase(TestCase):
    """Test case for get_project_dir when constants module is not defined."""

    # pylint: disable=E1101

    def setUp(self):
        self._constants = sys.modules.get(CONSTANTS_MODULE, NOT_DEFINED)
        sys.modules[CONSTANTS_MODULE] = None  # force ImportError

        self.memento = MementoHandler()
        self.memento.setLevel(logging.DEBUG)
        utils.logger.addHandler(self.memento)

    def tearDown(self):
        if self._constants is not NOT_DEFINED:
            sys.modules[CONSTANTS_MODULE] = self._constants
        else:
            sys.modules.pop(CONSTANTS_MODULE)

00066     def test_get_project_dir_relative(self):
        """The relative path for the data directory is correctly retrieved."""
        module = utils.os.path.dirname(utils.__file__)
        rel_data = utils.os.path.join(module, utils.os.path.pardir,
                                      utils.os.path.pardir, utils.DATA_SUFFIX)
        expected_dir = utils.os.path.abspath(rel_data)

        # ensure expected_path exists at os level
        self.patch(utils.os.path, 'exists', lambda path: path == expected_dir)

        result = utils.get_project_dir()
        self.assertEqual(expected_dir, result)

00079     def test_get_project_dir_none_exists(self):
        """No data directory exists, return None and log as error."""
        self.patch(utils.os.path, 'exists', lambda path: False)
        sys.modules[CONSTANTS_MODULE] = None

        result = utils.get_project_dir()
        self.assertTrue(result is None)
        msg = 'get_project_dir: can not build a valid path.'
        self.assertTrue(self.memento.check_error(msg))


00090 class GetProjectDirWithConstantsTestCase(GetProjectDirTestCase):
    """Test case for get_project_dir when constants module is defined."""

    def setUp(self):
        super(GetProjectDirWithConstantsTestCase, self).setUp()
        self.patch(utils.os.path, 'exists', lambda path: False)
        self._constants = sys.modules.get(CONSTANTS_MODULE, NOT_DEFINED)
        sys.modules[CONSTANTS_MODULE] = FakedConstantsModule()

00099     def test_get_project_dir(self):
        """If the constants.py module exists, use PROJECT_DIR from it."""
        result = utils.get_project_dir()
        self.assertEqual(sys.modules[CONSTANTS_MODULE].PROJECT_DIR, result)


00105 class GetDataFileTestCase(TestCase):
    """Test cases for get_data_file."""

00108     def test_get_data_file(self):
        """The path for a data file is correctly retrieved."""
        dummy_dir = '/yadda/yadda'
        dummy_file = 'test.png'
        self.patch(utils, 'get_project_dir', lambda: dummy_dir)
        result = utils.get_data_file(dummy_file)
        expected = utils.os.path.join(dummy_dir, dummy_file)
        self.assertEqual(expected, result)


00118 class ExceptionHandligTestCase(TestCase):
    """Test cases for exception handling."""

00121     def test_is_dbus_no_reply(self):
        """The failure is a dbus no_reply error."""
        exc = utils.dbus.exceptions.DBusException()
        exc._dbus_error_name = utils.DBUS_NO_REPLY

        result = utils.is_dbus_no_reply(FakedFailure(value=exc))

        self.assertTrue(result)

00130     def test_other_dbus_error_is_not_dbus_no_reply(self):
        """Another dbus exception is not a dbus no_reply error."""
        exc = utils.dbus.exceptions.DBusException()
        exc._dbus_error_name = utils.DBUS_SERVICE_UNKNOWN

        result = utils.is_dbus_no_reply(FakedFailure(value=exc))

        self.assertFalse(result)

00139     def test_no_dbus_exception_is_not_dbus_no_reply(self):
        """A non dbus exception is not a dbus no_reply error."""
        exc = AssertionError(utils.DBUS_NO_REPLY)

        result = utils.is_dbus_no_reply(FakedFailure(value=exc))

        self.assertFalse(result)

00147     def test_exception_to_error_dict(self):
        """Transform a regular Exception into a string-string dictionary."""
        msg = 'Something went wrong.'
        exc = AssertionError(msg)

        result = utils.exception_to_error_dict(exc)
        expected = {utils.ERROR_TYPE: exc.__class__.__name__,
                    utils.ERROR_MESSAGE: unicode(exc)}

        self.assertEqual(expected, result)

00158     def test_failure_to_error_dict(self):
        """Transform a Failure into a string-string dictionary."""
        msg = 'Something went wrong.'
        exc = AssertionError(msg)
        failure = FakedFailure(value=exc)

        result = utils.failure_to_error_dict(failure)
        expected = {utils.ERROR_TYPE: exc.__class__.__name__,
                    utils.ERROR_MESSAGE: unicode(exc)}

        self.assertEqual(expected, result)

Generated by  Doxygen 1.6.0   Back to index