Exemplo n.º 1
0
 def __call__(self, *args, **kwargs):
     obj = self._lookup(call(*args, **kwargs))
     if _is_exception(obj):
         raise obj
     if isinstance(obj, _Sequence):
         return obj()
     return obj
Exemplo n.º 2
0
 def __call__(self):
     if len(self.list) > 1:
         retval = self.list.pop(0)
     else:
         retval = self.list[0]
     if _is_exception(retval):
         raise retval
     return retval
Exemplo n.º 3
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
Exemplo n.º 4
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
Exemplo n.º 5
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
Exemplo n.º 6
0
 def __call__(self):
     retval = next(self._iterator)
     if _is_exception(retval):
         raise retval
     return retval