From 7f8e8d46cb15d10146a44fc5294694ffce77f6f3 Mon Sep 17 00:00:00 2001 From: Kevin Donahue Date: Tue, 12 Nov 2024 22:02:56 -0500 Subject: [PATCH] add tests --- README.md | 13 ++++++------- pc/pc/main.py | 4 ++-- pp/pp/cells/cell_covering.py | 2 +- pp/pp/cells/cell_publisher.py | 24 +++++++++++++----------- pp/tests/test_publisher.py | 17 ++++++++--------- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 6779b23..e8cdd96 100644 --- a/README.md +++ b/README.md @@ -56,13 +56,12 @@ docker compose up --build After rabbitmq starts up, the producer and consumer containers will start up, at which point you should see output like this: -```sh -producer-1 | 2024-10-28 13:16:27,502 [INFO] publishing {'uuid': 'e6c22004-9180-4599-9e87-36b86f68a5e7', 'lat': 43.42281493904898, 'lon': -97.42465926125905, 'h3_id': '8027fffffffffff', 'mpsas': 9.942784309387207, 'timestamp_utc': '2024-10-28T13:16:27.501192+00:00'} to brightness.prediction -producer-1 | 2024-10-28 13:16:27,580 [INFO] publishing {'uuid': 'a548be90-89f7-4995-9239-197523c3afd0', 'lat': 19.093680683484372, 'lon': 43.638818828910864, 'h3_id': '8053fffffffffff', 'mpsas': 9.202325820922852, 'timestamp_utc': '2024-10-28T13:16:27.579755+00:00'} to brightness.prediction -producer-1 | 2024-10-28 13:16:27,656 [INFO] publishing {'uuid': '2759f0e8-2f94-4efd-bf19-4b765947d983', 'lat': 60.432795263055546, 'lon': -77.20705748560815, 'h3_id': '800ffffffffffff', 'mpsas': 11.305692672729492, 'timestamp_utc': '2024-10-28T13:16:27.655087+00:00'} to brightness.prediction -producer-1 | 2024-10-28 13:16:27,736 [INFO] publishing {'uuid': 'd53872da-2505-41a9-84f1-d9336b0aff83', 'lat': -30.01574044171678, 'lon': 129.95847216046155, 'h3_id': '80b9fffffffffff', 'mpsas': 12.414505004882812, 'timestamp_utc': '2024-10-28T13:16:27.735392+00:00'} to brightness.prediction -producer-1 | 2024-10-28 13:16:27,737 [INFO] publishing {'start_time_utc': '2024-10-28T13:16:24.874541+00:00', 'end_time_utc': '2024-10-28T13:16:27.737791+00:00', 'duration_s': 2} to brightness.cycle -consumer-1 | 2024-10-28 13:16:27,744 [INFO] {'uuid': 'aa89439d-9a22-41e1-b8d2-674bea5263ee', 'lat': -74.92843438917433, 'lon': -34.64375807722018, 'h3_id': '80effffffffffff', 'mpsas': 23.74591636657715, 'timestamp_utc': datetime.datetime(2024, 10, 28, 13, 16, 25, 624186, tzinfo=datetime.timezone.utc)} +```log +producer-1 | 2024-11-13 03:01:02,478 [INFO] publishing {'uuid': 'c6df89c5-a4fa-48fc-bfd8-11d08494902f', 'lat': 16.702868303031234, 'lon': -13.374845104752373, 'h3_id': '8055fffffffffff', 'mpsas': 6.862955570220947, 'timestamp_utc': '2024-11-13T03:01:02.478000+00:00'} to brightness.prediction +producer-1 | 2024-11-13 03:01:02,553 [INFO] publishing {'uuid': '9b5f2e8b-c22d-4d05-900e-0156f78632ce', 'lat': 26.283628653081813, 'lon': 62.954274989658984, 'h3_id': '8043fffffffffff', 'mpsas': 9.472949028015137, 'timestamp_utc': '2024-11-13T03:01:02.552848+00:00'} to brightness.prediction +producer-1 | 2024-11-13 03:01:02,625 [INFO] publishing {'uuid': 'fbbc3cd5-839d-43de-a7c4-8f51100679fd', 'lat': -4.530154895350926, 'lon': -42.02241568705745, 'h3_id': '8081fffffffffff', 'mpsas': 9.065463066101074, 'timestamp_utc': '2024-11-13T03:01:02.624759+00:00'} to brightness.prediction +producer-1 | 2024-11-13 03:01:02,626 [INFO] publishing {'start_time_utc': '2024-11-13T03:01:00.114586+00:00', 'end_time_utc': '2024-11-13T03:01:02.626208+00:00', 'duration_s': 2} to brightness.cycle +consumer-1 | 2024-11-13 03:01:02,631 [INFO] cycle completed with {'uuid': '4bb0c627-596c-42be-a93a-26f36c5ca3c1', 'lat': 55.25746462939812, 'lon': 127.08774514928741, 'h3_id': '8015fffffffffff', 'mpsas': 23.763256072998047, 'timestamp_utc': datetime.datetime(2024, 11, 13, 3, 1, 1, 129155, tzinfo=datetime.timezone.utc)} ``` The above output means: diff --git a/pc/pc/main.py b/pc/pc/main.py index 0803714..33562a6 100644 --- a/pc/pc/main.py +++ b/pc/pc/main.py @@ -11,8 +11,8 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s") log = logging.getLogger(__name__) -def on_cycle_completion(bo: BrightnessObservation): - log.info(bo.model_dump()) +def on_cycle_completion(brightness_observation: BrightnessObservation): + log.info(f"cycle completed with {brightness_observation.model_dump()}") async def main(): diff --git a/pp/pp/cells/cell_covering.py b/pp/pp/cells/cell_covering.py index 3e484d3..aa91e70 100644 --- a/pp/pp/cells/cell_covering.py +++ b/pp/pp/cells/cell_covering.py @@ -20,7 +20,7 @@ def __init__(self): @staticmethod def get_polygon_of_feature(feature: typing.Dict) -> typing.Dict: - polygon=shape(feature["geometry"]) + polygon = shape(feature["geometry"]) if not isinstance(polygon, Polygon): raise TypeError("geojson is not a Polygon") diff --git a/pp/pp/cells/cell_publisher.py b/pp/pp/cells/cell_publisher.py index d61824f..a97b32b 100644 --- a/pp/pp/cells/cell_publisher.py +++ b/pp/pp/cells/cell_publisher.py @@ -37,9 +37,7 @@ def _publish(self, queue_name: str, message: typing.Dict[str, typing.Any]): log.info(f"publishing {message} to {queue_name}") self._channel.basic_publish(exchange="", routing_key=queue_name, body=json.dumps(message)) - def predict_cell_brightness(self, cell) -> None: - """ask brightness service for prediction of sky brightness on h3 cell - for the current time""" + def publish_cell_brightness_message(self, cell) -> None: lat, lon = h3_to_geo(cell) request = brightness_service_pb2.Coordinates(lat=lat, lon=lon) try: @@ -60,22 +58,26 @@ def predict_cell_brightness(self, cell) -> None: dumped["timestamp_utc"] = brightness_observation.timestamp_utc.isoformat() self._publish(self._prediction_queue, dumped) + def publish_cycle_completion_message(self, start: datetime, end: datetime) -> None: + cell_cycle = CellCycle(start_time_utc=start, end_time_utc=end, duration_s=int((end - start).total_seconds())) + cell_cycle = cell_cycle.model_dump() + cell_cycle["start_time_utc"] = cell_cycle["start_time_utc"].isoformat() + cell_cycle["end_time_utc"] = cell_cycle["end_time_utc"].isoformat() + self._publish(self._cycle_queue, cell_cycle) + def run(self): cells = self.covering if len(cells) == 0: raise ValueError("cell covering is empty!") - log.info(f"publishing brightness for {len(cells)} cells(s)") + while True: start_time_utc = datetime.now(timezone.utc) + for cell in cells: CellPublisher.cell_counts[cell] += 1 - self.predict_cell_brightness(cell) + self.publish_cell_brightness_message(cell) log.debug(f"{len(CellPublisher.cell_counts)} distinct cells have had observations published") - end_time_utc = datetime.now(timezone.utc) - cell_cycle = CellCycle(start_time_utc=start_time_utc, end_time_utc=end_time_utc, duration_s=int((end_time_utc - start_time_utc).total_seconds())) - cell_cycle = cell_cycle.model_dump() - cell_cycle["start_time_utc"] = cell_cycle["start_time_utc"].isoformat() - cell_cycle["end_time_utc"] = cell_cycle["end_time_utc"].isoformat() - self._publish(self._cycle_queue, cell_cycle) + end_time_utc = datetime.now(timezone.utc) + self.publish_cycle_completion_message(start_time_utc, end_time_utc) diff --git a/pp/tests/test_publisher.py b/pp/tests/test_publisher.py index c32beae..ea27d3d 100644 --- a/pp/tests/test_publisher.py +++ b/pp/tests/test_publisher.py @@ -1,4 +1,5 @@ from unittest.mock import MagicMock +from datetime import datetime, timedelta import uuid import pytest @@ -42,15 +43,13 @@ def publisher(mock_grpc_client, mock_pika_channel): prediction_queue="prediction", cycle_queue="cycle") -def test_publisher_publishes_prediction_on_channel(publisher, mock_pika_channel): +def test_brightness_message_publish(publisher, mock_pika_channel): cell = "89283082813ffff" - publisher.predict_cell_brightness(cell) + publisher.publish_cell_brightness_message(cell) mock_pika_channel.basic_publish.assert_called_once() -@pytest.mark.skip -def test_number_of_distinct_cells_published(publisher, mock_pika_channel): - pass - -@pytest.mark.skip -def test_cell_covering(publisher, mock_pika_channel): - pass +def test_cycle_completion_message_publish(publisher, mock_pika_channel): + then = datetime.now() - timedelta(minutes=5) + now = datetime.now() + publisher.publish_cycle_completion_message(then, now) + mock_pika_channel.basic_publish.assert_called_once()