From 986d29971c2604f7ae964d564e40667fecca5554 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Wed, 21 Aug 2024 16:20:43 +0200 Subject: [PATCH] Proof: date validation --- open_prices/proofs/models.py | 15 +++++++++++++++ open_prices/proofs/tests.py | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/open_prices/proofs/models.py b/open_prices/proofs/models.py index 5b8a1299..f0da72dc 100644 --- a/open_prices/proofs/models.py +++ b/open_prices/proofs/models.py @@ -58,6 +58,21 @@ class Meta: def clean(self, *args, **kwargs): # dict to store all ValidationErrors validation_errors = dict() + # proof rules + # - date should have the right format & not be in the future + if self.date: + if type(self.date) is str: + validation_errors = utils.add_validation_error( + validation_errors, + "date", + "Parsing error. Expected format: YYYY-MM-DD", + ) + elif self.date > timezone.now().date(): + validation_errors = utils.add_validation_error( + validation_errors, + "date", + "Should not be in the future", + ) # location rules # - location_osm_id should be set if location_osm_type is set # - location_osm_type should be set if location_osm_id is set diff --git a/open_prices/proofs/tests.py b/open_prices/proofs/tests.py index 77a65924..fb38f39e 100644 --- a/open_prices/proofs/tests.py +++ b/open_prices/proofs/tests.py @@ -17,6 +17,12 @@ def setUpTestData(cls): location_post_create_fetch_data_from_openstreetmap, sender=Location ) + def test_proof_date_validation(self): + for DATE_OK in [None, "2024-01-01"]: + ProofFactory(date=DATE_OK) + for DATE_NOT_OK in ["3000-01-01", "01-01-2000"]: + self.assertRaises(ValidationError, ProofFactory, date=DATE_NOT_OK) + def test_proof_location_validation(self): # both location_osm_id & location_osm_type not set ProofFactory(location_osm_id=None, location_osm_type=None)