Skip to content

Commit c50d9e2

Browse files
Add test for generating embedded wellknown types in outputs.
1 parent 35548cb commit c50d9e2

File tree

5 files changed

+71
-4
lines changed

5 files changed

+71
-4
lines changed

betterproto/tests/inputs/googletypes_response/googletypes_response.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ syntax = "proto3";
22

33
import "google/protobuf/wrappers.proto";
44

5-
// Tests that wrapped return values can be used
5+
// Tests that wrapped values can be used directly as return values
66

77
service Test {
88
rpc GetDouble (Input) returns (google.protobuf.DoubleValue);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
syntax = "proto3";
2+
3+
import "google/protobuf/wrappers.proto";
4+
5+
// Tests that wrapped values are supported as part of output message
6+
service Test {
7+
rpc getOutput (Input) returns (Output);
8+
}
9+
10+
message Input {
11+
12+
}
13+
14+
message Output {
15+
google.protobuf.DoubleValue double_value = 1;
16+
google.protobuf.FloatValue float_value = 2;
17+
google.protobuf.Int64Value int64_value = 3;
18+
google.protobuf.UInt64Value uint64_value = 4;
19+
google.protobuf.Int32Value int32_value = 5;
20+
google.protobuf.UInt32Value uint32_value = 6;
21+
google.protobuf.BoolValue bool_value = 7;
22+
google.protobuf.StringValue string_value = 8;
23+
google.protobuf.BytesValue bytes_value = 9;
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pytest
2+
3+
from betterproto.tests.mocks import MockChannel
4+
from betterproto.tests.output_betterproto.googletypes_response_embedded.googletypes_response_embedded import (
5+
Output,
6+
TestStub,
7+
)
8+
9+
10+
@pytest.mark.asyncio
11+
async def test_service_passes_through_unwrapped_values_embedded_in_response():
12+
"""
13+
We do not not need to implement value unwrapping for embedded well-known types,
14+
as this is already handled by grpclib. This test merely shows that this is the case.
15+
"""
16+
output = Output(
17+
double_value=10.0,
18+
float_value=12.0,
19+
int64_value=-13,
20+
uint64_value=14,
21+
int32_value=-15,
22+
uint32_value=16,
23+
bool_value=True,
24+
string_value="string",
25+
bytes_value=bytes(0xFF)[0:4],
26+
)
27+
28+
service = TestStub(MockChannel(responses=[output]))
29+
response = await service.get_output()
30+
31+
assert response.double_value == 10.0
32+
assert response.float_value == 12.0
33+
assert response.int64_value == -13
34+
assert response.uint64_value == 14
35+
assert response.int32_value == -15
36+
assert response.uint32_value == 16
37+
assert response.bool_value
38+
assert response.string_value == "string"
39+
assert response.bytes_value == bytes(0xFF)[0:4]

betterproto/tests/mocks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, responses: List) -> None:
2727
self.responses = responses
2828

2929
async def recv_message(self):
30-
return next(self.responses)
30+
return self.responses.pop(0)
3131

3232
async def send_message(self, *args, **kwargs):
3333
pass
@@ -36,4 +36,4 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
3636
return True
3737

3838
async def __aenter__(self):
39-
return True
39+
return self

betterproto/tests/test_inputs.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
from google.protobuf.json_format import Parse
1616

1717

18-
excluded_test_cases = {"googletypes_response", "service"}
18+
excluded_test_cases = {
19+
"googletypes_response",
20+
"googletypes_response_embedded",
21+
"service",
22+
}
1923
test_case_names = {*get_directories(inputs_path)} - excluded_test_cases
2024

2125
plugin_output_package = "betterproto.tests.output_betterproto"

0 commit comments

Comments
 (0)