1import sys
2
3import pytest
4
5import samples.greeter_pb2 as greeter_protos
6
7
8
9async def test_grpc_client_mock_say_hello(service_client, mock_grpc_greeter):
10 @mock_grpc_greeter('SayHello')
11 async def _mock_say_hello(request, context):
12 return greeter_protos.GreetingResponse(
13 greeting=f'Hello, {request.name} from mockserver!',
14 )
15
16 response = await service_client.post('/hello?case=say_hello', data='tests')
17 assert response.status == 200
18 assert 'text/plain' in response.headers['Content-Type']
19 assert response.text == 'Hello, tests from mockserver!'
20 assert _mock_say_hello.times_called == 1
21
22
23
24
25
26
27async def test_grpc_client_mock_say_hello_response_stream(
28 service_client, mock_grpc_greeter,
29):
30 @mock_grpc_greeter('SayHelloResponseStream')
31 async def _mock_say_hello_response_stream(request, context):
32 message = f'Hello, {request.name}'
33 for i in range(5):
34 message += '!'
35 yield greeter_protos.GreetingResponse(greeting=message)
36
37 response = await service_client.post(
38 '/hello?case=say_hello_response_stream', data='Python',
39 )
40 assert response.status == 200
41 assert 'text/plain' in response.headers['Content-Type']
42 assert (
43 response.text
44 == """Hello, Python!
45Hello, Python!!
46Hello, Python!!!
47Hello, Python!!!!
48Hello, Python!!!!!
49"""
50 )
51 assert _mock_say_hello_response_stream.times_called == 1
52
53
54
55
56
57
58async def test_grpc_client_mock_say_hello_request_stream(
59 service_client, mock_grpc_greeter,
60):
61 @mock_grpc_greeter('SayHelloRequestStream')
62 async def _mock_say_hello_request_stream(request_iterator, context):
63 message = 'Hello, '
64 async for request in request_iterator:
65 message += f'{request.name}'
66 return greeter_protos.GreetingResponse(greeting=message)
67
68 response = await service_client.post(
69 '/hello?case=say_hello_request_stream', data='Python\n!\n!\n!',
70 )
71 assert response.status == 200
72 assert 'text/plain' in response.headers['Content-Type']
73 assert response.text == 'Hello, Python!!!'
74 assert _mock_say_hello_request_stream.times_called == 1
75
76
77
78
79
80
81async def test_grpc_client_mock_say_hello_streams(
82 service_client, mock_grpc_greeter,
83):
84 @mock_grpc_greeter('SayHelloStreams')
85 async def _mock_say_hello_streams(request_iterator, context):
86 message = 'Hello, '
87 async for request in request_iterator:
88 message += f'{request.name}'
89 yield greeter_protos.GreetingResponse(greeting=message)
90
91 response = await service_client.post(
92 '/hello?case=say_hello_streams', data='Python\n!\n!\n!',
93 )
94 assert response.status == 200
95 assert 'text/plain' in response.headers['Content-Type']
96 assert (
97 response.text
98 == """Hello, Python
99Hello, Python!
100Hello, Python!!
101Hello, Python!!!
102"""
103 )
104 assert _mock_say_hello_streams.times_called == 1
105
106
107
108
109
110@pytest.mark.skipif(
111 sys.platform == 'darwin', reason='this test fails in old packages',
112)
113
114async def test_say_hello(grpc_client):
115 request = greeter_protos.GreetingRequest(name='Python')
116 response = await grpc_client.SayHello(request)
117 assert response.greeting == 'Hello, Python!'
118
119
120
121
122
123
124async def test_say_hello_response_stream(grpc_client):
125 request = greeter_protos.GreetingRequest(name='Python')
126 message = 'Hello, Python'
127 async for response in grpc_client.SayHelloResponseStream(request):
128 message += '!'
129 assert response.greeting == message
130
131
132
133
134
135
136async def test_say_hello_request_stream(grpc_client):
137 request = (
138 greeter_protos.GreetingRequest(name=name)
139 for name in ['Python', '!', '!', '!']
140 )
141 response = await grpc_client.SayHelloRequestStream(request)
142 assert response.greeting == 'Hello, Python!!!'
143
144
145
146
147
148
149async def test_say_hello_streams(grpc_client):
150 requests = (
151 greeter_protos.GreetingRequest(name=name)
152 for name in ['Python', '!', '!', '!']
153 )
154 message = 'Hello, Python'
155 async for response in grpc_client.SayHelloStreams(requests):
156 assert response.greeting == message
157 message += '!'
158
159
160