Skip to content

Commit e2ae725

Browse files
committed
test(timestamp): add unit tests for timestamp
Signed-off-by: Sarthak Tyagi <tsarthak878@gmail.com>
1 parent aaad58b commit e2ae725

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

tests/unit/timestamp_test.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ def test_from_date_unix_epoch():
6868
assert ts.nanos == 0
6969

7070

71+
72+
7173
def test_from_date_max_microseconds():
7274
"""Test from_date with maximum microseconds to ensure nanos calculation is correct."""
7375
dt = datetime(2020, 1, 1, 0, 0, 0, 999999, tzinfo=timezone.utc)
7476
ts = Timestamp.from_date(dt)
75-
assert 999_998_000 <= ts.nanos <= 999_999_000
7677

78+
expected = 999_999_000
79+
assert abs(ts.nanos - expected) < 1_000
7780

7881
@pytest.mark.parametrize("bad_input", [None, [], {}, 3.14])
7982
def test_from_date_invalid_type(bad_input):
@@ -174,19 +177,20 @@ def test_compare_greater_than():
174177

175178
def test_generate_without_jitter():
176179
"""Ensure generate without jitter produces a timestamp close to current time."""
177-
before = time.time()
178180
ts = Timestamp.generate(has_jitter=False)
179-
after = time.time()
180-
generated_time = ts.to_date().timestamp()
181-
assert before <= generated_time <= after
181+
delta = abs(ts.to_date().timestamp() - time.time())
182+
183+
assert delta < 0.1
184+
182185

183186

184187
def test_generate_with_jitter():
185188
"""Verify that generated timestamps with jitter remain close to the system time within a safe tolerance."""
186189
ts = Timestamp.generate(has_jitter=True)
187-
delta = abs(ts.to_date().timestamp() - time.time())
188-
assert delta < 0.05
190+
delta = time.time() - ts.to_date().timestamp()
189191

192+
# Jitter is explicitly 3–8 seconds backward
193+
assert 3.0 <= delta <= 9.0
190194

191195
# Protobuf serialization tests
192196

0 commit comments

Comments
 (0)