Skip to content

Commit

Permalink
better datetime compare in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steersbob committed Dec 4, 2023
1 parent 18dde2d commit 60d69c8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
20 changes: 14 additions & 6 deletions test/test_timeseries_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
TESTED = timeseries_api.__name__


class dt_eq:

def __init__(self, value: utils.DatetimeSrc_) -> None:
self.value = utils.parse_datetime(value)

def __eq__(self, __value: object) -> bool:
return self.value == utils.parse_datetime(__value)


@pytest.fixture
async def m_victoria(mocker):
m = mocker.patch(TESTED + '.victoria.CV').get.return_value
Expand Down Expand Up @@ -103,7 +112,6 @@ async def test_ranges(client: AsyncClient, m_victoria: Mock):

async def test_metrics(client: AsyncClient, m_victoria: Mock):
now = time_ns() // 1_000_000
now_str = utils.format_datetime(now, 'ISO8601')
m_victoria.metrics.return_value = [
TimeSeriesMetric(
metric='a',
Expand All @@ -124,9 +132,9 @@ async def test_metrics(client: AsyncClient, m_victoria: Mock):

resp = await client.post('/timeseries/metrics', json={'fields': ['a', 'b', 'c']})
assert resp.json() == [
{'metric': 'a', 'value': approx(1.2), 'timestamp': now_str},
{'metric': 'b', 'value': approx(2.2), 'timestamp': now_str},
{'metric': 'c', 'value': approx(3.2), 'timestamp': now_str},
{'metric': 'a', 'value': approx(1.2), 'timestamp': dt_eq(now)},
{'metric': 'b', 'value': approx(2.2), 'timestamp': dt_eq(now)},
{'metric': 'c', 'value': approx(3.2), 'timestamp': dt_eq(now)},
]

resp = await client.post('/timeseries/metrics', json={})
Expand Down Expand Up @@ -273,7 +281,7 @@ async def test_stream_error(sync_client: TestClient, m_victoria: Mock):
TimeSeriesMetric(
metric='a',
value=1.2,
timestamp=datetime(2021, 7, 15, 19, tzinfo=timezone.utc)
timestamp=dt
),
]

Expand Down Expand Up @@ -311,7 +319,7 @@ async def test_stream_error(sync_client: TestClient, m_victoria: Mock):
'metrics': [{
'metric': 'a',
'value': approx(1.2),
'timestamp': utils.format_datetime(dt, 'ISO8601'),
'timestamp': dt_eq(dt),
}],
},
}
2 changes: 1 addition & 1 deletion test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_parse_datetime():

def test_format_datetime():
time_s = 1626359370
iso_str = '2021-07-15T14:29:30.000000Z'
iso_str = '2021-07-15T14:29:30Z'
time_ms = time_s * 1000
dt = datetime.fromtimestamp(time_s, tz=timezone.utc)

Expand Down
2 changes: 1 addition & 1 deletion test/test_victoria.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async def test_metrics(client: AsyncClient,
assert result == [
TimeSeriesMetric(metric='service/f1',
value=1,
timestamp=now()),
timestamp=now),
TimeSeriesMetric(metric='service/f2',
value=2,
timestamp=now),
Expand Down

0 comments on commit 60d69c8

Please sign in to comment.