示例#1
0
 def set_response(self, response: R, *args, **kwargs):
     key = self._ordered_call(*args, **kwargs)
     self._validate_return(response)
     if has_matchers(key):
         self._matcher_responses[key] = ResponderBasic(response)
     else:
         self._responses[key] = ResponderBasic(response)
示例#2
0
class MockAttributeState(Generic[R]):
    def __init__(self, name: str, initial_value: R, type_hint: Type):
        self.name = name
        self.type_hint = type_hint
        self._responder: Responder = ResponderBasic(initial_value)
        self._call_count = 0
        self._set_calls: List[R] = []

    def _validate_return(self, response: R):
        if not isinstance(self.type_hint, Blank):
            if not is_type(response, self.type_hint):
                raise MockTypeSafetyError(
                    "Attribute: {} must be of type:{}".format(
                        self.name,
                        self.type_hint,
                    ))

    def set_response(self, response: R):
        self._validate_return(response)
        self._responder = ResponderBasic(response)

    def set_response_many(self, results: List[R], loop: bool):
        for response in results:
            self._validate_return(response)
        self._responder = ResponderMany(results, loop)

    def set_error_response(self, error: Exception):
        self._responder = ResponderRaise(error)

    def set_response_do(self, do_function: DoFunction):
        self._responder = ResponderDo(do_function, _null_ordered_call)

    def response(self) -> R:
        self._call_count += 1
        r = self._responder.response()
        self._validate_return(r)
        return r

    def call_count_gets(self) -> int:
        return self._call_count

    def called_set_with(self, item):
        self._validate_return(item)
        self._set_calls.append(item)
        self._responder = ResponderBasic(item)

    def called_set_record(self, expected_call) -> CalledSetRecord:
        other_calls = []
        count = 0
        for call in self._set_calls:
            if expected_call == call:
                count += 1
            else:
                other_calls.append(call)
        return CalledSetRecord(expected_call, count, other_calls)
示例#3
0
 def called_set_with(self, item):
     self._validate_return(item)
     self._set_calls.append(item)
     self._responder = ResponderBasic(item)
示例#4
0
 def set_response_do(self, do_function: DoFunction):
     self._responder = ResponderDo(do_function, _null_ordered_call)
示例#5
0
 def set_error_response(self, error: Exception):
     self._responder = ResponderRaise(error)
示例#6
0
 def set_response_many(self, results: List[R], loop: bool):
     for response in results:
         self._validate_return(response)
     self._responder = ResponderMany(results, loop)
示例#7
0
 def set_response(self, response: R):
     self._validate_return(response)
     self._responder = ResponderBasic(response)
示例#8
0
 def __init__(self, name: str, initial_value: R, type_hint: Type):
     self.name = name
     self.type_hint = type_hint
     self._responder: Responder = ResponderBasic(initial_value)
     self._call_count = 0
     self._set_calls: List[R] = []