Skip to content

Commit 1a61370

Browse files
committed
Add validation to prevent selecting 'None' with other respiratory conditions
1 parent 5509fc9 commit 1a61370

File tree

6 files changed

+63
-21
lines changed

6 files changed

+63
-21
lines changed

lung_cancer_screening/core/tests/acceptance/test_questionnaire.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_full_questionnaire_user_journey(self):
9797

9898
expect(page).to_have_url(f"{self.live_server_url}/respiratory-conditions")
9999
expect_back_link_to_have_url(page, "/education")
100-
fill_in_and_submit_respiratory_conditions(page, "None of the above")
100+
fill_in_and_submit_respiratory_conditions(page, "No, I have not had any of these respiratory conditions")
101101

102102
expect(page).to_have_url(f"{self.live_server_url}/asbestos-exposure")
103103
expect_back_link_to_have_url(page, "/respiratory-conditions")
@@ -123,6 +123,7 @@ def test_full_questionnaire_user_journey(self):
123123
expect(responses).to_contain_text("Which of these best describes you? Male")
124124
expect(responses).to_contain_text("What is your ethnic background? White")
125125
expect(responses).to_contain_text("Have you ever worked in a job where you might have been exposed to asbestos? No")
126+
expect(responses).to_contain_text("Have you ever been diagnosed with any of the following respiratory conditions?" ['N'])
126127

127128
page.click("text=Submit")
128129

lung_cancer_screening/core/tests/acceptance/test_questionnaire_validation_errors.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,37 @@ def test_ethnicity_validation_errors(self):
115115
expect(page.locator(".nhsuk-error-message")).to_contain_text(
116116
"Select your ethnic background."
117117
)
118+
119+
def test_respiratory_conditions_validation_errors(self):
120+
participant_id = '123'
121+
122+
page = self.browser.new_page()
123+
page.goto(f"{self.live_server_url}/start")
124+
fill_in_and_submit_participant_id(page, participant_id)
125+
page.goto(f"{self.live_server_url}/respiratory-conditions")
126+
127+
page.click("text=Continue")
128+
expect(page.locator(".nhsuk-error-message")).to_contain_text(
129+
"Select if you have had any respiratory conditions"
130+
)
131+
132+
# Select one respiratory condition
133+
page.get_by_label("Bronchitis").click()
134+
135+
# Select None option
136+
page.get_by_label("No, I have not had any of these respiratory conditions").click()
137+
expect(page.locator(".nhsuk-error-message")).to_contain_text(
138+
"Select if you have had any respiratory conditions"
139+
)
140+
141+
# Continue
142+
page.click("text=Continue")
143+
144+
# Assert error is shown
145+
expect(page.locator(".nhsuk-error-message")).to_contain_text(
146+
"Select if you have had any respiratory conditions, or select 'No, I have not had any of these respiratory conditions'"
147+
)
148+
149+
expect(page).to_have_url(f"{self.live_server_url}/respiratory-conditions")
150+
151+

lung_cancer_screening/questions/forms/respiratory_conditions_form.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def __init__(self, *args, **kwargs):
1818
label_classes="nhsuk-fieldset__legend--l",
1919
hint="Select all that apply",
2020
error_messages={
21-
'required': 'Select if you have had any respiratory conditions'
21+
'required': 'Select if you have had any respiratory conditions',
22+
'singleton_option': 'Select if you have had any respiratory conditions, or select \'No, I have not had any of these respiratory conditions\''
2223
}
2324
)
2425

lung_cancer_screening/questions/jinja2/responses.jinja

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424
<li>Which of these best describes you? {{ response_set.get_gender_display() }}</li>
2525
<li>What is your ethnic background? {{ response_set.get_ethnicity_display() }}</li>
2626
<li>Have you ever worked in a job where you might have been exposed to asbestos? {{ "Yes" if response_set.asbestos_exposure else "No" }}</li>
27+
<li>Have you ever been diagnosed with any of the following respiratory conditions? {{ response_set.respiratory_conditions }}</li>
2728
</ul>
28-
2929
<form action="{{ request.path }}" method="post">
3030
{{ csrf_input }}
31-
3231
{{ button({
3332
"text": "Submit"
3433
}) }}

lung_cancer_screening/questions/models/response_set.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ class RespiratoryConditionValues(models.TextChoices):
4040
BRONCHITIS = "B", "Chronic bronchitis"
4141
TUBERCULOSIS = "T", "Tuberculosis (TB)"
4242
COPD = "C", "Chronic obstructive pulmonary disease (COPD)"
43-
NONE = "N", "None of the above"
43+
NONE = "N", "No, I have not had any of these respiratory conditions"
44+
45+
def validate_singleton_option(value):
46+
if value and "N" in value and len(value) > 1:
47+
raise ValidationError(
48+
"Cannot have singleton value and other values selected",
49+
code="singleton_option",
50+
)
4451

4552
class ResponseSet(BaseModel):
4653
participant = models.ForeignKey(Participant, on_delete=models.CASCADE)
@@ -104,10 +111,12 @@ class ResponseSet(BaseModel):
104111
blank=True
105112
)
106113

114+
107115
respiratory_conditions = ArrayField(
108116
models.CharField(max_length=1, choices=RespiratoryConditionValues.choices),
109117
null=True,
110-
blank=True
118+
blank=True,
119+
validators=[validate_singleton_option]
111120
)
112121

113122
asbestos_exposure = models.BooleanField(
@@ -154,4 +163,3 @@ def formatted_weight(self):
154163
value = Decimal(self.weight_imperial)
155164
return f"{value // 14} stone {value % 14} pounds"
156165

157-

lung_cancer_screening/questions/tests/unit/forms/test_respiratory_conditions_form.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,16 @@ def test_is_invalid_when_no_option_is_selected(self):
7474
)
7575

7676
# TODO: Add test for when none of the above is selected and other options are selected
77-
# def test_is_invalid_with_none_of_the_above_selected_and_other_options_selected(self):
78-
# form = RespiratoryConditionsForm(
79-
# participant=self.participant,
80-
# data={
81-
# "respiratory_conditions": ["N", "P", "E", "C"]
82-
# }
83-
# )
84-
# self.assertFalse(form.is_valid())
85-
# self.assertEqual(
86-
# form.errors["respiratory_conditions"],
87-
# ["Select if you have had any respiratory conditions"]
88-
# )
89-
90-
77+
def test_is_invalid_with_none_of_the_above_selected_and_other_options_selected(self):
78+
form = RespiratoryConditionsForm(
79+
participant=self.participant,
80+
data={
81+
"respiratory_conditions": ["N", "P", "E", "C"]
82+
}
83+
)
84+
self.assertFalse(form.is_valid())
85+
print(form.errors.as_json())
86+
self.assertEqual(
87+
form.errors["respiratory_conditions"][0],
88+
"Select if you have had any respiratory conditions, or select 'No, I have not had any of these respiratory conditions'"
89+
)

0 commit comments

Comments
 (0)