Skip to content
Open
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
1 change: 1 addition & 0 deletions features/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def setUpClass(cls):
def before_all(context):
"""Set up before all tests run."""
# Set up live server (behave-django handles test database)
os.system('rm screenshots/*')
LiveServer.setUpClass()
context.live_server_url = LiveServer.live_server_url
context.live_server_class = LiveServer
Expand Down
5 changes: 3 additions & 2 deletions features/questionnaire.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@AgeWhenStartedSmoking
Feature: Questionnaire
Scenario: Cannot change responses once submitted
Given I am logged in
Expand Down Expand Up @@ -59,7 +58,6 @@ Feature: Questionnaire
Then I am on "/age-when-started-smoking"
When I fill in "How old were you when you started smoking?" as "18" and submit


Then I am on "/periods-when-you-stopped-smoking"
When I check "Yes"
And I fill in "Enter the total number of years you stopped smoking for" with "10"
Expand All @@ -70,6 +68,9 @@ Feature: Questionnaire
And I check "Pipe"
And I submit the form

Then I am on "/cigarettes-smoking-current"
When I check "Yes" and submit

Then I am on "/cigarettes-smoked-total-years"
When I fill in "Roughly how many years have you smoked cigarettes?" with "10"
And I submit the form
Expand Down
43 changes: 43 additions & 0 deletions features/smoking_current.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@SmokingCurrent
Feature: Smoking current page
Scenario: The page is accessible
Given I am logged in
And I have answered questions showing I am eligible
When I go to "/cigarettes-smoking-current"
Then there are no accessibility violations

Scenario: Form errors
Given I am logged in
And I have answered questions showing I am eligible
When I go to "/cigarettes-smoking-current"
And I click "Continue"
Then I am on "/cigarettes-smoking-current"
And I see a form error "Select if you currently smoke cigarettes"
And there are no accessibility violations

Scenario: Navigating backwards and forwards
Given I am logged in
And I have answered questions showing I am eligible
And I have answered questions showing I have smoked for "10" years
And I have answered questions showing I have smoked "Cigarettes"
When I go to "/cigarettes-smoking-current"
Then I see a back link to "/age-when-started-smoking"
When I check "Yes" and submit
Then I am on "/cigarettes-smoked-total-years"

Scenario: Checking responses and changing them
Given I am logged in
And I have answered questions showing I am eligible
And I have answered questions showing I have smoked for "10" years
And I have answered questions showing I have smoked "Cigarettes"
When I go to "/cigarettes-smoking-current"
When I check "Yes" and submit
When I go to "/check-your-answers"
Then I see "Yes" as a response to "Do you currently smoke cigarettes?" under "Smoking history"
And I see "/cigarettes-smoking-current?change=True" as a link to change "Do you currently smoke cigarettes?" under "Smoking history"
When I click the link to change "Do you currently smoke cigarettes?" under "Smoking history"
Then I am on "/cigarettes-smoking-current?change=True"
And I see "Yes" selected
When I check "No" and submit
Then I am on "/check-your-answers"
And I see "No" as a response to "Do you currently smoke cigarettes?" under "Smoking history"
4 changes: 2 additions & 2 deletions features/types_tobacco_smoking.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Feature: Types tobacco smoking page
Then I see a back link to "/periods-when-you-stopped-smoking"
When I check "Cigarettes"
And I submit the form
Then I am on "/cigarettes-smoked-total-years"
Then I am on "/cigarettes-smoking-current"

Scenario: Checking responses and changing them
Given I am logged in
Expand All @@ -45,6 +45,6 @@ Feature: Types tobacco smoking page
And I see "Cigars" selected
When I check "Pipe"
And I click "Continue"
Then I am on "/cigarettes-smoked-total-years"
Then I am on "/cigarettes-smoking-current"
When I go to "/check-your-answers"
Then I see "Cigarettes, Pipe, and Cigars" as a response to "Types of tobacco smoked" under "Smoking history"
25 changes: 25 additions & 0 deletions lung_cancer_screening/questions/forms/smoking_current_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django import forms

from ..models import SmokingCurrentResponse

from ...nhsuk_forms.typed_choice_field import TypedChoiceField

class SmokingCurrentForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.fields["value"] = TypedChoiceField(
choices=[(True, 'Yes'), (False, 'No')],
widget=forms.RadioSelect,
label="Do you currently smoke cigarettes?",
label_classes="nhsuk-fieldset__legend--l",
label_is_page_heading=True,
coerce=lambda x: x == 'True',
error_messages={
'required': 'Select if you currently smoke cigarettes',
}
)

class Meta:
model = SmokingCurrentResponse
fields = ['value']
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.2.11 on 2026-02-04 13:59

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('questions', '0050_alter_periodswhenyoustoppedsmokingresponse_duration_years_and_more'),
]

operations = [
migrations.CreateModel(
name='SmokingCurrentResponse',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('value', models.BooleanField()),
('tobacco_smoking_history', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='smoking_current_response', to='questions.tobaccosmokinghistory')),
],
options={
'abstract': False,
},
),
]
1 change: 1 addition & 0 deletions lung_cancer_screening/questions/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
from .relatives_age_when_diagnosed_response import RelativesAgeWhenDiagnosedResponse # noqa: F401
from .respiratory_conditions_response import RespiratoryConditionsResponse # noqa: F401
from .sex_at_birth_response import SexAtBirthResponse # noqa: F401
from .smoking_current_response import SmokingCurrentResponse # noqa: F401
from .tobacco_smoking_history import TobaccoSmokingHistory # noqa: F401
from .weight_response import WeightResponse # noqa: F401
12 changes: 12 additions & 0 deletions lung_cancer_screening/questions/models/smoking_current_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.db import models

from .base import BaseModel
from .tobacco_smoking_history import TobaccoSmokingHistory

class SmokingCurrentResponse(BaseModel):
tobacco_smoking_history = models.OneToOneField(
TobaccoSmokingHistory,
on_delete=models.CASCADE,
related_name='smoking_current_response')

value = models.BooleanField()
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,25 @@ def family_history_responses_items(self):


def smoking_history_types_responses_items(self):
return [*[
self._check_your_answer_item(
results = []
for type_history in self.response_set.tobacco_smoking_history.in_form_order():
results.extend(self.smoking_history_summary_items_for_type(type_history))
return results

def smoking_history_summary_items_for_type(self, type_history):
return [self._check_your_answer_item(
f"Do you currently smoke {type_history.human_type().lower()}?",
("Yes" if type_history.smoking_current_response.value else "No") if hasattr(type_history, 'smoking_current_response') else self.NOT_ANSWERED_TEXT,
"questions:smoking_current",
kwargs = { "tobacco_type": humps.kebabize(type_history.type) }
),
(self._check_your_answer_item(
f"Total number of years you have smoked {type_history.human_type().lower()}",
type_history.smoked_total_years_response.value if hasattr(type_history, 'smoked_total_years_response') else None,
type_history.smoked_total_years_response.value if hasattr(type_history, 'smoked_total_years_response') else self.NOT_ANSWERED_TEXT,
"questions:smoked_total_years",
kwargs = { "tobacco_type": humps.kebabize(type_history.type) },
)
for type_history in self.response_set.tobacco_smoking_history.in_form_order()
]]
kwargs = { "tobacco_type": humps.kebabize(type_history.type) }
))]


def smoking_history_responses_items(self):
return [
Expand All @@ -278,7 +288,7 @@ def smoking_history_responses_items(self):
self.types_tobacco_smoking,
"questions:types_tobacco_smoking",
),
*self.smoking_history_types_responses_items()
*self.smoking_history_types_responses_items(),
]


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import factory

from .tobacco_smoking_history_factory import TobaccoSmokingHistoryFactory

from ...models import SmokingCurrentResponse


class SmokingCurrentResponseFactory(factory.django.DjangoModelFactory):
class Meta:
model = SmokingCurrentResponse

tobacco_smoking_history = factory.SubFactory(TobaccoSmokingHistoryFactory)
value = factory.Faker('boolean')
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from django.test import TestCase, tag

from ....models.tobacco_smoking_history import TobaccoSmokingHistoryTypes
from ...factories.tobacco_smoking_history_factory import TobaccoSmokingHistoryFactory

from ....models.smoking_current_response import SmokingCurrentResponse
from ....forms.smoking_current_form import SmokingCurrentForm


@tag("SmokingCurrent")
class TestSmokingCurrentForm(TestCase):
def setUp(self):
self.smoking_history = TobaccoSmokingHistoryFactory.create(
type=TobaccoSmokingHistoryTypes.CIGARETTES.value
)
self.response = SmokingCurrentResponse.objects.create(
tobacco_smoking_history=self.smoking_history,
value=False
)


def test_is_valid_with_a_valid_value(self):
form = SmokingCurrentForm(
instance=self.response,
data={
"value": False
}
)
self.assertTrue(form.is_valid())
self.assertEqual(
form.cleaned_data["value"],
False
)

def test_is_invalid_with_an_invalid_value(self):
form = SmokingCurrentForm(
instance=self.response,
data={
"value": "invalid"
}
)
self.assertFalse(form.is_valid())
self.assertEqual(
form.errors["value"],
["Select a valid choice. invalid is not one of the available choices."]
)

def test_is_invalid_when_no_option_is_selected(self):
form = SmokingCurrentForm(
instance=self.response,
data={
"value": None
}
)
self.assertFalse(form.is_valid())
self.assertEqual(
form.errors["value"],
["Select if you currently smoke cigarettes"]
)
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ def test_is_invalid_if_duration_years_not_set_and_value_is_true(self):
"Enter the total number of years you stopped smoking for"
)


@tag("wip")
def test_is_invalid_if_duration_years_is_less_than_1(self):
response = PeriodsWhenYouStoppedSmokingResponse(
response_set=self.response_set,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.test import TestCase, tag

from lung_cancer_screening.questions.tests.factories.tobacco_smoking_history_factory import TobaccoSmokingHistoryFactory

from ....models.smoking_current_response import SmokingCurrentResponse

from ...factories.smoking_current_response_factory import SmokingCurrentResponseFactory

@tag("SmokingCurrent")
class TestSmokingCurrentResponse(TestCase):
def setUp(self):
self.tobacco_smoking_history = TobaccoSmokingHistoryFactory()

def test_has_a_valid_factory(self):
model = SmokingCurrentResponseFactory.build(tobacco_smoking_history=self.tobacco_smoking_history)
model.full_clean()


def test_has_response_set_as_foreign_key(self):
response = SmokingCurrentResponse.objects.create(
tobacco_smoking_history=self.tobacco_smoking_history,
value=True
)

self.assertEqual(response.tobacco_smoking_history, self.tobacco_smoking_history)

def test_has_value_as_bool(self):
response = SmokingCurrentResponse.objects.create(
tobacco_smoking_history=self.tobacco_smoking_history,
value=False
)

self.assertIsInstance(response.value, bool)
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ def test_types_tobacco_smoking(self):
presenter = ResponseSetPresenter(self.response_set)
self.assertEqual(presenter.types_tobacco_smoking, "Cigarettes, Cigars, and Shisha")


def test_smoking_history_types_responses_items_sets_the_correct_key(self):
TobaccoSmokingHistoryFactory.create(
response_set=self.response_set,
Expand All @@ -446,7 +445,7 @@ def test_smoking_history_types_responses_items_sets_the_correct_key(self):
presenter = ResponseSetPresenter(self.response_set)

response_items = presenter.smoking_history_types_responses_items()
cigarettes_response_item = response_items[0]
cigarettes_response_item = response_items[1]

self.assertEqual(
cigarettes_response_item.get("key").get("text"),
Expand All @@ -469,7 +468,7 @@ def test_smoking_history_types_responses_items_sets_the_correct_value(self):
presenter = ResponseSetPresenter(self.response_set)

response_items = presenter.smoking_history_types_responses_items()
cigarettes_response_item = response_items[0]
cigarettes_response_item = response_items[1]

self.assertEqual(
cigarettes_response_item.get("value").get("text"),
Expand All @@ -485,7 +484,7 @@ def test_smoking_history_types_responses_items_sets_the_correct_change_link(self
presenter = ResponseSetPresenter(self.response_set)

response_items = presenter.smoking_history_types_responses_items()
cigarettes_response_item = response_items[0]
cigarettes_response_item = response_items[1]

self.assertEqual(
cigarettes_response_item.get("actions").get("items")[0].get("href"),
Expand Down
Loading