From dce4e9aa9c7d68df5968c78ab01568716ab5404d Mon Sep 17 00:00:00 2001 From: aandres Date: Thu, 13 Jul 2023 19:34:34 +0100 Subject: [PATCH] Fix embarassing bug in truncate --- tests/random_generator.py | 3 +-- tests/test_conversion.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/random_generator.py b/tests/random_generator.py index 15f3fe7..6c83f63 100644 --- a/tests/random_generator.py +++ b/tests/random_generator.py @@ -169,8 +169,7 @@ def truncate_nanos(message: Message, timestamp_unit: str, time_unit: str) -> Mes else: for item in field_value: truncate_nanos(item, timestamp_unit, time_unit) - else: - message.HasField(field.name) + elif message.HasField(field.name): truncate_nanos( getattr(message, field.name), timestamp_unit, time_unit ) diff --git a/tests/test_conversion.py b/tests/test_conversion.py index b0e3eb2..de55c62 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -321,6 +321,26 @@ def test_truncate_nested(): ) +def test_truncate_nested_nested(): + assert truncate_nanos( + NestedExampleMessage( + example_message=ExampleMessage( + timestamp_value=Timestamp(seconds=10, nanos=123_456_789), + timestamp_string_map={"foo": Timestamp(seconds=10, nanos=123_456_789)}, + time_of_day_value=TimeOfDay(hours=1, nanos=123_456_789), + ), + ), + "us", + "ms", + ) == NestedExampleMessage( + example_message=ExampleMessage( + timestamp_value=Timestamp(seconds=10, nanos=123_456_000), + timestamp_string_map={"foo": Timestamp(seconds=10, nanos=123_456_000)}, + time_of_day_value=TimeOfDay(hours=1, nanos=123_000_000), + ) + ) + + @pytest.mark.parametrize( "enum_type", [pa.binary(), pa.dictionary(pa.int32(), pa.binary())] )