Source

denim / tests / _utils.py

import unittest
from denim.utils import set_api_wrapper, api_wrapper


class TestApiWrapper(object):
    """
    An easily replaceable wrapper around api commands to allow for easy
    testing.
    """
    def __init__(self):
        self.reset()

    def reset(self):
        """
        Clear all commands.
        """
        self.sudo_commands = []
        self.run_commands = []

    def sudo(self, command, *args, **kwargs):
        return self.sudo_commands.append((command, args, kwargs))

    def run(self, command, *args, **kwargs):
        return self.run_commands.append((command, args, kwargs))

set_api_wrapper(TestApiWrapper())


class ApiTestCase(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(ApiTestCase, self).__init__(*args, **kwargs)
        self.api = api_wrapper()

    def setUp(self):
        self.api.reset()

    def assertCommand(self, expected_command, actual_command):
        ec, ea, ekwa = expected_command
        ac, aa, akwa = actual_command

        self.assertEqual(ec, ac, msg='Command does not match. Expected "%s" got "%s"' % (ec, ac))
        self.assertTupleEqual(ea, aa, msg='Command arguments do not match.\nExpected: %s\nActual: %s' % (ea, aa))
        self.assertDictEqual(ekwa, akwa, msg='Command keyword arguments do not match.\nExpected: %s\nActual: %s' % (ekwa, akwa))

    def assertSudo(self, command, *args, **kwargs):
        command_count = len(self.api.sudo_commands)
        self.assertEqual(command_count, 1, msg="Expected a single sudo command got %s." % command_count)
        self.assertCommand((command, args, kwargs), self.api.sudo_commands[0])

    def assertRun(self, command, *args, **kwargs):
        command_count = len(self.api.run_commands)
        self.assertEqual(command_count, 1, msg="Expected a single run command got %s." % command_count)
        self.assertCommand((command, args, kwargs), self.api.run_commands[0])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.