Skip to content

Commit

Permalink
Reorder milestone date field validations
Browse files Browse the repository at this point in the history
Matches field class hierarchy:
- CustomMessageDateField for invalid_message
- MilestoneDateField for required_message
  • Loading branch information
markhobson committed Jan 23, 2025
1 parent 4bf21a3 commit a279103
Showing 2 changed files with 51 additions and 51 deletions.
4 changes: 2 additions & 2 deletions schemes/views/schemes/milestones.py
Original file line number Diff line number Diff line change
@@ -121,12 +121,12 @@ def create_class(milestone: Milestone) -> type[MilestoneDatesForm]:

class DynamicMilestoneDatesForm(MilestoneDatesForm):
planned = MilestoneDateField(
required_message=f"Enter a {milestone_name.lower()} planned date",
invalid_message=f"{milestone_name} planned date must be a real date",
required_message=f"Enter a {milestone_name.lower()} planned date",
)
actual = MilestoneDateField(
required_message=f"Enter a {milestone_name.lower()} actual date",
invalid_message=f"{milestone_name} actual date must be a real date",
required_message=f"Enter a {milestone_name.lower()} actual date",
)

return DynamicMilestoneDatesForm
98 changes: 49 additions & 49 deletions tests/views/schemes/test_milestones.py
Original file line number Diff line number Diff line change
@@ -186,24 +186,24 @@ def test_from_domain(self) -> None:

@pytest.mark.usefixtures("app")
class TestMilestoneDatesForm:
def test_create_class_sets_required_message(self) -> None:
def test_create_class_sets_invalid_message(self) -> None:
form_class = MilestoneDatesForm.create_class(Milestone.DETAILED_DESIGN_COMPLETED)

form = form_class(planned=date(2020, 1, 2), actual=date(2020, 1, 3))
form = form_class(formdata=MultiDict([("planned", "x"), ("actual", "x")]))
form.validate()
assert (
"Enter a detailed design completed planned date" in form.planned.errors
and "Enter a detailed design completed actual date" in form.actual.errors
"Detailed design completed planned date must be a real date" in form.planned.errors
and "Detailed design completed actual date must be a real date" in form.actual.errors
)

def test_create_class_sets_invalid_message(self) -> None:
def test_create_class_sets_required_message(self) -> None:
form_class = MilestoneDatesForm.create_class(Milestone.DETAILED_DESIGN_COMPLETED)

form = form_class(formdata=MultiDict([("planned", "x"), ("actual", "x")]))
form = form_class(planned=date(2020, 1, 2), actual=date(2020, 1, 3))
form.validate()
assert (
"Detailed design completed planned date must be a real date" in form.planned.errors
and "Detailed design completed actual date must be a real date" in form.actual.errors
"Enter a detailed design completed planned date" in form.planned.errors
and "Enter a detailed design completed actual date" in form.actual.errors
)

@pytest.mark.parametrize(
@@ -566,6 +566,47 @@ def test_no_errors_when_valid(self, field_name: str) -> None:

assert not form.errors

@pytest.mark.parametrize(
"field_name, expected_error",
zip(
field_names,
[
"Feasibility design completed planned date must be a real date",
"Feasibility design completed actual date must be a real date",
"Preliminary design completed planned date must be a real date",
"Preliminary design completed actual date must be a real date",
"Detailed design completed planned date must be a real date",
"Detailed design completed actual date must be a real date",
"Construction started planned date must be a real date",
"Construction started actual date must be a real date",
"Construction completed planned date must be a real date",
"Construction completed actual date must be a real date",
],
),
)
@pytest.mark.parametrize(
"date_",
[
("x", "x", "x"),
("99", "1", "2020"),
("", "1", "2020"),
("2", "", "2020"),
("2", "1", ""),
("", "", "2020"),
("", "1", ""),
("2", "", ""),
],
)
def test_date_is_a_date(self, field_name: str, expected_error: str, date_: tuple[str, str, str]) -> None:
scheme = build_scheme(id_=0, reference="", name="", authority_id=0, type_=SchemeType.CONSTRUCTION)
form_class = ChangeMilestoneDatesForm.create_class(scheme)
form = form_class(formdata=MultiDict([(field_name, date_[0]), (field_name, date_[1]), (field_name, date_[2])]))

form.validate()

(field_name1, field_name2) = field_name.split("-")
assert expected_error in form.errors[field_name1][field_name2]

@pytest.mark.parametrize("field_name", field_names)
def test_date_without_initial_value_is_optional(self, field_name: str) -> None:
scheme = build_scheme(id_=0, reference="", name="", authority_id=0, type_=SchemeType.CONSTRUCTION)
@@ -621,47 +662,6 @@ def test_date_with_initial_value_is_required(

assert expected_error in form.errors[field_name1][field_name2]

@pytest.mark.parametrize(
"field_name, expected_error",
zip(
field_names,
[
"Feasibility design completed planned date must be a real date",
"Feasibility design completed actual date must be a real date",
"Preliminary design completed planned date must be a real date",
"Preliminary design completed actual date must be a real date",
"Detailed design completed planned date must be a real date",
"Detailed design completed actual date must be a real date",
"Construction started planned date must be a real date",
"Construction started actual date must be a real date",
"Construction completed planned date must be a real date",
"Construction completed actual date must be a real date",
],
),
)
@pytest.mark.parametrize(
"date_",
[
("x", "x", "x"),
("99", "1", "2020"),
("", "1", "2020"),
("2", "", "2020"),
("2", "1", ""),
("", "", "2020"),
("", "1", ""),
("2", "", ""),
],
)
def test_date_is_a_date(self, field_name: str, expected_error: str, date_: tuple[str, str, str]) -> None:
scheme = build_scheme(id_=0, reference="", name="", authority_id=0, type_=SchemeType.CONSTRUCTION)
form_class = ChangeMilestoneDatesForm.create_class(scheme)
form = form_class(formdata=MultiDict([(field_name, date_[0]), (field_name, date_[1]), (field_name, date_[2])]))

form.validate()

(field_name1, field_name2) = field_name.split("-")
assert expected_error in form.errors[field_name1][field_name2]


class TestMilestoneRevisionRepr:
def test_from_domain(self) -> None:

0 comments on commit a279103

Please sign in to comment.