Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meomancer committed Oct 18, 2024
1 parent 950d346 commit 5f2285d
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 34 deletions.
4 changes: 3 additions & 1 deletion django_project/gap/providers/airborne_observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@ def get_measurements(self, start_date: datetime, end_date: datetime):
date_time__lte=end_date,
dataset_attribute__in=self.attributes,
station_history__in=nearest_histories
).order_by('date_time', 'station', 'dataset_attribute')
).order_by(
'date_time', 'station', 'dataset_attribute__attribute__name'
)
18 changes: 14 additions & 4 deletions django_project/gap/providers/cbam.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ class CBAMNetCDFReader(BaseNetCDFReader):
def __init__(
self, dataset: Dataset, attributes: List[DatasetAttribute],
location_input: DatasetReaderInput, start_date: datetime,
end_date: datetime) -> None:
end_date: datetime,
altitudes: (float, float) = None
) -> None:
"""Initialize CBAMNetCDFReader class.
:param dataset: Dataset from CBAM provider
Expand All @@ -104,9 +106,13 @@ def __init__(
:type start_date: datetime
:param end_date: End date time filter
:type end_date: datetime
:param altitudes: Altitudes for the reader
:type altitudes: (float, float)
"""
super().__init__(
dataset, attributes, location_input, start_date, end_date)
dataset, attributes, location_input, start_date, end_date,
altitudes=altitudes
)

def read_historical_data(self, start_date: datetime, end_date: datetime):
"""Read historical data from dataset.
Expand Down Expand Up @@ -213,10 +219,14 @@ class CBAMZarrReader(BaseZarrReader, CBAMNetCDFReader):
def __init__(
self, dataset: Dataset, attributes: List[DatasetAttribute],
location_input: DatasetReaderInput, start_date: datetime,
end_date: datetime) -> None:
end_date: datetime,
altitudes: (float, float) = None
) -> None:
"""Initialize CBAMZarrReader class."""
super().__init__(
dataset, attributes, location_input, start_date, end_date)
dataset, attributes, location_input, start_date, end_date,
altitudes=altitudes
)

def _read_variables_by_point(
self, dataset: xrDataset, variables: List[str],
Expand Down
16 changes: 12 additions & 4 deletions django_project/gap/providers/salient.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ class SalientNetCDFReader(BaseNetCDFReader):
def __init__(
self, dataset: Dataset, attributes: List[DatasetAttribute],
location_input: DatasetReaderInput, start_date: datetime,
end_date: datetime) -> None:
end_date: datetime,
altitudes: (float, float) = None
) -> None:
"""Initialize SalientNetCDFReader class.
:param dataset: Dataset from Salient provider
Expand All @@ -143,7 +145,9 @@ def __init__(
:type end_date: datetime
"""
super().__init__(
dataset, attributes, location_input, start_date, end_date)
dataset, attributes, location_input, start_date, end_date,
altitudes=altitudes
)
self.latest_forecast_date = None

def read_forecast_data(self, start_date: datetime, end_date: datetime):
Expand Down Expand Up @@ -266,10 +270,14 @@ class SalientZarrReader(BaseZarrReader, SalientNetCDFReader):
def __init__(
self, dataset: Dataset, attributes: List[DatasetAttribute],
location_input: DatasetReaderInput, start_date: datetime,
end_date: datetime) -> None:
end_date: datetime,
altitudes: (float, float) = None
) -> None:
"""Initialize SalientZarrReader class."""
super().__init__(
dataset, attributes, location_input, start_date, end_date)
dataset, attributes, location_input, start_date, end_date,
altitudes=altitudes
)

def read_forecast_data(self, start_date: datetime, end_date: datetime):
"""Read forecast data from dataset.
Expand Down
16 changes: 12 additions & 4 deletions django_project/gap/providers/tio.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,14 @@ class TomorrowIODatasetReader(BaseDatasetReader):
def __init__(
self, dataset: Dataset, attributes: List[DatasetAttribute],
location_input: DatasetReaderInput, start_date: datetime,
end_date: datetime, verbose = False) -> None:
end_date: datetime, verbose = False,
altitudes: (float, float) = None
) -> None:
"""Initialize Dataset Reader."""
super().__init__(
dataset, attributes, location_input, start_date, end_date)
dataset, attributes, location_input, start_date, end_date,
altitudes=altitudes
)
self.errors = None
self.warnings = None
self.results = []
Expand Down Expand Up @@ -583,10 +587,14 @@ class TioZarrReader(BaseZarrReader):
def __init__(
self, dataset: Dataset, attributes: List[DatasetAttribute],
location_input: DatasetReaderInput, start_date: datetime,
end_date: datetime) -> None:
end_date: datetime,
altitudes: (float, float) = None
) -> None:
"""Initialize TioZarrReader class."""
super().__init__(
dataset, attributes, location_input, start_date, end_date)
dataset, attributes, location_input, start_date, end_date,
altitudes=altitudes
)
self.latest_forecast_date = None

def read_forecast_data(self, start_date: datetime, end_date: datetime):
Expand Down
10 changes: 8 additions & 2 deletions django_project/gap/utils/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ class BaseNetCDFReader(BaseDatasetReader):
def __init__(
self, dataset: Dataset, attributes: List[DatasetAttribute],
location_input: DatasetReaderInput,
start_date: datetime, end_date: datetime) -> None:
start_date: datetime, end_date: datetime,
altitudes: (float, float) = None
) -> None:
"""Initialize BaseNetCDFReader class.
:param dataset: Dataset for reading
Expand All @@ -195,9 +197,13 @@ def __init__(
:type start_date: datetime
:param end_date: End date time filter
:type end_date: datetime
:param altitudes: Altitudes for the reader
:type altitudes: (float, float)
"""
super().__init__(
dataset, attributes, location_input, start_date, end_date)
dataset, attributes, location_input, start_date, end_date,
altitudes=altitudes
)
self.xrDatasets = []

def setup_reader(self):
Expand Down
2 changes: 2 additions & 0 deletions django_project/gap/utils/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ def __init__(
:type end_date: datetime
:param output_type: Output type
:type output_type: str
:param altitudes: Altitudes for the reader
:type altitudes: (float, float)
"""
self.dataset = dataset
self.attributes = attributes
Expand Down
8 changes: 6 additions & 2 deletions django_project/gap/utils/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class BaseZarrReader(BaseNetCDFReader):
def __init__(
self, dataset: Dataset, attributes: List[DatasetAttribute],
location_input: DatasetReaderInput,
start_date: datetime, end_date: datetime) -> None:
start_date: datetime, end_date: datetime,
altitudes: (float, float) = None
) -> None:
"""Initialize BaseZarrReader class.
:param dataset: Dataset for reading
Expand All @@ -53,7 +55,9 @@ def __init__(
:type end_date: datetime
"""
super().__init__(
dataset, attributes, location_input, start_date, end_date)
dataset, attributes, location_input, start_date, end_date,
altitudes=altitudes
)

@classmethod
def get_s3_variables(cls) -> dict:
Expand Down
34 changes: 21 additions & 13 deletions django_project/gap_api/api_views/measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Polygon
)
from django.db.models.functions import Lower
from django.db.utils import ProgrammingError
from django.http import StreamingHttpResponse
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
Expand All @@ -44,22 +45,28 @@

def attribute_list():
"""Attribute list."""
return list(
Attribute.objects.all().values_list(
'variable_name', flat=True
).order_by('variable_name')
)
try:
return list(
Attribute.objects.all().values_list(
'variable_name', flat=True
).order_by('variable_name')
)
except ProgrammingError:
pass


def default_attribute_list():
"""Attribute list."""
first = Attribute.objects.all().order_by('variable_name').first()
if first:
return Attribute.objects.all().order_by(
'variable_name'
).first().variable_name
else:
return ''
try:
first = Attribute.objects.all().order_by('variable_name').first()
if first:
return Attribute.objects.all().order_by(
'variable_name'
).first().variable_name
else:
return ''
except ProgrammingError:
pass


class MeasurementAPI(APIView):
Expand Down Expand Up @@ -407,7 +414,8 @@ def get_response_data(self) -> Response:
da.dataset, [da], location, start_dt, end_dt,
altitudes=(min_altitudes, max_altitudes),
)
except TypeError:
except TypeError as e:
print(e)
pass

response = None
Expand Down
11 changes: 7 additions & 4 deletions django_project/gap_api/tests/test_measurement_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
class MockDatasetReader(BaseDatasetReader):
"""Class to mock a dataset reader."""

def __init__(self, dataset, attributes: List[DatasetAttribute],
location_input: DatasetReaderInput, start_date: datetime,
end_date: datetime,
output_type=DatasetReaderOutputType.JSON) -> None:
def __init__(
self, dataset, attributes: List[DatasetAttribute],
location_input: DatasetReaderInput, start_date: datetime,
end_date: datetime,
output_type=DatasetReaderOutputType.JSON,
altitudes: (float, float) = None
) -> None:
"""Initialize MockDatasetReader class."""
super().__init__(
dataset, attributes, location_input,
Expand Down

0 comments on commit 5f2285d

Please sign in to comment.