Skip to content

Commit

Permalink
update date filters from yyyy-mm-dd to dd-mm-yyyy
Browse files Browse the repository at this point in the history
  • Loading branch information
Donna-H authored and jacksonj04 committed Oct 3, 2023
1 parent aa07964 commit c758a4a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
27 changes: 18 additions & 9 deletions judgments/test_date_parsing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import date
from typing import Dict

from django.test import TestCase
Expand Down Expand Up @@ -41,15 +42,15 @@ def test_a_blank_date_does_not_count_as_provided(self):
"""
params = {"date": "", "date_day": "3", "date_month": "1", "date_year": "2020"}
parsed = parse_date_parameter(params, "date")
self.assertEqual(parsed, "2020-01-03")
self.assertEqual(parsed, date(2020, 1, 3))

def test_constructs_a_date_from_date_parts(self):
"""
When no date is provided directly, it constructs one from the given day month and year.
"""
params = {"date_day": "3", "date_month": "1", "date_year": "2020"}
parsed = parse_date_parameter(params, "date")
self.assertEqual(parsed, "2020-01-03")
self.assertEqual(parsed, date(2020, 1, 3))

def test_returns_none_if_no_date_provided(self):
"""
Expand All @@ -65,15 +66,15 @@ def test_returns_beginning_of_month_when_no_day(self):
"""
params = {"date_month": "5", "date_year": "2020"}
parsed = parse_date_parameter(params, "date")
self.assertEqual(parsed, "2020-05-01")
self.assertEqual(parsed, date(2020, 5, 1))

def test_returns_january_when_no_month(self):
"""
When a year is provided but no month or day, it defaults to the first day of January of that year.
"""
params = {"date_year": "2020"}
parsed = parse_date_parameter(params, "date")
self.assertEqual(parsed, "2020-01-01")
self.assertEqual(parsed, date(2020, 1, 1))

def test_returns_none_if_year_is_blank(self):
"""When a year parameter is provided but empty, it returns none"""
Expand All @@ -85,7 +86,7 @@ def test_blank_months_and_days_count_as_undefined(self):
"""Blank months and days are treated as undefined, and default to the first month / day."""
params = {"date_year": "2009", "date_month": "", "date_day": ""}
parsed = parse_date_parameter(params, "date")
self.assertEqual(parsed, "2009-01-01")
self.assertEqual(parsed, date(2009, 1, 1))

def test_returns_december_when_no_month_and_default_to_last_selected(self):
"""
Expand All @@ -94,7 +95,7 @@ def test_returns_december_when_no_month_and_default_to_last_selected(self):
"""
params = {"date_year": "2020"}
parsed = parse_date_parameter(params, "date", default_to_last=True)
self.assertEqual(parsed, "2020-12-31")
self.assertEqual(parsed, date(2020, 12, 31))

def test_returns_lastday_long_month_when_year_and_month_given(self):
"""
Expand All @@ -103,7 +104,7 @@ def test_returns_lastday_long_month_when_year_and_month_given(self):
"""
params = {"date_month": "5", "date_year": "2020"}
parsed = parse_date_parameter(params, "date", default_to_last=True)
self.assertEqual(parsed, "2020-05-31")
self.assertEqual(parsed, date(2020, 5, 31))

def test_returns_lastday_short_month_when_year_and_month_given(self):
"""
Expand All @@ -112,7 +113,7 @@ def test_returns_lastday_short_month_when_year_and_month_given(self):
"""
params = {"date_month": "2", "date_year": "2021"}
parsed = parse_date_parameter(params, "date", default_to_last=True)
self.assertEqual(parsed, "2021-02-28")
self.assertEqual(parsed, date(2021, 2, 28))

def test_returns_lastday_month_when_leap_year_and_month_given(self):
"""
Expand All @@ -121,7 +122,7 @@ def test_returns_lastday_month_when_leap_year_and_month_given(self):
"""
params = {"date_month": "2", "date_year": "2020"}
parsed = parse_date_parameter(params, "date", default_to_last=True)
self.assertEqual(parsed, "2020-02-29")
self.assertEqual(parsed, date(2020, 2, 29))

def test_raises_error_when_a_silly_month_is_given(self):
"""
Expand All @@ -136,3 +137,11 @@ def test_raises_error_when_a_silly_day_is_given(self):
"""
params = {"date_month": "04", "date_day": "31", "date_year": "2020"}
self.assertRaises(ValueError, parse_date_parameter, params, "date")

def test_checks_return_date_format_as_day_month_year(self):
"""
Checks date is provided in day, month, year format.
"""
params = {"date_day": "3", "date_month": "1", "date_year": "2020"}
parsed = parse_date_parameter(params, "date")
self.assertEqual(parsed, date(2020, 1, 3))
8 changes: 4 additions & 4 deletions judgments/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import math
import re
from calendar import monthrange
from datetime import datetime
from datetime import date, datetime
from typing import Optional, TypedDict
from urllib.parse import parse_qs, urlparse

Expand Down Expand Up @@ -190,7 +190,7 @@ def parse_parameter_as_int(params, parameter_name, default=None):
return default


def parse_date_parameter(params, param_name, default_to_last=False):
def parse_date_parameter(params, param_name, default_to_last=False) -> Optional[date]:
year_param_name = f"{param_name}_year"
month_param_name = f"{param_name}_month"
day_param_name = f"{param_name}_day"
Expand All @@ -204,12 +204,12 @@ def parse_date_parameter(params, param_name, default_to_last=False):
default_day = monthrange(year, month)[1] if default_to_last else 1
day = parse_parameter_as_int(params, day_param_name, default=default_day)

dt = datetime(year, month, day)
return dt.strftime("%Y-%m-%d")
return date(year, month, day)
elif parameter_provided(params, month_param_name) or parameter_provided(
params, day_param_name
):
raise ValueError(gettext("search.errors.missing_year_detail"))
return None


def get_document_by_uri(document_uri: str) -> Document:
Expand Down

0 comments on commit c758a4a

Please sign in to comment.