Skip to content

Commit

Permalink
Proof: date validation
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Aug 23, 2024
1 parent 3c471a5 commit 986d299
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions open_prices/proofs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions open_prices/proofs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 986d299

Please sign in to comment.