Example #1
0
def test_returns_json(request_builder, mocker):
    mock_response = mocker.Mock()
    mock_response.json.return_value = {"key": "value"}
    request_builder.get_converter.return_value = str
    returns_json = returns.json(str, ())
    request_builder.return_type = returns.ReturnType.with_decorator(
        None, returns_json)
    returns_json.modify_request(request_builder)
    assert isinstance(request_builder.return_type, returns.ReturnType)
    assert callable(request_builder.return_type)
    assert request_builder.return_type(mock_response) == str(
        mock_response.json())

    # Verify: Idempotent
    returns_json.modify_request(request_builder)
    assert isinstance(request_builder.return_type, returns.ReturnType)
    assert callable(request_builder.return_type)
    assert request_builder.return_type(mock_response) == str(
        mock_response.json())

    # Verify: Doesn't apply to unsupported types
    request_builder.get_converter.return_value = None
    returns_json = returns.json(str, ())
    request_builder.return_type = returns.ReturnType.with_decorator(
        None, returns_json)
    returns_json.modify_request(request_builder)
    assert not callable(request_builder.return_type)
Example #2
0
def test_returns_json(request_builder):
    request_builder.get_converter.return_value = str
    returns_json = returns.json(str, ())
    returns_json.modify_request(request_builder)
    assert isinstance(request_builder.return_type, returns._StrategyWrapper)

    # Verify: Idempotent
    returns_json.modify_request(request_builder)
    assert isinstance(request_builder.return_type, returns._StrategyWrapper)
    assert request_builder.return_type.unwrap() is str

    # Verify: Doesn't apply to unsupported types
    request_builder.get_converter.return_value = None
    request_builder.return_type = None
    returns_json = returns.json(str, ())
    returns_json.modify_request(request_builder)
    assert request_builder.return_type is None
Example #3
0
 def prepare_test(request_builder, mocker, value=default_value, **kwargs):
     mock_response = mocker.Mock()
     mock_response.json.return_value = value
     request_builder.get_converter.return_value = None
     returns_json = returns.json(**kwargs)
     request_builder.return_type = returns.ReturnType.with_decorator(
         None, returns_json)
     returns_json.modify_request(request_builder)
     return mock_response
Example #4
0
def test_returns_json_builtin_type(request_builder, mocker):
    mock_response = mocker.Mock()
    mock_response.json.return_value = {"key": "1"}
    request_builder.get_converter.return_value = None
    returns_json = returns.json(type=int, key="key")
    request_builder.return_type = returns.ReturnType.with_decorator(
        None, returns_json)
    returns_json.modify_request(request_builder)
    print(request_builder.return_type)
    assert callable(request_builder.return_type)
    assert request_builder.return_type(mock_response) == 1
Example #5
0
def test_create_response_body_converter__returns_json(mocker, message_mock,
                                                      request_definition_mock):
    # Setup
    json_response = {"hello": "world"}
    request_definition_mock.method_annotations = (returns.json(), )
    ParseDict = mocker.patch("google.protobuf.json_format.ParseDict")

    # Run
    run_create_response_body_converter_test(mocker, message_mock,
                                            request_definition_mock,
                                            json_response)

    # Verify
    ParseDict.assert_called_with(json_response, message_mock)
Example #6
0
def test_returns_with_multiple_decorators(request_builder, mocker):
    decorator1 = returns(str)
    decorator2 = returns.json()
    request_builder.get_converter.return_value = str
    first_type = returns.ReturnType.with_decorator(None, decorator1)
    second_type = (
        request_builder.return_type) = returns.ReturnType.with_decorator(
            first_type, decorator2)

    # Verify that the return type doesn't change after being handled by first decorator
    decorator1.modify_request(request_builder)
    assert request_builder.return_type is second_type

    # Verify that the second decorator does handle the return type
    mock_response = mocker.Mock()
    mock_response.json.return_value = {"key": "value"}
    decorator2.modify_request(request_builder)
    assert request_builder.return_type(mock_response) == str(
        mock_response.json())
Example #7
0
    # Verify: not relevant
    request_definition.argument_annotations = ()
    request_definition.method_annotations = ()
    value = getattr(factory, method)(None, request_definition)
    assert value is None

    # Verify: relevant
    request_definition.argument_annotations = (object(), )
    value = getattr(factory, method)(object, request_definition)
    assert callable(value)


@pytest.mark.parametrize(
    "cls, method, decorator",
    [
        (loads.from_json, "create_response_body_converter", returns.json()),
        (dumps.to_json, "create_request_body_converter", json()),
    ],
)
def test_json_builders(mocker, cls, method, decorator, request_definition):
    # Setup
    func = mocker.stub()
    func.return_value = 1
    obj = cls(object)
    factory = obj.using(func)

    # Verify: not relevant
    request_definition.argument_annotations = ()
    request_definition.method_annotations = ()
    value = getattr(factory, method)(object, request_definition)
    assert value is None