Skip to content

Commit 1918c88

Browse files
committed
chore: test case for pydantic-version google pb
1 parent 536cf24 commit 1918c88

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

tests/test_get_ref_type.py

+39
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,45 @@ def test_reference_google_wellknown_types_non_wrappers(
4545
), f"{expected_import} not found in {imports}"
4646

4747

48+
@pytest.mark.parametrize(
49+
["google_type", "expected_name", "expected_import"],
50+
[
51+
(
52+
".google.protobuf.Empty",
53+
'"betterproto_lib_pydantic_google_protobuf.Empty"',
54+
"import betterproto.lib.pydantic.google.protobuf as betterproto_lib_pydantic_google_protobuf",
55+
),
56+
(
57+
".google.protobuf.Struct",
58+
'"betterproto_lib_pydantic_google_protobuf.Struct"',
59+
"import betterproto.lib.pydantic.google.protobuf as betterproto_lib_pydantic_google_protobuf",
60+
),
61+
(
62+
".google.protobuf.ListValue",
63+
'"betterproto_lib_pydantic_google_protobuf.ListValue"',
64+
"import betterproto.lib.pydantic.google.protobuf as betterproto_lib_pydantic_google_protobuf",
65+
),
66+
(
67+
".google.protobuf.Value",
68+
'"betterproto_lib_pydantic_google_protobuf.Value"',
69+
"import betterproto.lib.pydantic.google.protobuf as betterproto_lib_pydantic_google_protobuf",
70+
),
71+
],
72+
)
73+
def test_reference_google_wellknown_types_non_wrappers_pydantic(
74+
google_type: str, expected_name: str, expected_import: str
75+
):
76+
imports = set()
77+
name = get_type_reference(
78+
package="", imports=imports, source_type=google_type, pydantic=True
79+
)
80+
81+
assert name == expected_name
82+
assert imports.__contains__(
83+
expected_import
84+
), f"{expected_import} not found in {imports}"
85+
86+
4887
@pytest.mark.parametrize(
4988
["google_type", "expected_name"],
5089
[

tests/test_struct.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22

33
from betterproto.lib.google.protobuf import Struct
4+
from betterproto.lib.pydantic.google.protobuf import Struct as StructPydantic
45

56

67
def test_struct_roundtrip():
@@ -22,3 +23,14 @@ def test_struct_roundtrip():
2223
assert struct_from_json.to_dict() == data
2324
assert struct_from_json == struct_from_dict
2425
assert struct_from_json.to_json() == data_json
26+
27+
struct_pyd_from_dict = StructPydantic(fields={}).from_dict(data)
28+
assert struct_pyd_from_dict.fields == data
29+
assert struct_pyd_from_dict.to_dict() == data
30+
assert struct_pyd_from_dict.to_json() == data_json
31+
32+
struct_pyd_from_dict = StructPydantic(fields={}).from_json(data_json)
33+
assert struct_pyd_from_dict.fields == data
34+
assert struct_pyd_from_dict.to_dict() == data
35+
assert struct_pyd_from_dict == struct_pyd_from_dict
36+
assert struct_pyd_from_dict.to_json() == data_json

0 commit comments

Comments
 (0)