-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a synchronous grpc client #647
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
""" Testing the sync version of the client stubs. | ||
|
||
This is not testing the lower level grpc calls, but rather the generated client stubs. | ||
So instead of creating a real service and a real grpc channel, | ||
we are going to mock the channel and simply test the client. | ||
|
||
If we wanted to test without mocking we would need to use all the machinery here: | ||
https://github.com/grpc/grpc/blob/master/src/python/grpcio_tests/tests/testing/_client_test.py | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or I could spawn an actual server. I would think that's the best testing, but it is not done so in the rest of the code (which uses the nice grpclib |
||
|
||
""" | ||
|
||
import re | ||
from sys import version | ||
from tests.output_betterproto.service import ( | ||
DoThingRequest, | ||
DoThingResponse, | ||
GetThingRequest, | ||
GetThingResponse, | ||
TestSyncStub as ThingServiceClient, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the new synchronous stub. |
||
) | ||
|
||
|
||
class ChannelMock: | ||
"""channel.unary_unary( | ||
"/service.Test/DoThing", | ||
DoThingRequest.SerializeToString, | ||
DoThingResponse.FromString, | ||
)(do_thing_request) | ||
the method calls the serialize, then use the deserialize and returns the response""" | ||
|
||
def unary_unary(self, route, request_serializer, response_deserializer): | ||
"""mock the unary_unary call""" | ||
def _unary_unary(req): | ||
return response_deserializer(request_serializer(req)) | ||
return _unary_unary | ||
|
||
def stream_unary(self, route, request_serializer, response_deserializer): | ||
"""mock the stream_unary call""" | ||
def _stream_unary(req): | ||
return response_deserializer(request_serializer(next(req))) | ||
return _stream_unary | ||
|
||
def stream_stream(self, route, request_serializer, response_deserializer): | ||
"""mock the stream_stream call""" | ||
def _stream_stream(req): | ||
return (response_deserializer(request_serializer(r)) for r in req) | ||
return _stream_stream | ||
|
||
def unary_stream(self, route, request_serializer, response_deserializer): | ||
"""mock the unary_stream call""" | ||
def _unary_stream(req): | ||
return iter([response_deserializer(request_serializer(req))]*6) | ||
return _unary_stream | ||
|
||
|
||
def test_do_thing_call(mocker): | ||
"""mock the channel and test the client stub""" | ||
client = ThingServiceClient(channel=ChannelMock()) | ||
response = client.do_thing(DoThingRequest(name="clean room")) | ||
assert response.names == ["clean room"] | ||
|
||
def test_do_many_things_call(mocker): | ||
"""mock the channel and test the client stub""" | ||
client = ThingServiceClient(channel=ChannelMock()) | ||
response = client.do_many_things(iter([ | ||
DoThingRequest(name="only"), | ||
DoThingRequest(name="room")])) | ||
assert response == DoThingResponse(names=["only"]) #protobuf is stunning | ||
|
||
def test_get_thing_versions_call(mocker): | ||
"""mock the channel and test the client stub""" | ||
client = ThingServiceClient(channel=ChannelMock()) | ||
response = client.get_thing_versions(GetThingRequest(name="extra")) | ||
response = list(response) | ||
assert response == [GetThingResponse(name="extra")]*6 | ||
|
||
def test_get_different_things_call(mocker): | ||
"""mock the channel and test the client stub""" | ||
client = ThingServiceClient(channel=ChannelMock()) | ||
response = client.get_different_things([ | ||
GetThingRequest(name="apple"), | ||
GetThingRequest(name="orange")]) | ||
response = list(response) | ||
assert response == [GetThingResponse(name="apple"), GetThingResponse(name="orange")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This and the next diff are in fact fixes for the existing stub generation. If wanted I could split it into a separate PR.