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

Add completeness and time validation to Report #133

Merged
merged 2 commits into from
Jan 25, 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
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
60 changes: 44 additions & 16 deletions tests/measurement/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,40 +84,68 @@ def setUp(self) -> None:
variable=variable,
value=42,
report_type=ReportType.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
Loading