Fix _Timestamp edge cases (#534)

* Add failing test cases for timestamp conversion

* Fix timestamp to datetime conversion

* Fix formatting

* Move timestamp tests outside of inputs folder

---------

Co-authored-by: Lukas Bindreiter <lukas.bindreiter@tilebox.io>
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
This commit is contained in:
Lukas Bindreiter
2023-10-18 01:41:09 +02:00
committed by GitHub
parent 1dd001b6d3
commit 02aa4e88b7
2 changed files with 43 additions and 7 deletions

27
tests/test_timestamp.py Normal file
View File

@@ -0,0 +1,27 @@
from datetime import (
datetime,
timezone,
)
import pytest
from betterproto import _Timestamp
@pytest.mark.parametrize(
"dt",
[
datetime(2023, 10, 11, 9, 41, 12, tzinfo=timezone.utc),
datetime.now(timezone.utc),
# potential issue with floating point precision:
datetime(2242, 12, 31, 23, 0, 0, 1, tzinfo=timezone.utc),
# potential issue with negative timestamps:
datetime(1969, 12, 31, 23, 0, 0, 1, tzinfo=timezone.utc),
],
)
def test_timestamp_to_datetime_and_back(dt: datetime):
"""
Make sure converting a datetime to a protobuf timestamp message
and then back again ends up with the same datetime.
"""
assert _Timestamp.from_datetime(dt).to_datetime() == dt