Skip to content

Commit

Permalink
Add completeness and time validation to Report
Browse files Browse the repository at this point in the history
  • Loading branch information
dalonsoa committed Jan 25, 2024
1 parent 8b973de commit 6c99dc2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
10 changes: 10 additions & 0 deletions measurement/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Report(MeasurementBase):
used_for_monthly = models.BooleanField(
verbose_name="Used for monthly?", default=False
)
completeness = models.DecimalField(max_digits=4, decimal_places=1, null=False)

class Meta:
default_permissions = ()
Expand All @@ -92,6 +93,15 @@ def clean(self) -> None:
"Only daily data can be used for monthly report calculations."
)

if self.report_type == ReportType.HOURLY:
self.time = self.time.replace(minute=0, second=0, microsecond=0)
elif self.report_type == ReportType.DAILY:
self.time = self.time.replace(hour=0, minute=0, second=0, microsecond=0)
elif self.report_type == ReportType.MONTLY:
self.time = self.time.replace(
day=1, hour=0, minute=0, second=0, microsecond=0
)


class Measurement(MeasurementBase):
"""Class to store the measurements and their validation status.
Expand Down
64 changes: 46 additions & 18 deletions tests/measurement/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_query_ordering(self):

class TestReport(TestCase):
def setUp(self) -> None:
from measurement.models import Report, ReportType
from measurement.models import Report
from station.models import Station
from variable.models import Variable

Expand All @@ -83,41 +83,69 @@ def setUp(self) -> None:
station=station,
variable=variable,
value=42,
report_type=ReportType.HOURLY,
report_type=Report.HOURLY,
completeness=1,
)

def test_clean(self):
def test_clean_report_type_hourly(self):
from measurement.models import ReportType

# We check 'used_for_daily' compatibility
# Works if report type is Hourly
self.model.used_for_daily = True
self.model.clean()
self.model.report_type = ReportType.HOURLY
self.model.clean() # Should not raise any exception

# But fails for the other two
def test_clean_report_type_daily(self):
from measurement.models import ReportType

self.model.used_for_monthly = True
self.model.report_type = ReportType.DAILY
with self.assertRaises(ValidationError):
self.model.clean()
self.model.clean() # Should not raise any exception

def test_clean_report_type_monthly(self):
from measurement.models import ReportType

self.model.report_type = ReportType.MONTLY
self.model.clean() # Should not raise any exception

def test_clean_inconsistent_report_type_hourly(self):
from measurement.models import ReportType

self.model.used_for_daily = True
self.model.report_type = ReportType.DAILY
with self.assertRaises(ValidationError):
self.model.clean()

# We check 'used_for_monthly' compatibility
# Works if report type is Daily
self.model.used_for_daily = False
self.model.used_for_monthly = True
self.model.report_type = ReportType.DAILY
self.model.clean()
def test_clean_inconsistent_report_type_daily(self):
from measurement.models import ReportType

# But not for the other two
self.model.used_for_monthly = True
self.model.report_type = ReportType.HOURLY
with self.assertRaises(ValidationError):
self.model.clean()

def test_clean_time_hourly(self):
from measurement.models import ReportType

self.model.report_type = ReportType.HOURLY
self.model.clean()
expected_time = datetime(2018, 1, 9, 23, 0, 0, tzinfo=pytz.UTC)
self.assertEqual(self.model.time, expected_time)

def test_clean_time_daily(self):
from measurement.models import ReportType

self.model.report_type = ReportType.DAILY
self.model.clean()
expected_time = datetime(2018, 1, 9, 0, 0, 0, tzinfo=pytz.UTC)
self.assertEqual(self.model.time, expected_time)

def test_clean_time_monthly(self):
from measurement.models import ReportType

self.model.report_type = ReportType.MONTLY
with self.assertRaises(ValidationError):
self.model.clean()
self.model.clean()
expected_time = datetime(2018, 1, 1, 0, 0, 0, tzinfo=pytz.UTC)
self.assertEqual(self.model.time, expected_time)


class TestMeasurement(TestCase):
Expand Down

0 comments on commit 6c99dc2

Please sign in to comment.