Skip to content

Commit

Permalink
Further updates to the API source enum (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
rstrange-scottlogic authored Jun 27, 2024
1 parent f49da87 commit 3d889de
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
map_measurements,
map_summarized_measurements,
)
from air_quality.api.types import ApiSource, MeasurementSummaryDto, MeasurementDto
from air_quality.database.in_situ import get_averaged, find_by_criteria
from air_quality.api.types import MeasurementSummaryDto, MeasurementDto
from air_quality.database.in_situ import ApiSource, get_averaged, find_by_criteria
from air_quality.database.locations import AirQualityLocationType

router = APIRouter()
Expand Down
8 changes: 2 additions & 6 deletions air-quality-backend/src/air_quality/api/types.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from datetime import datetime
from enum import Enum

from typing_extensions import Generic, TypedDict, NotRequired, TypeVar

from air_quality.database.in_situ import ApiSource
from air_quality.database.locations import AirQualityLocationType


class ApiSource(Enum):
OPENAQ = "openaq"


class PollutantDataDto(TypedDict):
aqi_level: int
value: float
Expand All @@ -32,7 +28,7 @@ class MeasurementDto(TypedDict):
measurement_date: datetime
location_type: AirQualityLocationType
location_name: str
api_source: str
api_source: ApiSource
no2: NotRequired[float]
o3: NotRequired[float]
pm2_5: NotRequired[float]
Expand Down
8 changes: 6 additions & 2 deletions air-quality-backend/src/air_quality/database/in_situ.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
from datetime import datetime, timedelta
from enum import Enum
from typing import List, TypedDict, NotRequired, Optional

from air_quality.api.types import ApiSource
from bson import ObjectId

from air_quality.database.locations import AirQualityLocationType
Expand All @@ -12,6 +12,10 @@
collection_name = "in_situ_data"


class ApiSource(Enum):
OPENAQ = "OpenAQ"


class InSituMetadata(TypedDict, total=False):
entity: str
sensor_type: str
Expand Down Expand Up @@ -87,7 +91,7 @@ def find_by_criteria(
if locations is not None:
criteria["name"] = {"$in": locations}
if api_source is not None:
criteria["api_source"] = api_source
criteria["api_source"] = api_source.value
logging.info(f"Querying collection with criteria: {criteria}")
return list(get_collection(collection_name).find(criteria))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
PollutantType,
pollutants_with_molecular_weight,
)
from air_quality.database.in_situ import InSituMeasurement
from air_quality.database.in_situ import InSituMeasurement, ApiSource
from air_quality.etl.common.unit_converter import convert_ppm_to_mgm3
from air_quality.etl.forecast.forecast_data import ForecastData, ForecastDataType

Expand All @@ -32,7 +32,7 @@ def _create_document(
measurement, city_name: str, location_type: AirQualityLocationType
) -> InSituMeasurement:
return {
"api_source": "OpenAQ",
"api_source": ApiSource.OPENAQ.value,
"measurement_date": datetime.strptime(
measurement["date"]["utc"], "%Y-%m-%dT%H:%M:%S%z"
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test__measurements_applies_appropriate_filters__when_request_valid():
params = {
**measurement_request_defaults,
"location_names": ["London", "Paris"],
"api_source": "openaq",
"api_source": "OpenAQ",
}
with patch(
"air_quality.api.measurements_controller.find_by_criteria", return_value=[]
Expand Down
22 changes: 7 additions & 15 deletions air-quality-backend/tests/database/in_situ_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import mongomock
import pytest
from unittest.mock import patch

from air_quality.database.in_situ import (
find_by_criteria,
insert_data,
delete_data_before,
get_averaged,
ApiSource,
)
from air_quality.database.locations import AirQualityLocationType
from tests.util.mock_measurement import create_mock_measurement_document
Expand Down Expand Up @@ -117,20 +119,10 @@ def test__delete_data_before(mock_collection):
"measurement_date_to": datetime(2024, 5, 1, 11, 0),
"location_type": AirQualityLocationType.CITY,
"locations": ["London"],
"api_source": "test_api",
"api_source": ApiSource.OPENAQ,
},
["London"],
),
(
{
"measurement_date_from": datetime(2024, 5, 1, 5, 0),
"measurement_date_to": datetime(2024, 5, 1, 11, 0),
"location_type": AirQualityLocationType.CITY,
"locations": ["London"],
"api_source": "real_api",
},
[],
),
],
)
def test__find_by_criteria(params, expected_names, mock_collection):
Expand All @@ -141,7 +133,7 @@ def test__find_by_criteria(params, expected_names, mock_collection):
"o3": 123,
"location_type": "city",
"location_name": "test",
"api_source": "test_api",
"api_source": "OpenAQ",
}
mock_collection.insert_many(
[
Expand All @@ -158,9 +150,9 @@ def test__find_by_criteria(params, expected_names, mock_collection):
]
)

assert (
list(map(lambda x: x["name"], find_by_criteria(**params))) == expected_names
)
response = find_by_criteria(**params)

assert list(map(lambda x: x["name"], response)) == expected_names


def test__get_averaged(mock_collection):
Expand Down
4 changes: 2 additions & 2 deletions air-quality-ui/src/services/measurement-data-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ describe('Measurement Data Service', () => {
dateTo,
'city',
[],
'openaq',
'OpenAQ',
)

expect(result).toEqual([])
expect(global.fetch).toHaveBeenCalledWith(
expect.stringMatching('&api_source=openaq'),
expect.stringMatching('&api_source=OpenAQ'),
expect.anything(),
)
})
Expand Down

0 comments on commit 3d889de

Please sign in to comment.