Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nonnontrivial committed Oct 18, 2024
1 parent a66113c commit b7ee4bf
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pytest
pytest==7.4.3
35 changes: 23 additions & 12 deletions api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pc/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest
pytest-async
Empty file added pc/tests/__init__.py
Empty file.
1 change: 1 addition & 0 deletions pc/tests/test_consumer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import pytest

0 comments on commit b7ee4bf

Please sign in to comment.