Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
@uplink.returns(User)
@uplink.get("/users/{user}")
def get_user(self, user):
pass
def test_returns(request_builder):
custom = returns(str)
request_builder.get_converter.return_value = str
request_builder.return_type = returns.ReturnType.with_decorator(
None, custom
)
custom.modify_request(request_builder)
assert request_builder.return_type(2) == "2"
(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
@uplink.returns.schema(uplink.types.List[Repo])
@uplink.get("/users/{user}/repos")
def get_repos(self, user):
pass
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"}
@uplink.returns.json(member=("data", "repository"))
@uplink.args(body=uplink.Body)
@graphql
def get_repository(self, **body):
pass
@uplink.returns.json
@uplink.get("/users/{user}/repos/{repo}")
def get_repo(self, user, repo):
pass
@uplink.returns.json
@uplink.get("/users/{user}/repos/{repo}")
def get_repo(self, user, repo):
pass
import inspect
# Third party imports
from google.protobuf import message
from uplink import converters, returns, json
# Local imports
from uplink_protobuf import helpers, json_options
__all__ = ["ProtocolBuffersConverter"]
class ProtocolBuffersConverter(converters.Factory):
__DECODING_STRATEGIES = {
returns.json: json_options.parse_json
}
__ENCODING_STRATEGIES = {
json: json_options.convert_message_to_dict
}
# === BEGIN Helpers === #
@staticmethod
def is_protocol_buffer_class(cls):
"""
Returns whether or not the given `cls` is a protobuf message
subclass.
"""
return inspect.isclass(cls) and issubclass(cls, message.Message)