Skip to content

Commit 299e9d5

Browse files
authored
test(time): Make nanos assertable (#4562)
For: #4559 Make millies/micros/nanos assertable with our `time_*` assertion helpers.
1 parent 6208719 commit 299e9d5

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

tests/integration/asserts/time.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
from datetime import timedelta, datetime, timezone
2+
from enum import StrEnum
23

34

4-
def _to_datetime(v):
5+
class Resolution(StrEnum):
6+
Seconds = "s"
7+
MilliSeconds = "ms"
8+
MicroSeconds = "us"
9+
NanoSeconds = "ns"
10+
11+
12+
def _to_datetime(v, resolution=Resolution.Seconds):
513
if isinstance(v, datetime):
614
return v
715
elif isinstance(v, (int, float)):
8-
return datetime.fromtimestamp(v, timezone.utc)
16+
resolution = Resolution(resolution)
17+
to_secs_d = {
18+
Resolution.Seconds: 1,
19+
Resolution.MilliSeconds: 1_000,
20+
Resolution.MicroSeconds: 1_000_000,
21+
Resolution.NanoSeconds: 1_000_000_000,
22+
}[resolution]
23+
return datetime.fromtimestamp(v / to_secs_d, timezone.utc)
924
elif isinstance(v, str):
1025
return datetime.fromisoformat(v)
1126
else:
1227
assert False, f"cannot convert {v} to datetime"
1328

1429

1530
class _WithinBounds:
16-
def __init__(self, lower_bound, upper_bound):
31+
def __init__(self, lower_bound, upper_bound, expect_resolution=Resolution.Seconds):
1732
self._lower_bound = lower_bound
1833
self._upper_bound = upper_bound
34+
self._expect_resolution = expect_resolution
1935

2036
def __eq__(self, other):
21-
other = _to_datetime(other)
37+
other = _to_datetime(other, resolution=self._expect_resolution)
2238
return self._lower_bound <= other <= self._upper_bound
2339

2440
def __str__(self):
@@ -28,18 +44,18 @@ def __repr__(self) -> str:
2844
return str(self)
2945

3046

31-
def time_after(lower_bound):
47+
def time_after(lower_bound, **kwargs):
3248
upper_bound = datetime.now(tz=timezone.utc)
33-
return time_within(lower_bound, upper_bound)
49+
return time_within(lower_bound, upper_bound, **kwargs)
3450

3551

36-
def time_within(lower_bound, upper_bound):
52+
def time_within(lower_bound, upper_bound, **kwargs):
3753
lower_bound = _to_datetime(lower_bound)
3854
upper_bound = _to_datetime(upper_bound)
3955
assert lower_bound <= upper_bound
40-
return _WithinBounds(lower_bound, upper_bound)
56+
return _WithinBounds(lower_bound, upper_bound, **kwargs)
4157

4258

43-
def time_within_delta(time=None, delta=timedelta(seconds=30)):
59+
def time_within_delta(time=None, delta=timedelta(seconds=30), **kwargs):
4460
time = _to_datetime(time) if time is not None else datetime.now(tz=timezone.utc)
45-
return _WithinBounds(time - delta, time + delta)
61+
return _WithinBounds(time - delta, time + delta, **kwargs)

tests/integration/test_ourlogs.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from sentry_sdk.envelope import Envelope, Item, PayloadRef
55

6+
from .asserts.time import time_within_delta
7+
68

79
TEST_CONFIG = {
810
"aggregator": {
@@ -77,7 +79,9 @@ def test_ourlog_extraction(
7779
"project_id": 42,
7880
"retention_days": 90,
7981
"timestamp_nanos": int(start.timestamp() * 1e9),
80-
"observed_timestamp_nanos": int(end.timestamp() * 1e9),
82+
"observed_timestamp_nanos": time_within_delta(
83+
start.timestamp(), expect_resolution="ns"
84+
),
8185
"trace_id": "5b8efff798038103d269b633813fc60c",
8286
"body": "Example log record",
8387
"trace_flags": 0,

0 commit comments

Comments
 (0)