From 4b73f19ca959a4cde2de8d82501031a66dec8ab2 Mon Sep 17 00:00:00 2001 From: SaisakthiM Date: Sun, 11 Oct 2026 09:42:31 +0530 Subject: [PATCH 1/2] changes --- .../tests/test_default_values.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 codegen/opentelemetry-codegen-json/tests/test_default_values.py diff --git a/codegen/opentelemetry-codegen-json/tests/test_default_values.py b/codegen/opentelemetry-codegen-json/tests/test_default_values.py new file mode 100644 index 00000000000..9db91734ddb --- /dev/null +++ b/codegen/opentelemetry-codegen-json/tests/test_default_values.py @@ -0,0 +1,54 @@ +import pytest +from google.protobuf import descriptor_pb2 as descriptor + +from opentelemetry.codegen.json.types import ( + PROTO_DEFAULTS, + PROTO_TO_PYTHON, + get_default_value, + get_python_type, +) + +FDP = descriptor.FieldDescriptorProto + + +@pytest.mark.parametrize( + "proto_type, expected", + [ + (FDP.TYPE_DOUBLE, "0.0"), + (FDP.TYPE_FLOAT, "0.0"), + (FDP.TYPE_INT64, "0"), + (FDP.TYPE_UINT32, "0"), + (FDP.TYPE_SINT64, "0"), + (FDP.TYPE_BOOL, "False"), + (FDP.TYPE_STRING, '""'), + (FDP.TYPE_BYTES, 'b""'), + ], +) +def test_get_default_value_known_types(proto_type, expected): + assert get_default_value(proto_type) == expected + + +@pytest.mark.parametrize( + "proto_type", + [FDP.TYPE_GROUP, FDP.TYPE_MESSAGE, FDP.TYPE_ENUM], +) +def test_get_default_value_valid_types_without_default(proto_type): + assert get_default_value(proto_type) == "None" + + +@pytest.mark.parametrize("proto_type", [0, -1, 999, 2**31]) +def test_get_default_value_out_of_range(proto_type): + assert get_default_value(proto_type) == "None" + + +def test_every_mapped_type_has_a_default(): + # keeps PROTO_TO_PYTHON and PROTO_DEFAULTS from drifting apart + assert set(PROTO_TO_PYTHON) == set(PROTO_DEFAULTS) + + +@pytest.mark.parametrize("proto_type", PROTO_DEFAULTS) +def test_default_is_valid_python_literal(proto_type): + # the default is emitted into generated code, so it must eval cleanly + value = eval(get_default_value(proto_type)) + expected_type = eval(get_python_type(proto_type).replace("builtins.", "")) + assert isinstance(value, expected_type) \ No newline at end of file From d3fd52a00057ebf02b778cbfe68ece200d05b0e2 Mon Sep 17 00:00:00 2001 From: SaisakthiM Date: Thu, 15 Oct 2026 14:17:41 +0530 Subject: [PATCH 2/2] merged the tests and removed the test_default_values.py --- .../tests/test_default_values.py | 54 --------- .../tests/test_types.py | 111 +++++++++++------- 2 files changed, 70 insertions(+), 95 deletions(-) delete mode 100644 codegen/opentelemetry-codegen-json/tests/test_default_values.py diff --git a/codegen/opentelemetry-codegen-json/tests/test_default_values.py b/codegen/opentelemetry-codegen-json/tests/test_default_values.py deleted file mode 100644 index 9db91734ddb..00000000000 --- a/codegen/opentelemetry-codegen-json/tests/test_default_values.py +++ /dev/null @@ -1,54 +0,0 @@ -import pytest -from google.protobuf import descriptor_pb2 as descriptor - -from opentelemetry.codegen.json.types import ( - PROTO_DEFAULTS, - PROTO_TO_PYTHON, - get_default_value, - get_python_type, -) - -FDP = descriptor.FieldDescriptorProto - - -@pytest.mark.parametrize( - "proto_type, expected", - [ - (FDP.TYPE_DOUBLE, "0.0"), - (FDP.TYPE_FLOAT, "0.0"), - (FDP.TYPE_INT64, "0"), - (FDP.TYPE_UINT32, "0"), - (FDP.TYPE_SINT64, "0"), - (FDP.TYPE_BOOL, "False"), - (FDP.TYPE_STRING, '""'), - (FDP.TYPE_BYTES, 'b""'), - ], -) -def test_get_default_value_known_types(proto_type, expected): - assert get_default_value(proto_type) == expected - - -@pytest.mark.parametrize( - "proto_type", - [FDP.TYPE_GROUP, FDP.TYPE_MESSAGE, FDP.TYPE_ENUM], -) -def test_get_default_value_valid_types_without_default(proto_type): - assert get_default_value(proto_type) == "None" - - -@pytest.mark.parametrize("proto_type", [0, -1, 999, 2**31]) -def test_get_default_value_out_of_range(proto_type): - assert get_default_value(proto_type) == "None" - - -def test_every_mapped_type_has_a_default(): - # keeps PROTO_TO_PYTHON and PROTO_DEFAULTS from drifting apart - assert set(PROTO_TO_PYTHON) == set(PROTO_DEFAULTS) - - -@pytest.mark.parametrize("proto_type", PROTO_DEFAULTS) -def test_default_is_valid_python_literal(proto_type): - # the default is emitted into generated code, so it must eval cleanly - value = eval(get_default_value(proto_type)) - expected_type = eval(get_python_type(proto_type).replace("builtins.", "")) - assert isinstance(value, expected_type) \ No newline at end of file diff --git a/codegen/opentelemetry-codegen-json/tests/test_types.py b/codegen/opentelemetry-codegen-json/tests/test_types.py index a9d8a4eee5e..1e0f8dacdb3 100644 --- a/codegen/opentelemetry-codegen-json/tests/test_types.py +++ b/codegen/opentelemetry-codegen-json/tests/test_types.py @@ -3,10 +3,15 @@ # pylint: disable=no-member +import ast +import builtins + import pytest # type: ignore from google.protobuf import descriptor_pb2 as descriptor from opentelemetry.codegen.json.types import ( + PROTO_DEFAULTS, + PROTO_TO_PYTHON, get_default_value, get_json_allowed_types, get_python_type, @@ -17,15 +22,17 @@ to_json_field_name, ) +FDP = descriptor.FieldDescriptorProto + @pytest.mark.parametrize( "proto_type, expected", [ - (descriptor.FieldDescriptorProto.TYPE_DOUBLE, "builtins.float"), - (descriptor.FieldDescriptorProto.TYPE_INT64, "builtins.int"), - (descriptor.FieldDescriptorProto.TYPE_BOOL, "builtins.bool"), - (descriptor.FieldDescriptorProto.TYPE_STRING, "builtins.str"), - (descriptor.FieldDescriptorProto.TYPE_BYTES, "builtins.bytes"), + (FDP.TYPE_DOUBLE, "builtins.float"), + (FDP.TYPE_INT64, "builtins.int"), + (FDP.TYPE_BOOL, "builtins.bool"), + (FDP.TYPE_STRING, "builtins.str"), + (FDP.TYPE_BYTES, "builtins.bytes"), ], ) def test_get_python_type(proto_type: int, expected: str) -> None: @@ -35,28 +42,56 @@ def test_get_python_type(proto_type: int, expected: str) -> None: @pytest.mark.parametrize( "proto_type, expected", [ - (descriptor.FieldDescriptorProto.TYPE_DOUBLE, "0.0"), - (descriptor.FieldDescriptorProto.TYPE_INT64, "0"), - (descriptor.FieldDescriptorProto.TYPE_BOOL, "False"), - (descriptor.FieldDescriptorProto.TYPE_STRING, '""'), - (descriptor.FieldDescriptorProto.TYPE_BYTES, 'b""'), - (999, "None"), + (FDP.TYPE_DOUBLE, "0.0"), + (FDP.TYPE_FLOAT, "0.0"), + (FDP.TYPE_INT64, "0"), + (FDP.TYPE_UINT32, "0"), + (FDP.TYPE_SINT64, "0"), + (FDP.TYPE_BOOL, "False"), + (FDP.TYPE_STRING, '""'), + (FDP.TYPE_BYTES, 'b""'), ], ) def test_get_default_value(proto_type: int, expected: str) -> None: assert get_default_value(proto_type) == expected +@pytest.mark.parametrize( + "proto_type", + [FDP.TYPE_GROUP, FDP.TYPE_MESSAGE, FDP.TYPE_ENUM], +) +def test_get_default_value_types_without_default(proto_type: int) -> None: + assert get_default_value(proto_type) == "None" + + +@pytest.mark.parametrize("proto_type", [0, -1, 999, 2**31]) +def test_get_default_value_out_of_range(proto_type: int) -> None: + assert get_default_value(proto_type) == "None" + + +def test_every_mapped_type_has_a_default() -> None: + # keeps PROTO_TO_PYTHON and PROTO_DEFAULTS from drifting apart + assert set(PROTO_TO_PYTHON) == set(PROTO_DEFAULTS) + + +@pytest.mark.parametrize("proto_type", PROTO_DEFAULTS) +def test_default_is_valid_python_literal(proto_type: int) -> None: + # the default is emitted into generated code, so it must parse cleanly + value = ast.literal_eval(get_default_value(proto_type)) + type_name = get_python_type(proto_type).removeprefix("builtins.") + assert isinstance(value, getattr(builtins, type_name)) + + @pytest.mark.parametrize( "proto_type, expected", [ - (descriptor.FieldDescriptorProto.TYPE_INT64, True), - (descriptor.FieldDescriptorProto.TYPE_UINT64, True), - (descriptor.FieldDescriptorProto.TYPE_FIXED64, True), - (descriptor.FieldDescriptorProto.TYPE_SFIXED64, True), - (descriptor.FieldDescriptorProto.TYPE_SINT64, True), - (descriptor.FieldDescriptorProto.TYPE_INT32, False), - (descriptor.FieldDescriptorProto.TYPE_STRING, False), + (FDP.TYPE_INT64, True), + (FDP.TYPE_UINT64, True), + (FDP.TYPE_FIXED64, True), + (FDP.TYPE_SFIXED64, True), + (FDP.TYPE_SINT64, True), + (FDP.TYPE_INT32, False), + (FDP.TYPE_STRING, False), ], ) def test_is_int64_type(proto_type: int, expected: bool) -> None: @@ -66,8 +101,8 @@ def test_is_int64_type(proto_type: int, expected: bool) -> None: @pytest.mark.parametrize( "proto_type, expected", [ - (descriptor.FieldDescriptorProto.TYPE_BYTES, True), - (descriptor.FieldDescriptorProto.TYPE_STRING, False), + (FDP.TYPE_BYTES, True), + (FDP.TYPE_STRING, False), ], ) def test_is_bytes_type(proto_type: int, expected: bool) -> None: @@ -104,11 +139,11 @@ def test_to_json_field_name(snake_name: str, expected: str) -> None: @pytest.mark.parametrize( "proto_type, expected", [ - (descriptor.FieldDescriptorProto.TYPE_DOUBLE, True), - (descriptor.FieldDescriptorProto.TYPE_INT64, True), - (descriptor.FieldDescriptorProto.TYPE_INT32, True), - (descriptor.FieldDescriptorProto.TYPE_BOOL, False), - (descriptor.FieldDescriptorProto.TYPE_STRING, False), + (FDP.TYPE_DOUBLE, True), + (FDP.TYPE_INT64, True), + (FDP.TYPE_INT32, True), + (FDP.TYPE_BOOL, False), + (FDP.TYPE_STRING, False), ], ) def test_is_numeric_type(proto_type: int, expected: bool) -> None: @@ -118,25 +153,19 @@ def test_is_numeric_type(proto_type: int, expected: bool) -> None: @pytest.mark.parametrize( "proto_type, field_name, expected", [ - (descriptor.FieldDescriptorProto.TYPE_BYTES, "data", "builtins.str"), - ( - descriptor.FieldDescriptorProto.TYPE_STRING, - "trace_id", - "builtins.str", - ), - ( - descriptor.FieldDescriptorProto.TYPE_INT64, - "count", - "(builtins.int, builtins.str)", - ), + (FDP.TYPE_BYTES, "data", "builtins.str"), + (FDP.TYPE_STRING, "trace_id", "builtins.str"), + (FDP.TYPE_INT64, "count", "(builtins.int, builtins.str)"), ( - descriptor.FieldDescriptorProto.TYPE_DOUBLE, + FDP.TYPE_DOUBLE, "value", "(builtins.float, builtins.int, builtins.str)", ), - (descriptor.FieldDescriptorProto.TYPE_BOOL, "flag", "builtins.bool"), - (descriptor.FieldDescriptorProto.TYPE_INT32, "id", "builtins.int"), + (FDP.TYPE_BOOL, "flag", "builtins.bool"), + (FDP.TYPE_INT32, "id", "builtins.int"), ], ) -def test_get_json_allowed_types(proto_type: int, field_name: str, expected: str) -> None: - assert get_json_allowed_types(proto_type, field_name) == expected +def test_get_json_allowed_types( + proto_type: int, field_name: str, expected: str +) -> None: + assert get_json_allowed_types(proto_type, field_name) == expected \ No newline at end of file