Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import datetime

from django import forms
from django.forms import Form
from django.forms.widgets import Textarea
Expand All @@ -8,7 +6,7 @@
from manage_breast_screening.nhsuk_forms.fields import (
BooleanField,
CharField,
IntegerField,
YearField,
)
from manage_breast_screening.nhsuk_forms.fields.choice_fields import (
MultipleChoiceField,
Expand All @@ -22,6 +20,8 @@ class BreastAugmentationHistoryForm(Form):
right_breast_procedures = MultipleChoiceField(
label="Right breast",
label_classes="nhsuk-fieldset__legend--s",
visually_hidden_label_prefix="What procedure have they had in their ",
visually_hidden_label_suffix="?",
choices=BreastAugmentationHistoryItem.Procedure,
error_messages={
"required": "Select procedures for the right breast",
Expand All @@ -31,17 +31,31 @@ class BreastAugmentationHistoryForm(Form):
left_breast_procedures = MultipleChoiceField(
label="Left breast",
label_classes="nhsuk-fieldset__legend--s",
visually_hidden_label_prefix="What procedure have they had in their ",
visually_hidden_label_suffix="?",
choices=BreastAugmentationHistoryItem.Procedure,
error_messages={
"required": "Select procedures for the left breast",
},
exclusive_choices={"NO_PROCEDURES"},
)
procedure_year = YearField(
hint="Leave blank if unknown",
required=False,
label="Year of procedure (optional)",
label_classes="nhsuk-label--m",
classes="nhsuk-input--width-4",
)
implants_have_been_removed = BooleanField(
required=False,
label="Implants have been removed",
classes="app-checkboxes",
)
removal_year = YearField(
required=False,
label="Year removed (if available)",
classes="nhsuk-input--width-4",
)
additional_details = CharField(
hint="Include any other relevant information about the procedure",
required=False,
Expand All @@ -52,44 +66,6 @@ class BreastAugmentationHistoryForm(Form):
error_messages={"max_words": "Additional details must be 500 words or less"},
)

def __init__(self, *args, participant, **kwargs):
super().__init__(*args, **kwargs)

# if entered, years should be between 80 years ago and this year
max_year = datetime.date.today().year
min_year = max_year - 80
year_outside_range_error_message = (
f"Year should be between {min_year} and {max_year}."
)
year_invalid_format_error_message = "Enter year as a number."

self.fields["procedure_year"] = IntegerField(
hint="Leave blank if unknown",
required=False,
label="Year of procedure (optional)",
label_classes="nhsuk-label--m",
classes="nhsuk-input--width-4",
min_value=min_year,
max_value=max_year,
error_messages={
"min_value": year_outside_range_error_message,
"max_value": year_outside_range_error_message,
"invalid": year_invalid_format_error_message,
},
)
self.fields["removal_year"] = IntegerField(
required=False,
label="Year removed (if available)",
classes="nhsuk-input--width-4",
min_value=min_year,
max_value=max_year,
error_messages={
"min_value": year_outside_range_error_message,
"max_value": year_outside_range_error_message,
"invalid": year_invalid_format_error_message,
},
)

def model_values(self):
return dict(
left_breast_procedures=self.cleaned_data.get("left_breast_procedures", []),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
@pytest.mark.django_db
class TestBreastAugmentationHistoryForm:
def test_no_data(self, clinical_user):
appointment = AppointmentFactory()
request = RequestFactory().get("/test-form")
request.user = clinical_user

form = BreastAugmentationHistoryForm(
QueryDict(), participant=appointment.participant
)
form = BreastAugmentationHistoryForm(QueryDict())

assert not form.is_valid()
assert form.errors == {
Expand All @@ -32,7 +29,6 @@ def test_no_data(self, clinical_user):
}

def test_procedure_year_invalid_format(self, clinical_user):
appointment = AppointmentFactory()
request = RequestFactory().get("/test-form")
request.user = clinical_user

Expand All @@ -51,11 +47,10 @@ def test_procedure_year_invalid_format(self, clinical_user):
doseq=True,
)
),
participant=appointment.participant,
)

assert not form.is_valid()
assert form.errors == {"procedure_year": ["Enter year as a number."]}
assert form.errors == {"procedure_year": ["Enter a whole number."]}

@pytest.mark.parametrize(
"selected_breast_procedures",
Expand All @@ -78,7 +73,6 @@ def test_procedure_year_invalid_format(self, clinical_user):
def test_no_procedures_and_other_options(
self, clinical_user, selected_breast_procedures
):
appointment = AppointmentFactory()
request = RequestFactory().get("/test-form")
request.user = clinical_user

Expand All @@ -94,7 +88,6 @@ def test_no_procedures_and_other_options(
doseq=True,
)
),
participant=appointment.participant,
)

assert not form.is_valid()
Expand All @@ -116,7 +109,6 @@ def test_no_procedures_and_other_options(
doseq=True,
)
),
participant=appointment.participant,
)

assert not form.is_valid()
Expand All @@ -127,7 +119,6 @@ def test_no_procedures_and_other_options(
}

def test_removal_year_invalid_format(self, clinical_user):
appointment = AppointmentFactory()
request = RequestFactory().get("/test-form")
request.user = clinical_user

Expand All @@ -147,13 +138,12 @@ def test_removal_year_invalid_format(self, clinical_user):
doseq=True,
)
),
participant=appointment.participant,
)

assert not form.is_valid()
assert form.errors == {
"removal_year": [
"Enter year as a number.",
"Enter a whole number.",
]
}

Expand All @@ -168,14 +158,15 @@ def test_removal_year_invalid_format(self, clinical_user):
],
)
def test_procedure_year_outside_range(self, clinical_user, procedure_year):
appointment = AppointmentFactory()
request = RequestFactory().get("/test-form")
request.user = clinical_user

max_year = datetime.date.today().year
min_year = max_year - 80
year_outside_range_error_message = (
f"Year should be between {min_year} and {max_year}."
(f"Year must be {max_year} or earlier")
if procedure_year > max_year
else (f"Year must be {min_year} or later")
)
form = BreastAugmentationHistoryForm(
QueryDict(
Expand All @@ -192,7 +183,6 @@ def test_procedure_year_outside_range(self, clinical_user, procedure_year):
doseq=True,
)
),
participant=appointment.participant,
)

assert not form.is_valid()
Expand All @@ -209,14 +199,15 @@ def test_procedure_year_outside_range(self, clinical_user, procedure_year):
],
)
def test_removal_year_outside_range(self, clinical_user, removal_year):
appointment = AppointmentFactory()
request = RequestFactory().get("/test-form")
request.user = clinical_user

max_year = datetime.date.today().year
min_year = max_year - 80
year_outside_range_error_message = (
f"Year should be between {min_year} and {max_year}."
(f"Year must be {max_year} or earlier")
if removal_year > max_year
else (f"Year must be {min_year} or later")
)
form = BreastAugmentationHistoryForm(
QueryDict(
Expand All @@ -234,7 +225,6 @@ def test_removal_year_outside_range(self, clinical_user, removal_year):
doseq=True,
)
),
participant=appointment.participant,
)

assert not form.is_valid()
Expand All @@ -255,7 +245,6 @@ def test_removal_year_outside_range(self, clinical_user, removal_year):
def test_removal_year_before_procedure_year(
self, clinical_user, procedure_year, removal_year
):
appointment = AppointmentFactory()
request = RequestFactory().get("/test-form")
request.user = clinical_user

Expand All @@ -276,7 +265,6 @@ def test_removal_year_before_procedure_year(
doseq=True,
)
),
participant=appointment.participant,
)

assert not form.is_valid()
Expand Down Expand Up @@ -306,7 +294,6 @@ def test_removal_year_when_not_removed(self, clinical_user):
doseq=True,
)
),
participant=appointment.participant,
)

# confirm full_clean removes removal_year but keeps procedure_year
Expand Down Expand Up @@ -425,7 +412,6 @@ def test_success(self, clinical_user, data):

form = BreastAugmentationHistoryForm(
QueryDict(urlencode(data, doseq=True)),
participant=appointment.participant,
)

assert form.is_valid()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from functools import cached_property

from django.contrib import messages
from django.urls import reverse
from django.views.generic import FormView
Expand Down Expand Up @@ -55,12 +53,3 @@ def get_context_data(self, **kwargs):
)

return context

@cached_property
def participant(self):
return self.appointment.participant

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["participant"] = self.participant
return kwargs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_adding_breast_augmentation(self):
self.then_i_see_the_add_breast_augmentation_form()

self.when_i_click_save_without_entering_details()
self.then_i_see_validation_errors_for_missing_benign_lump_details()
self.then_i_see_validation_errors_for_missing_breast_augmentation_details()

self.when_i_select_procedures()
self.and_i_enter_the_procedure_year()
Expand Down Expand Up @@ -65,7 +65,7 @@ def then_i_see_the_add_breast_augmentation_form(self):
"Add details of breast implants or augmentation"
)

def then_i_see_validation_errors_for_missing_benign_lump_details(self):
def then_i_see_validation_errors_for_missing_breast_augmentation_details(self):
self.expect_validation_error(
error_text="Select procedures for the right breast",
fieldset_legend="Right breast",
Expand Down Expand Up @@ -97,12 +97,11 @@ def and_i_enter_additional_details(self):
"additional details for test of breast augmentation history"
)

def when_i_click_save_without_entering_details(self):
self.and_i_click_save_augmentation()

def and_i_click_save_augmentation(self):
self.page.get_by_text("Save").click()

when_i_click_save_without_entering_details = and_i_click_save_augmentation

def then_i_am_back_on_the_medical_information_page(self):
self.expect_url("mammograms:record_medical_information", pk=self.appointment.pk)

Expand Down