Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert hfi_calc bdd tests #4128

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions api/app/tests/hfi/test_hfi_planning_areas.feature

This file was deleted.

71 changes: 19 additions & 52 deletions api/app/tests/hfi/test_hfi_planning_areas.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,38 @@
""" BDD tests for API /hfi-calc/ """
from pytest_bdd import scenario, given, then, parsers
import pytest
from starlette.testclient import TestClient
from aiohttp import ClientSession
from sqlalchemy.orm import Session
from app.tests.common import default_mock_client_get
import app.main
from app.db.models.hfi_calc import PlanningWeatherStation, FireCentre, FuelType, PlanningArea
import app.routers.hfi_calc


@pytest.mark.usefixtures("mock_jwt_decode")
@scenario('test_hfi_planning_areas.feature', 'Get fire centres, planning areas, and weather stations')
def test_hfi_planning_areas():
""" BDD Scenario. """
def mock_get_fire_weather_stations(_: Session):
fire_centre = FireCentre(id=1, name="Kamloops Fire Centre")
planning_area_1 = PlanningArea(id=2, name="Kamloops (K2)", fire_centre_id=1)
planning_area_2 = PlanningArea(id=3, name="Vernon (K4)", fire_centre_id=1)
fuel_type_1 = FuelType(id=1, abbrev="O1B", description="neigh", fuel_type_code="O1B", percentage_conifer=0, percentage_dead_fir=0)
fuel_type_2 = FuelType(id=2, abbrev="C7B", description="moo", fuel_type_code="C7", percentage_conifer=100, percentage_dead_fir=0)
return (
(PlanningWeatherStation(station_code=322, fuel_type_id=1, planning_area_id=1), fuel_type_1, planning_area_1, fire_centre),
(PlanningWeatherStation(station_code=346, fuel_type_id=2, planning_area_id=2), fuel_type_2, planning_area_2, fire_centre),
(PlanningWeatherStation(station_code=334, fuel_type_id=2, planning_area_id=2), fuel_type_2, planning_area_2, fire_centre),
)


@given('I request all fire centres, planning areas, etc.', target_fixture='response')
def given_hfi_planning_areas_request(monkeypatch):
""" Make /hfi-calc/ request using mocked out ClientSession.
"""

def mock_get_fire_weather_stations(_: Session):
fire_centre = FireCentre(id=1, name='Kamloops Fire Centre')
planning_area_1 = PlanningArea(id=2, name='Kamloops (K2)', fire_centre_id=1)
planning_area_2 = PlanningArea(id=3, name='Vernon (K4)', fire_centre_id=1)
fuel_type_1 = FuelType(id=1, abbrev='O1B', description='neigh', fuel_type_code="O1B",
percentage_conifer=0, percentage_dead_fir=0)
fuel_type_2 = FuelType(id=2, abbrev='C7B', description='moo', fuel_type_code='C7',
percentage_conifer=100, percentage_dead_fir=0)
return (
(PlanningWeatherStation(station_code=322, fuel_type_id=1,
planning_area_id=1), fuel_type_1, planning_area_1, fire_centre),
(PlanningWeatherStation(station_code=346, fuel_type_id=2,
planning_area_id=2), fuel_type_2, planning_area_2, fire_centre),
(PlanningWeatherStation(station_code=334, fuel_type_id=2,
planning_area_id=2), fuel_type_2, planning_area_2, fire_centre)
)

monkeypatch.setattr(ClientSession, 'get', default_mock_client_get)
monkeypatch.setattr(app.hfi.hfi_calc, 'get_fire_weather_stations', mock_get_fire_weather_stations)

# Create API client and get the response.
@pytest.mark.usefixtures("mock_client_session", "mock_jwt_decode")
def test_get_fire_weather_stations(monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(app.hfi.hfi_calc, "get_fire_weather_stations", mock_get_fire_weather_stations)
client = TestClient(app.main.app)
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer token'}

return dict(response=client.get('/api/hfi-calc/fire-centres/', headers=headers))
headers = {"Content-Type": "application/json", "Authorization": "Bearer token"}
response = client.get("/api/hfi-calc/fire-centres/", headers=headers)


@then(parsers.parse('there are at least {num_fire_centres} fire centres'),
converters={'num_fire_centres': int})
def assert_number_of_fire_centres(response, num_fire_centres):
""" Assert that we receive the minimum expected number of fire centres """
assert len(response['response'].json()['fire_centres']) >= num_fire_centres

assert len(response.json()["fire_centres"]) >= 1

@then('each fire centre has at least 1 planning area')
def assert_min_num_planning_areas_in_fire_centre(response):
""" Assert that each fire centre returned has at least 1 planning area assigned to it """
for fire_centre in response['response'].json()['fire_centres']:
for fire_centre in response.json()["fire_centres"]:
assert len(fire_centre['planning_areas']) >= 1


@then('each planning area has at least 1 weather station')
def assert_min_num_stations_in_planning_area(response):
""" Assert that each planning area returned has at least 1 weather station assigned to it """
for fire_centre in response['response'].json()['fire_centres']:
for fire_centre in response.json()["fire_centres"]:
for planning_area in fire_centre['planning_areas']:
assert len(planning_area['stations']) >= 1
Loading