Skip to content

Commit

Permalink
add producer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nonnontrivial committed Dec 21, 2024
1 parent ad8936e commit 79ccbdc
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion pp/pp/cells/cell_covering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 5 additions & 4 deletions pp/pp/cells/cell_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions pp/tests/empty.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "FeatureCollection",
"features": []
}
3 changes: 2 additions & 1 deletion pp/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest.mock import MagicMock
from pathlib import Path
import uuid

import pytest
Expand Down Expand Up @@ -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,
Expand Down
31 changes: 19 additions & 12 deletions pp/tests/test_publisher.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 79ccbdc

Please sign in to comment.