예제 #1
0
def test_mock_result_assert_rpc_called_times(mock_result: testing.MockResult,
                                             method_name):
    assert_rpc_called = getattr(mock_result, method_name)
    mock_result.rpc.rpcs = [
        (RpcMessage(api_name="api", procedure_name="rpc"), {}),
        (RpcMessage(api_name="api", procedure_name="rpc"), {}),
    ]

    # No error
    try:
        assert_rpc_called("api.rpc")
    except AssertionError as e:
        assert False, f"{method_name} incorrectly raised an assertion error: {e}"

    # No error
    try:
        assert_rpc_called("api.rpc", times=2)
    except AssertionError as e:
        assert False, f"{method_name} incorrectly raised an assertion error: {e}"

    # Error
    with pytest.raises(AssertionError):
        assert_rpc_called("api.rpc", times=0)

    # Error
    with pytest.raises(AssertionError):
        assert_rpc_called("api.rpc", times=1)

    # Error
    with pytest.raises(AssertionError):
        assert_rpc_called("api.rpc", times=3)
예제 #2
0
def test_mock_result_rpc_names_fired(mock_result: testing.MockResult,
                                     property_name):
    mock_result.event.events = [
        (RpcMessage(api_name="api", procedure_name="rpc"), {}),
        (RpcMessage(api_name="api2", procedure_name="rpc2"), {}),
    ]
    assert getattr(mock_result, property_name) == ["api.rpc", "api2.rpc2"]
예제 #3
0
def test_mock_result_get_rpc_messages_filtered(mock_result: testing.MockResult,
                                               method_name):
    get_rpc_messages = getattr(mock_result, method_name)

    rpc1 = RpcMessage(api_name="api", procedure_name="rpc1")
    rpc2 = RpcMessage(api_name="api", procedure_name="rpc2")

    mock_result.rpc.rpcs = [(rpc1, {}), (rpc2, {})]

    assert get_rpc_messages("api.rpc2") == [rpc2]
예제 #4
0
def test_mock_result_rpc_names_called(mock_result: testing.MockResult, property_name):
    rpc_message1 = RpcMessage(api_name="api", procedure_name="rpc")
    rpc_message2 = RpcMessage(api_name="api2", procedure_name="rpc2")
    # fmt: off
    mock_result.mocker_context.rpc_result.to_transport.put_items = [
        (
            CallRpcCommand(message=rpc_message1, options={}),
            None,
        ), (
            CallRpcCommand(message=rpc_message2, options={}),
            None,
        ),
    ]
    # fmt: on
    assert getattr(mock_result, property_name) == ["api.rpc", "api2.rpc2"]
예제 #5
0
def test_rpc_validate_incoming(create_bus_client_with_unhappy_schema):
    client: BusClient = create_bus_client_with_unhappy_schema()

    message = RpcMessage(api_name="api", procedure_name="proc", kwargs={"p": 1})
    with pytest.raises(ValidationError):
        validate_outgoing(config=client.config, schema=client.schema, message=message)
    jsonschema.validate.assert_called_with({"p": 1}, {"p": {}})
예제 #6
0
def test_validate_strict_missing_api(create_bus_client_with_unhappy_schema):
    client: BusClient = create_bus_client_with_unhappy_schema(strict_validation=True)

    # Using 'missing_api', which there is no schema for, so it
    # raise an error as strict_validation=True
    message = RpcMessage(api_name="missing_api", procedure_name="proc", kwargs={"p": 1})
    with pytest.raises(UnknownApi):
        validate_outgoing(config=client.config, schema=client.schema, message=message)
예제 #7
0
def test_validate_non_strict(create_bus_client_with_unhappy_schema):
    client: BusClient = create_bus_client_with_unhappy_schema(strict_validation=False)

    # Using 'missing_api', which there is no schema for, so it
    # should validate just fine as strict_validation=False
    # (albeit with a warning)
    message = RpcMessage(api_name="missing_api", procedure_name="proc", kwargs={"p": 1})
    validate_outgoing(config=client.config, schema=client.schema, message=message)
예제 #8
0
def test_validate_disabled(create_bus_client_with_unhappy_schema):
    client: BusClient = create_bus_client_with_unhappy_schema(validate=False)

    message = RpcMessage(api_name="api",
                         procedure_name="proc",
                         kwargs={"p": 1})
    validate_outgoing(config=client.config,
                      schema=client.schema,
                      message=message)
예제 #9
0
def test_mock_result_get_rpc_messages_filtered(mock_result: testing.MockResult, method_name):
    get_rpc_messages = getattr(mock_result, method_name)

    rpc_message1 = RpcMessage(api_name="api", procedure_name="rpc1")
    rpc_message2 = RpcMessage(api_name="api", procedure_name="rpc2")
    # fmt: off
    mock_result.mocker_context.rpc_result.to_transport.put_items = [
        (
            CallRpcCommand(message=rpc_message1, options={}),
            None,
        ), (
            CallRpcCommand(message=rpc_message2, options={}),
            None,
        ),
    ]
    # fmt: on

    assert get_rpc_messages("api.rpc2") == [rpc_message2]
예제 #10
0
def test_validate_disabled(create_bus_client_with_unhappy_schema):
    client: BusClient = create_bus_client_with_unhappy_schema(validate=False)

    message = RpcMessage(api_name="api",
                         procedure_name="proc",
                         kwargs={"p": 1})
    client._validate(message,
                     direction="outgoing",
                     api_name="api",
                     procedure_name="proc")
예제 #11
0
def test_mock_result_assert_rpc_called_times(mock_result: testing.MockResult,
                                             method_name):
    assert_rpc_called = getattr(mock_result, method_name)

    rpc_message1 = RpcMessage(api_name="api", procedure_name="rpc")
    rpc_message2 = RpcMessage(api_name="api", procedure_name="rpc")
    # fmt: off
    mock_result.mocker_context.rpc_result.to_transport.put_items = [
        (
            CallRpcCommand(message=rpc_message1, options={}),
            None,
        ),
        (
            CallRpcCommand(message=rpc_message2, options={}),
            None,
        ),
    ]
    # fmt: on

    # No error
    try:
        assert_rpc_called("api.rpc")
    except AssertionError as e:
        assert False, f"{method_name} incorrectly raised an assertion error: {e}"

    # No error
    try:
        assert_rpc_called("api.rpc", times=2)
    except AssertionError as e:
        assert False, f"{method_name} incorrectly raised an assertion error: {e}"

    # Error
    with pytest.raises(AssertionError):
        assert_rpc_called("api.rpc", times=0)

    # Error
    with pytest.raises(AssertionError):
        assert_rpc_called("api.rpc", times=1)

    # Error
    with pytest.raises(AssertionError):
        assert_rpc_called("api.rpc", times=3)
예제 #12
0
def test_validate_outgoing_enabled(create_bus_client_with_unhappy_schema):
    client: BusClient = create_bus_client_with_unhappy_schema(
        validate={"outgoing": True})

    message = RpcMessage(api_name="api",
                         procedure_name="proc",
                         kwargs={"p": 1})
    with pytest.raises(ValidationError):
        validate_outgoing(config=client.config,
                          schema=client.schema,
                          message=message)
예제 #13
0
def test_validate_outgoing_enabled(create_bus_client_with_unhappy_schema):
    client: BusClient = create_bus_client_with_unhappy_schema(
        validate={"outgoing": True})

    message = RpcMessage(api_name="api",
                         procedure_name="proc",
                         kwargs={"p": 1})
    with pytest.raises(ValidationError):
        client._validate(message,
                         direction="outgoing",
                         api_name="api",
                         procedure_name="proc")
예제 #14
0
def test_mock_result_assert_rpc_not_called(mock_result: testing.MockResult,
                                           method_name):
    assert_rpc_not_called = getattr(mock_result, method_name)
    mock_result.rpc.rpcs = [(RpcMessage(api_name="api",
                                        procedure_name="rpc"), {})]

    # No exception
    try:
        assert_rpc_not_called("api.bad_rpc")
    except AssertionError as e:
        assert False, f"{method_name} incorrectly raised an assertion error: {e}"

    with pytest.raises(AssertionError):
        assert_rpc_not_called("api.rpc")