From b7ee4bf62fa37e3345baffffea30f204bee1e4ee Mon Sep 17 00:00:00 2001 From: Kevin Donahue Date: Fri, 18 Oct 2024 09:32:07 -0400 Subject: [PATCH] add more tests --- api/requirements-test.txt | 2 +- api/tests/test_api.py | 35 +++++++++++++++++++++++------------ pc/README.md | 2 +- pc/requirements-test.txt | 2 ++ pc/tests/__init__.py | 0 pc/tests/test_consumer.py | 1 + 6 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 pc/requirements-test.txt create mode 100644 pc/tests/__init__.py create mode 100644 pc/tests/test_consumer.py diff --git a/api/requirements-test.txt b/api/requirements-test.txt index e079f8a..f9708e4 100644 --- a/api/requirements-test.txt +++ b/api/requirements-test.txt @@ -1 +1 @@ -pytest +pytest==7.4.3 diff --git a/api/tests/test_api.py b/api/tests/test_api.py index fb77ba7..1bad630 100644 --- a/api/tests/test_api.py +++ b/api/tests/test_api.py @@ -8,14 +8,14 @@ from api.stubs import brightness_service_pb2 from api.service.brightness_servicer import BrightnessServicer - @pytest.fixture(scope='module') def grpc_server(): server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) + brightness_service_pb2_grpc.add_BrightnessServiceServicer_to_server(BrightnessServicer(), server) + server.add_insecure_port('[::]:50051') server.start() - yield server.stop(0) @@ -25,28 +25,39 @@ def brightness_service_stub(grpc_server): stub = brightness_service_pb2_grpc.BrightnessServiceStub(channel) yield stub -def test_apis(brightness_service_stub): - request = brightness_service_pb2.Coordinates(lat=42.,lon=42.) +@pytest.mark.parametrize('lat, lon, channel_value', [ + (40.712776, -74.005974, 255), + (-23.550520, -46.633308, 255), + (28.613939, 77.209021, 255), + (31.230416, 121.473701, 255), + (0., 0., 0), +]) +def test_pollution(brightness_service_stub, lat, lon, channel_value): + request = brightness_service_pb2.Coordinates(lat=lat,lon=lon) response = brightness_service_stub.GetPollution(request) - assert isinstance(response.r,int) - assert isinstance(response.g,int) - assert isinstance(response.b,int) - assert isinstance(response.a,int) + channels = (response.r, response.g, response.b, response.a) + all_color_channels_zero = all(x == 0 for x in channels) + + assert all(c == channel_value for c in channels[:3]) +@pytest.mark.parametrize('lat, lon', [ + (40.712776, -74.005974), + (-23.550520, -46.633308), + (28.613939, 77.209021), + (31.230416, 121.473701), +]) @patch('api.service.open_meteo.open_meteo_client.requests.get') -def test_brightness_observation(mock_get, brightness_service_stub): - from datetime import datetime,UTC +def test_brightness_observation(mock_get, lat, lon, brightness_service_stub): + from datetime import datetime, UTC mock_response = mock_get.return_value mock_response.status_code = 200 mock_response.json.return_value={"elevation":0.,"hourly":{"cloud_cover":[0]*24}} - lat,lon = (42.,42.) request = brightness_service_pb2.Coordinates(lat=lat,lon=lon) response = brightness_service_stub.GetBrightnessObservation(request) - assert response.lat == lat assert response.lon == lon parsed_utc_iso = datetime.fromisoformat(response.utc_iso) diff --git a/pc/README.md b/pc/README.md index ab46a84..990ddf0 100644 --- a/pc/README.md +++ b/pc/README.md @@ -4,7 +4,7 @@ Pulls brightness observation messages off of the prediction queue and: -- inserts to postgres +- inserts to postgres (`brightnessobservation` table) - broadcasts over websockets connection ```shell diff --git a/pc/requirements-test.txt b/pc/requirements-test.txt new file mode 100644 index 0000000..5a7b384 --- /dev/null +++ b/pc/requirements-test.txt @@ -0,0 +1,2 @@ +pytest +pytest-async diff --git a/pc/tests/__init__.py b/pc/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pc/tests/test_consumer.py b/pc/tests/test_consumer.py new file mode 100644 index 0000000..5871ed8 --- /dev/null +++ b/pc/tests/test_consumer.py @@ -0,0 +1 @@ +import pytest