Skip to content

Commit

Permalink
Fix test_format_metrics_time_series datetime failure
Browse files Browse the repository at this point in the history
pandas.Timestamp doesn't represent local time but UTC. This makes the test
fails as the returned timestamp has 2h difference from the value of
datetime.datetime which instead gets the local time.
Additional changes are just pep considerations regarding longest line limits.

Signed-off-by: ybonatakis <[email protected]>
  • Loading branch information
b10n1k committed Oct 23, 2023
1 parent 87034cb commit 3ea5682
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions tests/test_formater_influxdb.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
from datetime import datetime, timedelta
from formatters.influxdb_formatter import MetricsInfluxdbFormatter
from metrics.metrics_result import MetricsResults, MetricsTimeSeries
import pytz


def test_format_metrics_results():
date = datetime.now()
metrics = MetricsResults(data={"v1": 1, "v2": 1, "v3": 2}, metadata={"project": 1}, date=date)
metrics = MetricsResults(data={"v1": 1, "v2": 1, "v3": 2},
metadata={"project": 1},
date=date)
lines = MetricsInfluxdbFormatter.format("test", metrics)
assert lines[0] == f"test,project=1 v1=1,v2=1,v3=2 {int(date.timestamp() * 1e9)}"
date_as_e9 = int(date.timestamp() * 1e9)
assert lines[0] == f"test,project=1 v1=1,v2=1,v3=2 {date_as_e9}"


def test_format_metrics_time_series():
date1 = datetime.now()
date1 = datetime.now(tz=pytz.UTC)
date2 = date1 + timedelta(days=1)
metrics_time_series = MetricsTimeSeries()

Expand All @@ -22,15 +26,18 @@ def test_format_metrics_time_series():
{"date": date2, "v1": 2, "v2": 2, "v3": 3},
]
)

lines = MetricsInfluxdbFormatter.format("test", metrics_time_series)
assert lines[0] == f"test,project=containers v1=1,v2=1,v3=2 {int(date1.timestamp() * 1e9)}"
assert lines[1] == f"test,project=containers v1=2,v2=2,v3=3 {int(date2.timestamp() * 1e9)}"
date1_as_e9 = int(date1.timestamp() * 1e9)
date2_as_e9 = int(date2.timestamp() * 1e9)
assert lines[0] == f"test,project=containers v1=1,v2=1,v3=2 {date1_as_e9}"
assert lines[1] == f"test,project=containers v1=2,v2=2,v3=3 {date2_as_e9}"


def test_print_metrics_results(capsys):
date = datetime.now()
metrics = MetricsResults(data={"v1": 1, "v2": 1, "v3": 2}, metadata={"project": 1}, date=date)
metrics = MetricsResults(data={"v1": 1, "v2": 1, "v3": 2},
metadata={"project": 1},
date=date)
MetricsInfluxdbFormatter.print("test", metrics)

captured = capsys.readouterr()
Expand Down

0 comments on commit 3ea5682

Please sign in to comment.