From ce65c5beefef5bc83aef797591f58e461ec20f9a Mon Sep 17 00:00:00 2001 From: Matej Urbas Date: Sat, 23 Nov 2024 09:06:46 +0000 Subject: [PATCH] refactor: shrink readings_source API --- py_air_control_exporter/readings_source.py | 8 +++---- test/test_readings_source.py | 28 +--------------------- 2 files changed, 5 insertions(+), 31 deletions(-) diff --git a/py_air_control_exporter/readings_source.py b/py_air_control_exporter/readings_source.py index d2acdb2..27917ca 100644 --- a/py_air_control_exporter/readings_source.py +++ b/py_air_control_exporter/readings_source.py @@ -16,13 +16,13 @@ class _Target: def from_config(targets_config: dict[str, dict]) -> ReadingsSource | None: - targets = create_targets(targets_config) + targets = _create_targets(targets_config) if not targets: return None - return create_readings_source(targets) + return _create_readings_source(targets) -def create_readings_source( +def _create_readings_source( targets: dict[str, _Target], ) -> ReadingsSource: def _fetch() -> dict[str, fetchers_api.TargetReading]: @@ -31,7 +31,7 @@ def _fetch() -> dict[str, fetchers_api.TargetReading]: return _fetch -def create_targets( +def _create_targets( targets_config: dict[str, dict], ) -> dict[str, _Target] | None: targets = {} diff --git a/test/test_readings_source.py b/test/test_readings_source.py index 03a617a..f4acf35 100644 --- a/test/test_readings_source.py +++ b/test/test_readings_source.py @@ -17,36 +17,10 @@ def test_from_config(mocker): assert source() == {"foo": expected_reading} -def test_create_targets(mocker): - """Check that the targets dictionary is created and ready to fetch.""" - mock_get_reading = mocker.patch( - "py_air_control_exporter.fetchers.http_philips.get_reading", - autospec=True, - ) - - targets_config = { - "foo": {"host": "1.2.3.4", "protocol": "http"}, - "bar": {"host": "1.2.3.5", "protocol": "http"}, - } - targets = readings_source.create_targets(targets_config) - assert isinstance(targets, dict) - assert len(targets) == 2 - assert "foo" in targets - assert "bar" in targets - - # Call first fetcher and verify arguments - targets["foo"].fetcher() - mock_get_reading.assert_called_with("1.2.3.4") - - # Call second fetcher and verify arguments - targets["bar"].fetcher() - mock_get_reading.assert_called_with("1.2.3.5") - - def test_unknown_protocol_in_config(caplog): """Check that error is logged when config contains unknown protocol""" targets_config = {"test": {"host": "1.2.3.4", "protocol": "invalid"}} - result = readings_source.create_targets(targets_config) + result = readings_source.from_config(targets_config) assert result is None assert "Unknown protocol 'invalid' for target 'test'" in caplog.text assert "Known protocols:" in caplog.text