Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lotif committed Feb 27, 2024
1 parent c06e7b4 commit 9c78fe5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
60 changes: 60 additions & 0 deletions florist/tests/api/monitoring/test_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import datetime
import json
from unittest.mock import Mock

from fl4health.reporting.metrics import DateTimeEncoder
from freezegun import freeze_time

from florist.api.monitoring.metrics import RedisMetricsReporter


@freeze_time("2012-12-11 10:09:08")
def test_add_to_metrics() -> None:
mock_redis_connection = Mock()
test_run_id = "123"
test_data = {"test": "data", "date": datetime.datetime.now()}

redis_metric_reporter = RedisMetricsReporter(mock_redis_connection, test_run_id)
redis_metric_reporter.add_to_metrics(test_data)

mock_redis_connection.set.assert_called_once_with(test_run_id, json.dumps(test_data, cls=DateTimeEncoder))


@freeze_time("2012-12-11 10:09:08")
def test_add_to_metrics_at_round() -> None:
mock_redis_connection = Mock()
test_run_id = "123"
test_data = {"test": "data", "date": datetime.datetime.now()}
test_round = 2

redis_metric_reporter = RedisMetricsReporter(mock_redis_connection, test_run_id)
redis_metric_reporter.add_to_metrics_at_round(test_round, test_data)

expected_data = {
"rounds": {
str(test_round): test_data,
}
}
mock_redis_connection.set.assert_called_once_with(test_run_id, json.dumps(expected_data, cls=DateTimeEncoder))


@freeze_time("2012-12-11 10:09:08")
def test_dump() -> None:
mock_redis_connection = Mock()
test_run_id = "123"
test_data = {"test": "data", "date": datetime.datetime.now()}
test_round = 2

redis_metric_reporter = RedisMetricsReporter(mock_redis_connection, test_run_id)
redis_metric_reporter.add_to_metrics(test_data)
redis_metric_reporter.add_to_metrics_at_round(test_round, test_data)
redis_metric_reporter.dump()

expected_data = {
**test_data,
"rounds": {
str(test_round): test_data,
},
}
assert mock_redis_connection.set.call_args_list[2][0][0] == test_run_id
assert mock_redis_connection.set.call_args_list[2][0][1] == json.dumps(expected_data, cls=DateTimeEncoder)
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ types-setuptools = "^69.0.0.20240125"
[tool.poetry.group.test.dependencies]
pytest = "^8.0.0"
pytest-cov = "^4.1.0"
freezegun = "^1.4.0"

[build-system]
requires = ["setuptools", "wheel"]
Expand Down

0 comments on commit 9c78fe5

Please sign in to comment.