fix: support pydantic-version google pb (#568)
* feat: pydantic version of google pb * fix: patch pb Struct to support json, dict rountrip * fix: pydantic-version google pb, json, dict rntrip * chore: remove `@generated`, remove gen, code fmt * chore: test case for pydantic-version google pb
This commit is contained in:
@@ -35,7 +35,48 @@ def test_reference_google_wellknown_types_non_wrappers(
|
||||
google_type: str, expected_name: str, expected_import: str
|
||||
):
|
||||
imports = set()
|
||||
name = get_type_reference(package="", imports=imports, source_type=google_type)
|
||||
name = get_type_reference(
|
||||
package="", imports=imports, source_type=google_type, pydantic=False
|
||||
)
|
||||
|
||||
assert name == expected_name
|
||||
assert imports.__contains__(
|
||||
expected_import
|
||||
), f"{expected_import} not found in {imports}"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
["google_type", "expected_name", "expected_import"],
|
||||
[
|
||||
(
|
||||
".google.protobuf.Empty",
|
||||
'"betterproto_lib_pydantic_google_protobuf.Empty"',
|
||||
"import betterproto.lib.pydantic.google.protobuf as betterproto_lib_pydantic_google_protobuf",
|
||||
),
|
||||
(
|
||||
".google.protobuf.Struct",
|
||||
'"betterproto_lib_pydantic_google_protobuf.Struct"',
|
||||
"import betterproto.lib.pydantic.google.protobuf as betterproto_lib_pydantic_google_protobuf",
|
||||
),
|
||||
(
|
||||
".google.protobuf.ListValue",
|
||||
'"betterproto_lib_pydantic_google_protobuf.ListValue"',
|
||||
"import betterproto.lib.pydantic.google.protobuf as betterproto_lib_pydantic_google_protobuf",
|
||||
),
|
||||
(
|
||||
".google.protobuf.Value",
|
||||
'"betterproto_lib_pydantic_google_protobuf.Value"',
|
||||
"import betterproto.lib.pydantic.google.protobuf as betterproto_lib_pydantic_google_protobuf",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_reference_google_wellknown_types_non_wrappers_pydantic(
|
||||
google_type: str, expected_name: str, expected_import: str
|
||||
):
|
||||
imports = set()
|
||||
name = get_type_reference(
|
||||
package="", imports=imports, source_type=google_type, pydantic=True
|
||||
)
|
||||
|
||||
assert name == expected_name
|
||||
assert imports.__contains__(
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import json
|
||||
|
||||
from betterproto.lib.google.protobuf import Struct
|
||||
from betterproto.lib.pydantic.google.protobuf import Struct as StructPydantic
|
||||
|
||||
|
||||
def test_struct_roundtrip():
|
||||
@@ -22,3 +23,14 @@ def test_struct_roundtrip():
|
||||
assert struct_from_json.to_dict() == data
|
||||
assert struct_from_json == struct_from_dict
|
||||
assert struct_from_json.to_json() == data_json
|
||||
|
||||
struct_pyd_from_dict = StructPydantic(fields={}).from_dict(data)
|
||||
assert struct_pyd_from_dict.fields == data
|
||||
assert struct_pyd_from_dict.to_dict() == data
|
||||
assert struct_pyd_from_dict.to_json() == data_json
|
||||
|
||||
struct_pyd_from_dict = StructPydantic(fields={}).from_json(data_json)
|
||||
assert struct_pyd_from_dict.fields == data
|
||||
assert struct_pyd_from_dict.to_dict() == data
|
||||
assert struct_pyd_from_dict == struct_pyd_from_dict
|
||||
assert struct_pyd_from_dict.to_json() == data_json
|
||||
|
Reference in New Issue
Block a user