Skip to content

Commit

Permalink
refactor: shrink readings_source API
Browse files Browse the repository at this point in the history
  • Loading branch information
urbas committed Nov 23, 2024
1 parent fe361ea commit ce65c5b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 31 deletions.
8 changes: 4 additions & 4 deletions py_air_control_exporter/readings_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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 = {}
Expand Down
28 changes: 1 addition & 27 deletions test/test_readings_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ce65c5b

Please sign in to comment.