diff --git a/README.md b/README.md index ab3013a..0fb4532 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,7 @@ > note: this is alpha software; apis may change quickly, and quality of the brightness prediction is still being ironed out -CTTS is an open source application for reading [sky brightness](https://en.wikipedia.org/wiki/Sky_brightness) all over the -earth, without a sensor. +CTTS is an open source application for reading [sky brightness](https://en.wikipedia.org/wiki/Sky_brightness) all over the earth's landmass, without a sensor. ## features diff --git a/pp/pp/cells/cell_covering.py b/pp/pp/cells/cell_covering.py index 280ba36..4fca855 100644 --- a/pp/pp/cells/cell_covering.py +++ b/pp/pp/cells/cell_covering.py @@ -8,7 +8,10 @@ from ..config import resolution class CellCovering: - def __init__(self, path_to_geojson: Path = Path(__file__).parent / "land.geojson"): + def __init__(self, path_to_geojson: Path | None = None): + if path_to_geojson is None: + path_to_geojson = Path(__file__).parent / "land.geojson" + with open(path_to_geojson, "r") as file: geojson = json.load(file) diff --git a/pp/pp/cells/cell_publisher.py b/pp/pp/cells/cell_publisher.py index b026fd8..79bd980 100644 --- a/pp/pp/cells/cell_publisher.py +++ b/pp/pp/cells/cell_publisher.py @@ -3,6 +3,7 @@ import typing from datetime import datetime, timezone from collections import defaultdict +from pathlib import Path import grpc from h3 import h3_to_geo @@ -21,8 +22,8 @@ class CellPublisher(CellCovering): cell_counts = defaultdict(int) def __init__(self, api_host: str, api_port: int, channel: BlockingChannel, - prediction_queue: str, cycle_queue: str): - super().__init__() + prediction_queue: str, cycle_queue: str, path_to_geojson: Path | None = None): + super().__init__(path_to_geojson=path_to_geojson) self._prediction_queue = prediction_queue self._cycle_queue = cycle_queue @@ -67,10 +68,10 @@ def publish_cycle_completion_message(self, start: datetime, end: datetime) -> No def run(self): cells = self.covering - if len(cells) == 0: + if not cells: raise ValueError("cell covering is empty!") - log.info(f"publishing brightness for {len(cells)} cells(s)") + log.info(f"publishing brightness for {len(cells)} cells(s) resoulution {resolution}") while True: start_time_utc = datetime.now(timezone.utc) diff --git a/pp/tests/empty.geojson b/pp/tests/empty.geojson new file mode 100644 index 0000000..8b3698f --- /dev/null +++ b/pp/tests/empty.geojson @@ -0,0 +1,4 @@ +{ + "type": "FeatureCollection", + "features": [] +} diff --git a/pp/tests/fixtures.py b/pp/tests/fixtures.py index 4b2390b..f9aa16e 100644 --- a/pp/tests/fixtures.py +++ b/pp/tests/fixtures.py @@ -1,4 +1,5 @@ from unittest.mock import MagicMock +from pathlib import Path import uuid import pytest @@ -33,7 +34,7 @@ def mock_pika_channel(mocker): @pytest.fixture -def publisher(mock_grpc_client, mock_pika_channel): +def cell_publisher(mock_grpc_client, mock_pika_channel): return CellPublisher(api_host="localhost", api_port=50051, channel=mock_pika_channel, diff --git a/pp/tests/test_publisher.py b/pp/tests/test_publisher.py index 0630881..a67a8e3 100644 --- a/pp/tests/test_publisher.py +++ b/pp/tests/test_publisher.py @@ -1,22 +1,29 @@ from datetime import datetime, timedelta +import h3 import pytest from .fixtures import * -@pytest.mark.parametrize("cell_id", [ - ("89283082813ffff"), - ("8928308280fffff"), - ("89283082807ffff"), -]) -def test_can_publish_cell_brightness(cell_id, publisher, mock_pika_channel): - publisher.publish_cell_brightness_message(cell_id) +@pytest.mark.parametrize("cell_id", [x for x in h3.get_res0_indexes()]) +def test_can_publish_cell_brightness_at_h3_indexes(cell_id, cell_publisher, mock_pika_channel): + cell_publisher.publish_cell_brightness_message(cell_id) mock_pika_channel.basic_publish.assert_called_once() -@pytest.mark.parametrize("minutes_ago", [ - (i) for i in range(1, 10) -]) -def test_can_publish_cycle_complete(minutes_ago, publisher, mock_pika_channel): +@pytest.mark.parametrize("minutes_ago", [i for i in range(1, 10)]) +def test_can_publish_cycle_complete(minutes_ago, cell_publisher, mock_pika_channel): then = datetime.now() - timedelta(minutes=minutes_ago) now = datetime.now() - publisher.publish_cycle_completion_message(then, now) + cell_publisher.publish_cycle_completion_message(then, now) mock_pika_channel.basic_publish.assert_called_once() + +def test_publisher_raises_on_empty_cells(mock_grpc_client, mock_pika_channel): + with pytest.raises(ValueError): + cell_publisher = CellPublisher( + api_host="localhost", + api_port=50051, + channel=mock_pika_channel, + prediction_queue="prediction", + cycle_queue="cycle", + path_to_geojson=Path(__file__).parent / "empty.geojson" + ) + cell_publisher.run()