示例#1
0
    def test_call_magic_method(self):
        class Callable(object):
            def __call__(self):
                pass

        instance = Callable()
        self.assertTrue(_callable(instance))
示例#2
0
    def test_classmethod(self):
        class WithClassMethod(object):
            @classmethod
            def classfunc(cls):
                pass

        self.assertTrue(_callable(WithClassMethod.classfunc))
示例#3
0
    def test_staticmethod(self):
        class WithStaticMethod(object):
            @staticmethod
            def staticfunc():
                pass

        self.assertTrue(_callable(WithStaticMethod.staticfunc))
示例#4
0
def _patched_callable(obj):
    "Monkeypatch to allow automocking of classmethods and staticmethods."
    # See https://code.google.com/p/mock/issues/detail?id=241 and
    # http://bugs.python.org/issue23078 for the relevant bugs this
    # monkeypatch fixes
    if isinstance(obj, type):
        return True
    if getattr(obj, '__call__', None) is not None:
        return True
    if (isinstance(obj, (staticmethod, classmethod)) and
            mock._callable(obj.__func__)):
        return True
    return False
示例#5
0
    def _configure_mock_attrs(cls, method_responses):
        """Configure method-response pairs for API.

        For example, dict(create=KeyError, get_by_id=pb_foo_bar, get_all='{"foo": "bar"}')
        becomes
        {
            "create.side_effect": KeyError,
            "get_by_id.return_value": ApiResponse(data=pb_foo_bar),
            "get_all.return_value": ApiResponse(data='{"foo": "bar"}')
        }
        """
        import mock
        attrs = dict()
        for m, r in method_responses.items():
            if mock._is_exception(r) or mock._callable(r):
                k, v = m + '.side_effect', r
            else:
                k, v = m + '.return_value', cls._make_mock_api_response(r)
            attrs[k] = v
        return attrs
示例#6
0
    def test_non_callable_classmethod(self):
        class BadClassMethod(object):
            not_callable = classmethod(None)

        self.assertFalse(_callable(BadClassMethod.not_callable))
示例#7
0
    def test_non_callable_staticmethod(self):
        class BadStaticMethod(object):
            not_callable = staticmethod(None)

        self.assertFalse(_callable(BadStaticMethod.not_callable))
示例#8
0
 def test_type(self):
     for obj in [str, bytes, int, list, tuple, SomeClass]:
         self.assertTrue(_callable(obj))
示例#9
0
    def _mock_call(_mock_self, *args, **kwargs):
        self = _mock_self
        self.called = True
        self.call_count += 1
        self.call_args = mock._Call((args, kwargs), two=True)
        self.call_args_list.append(mock._Call((args, kwargs), two=True))

        _new_name = self._mock_new_name
        _new_parent = self._mock_new_parent
        self.mock_calls.append(mock._Call(('', args, kwargs)))

        seen = set()
        skip_next_dot = _new_name == '()'
        do_method_calls = self._mock_parent is not None
        name = self._mock_name
        while _new_parent is not None:
            this_mock_call = mock._Call((_new_name, args, kwargs))
            if _new_parent._mock_new_name:
                dot = '.'
                if skip_next_dot:
                    dot = ''

                skip_next_dot = False
                if _new_parent._mock_new_name == '()':
                    skip_next_dot = True

                _new_name = _new_parent._mock_new_name + dot + _new_name

            if do_method_calls:
                if _new_name == name:
                    this_method_call = this_mock_call
                else:
                    this_method_call = mock._Call(name, args, kwargs)
                _new_parent.method_calls.append(this_method_call)

                do_method_calls = _new_parent._mock_parent is not None
                if do_method_calls:
                    name = _new_parent._mock_name + '.' + name

            _new_parent.mock_calls.append(this_mock_call)
            _new_parent = _new_parent._mock_new_parent

            # use ids here so as not to call __hash__ on the mocks
            _new_parent_id = id(_new_parent)
            if _new_parent_id in seen:
                break
            seen.add(_new_parent_id)


        if repr(mock.call(args, kwargs)) in self._expected_calls:
            expector, call_args = self._expected_calls[repr(mock.call(args, kwargs))]
            if expector._return_exception:
                raise expector._return_exception
            else:
                return expector._return_value

        ret_val = mock.DEFAULT
        effect = self.side_effect
        if effect is not None:
            if mock._is_exception(effect):
                raise effect

            if not mock._callable(effect):
                return next(effect)

            ret_val = effect(*args, **kwargs)
            if ret_val is mock.DEFAULT:
                ret_val = self.return_value

        if (self._mock_wraps is not None and
            self._mock_return_value is mock.DEFAULT):
            return self._mock_wraps(*args, **kwargs)
        if ret_val is mock.DEFAULT:
            ret_val = self.return_value
        return ret_val
示例#10
0
    def _mock_call(_mock_self, *args, **kwargs):
        self = _mock_self
        self.called = True
        self.call_count += 1
        self.call_args = mock._Call((args, kwargs), two=True)
        self.call_args_list.append(mock._Call((args, kwargs), two=True))

        _new_name = self._mock_new_name
        _new_parent = self._mock_new_parent
        self.mock_calls.append(mock._Call(('', args, kwargs)))

        seen = set()
        skip_next_dot = _new_name == '()'
        do_method_calls = self._mock_parent is not None
        name = self._mock_name
        while _new_parent is not None:
            this_mock_call = mock._Call((_new_name, args, kwargs))
            if _new_parent._mock_new_name:
                dot = '.'
                if skip_next_dot:
                    dot = ''

                skip_next_dot = False
                if _new_parent._mock_new_name == '()':
                    skip_next_dot = True

                _new_name = _new_parent._mock_new_name + dot + _new_name

            if do_method_calls:
                if _new_name == name:
                    this_method_call = this_mock_call
                else:
                    this_method_call = mock._Call(name, args, kwargs)
                _new_parent.method_calls.append(this_method_call)

                do_method_calls = _new_parent._mock_parent is not None
                if do_method_calls:
                    name = _new_parent._mock_name + '.' + name

            _new_parent.mock_calls.append(this_mock_call)
            _new_parent = _new_parent._mock_new_parent

            # use ids here so as not to call __hash__ on the mocks
            _new_parent_id = id(_new_parent)
            if _new_parent_id in seen:
                break
            seen.add(_new_parent_id)

        if repr(mock.call(args, kwargs)) in self._expected_calls:
            expector, call_args = self._expected_calls[repr(
                mock.call(args, kwargs))]
            if expector._return_exception:
                raise expector._return_exception
            else:
                return expector._return_value

        ret_val = mock.DEFAULT
        effect = self.side_effect
        if effect is not None:
            if mock._is_exception(effect):
                raise effect

            if not mock._callable(effect):
                return next(effect)

            ret_val = effect(*args, **kwargs)
            if ret_val is mock.DEFAULT:
                ret_val = self.return_value

        if (self._mock_wraps is not None
                and self._mock_return_value is mock.DEFAULT):
            return self._mock_wraps(*args, **kwargs)
        if ret_val is mock.DEFAULT:
            ret_val = self.return_value
        return ret_val