8000 renamed ServiceImplementation; refactored test · danielgtaylor/python-betterproto@fcd3f9d · GitHub
[go: up one dir, main page]

Skip to content

Commit fcd3f9d

Browse files
committed
renamed ServiceImplementation; refactored test
1 parent a54d0ce commit fcd3f9d

File tree

5 files changed

+39
-52
lines changed

5 files changed

+39
-52
lines changed

src/betterproto/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from ._types import T
2626
from .casing import camel_case, safe_snake_case, snake_case
2727
from .grpc.grpclib_client import ServiceStub
28-
from .grpc.grpclib_server import ServiceImplementation
2928

3029
if sys.version_info[:2] < (3, 7):
3130
# Apply backport of datetime.fromisoformat from 3.7

src/betterproto/grpc/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .grpclib_server import ServiceBase

src/betterproto/grpc/grpclib_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import grpclib.server
77

88

9-
class ServiceImplementation(ABC):
9+
class ServiceBase(ABC):
1010
"""
1111
Base class for async gRPC servers.
1212
"""

src/betterproto/templates/template.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub):
155155
{% endfor %}
156156

157157
{% for service in output_file.services %}
158-
class {{ service.py_name }}Implementation(betterproto.ServiceImplementation):
158+
class {{ service.py_name }}Base(betterproto.grpc.ServiceBase):
159159
{% if service.comment %}
160160
{{ service.comment }}
161161

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import asyncio
21
from typing import AsyncIterator, AsyncIterable
32

4-
from grpclib.client import Channel
5-
from grpclib.server import Server
3+
import pytest
4+
from grpclib.testing import ChannelFor
65

76
from tests.output_betterproto.example_service.example_service import (
8-
ExampleServiceImplementation,
7+
ExampleServiceBase,
98
ExampleServiceStub,
109
ExampleRequest,
1110
ExampleResponse,
1211
)
1312

1413

15-
class ExampleService(ExampleServiceImplementation):
14+
class ExampleService(ExampleServiceBase):
1615
async def example_unary_unary(
1716
self, example_string: str, example_integer: int
1817
) -> "ExampleResponse":
@@ -51,58 +50,46 @@ async def example_stream_stream(
5150
)
5251

5352

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():
5855
test_string = "test string"
5956
test_int = 42
6057

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+
)
8266
assert response.example_string == test_string
8367
assert response.example_integer == test_int
8468

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+
)
9582

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
9987

100-
# stream stream
101-
async for response in stub.example_stream_stream(request_iterator()):
88+
response = await stub.example_stream_unary(request_iterator())
10289
assert response.example_string == test_string
10390
assert response.example_integer == test_int
10491

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

Comments
 (0)
0