Skip to content

Commit e7b83c2

Browse files
committed
Update BooleanField and tests
1 parent a17d97c commit e7b83c2

File tree

8 files changed

+49
-21
lines changed

8 files changed

+49
-21
lines changed

lung_cancer_screening/core/tests/acceptance/helpers/user_interaction_helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,11 @@ def fill_in_and_submit_ethnicity(page, ethnicity):
7676
page.get_by_label(ethnicity, exact=True).check()
7777

7878
page.click("text=Continue")
79+
80+
def fill_in_and_submit_asbestos_exposure(page, answer):
81+
expect(page.locator("legend")).to_have_text(
82+
"Have you ever worked in a job where you might have been exposed to asbestos?")
83+
84+
page.get_by_label(answer, exact=True).check()
85+
86+
page.click("text=Continue")

lung_cancer_screening/core/tests/acceptance/test_cannot_change_answers_after_submission.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
fill_in_and_submit_date_of_birth,
1313
fill_in_and_submit_sex_at_birth,
1414
fill_in_and_submit_gender,
15-
fill_in_and_submit_ethnicity
15+
fill_in_and_submit_ethnicity,
16+
fill_in_and_submit_asbestos_exposure
1617
)
1718

1819
class TestQuestionnaire(StaticLiveServerTestCase):
@@ -46,6 +47,7 @@ def test_cannot_change_responses_once_checked_and_submitted(self):
4647
fill_in_and_submit_sex_at_birth(page, "Male")
4748
fill_in_and_submit_gender(page, "Male")
4849
fill_in_and_submit_ethnicity(page, "White")
50+
fill_in_and_submit_asbestos_exposure(page, "No")
4951

5052
page.click("text=Submit")
5153

lung_cancer_screening/core/tests/acceptance/test_questionnaire.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
fill_in_and_submit_weight_imperial,
1515
fill_in_and_submit_sex_at_birth,
1616
fill_in_and_submit_gender,
17-
fill_in_and_submit_ethnicity
17+
fill_in_and_submit_ethnicity,
18+
fill_in_and_submit_asbestos_exposure
1819
)
1920

2021
from .helpers.assertion_helpers import expect_back_link_to_have_url
@@ -90,9 +91,14 @@ def test_full_questionnaire_user_journey(self):
9091

9192
fill_in_and_submit_ethnicity(page, "White")
9293

93-
expect(page).to_have_url(f"{self.live_server_url}/responses")
94+
expect(page).to_have_url(f"{self.live_server_url}/asbestos-exposure")
9495
expect_back_link_to_have_url(page, "/ethnicity")
9596

97+
fill_in_and_submit_asbestos_exposure(page, "No")
98+
99+
expect(page).to_have_url(f"{self.live_server_url}/responses")
100+
expect_back_link_to_have_url(page, "/asbestos-exposure")
101+
96102
responses = page.locator(".responses")
97103
expect(responses).to_contain_text("Have you ever smoked? Yes, I used to smoke regularly")
98104
expect(responses).to_contain_text(
@@ -102,6 +108,7 @@ def test_full_questionnaire_user_journey(self):
102108
expect(responses).to_contain_text("What was your sex at birth? Male")
103109
expect(responses).to_contain_text("Which of these best describes you? Male")
104110
expect(responses).to_contain_text("What is your ethnic background? White")
111+
expect(responses).to_contain_text("Have you ever worked in a job where you might have been exposed to asbestos? No")
105112

106113
page.click("text=Submit")
107114

lung_cancer_screening/questions/forms/asbestos_exposure_form.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django import forms
2-
from ...nhsuk_forms.choice_field import ChoiceField
3-
from ..models.response_set import ResponseSet, AsbestosExposureValues
2+
from ...nhsuk_forms.typed_choice_field import TypedChoiceField
3+
from ..models.response_set import ResponseSet
44

55

66
class AsbestosExposureForm(forms.ModelForm):
@@ -9,11 +9,12 @@ def __init__(self, *args, **kwargs):
99
super().__init__(*args, **kwargs)
1010
self.instance.participant = self.participant
1111

12-
self.fields["asbestos_exposure"] = ChoiceField(
13-
choices=AsbestosExposureValues.choices,
12+
self.fields["asbestos_exposure"] = TypedChoiceField(
13+
choices=[(True, 'Yes'), (False, 'No')],
1414
widget=forms.RadioSelect,
1515
label="Have you ever worked in a job where you might have been exposed to asbestos?",
1616
label_classes="nhsuk-fieldset__legend--m",
17+
coerce=lambda x: x == 'True',
1718
error_messages={
1819
'required': 'Select if you have been exposed to asbestos.'
1920
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.2.7 on 2025-11-12 11:00
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('questions', '0016_responseset_asbestos_exposure'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='responseset',
15+
name='asbestos_exposure',
16+
field=models.BooleanField(blank=True, null=True),
17+
),
18+
]

lung_cancer_screening/questions/models/response_set.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ class EthnicityValues(models.TextChoices):
3333
OTHER = "O", "Other ethnic group"
3434
PREFER_NOT_TO_SAY = "N", "I'd prefer not to say"
3535

36-
class AsbestosExposureValues(models.TextChoices):
37-
YES = "Y", "Yes"
38-
NO = "N", "No"
39-
4036
class ResponseSet(BaseModel):
4137
participant = models.ForeignKey(Participant, on_delete=models.CASCADE)
4238

@@ -99,9 +95,7 @@ class ResponseSet(BaseModel):
9995
blank=True
10096
)
10197

102-
asbestos_exposure = models.CharField(
103-
max_length=1,
104-
choices=AsbestosExposureValues.choices,
98+
asbestos_exposure = models.BooleanField(
10599
null=True,
106100
blank=True
107101
)

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from django.test import TestCase
22

3-
from ....models.response_set import AsbestosExposureValues
43
from ....models.participant import Participant
54
from ....forms.asbestos_exposure_form import AsbestosExposureForm
65

@@ -13,26 +12,26 @@ def test_is_valid_with_yes(self):
1312
form = AsbestosExposureForm(
1413
participant=self.participant,
1514
data={
16-
"asbestos_exposure": AsbestosExposureValues.YES
15+
"asbestos_exposure": True
1716
}
1817
)
1918
self.assertTrue(form.is_valid())
2019
self.assertEqual(
2120
form.cleaned_data["asbestos_exposure"],
22-
AsbestosExposureValues.YES.value
21+
True
2322
)
2423

2524
def test_is_valid_with_no(self):
2625
form = AsbestosExposureForm(
2726
participant=self.participant,
2827
data={
29-
"asbestos_exposure": AsbestosExposureValues.NO
28+
"asbestos_exposure": False
3029
}
3130
)
3231
self.assertTrue(form.is_valid())
3332
self.assertEqual(
3433
form.cleaned_data["asbestos_exposure"],
35-
AsbestosExposureValues.NO.value
34+
False
3635
)
3736

3837
def test_is_invalid_with_an_invalid_value(self):

lung_cancer_screening/questions/tests/unit/views/test_asbestos_exposure.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
from django.urls import reverse
33

44
from lung_cancer_screening.questions.models.participant import Participant
5-
from lung_cancer_screening.questions.models.response_set import AsbestosExposureValues
65

76

87
class TestAsbestosExposure(TestCase):
98
def setUp(self):
109
self.participant = Participant.objects.create(unique_id="12345")
1110
self.participant.responseset_set.create()
12-
self.valid_params = {"asbestos_exposure": AsbestosExposureValues.YES}
11+
self.valid_params = {"asbestos_exposure": True}
1312

1413
session = self.client.session
1514
session['participant_id'] = self.participant.unique_id

0 commit comments

Comments
 (0)