Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 70 additions & 41 deletions codegen/opentelemetry-codegen-json/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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