Exemplo n.º 1
0
def test_get_by_call_empty() -> None:
    """It should return None if store is empty."""
    subject = StubStore()
    result = subject.get_by_call(call=SpyEvent(
        spy_id=42, spy_name="my_spy", payload=SpyCall(args=(), kwargs={})))

    assert result is None
Exemplo n.º 2
0
def test_when_then_return_multiple_values(
    decoy: Decoy,
    spy_log: SpyLog,
    stub_store: StubStore,
    subject: DecoyCore,
) -> None:
    """It should add multiple return values to a stub."""
    rehearsal = WhenRehearsal(spy_id=1,
                              spy_name="my_spy",
                              payload=SpyCall(args=(), kwargs={}))
    decoy.when(spy_log.consume_when_rehearsal(
        ignore_extra_args=False)).then_return(rehearsal)

    result = subject.when(0, ignore_extra_args=False)
    result.then_return(42, 43, 44)

    decoy.verify(
        stub_store.add(
            rehearsal=rehearsal,
            behavior=StubBehavior(return_value=44, once=False),
        ),
        stub_store.add(
            rehearsal=rehearsal,
            behavior=StubBehavior(return_value=43, once=True),
        ),
        stub_store.add(
            rehearsal=rehearsal,
            behavior=StubBehavior(return_value=42, once=True),
        ),
    )
Exemplo n.º 3
0
def test_get_by_call() -> None:
    """It should be able to add a StubBehavior to the store and get it back."""
    subject = StubStore()
    rehearsal = WhenRehearsal(spy_id=42,
                              spy_name="my_spy",
                              payload=SpyCall(args=(), kwargs={}))
    behavior = StubBehavior(return_value="hello world")

    subject.add(rehearsal=rehearsal, behavior=behavior)
    result = subject.get_by_call(call=SpyEvent(
        spy_id=42, spy_name="my_spy", payload=SpyCall(args=(), kwargs={})))

    assert result == behavior
Exemplo n.º 4
0
def test_get_by_call_no_match(call: SpyEvent) -> None:
    """It should return a no-op StubBehavior if there are no matching calls."""
    subject = StubStore()
    rehearsal = WhenRehearsal(
        spy_id=42,
        spy_name="my_spy",
        payload=SpyCall(
            args=("hello", "world"),
            kwargs={"goodbye": "so long"},
        ),
    )
    behavior = StubBehavior(return_value="fizzbuzz")

    subject.add(rehearsal=rehearsal, behavior=behavior)

    result = subject.get_by_call(call=call)
    assert result is None
Exemplo n.º 5
0
def test_get_by_call_prefers_latest() -> None:
    """It should be prefer later stubs if multiple exist."""
    subject = StubStore()
    rehearsal_1 = WhenRehearsal(spy_id=42,
                                spy_name="my_spy",
                                payload=SpyCall(args=(), kwargs={}))
    behavior_1 = StubBehavior(return_value="hello")
    rehearsal_2 = WhenRehearsal(spy_id=42,
                                spy_name="my_spy",
                                payload=SpyCall(args=(), kwargs={}))
    behavior_2 = StubBehavior(return_value="goodbye")

    subject.add(rehearsal=rehearsal_1, behavior=behavior_1)
    subject.add(rehearsal=rehearsal_2, behavior=behavior_2)
    result = subject.get_by_call(call=SpyEvent(
        spy_id=42, spy_name="my_spy", payload=SpyCall(args=(), kwargs={})))

    assert result == behavior_2
Exemplo n.º 6
0
def test_handle_call_with_raise(
    decoy: Decoy,
    spy_log: SpyLog,
    stub_store: StubStore,
    subject: CallHandler,
) -> None:
    """It raise a Stub's configured error."""
    spy_call = SpyEvent(spy_id=42,
                        spy_name="spy_name",
                        payload=SpyCall(args=(), kwargs={}))
    behavior = StubBehavior(error=RuntimeError("oh no"))

    decoy.when(stub_store.get_by_call(spy_call)).then_return(behavior)

    with pytest.raises(RuntimeError, match="oh no"):
        subject.handle(spy_call)

    decoy.verify(spy_log.push(spy_call))
Exemplo n.º 7
0
def test_handle_call_with_return(
    decoy: Decoy,
    spy_log: SpyLog,
    stub_store: StubStore,
    subject: CallHandler,
) -> None:
    """It return a Stub's configured return value."""
    spy_call = SpyEvent(spy_id=42,
                        spy_name="spy_name",
                        payload=SpyCall(args=(), kwargs={}))
    behavior = StubBehavior(return_value="hello world")

    decoy.when(stub_store.get_by_call(spy_call)).then_return(behavior)

    result = subject.handle(spy_call)

    assert result == CallHandlerResult("hello world")
    decoy.verify(spy_log.push(spy_call))
Exemplo n.º 8
0
def test_handle_call_with_no_stubbing(
    decoy: Decoy,
    spy_log: SpyLog,
    stub_store: StubStore,
    subject: CallHandler,
) -> None:
    """It should noop and add the call to the stack if no stubbing is configured."""
    spy_call = SpyEvent(spy_id=42,
                        spy_name="spy_name",
                        payload=SpyCall(args=(), kwargs={}))
    behavior = None

    decoy.when(stub_store.get_by_call(spy_call)).then_return(behavior)

    result = subject.handle(spy_call)

    assert result is None
    decoy.verify(spy_log.push(spy_call))
Exemplo n.º 9
0
def test_handle_call_with_context_enter(
    decoy: Decoy,
    spy_log: SpyLog,
    stub_store: StubStore,
    subject: CallHandler,
) -> None:
    """It return a Stub's configured context value."""
    spy_call = SpyEvent(spy_id=42,
                        spy_name="spy_name",
                        payload=SpyCall(args=(), kwargs={}))
    behavior = StubBehavior(context_value="hello world")

    decoy.when(stub_store.get_by_call(spy_call)).then_return(behavior)

    with subject.handle(spy_call).value as result:  # type: ignore[union-attr]
        assert result == "hello world"

    decoy.verify(spy_log.push(spy_call))
Exemplo n.º 10
0
def test_clear() -> None:
    """It should consume any behavior marked with the `once` flag."""
    subject = StubStore()
    call = SpyEvent(spy_id=42,
                    spy_name="my_spy",
                    payload=SpyCall(args=(), kwargs={}))
    rehearsal = WhenRehearsal(spy_id=42,
                              spy_name="my_spy",
                              payload=SpyCall(args=(), kwargs={}))
    behavior = StubBehavior(return_value="fizzbuzz")

    subject.add(rehearsal=rehearsal, behavior=behavior)
    subject.clear()

    result = subject.get_by_call(call=call)

    assert result is None
Exemplo n.º 11
0
def test_handle_call_with_action(
    decoy: Decoy,
    spy_log: SpyLog,
    stub_store: StubStore,
    subject: CallHandler,
) -> None:
    """It should trigger a stub's configured action."""
    action = decoy.mock()
    spy_call = SpyEvent(
        spy_id=42,
        spy_name="spy_name",
        payload=SpyCall(args=(1, ), kwargs={"foo": "bar"}),
    )
    behavior = StubBehavior(action=action)

    decoy.when(stub_store.get_by_call(spy_call)).then_return(behavior)
    decoy.when(action(1, foo="bar")).then_return("hello world")

    result = subject.handle(spy_call)

    assert result == CallHandlerResult("hello world")
Exemplo n.º 12
0
def test_reset(
    decoy: Decoy,
    spy_log: SpyLog,
    stub_store: StubStore,
    warning_checker: WarningChecker,
    subject: DecoyCore,
) -> None:
    """It should reset the stores."""
    call = SpyEvent(spy_id=1,
                    spy_name="my_spy",
                    payload=SpyCall(args=(), kwargs={}))

    decoy.when(spy_log.get_all()).then_return([call])

    subject.reset()

    decoy.verify(
        warning_checker.check([call]),
        spy_log.clear(),
        stub_store.clear(),
    )
Exemplo n.º 13
0
def test_when_ignore_extra_args(
    decoy: Decoy,
    spy_log: SpyLog,
    stub_store: StubStore,
    subject: DecoyCore,
) -> None:
    """It should be able to register a new stubbing."""
    rehearsal = WhenRehearsal(spy_id=1,
                              spy_name="my_spy",
                              payload=SpyCall(args=(), kwargs={}))
    decoy.when(spy_log.consume_when_rehearsal(
        ignore_extra_args=True)).then_return(rehearsal)

    result = subject.when("__rehearsal__", ignore_extra_args=True)
    result.then_return("hello")

    decoy.verify(
        stub_store.add(
            rehearsal=rehearsal,
            behavior=StubBehavior(return_value="hello", once=False),
        ))
Exemplo n.º 14
0
def test_handle_prop_get_with_action(
    decoy: Decoy,
    spy_log: SpyLog,
    stub_store: StubStore,
    subject: CallHandler,
) -> None:
    """It should trigger a prop get stub's configured action."""
    action = decoy.mock()
    spy_call = SpyEvent(
        spy_id=42,
        spy_name="spy_name",
        payload=SpyPropAccess(prop_name="prop",
                              access_type=PropAccessType.GET),
    )
    behavior = StubBehavior(action=action)

    decoy.when(stub_store.get_by_call(spy_call)).then_return(behavior)
    decoy.when(action()).then_return("hello world")

    result = subject.handle(spy_call)

    assert result == CallHandlerResult("hello world")
Exemplo n.º 15
0
def test_when_then_do(
    decoy: Decoy,
    spy_log: SpyLog,
    stub_store: StubStore,
    subject: DecoyCore,
) -> None:
    """It should add an action behavior to a stub."""
    rehearsal = WhenRehearsal(spy_id=1,
                              spy_name="my_spy",
                              payload=SpyCall(args=(), kwargs={}))
    decoy.when(spy_log.consume_when_rehearsal(
        ignore_extra_args=False)).then_return(rehearsal)

    action = lambda: "hello world"  # noqa: E731
    result = subject.when("__rehearsal__", ignore_extra_args=False)
    result.then_do(action)

    decoy.verify(
        stub_store.add(
            rehearsal=rehearsal,
            behavior=StubBehavior(action=action),
        ))
Exemplo n.º 16
0
def test_when_then_raise(
    decoy: Decoy,
    spy_log: SpyLog,
    stub_store: StubStore,
    subject: DecoyCore,
) -> None:
    """It should add a raise behavior to a stub."""
    rehearsal = WhenRehearsal(spy_id=1,
                              spy_name="my_spy",
                              payload=SpyCall(args=(), kwargs={}))
    decoy.when(spy_log.consume_when_rehearsal(
        ignore_extra_args=False)).then_return(rehearsal)

    error = RuntimeError("oh no")
    result = subject.when("__rehearsal__", ignore_extra_args=False)
    result.then_raise(error)

    decoy.verify(
        stub_store.add(
            rehearsal=rehearsal,
            behavior=StubBehavior(error=error),
        ))