forked from petercable/grpc_requests
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstub_client_test.py
81 lines (60 loc) · 2.71 KB
/
stub_client_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import logging
import pytest
from grpc_requests.client import StubClient
from test_servers.helloworld.helloworld_pb2 import _GREETER
from google.protobuf.json_format import ParseError
"""
Test cases for reflection based client
"""
logger = logging.getLogger("name")
@pytest.fixture(scope="module")
def helloworld_stub_client():
try:
client = StubClient("localhost:50051", [_GREETER])
yield client
except: # noqa: E722
pytest.fail("Could not connect to local HelloWorld server")
def test_unary_unary(helloworld_stub_client):
response = helloworld_stub_client.unary_unary(
"helloworld.Greeter", "SayHello", {"name": "sinsky"}
)
assert isinstance(response, dict)
assert response == {"message": "Hello, sinsky!"}
def test_empty_body_request(helloworld_stub_client):
response = helloworld_stub_client.unary_unary("helloworld.Greeter", "SayHello", {})
logger.warning(f"Response: {response}")
assert isinstance(response, dict)
def test_nonexistent_service(helloworld_stub_client):
with pytest.raises(ValueError):
helloworld_stub_client.unary_unary("helloworld.Speaker", "SingHello", {})
def test_nonexistent_method(helloworld_stub_client):
with pytest.raises(ValueError):
helloworld_stub_client.unary_unary("helloworld.Greeter", "SayGoodbye", {})
def test_unsupported_argument(helloworld_stub_client):
with pytest.raises(ParseError):
helloworld_stub_client.unary_unary(
"helloworld.Greeter", "SayHello", {"foo": "bar"}
)
def test_unary_stream(helloworld_stub_client):
name_list = ["sinsky", "viridianforge", "jack", "harry"]
responses = helloworld_stub_client.unary_stream(
"helloworld.Greeter", "SayHelloGroup", {"name": "".join(name_list)}
)
assert all(isinstance(response, dict) for response in responses)
for response, name in zip(responses, name_list):
assert response == {"message": f"Hello, {name}!"}
def test_stream_unary(helloworld_stub_client):
name_list = ["sinsky", "viridianforge", "jack", "harry"]
response = helloworld_stub_client.stream_unary(
"helloworld.Greeter", "HelloEveryone", [{"name": name} for name in name_list]
)
assert isinstance(response, dict)
assert response == {"message": f'Hello, {" ".join(name_list)}!'}
def test_stream_stream(helloworld_stub_client):
name_list = ["sinsky", "viridianforge", "jack", "harry"]
responses = helloworld_stub_client.stream_stream(
"helloworld.Greeter", "SayHelloOneByOne", [{"name": name} for name in name_list]
)
assert all(isinstance(response, dict) for response in responses)
for response, name in zip(responses, name_list):
assert response == {"message": f"Hello, {name}!"}