|
1 |
| -import asyncio |
2 | 1 | from typing import AsyncIterator, AsyncIterable
|
3 | 2 |
|
4 |
| -from grpclib.client import Channel |
5 |
| -from grpclib.server import Server |
| 3 | +import pytest |
| 4 | +from grpclib.testing import ChannelFor |
6 | 5 |
|
7 | 6 | from tests.output_betterproto.example_service.example_service import (
|
8 |
| - ExampleServiceImplementation, |
| 7 | + ExampleServiceBase, |
9 | 8 | ExampleServiceStub,
|
10 | 9 | ExampleRequest,
|
11 | 10 | ExampleResponse,
|
12 | 11 | )
|
13 | 12 |
|
14 | 13 |
|
15 |
| -class ExampleService(ExampleServiceImplementation): |
| 14 | +class ExampleService(ExampleServiceBase): |
16 | 15 | async def example_unary_unary(
|
17 | 16 | self, example_string: str, example_integer: int
|
18 | 17 | ) -> "ExampleResponse":
|
@@ -51,58 +50,46 @@ async def example_stream_stream(
|
51 | 50 | )
|
52 | 51 |
|
53 | 52 |
|
54 |
| -async def async_test_server_start(): |
55 |
| - host = "127.0.0.1" |
56 |
| - port = 13337 |
57 |
| - |
| 53 | +@pytest.mark.asyncio |
| 54 | +async def test_calls_with_different_cardinalities(): |
58 | 55 | test_string = "test string"
|
59 | 56 | test_int = 42
|
60 | 57 |
|
61 |
| - # start server |
62 |
| - server = Server([ExampleService()]) |
63 |
| - await server.start(host, port) |
64 |
| - |
65 |
| - # start client |
66 |
| - channel = Channel(host=host, port=port) |
67 |
| - stub = ExampleServiceStub(channel) |
68 |
| - |
69 |
| - # unary unary |
70 |
| - response = await stub.example_unary_unary( |
71 |
| - example_string="test string", |
72 |
| - example_integer=42, |
73 |
| - ) |
74 |
| - assert response.example_string == test_string |
75 |
| - assert response.example_integer == test_int |
76 |
| - |
77 |
| - # unary stream |
78 |
| - async for response in stub.example_unary_stream( |
79 |
| - example_string="test string", |
80 |
| - example_integer=42, |
81 |
| - ): |
| 58 | + async with ChannelFor([ExampleService()]) as channel: |
| 59 | + stub = ExampleServiceStub(channel) |
| 60 | + |
| 61 | + # unary unary |
| 62 | + response = await stub.example_unary_unary( |
| 63 | + example_string="test string", |
| 64 | + example_integer=42, |
| 65 | + ) |
82 | 66 | assert response.example_string == test_string
|
83 | 67 | assert response.example_integer == test_int
|
84 | 68 |
|
85 |
| - # stream unary |
86 |
| - request = ExampleRequest( |
87 |
| - example_string=test_string, |
88 |
| - example_integer=42, |
89 |
| - ) |
90 |
| - |
91 |
| - async def request_iterator(
1E0A
): |
92 |
| - yield request |
93 |
| - yield request |
94 |
| - yield request |
| 69 | + # unary stream |
| 70 | + async for response in stub.example_unary_stream( |
| 71 | + example_string="test string", |
| 72 | + example_integer=42, |
| 73 | + ): |
| 74 | + assert response.example_string == test_string |
| 75 | + assert response.example_integer == test_int |
| 76 | + |
| 77 | + # stream unary |
| 78 | + request = ExampleRequest( |
| 79 | + example_string=test_string, |
| 80 | + example_integer=42, |
| 81 | + ) |
95 | 82 |
|
96 |
| - response = await stub.example_stream_unary(request_iterator()) |
97 |
| - assert response.example_string == test_string |
98 |
| - assert response.example_integer == test_int |
| 83 | + async def request_iterator(): |
| 84 | + yield request |
| 85 | + yield request |
| 86 | + yield request |
99 | 87 |
|
100 |
| - # stream stream |
101 |
| - async for response in stub.example_stream_stream(request_iterator()): |
| 88 | + response = await stub.example_stream_unary(request_iterator()) |
102 | 89 | assert response.example_string == test_string
|
103 | 90 | assert response.example_integer == test_int
|
104 | 91 |
|
105 |
| - |
106 |
| -def test_server_start(): |
107 |
| - loop = asyncio.get_event_loop() |
108 |
| - loop.run_until_complete(async_test_server_start()) |
| 92 | + # stream stream |
| 93 | + async for response in stub.example_stream_stream(request_iterator()): |
| 94 | + assert response.example_string == test_string |
| 95 | + assert response.example_integer == test_int |
0 commit comments