Skip to content
This repository
tag: 0.5.0
Fetching contributors…

Cannot retrieve contributors at this time

file 46 lines (38 sloc) 1.531 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
import mock


class SharedMock(mock.MagicMock):
    def __init__(self, *args, **kwargs):
        super(SharedMock, self).__init__(*args, **kwargs)
        parent = mock.MagicMock()
        parent.child = self
        self.__parent = parent

    def _get_child_mock(self, **kwargs):
        name = kwargs.get('name', '')
        if name[:2] == name[-2:] == '__':
            return super(SharedMock, self)._get_child_mock(**kwargs)
        return self

    def __getattr__(self, name):
        result = super(SharedMock, self).__getattr__(name)
        if result is self:
            result._mock_name = result._mock_new_name = name
        return result

    def assert_chain_calls(self, *calls):
        """
Asserts that a chained method was called (parents in the chain do not
matter, nor are they tracked).

>>> obj.assert_chain_calls(call.filter(foo='bar'))
>>> obj.assert_chain_calls(call.select_related('baz'))
"""
        all_calls = self.__parent.mock_calls[:]

        not_found = []
        for kall in calls:
            try:
                all_calls.remove(kall)
            except ValueError:
                not_found.append(kall)
        if not_found:
            if self.__parent.mock_calls:
                message = '%r not all found in call list, %d other(s) were:\n%r' % (not_found, len(self.__parent.mock_calls),
                    self.__parent.mock_calls)
            else:
                message = 'no calls were found'

            raise AssertionError(message)
Something went wrong with that request. Please try again.