Skip to content

Commit 2acc434

Browse files
committed
PPHA-525: Ensure user has to change periods stopped smoking if changeing age started smoking
1 parent 8c1ca7b commit 2acc434

8 files changed

Lines changed: 98 additions & 8 deletions

features/age_when_started_smoking.feature

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,4 @@ Feature: Age when started smoking
4040
Then I am on "/age-when-started-smoking?change=True"
4141
And I see "18" filled in for "How old were you when you started smoking?"
4242
When I fill in "How old were you when you started smoking?" as "22" and submit
43-
Then I am on "/check-your-answers"
44-
And I see "22" as a response to "Age you started smoking" under "Smoking history"
43+
Then I am on "/periods-when-you-stopped-smoking?change=True"

features/steps/preflight_steps.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
from behave import given
2-
from datetime import datetime
3-
from dateutil.relativedelta import relativedelta
2+
3+
from features.steps.form_steps import (
4+
when_i_fill_in_and_submit_my_date_of_birth_as_x_years_ago,
5+
when_i_submit_the_form,
6+
when_i_check_label,
7+
when_i_fill_in_label_with_value
8+
)
9+
410
from lung_cancer_screening.questions.tests.factories.have_you_ever_smoked_response_factory import (
511
ResponseSetFactory,
612
)
@@ -23,6 +29,7 @@ def get_or_create_response_set(context):
2329
)
2430
)
2531

32+
2633
@given('I have answered have you ever smoked with an eligible response')
2734
def given_i_have_answered_have_your_ever_smoked_with_an_eligible_response(context):
2835
response_set = get_or_create_response_set(context)
@@ -57,3 +64,16 @@ def given_i_have_answered_questions_showing_i_have_smoked_for_years_years(contex
5764
response_set=response_set,
5865
value=response_set.date_of_birth_response.age_in_years() - int(years),
5966
)
67+
68+
@given('I have answered questions showing I am aged "{years}" years old')
69+
def given_i_have_answered_questions_showing_i_am_aged_60_years_old(context, years):
70+
context.page.goto(f"{context.live_server_url}/date-of-birth")
71+
when_i_fill_in_and_submit_my_date_of_birth_as_x_years_ago(context, years)
72+
73+
74+
@given('I have answered questions showing I stopped smoking for "{years}" years')
75+
def given_i_have_answered_questions_showing_i_stopped_smoking_for_years_years(context, years):
76+
context.page.goto(f"{context.live_server_url}/periods-when-you-stopped-smoking")
77+
when_i_check_label(context, "Yes")
78+
when_i_fill_in_label_with_value(context, "Enter the total number of years you stopped smoking for", years)
79+
when_i_submit_the_form(context)

lung_cancer_screening/questions/forms/age_when_started_smoking_form.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,18 @@ def __init__(self, *args, **kwargs):
2828
class Meta:
2929
model = AgeWhenStartedSmokingResponse
3030
fields = ["value"]
31+
32+
33+
def save(self, commit=True):
34+
instance = super(AgeWhenStartedSmokingForm, self).save(commit=False)
35+
36+
self._cleanup_periods_stopped_smoking_if_value_changed()
37+
38+
if commit:
39+
instance.save()
40+
41+
return instance
42+
43+
def _cleanup_periods_stopped_smoking_if_value_changed(self):
44+
if 'value' in self.changed_data and hasattr(self.instance.response_set, 'periods_when_you_stopped_smoking_response'):
45+
self.instance.response_set.periods_when_you_stopped_smoking_response.delete()

lung_cancer_screening/questions/tests/factories/periods_when_you_stopped_smoking_response_factory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ class Meta:
1010

1111
response_set = factory.SubFactory(ResponseSetFactory)
1212
value = False
13+
duration_years = None

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from ...factories.response_set_factory import ResponseSetFactory
99
from ...factories.date_of_birth_response_factory import DateOfBirthResponseFactory
1010
from ....forms.age_when_started_smoking_form import AgeWhenStartedSmokingForm
11+
from ...factories.periods_when_you_stopped_smoking_response_factory import PeriodsWhenYouStoppedSmokingResponseFactory
12+
from ....models.periods_when_you_stopped_smoking_response import PeriodsWhenYouStoppedSmokingResponse
1113

1214

1315
@tag("AgeWhenStartedSmoking")
@@ -106,3 +108,43 @@ def test_is_invalid_when_age_entered_greater_than_current_age(self):
106108
form.errors["value"],
107109
["The age you started smoking must be the same as, or less than your current age"]
108110
)
111+
112+
113+
def test_deletes_periods_when_stopped_smoking_response_if_age_started_smoking_is_changed(self):
114+
self.response.value = 17
115+
self.response.save()
116+
117+
PeriodsWhenYouStoppedSmokingResponseFactory.create(
118+
response_set=self.response_set,
119+
value=True,
120+
duration_years=self.response.years_smoked_including_stopped() - 1
121+
)
122+
form = AgeWhenStartedSmokingForm(
123+
instance=self.response,
124+
data={
125+
"value": 18
126+
}
127+
)
128+
form.save()
129+
130+
self.assertFalse(PeriodsWhenYouStoppedSmokingResponse.objects.filter(response_set=self.response_set).exists())
131+
132+
133+
def test_does_not_delete_periods_when_stopped_smoking_response_if_age_started_smoking_is_not_changed(self):
134+
self.response.value = 18
135+
self.response.save()
136+
137+
PeriodsWhenYouStoppedSmokingResponseFactory.create(
138+
response_set=self.response_set,
139+
value=True,
140+
duration_years=self.response.years_smoked_including_stopped() - 1
141+
)
142+
form = AgeWhenStartedSmokingForm(
143+
instance=self.response,
144+
data={
145+
"value": 18
146+
}
147+
)
148+
form.save()
149+
150+
self.assertTrue(PeriodsWhenYouStoppedSmokingResponse.objects.filter(response_set=self.response_set).exists())

lung_cancer_screening/questions/tests/unit/models/test_periods_when_you_stopped_smoking_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def setUp(self):
1818

1919
# Following responses required by validator
2020
self.date_of_birth_response = DateOfBirthResponseFactory.create(response_set=self.response_set)
21-
self.age_when_started_smoking_response = q.create(response_set=self.response_set)
21+
self.age_when_started_smoking_response = AgeWhenStartedSmokingResponseFactory.create(response_set=self.response_set)
2222

2323

2424
def test_has_a_valid_factory(self):

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_redirects_to_periods_when_you_stopped_smoking(self):
115115

116116
self.assertRedirects(response, reverse("questions:periods_when_you_stopped_smoking"))
117117

118-
def test_redirects_to_responses_if_change_query_param_is_true(self):
118+
def test_redirects_to_periods_when_you_stopped_smoking_if_change_query_param_is_true(self):
119119
ResponseSetFactory.create(user=self.user, eligible=True)
120120

121121
response = self.client.post(
@@ -126,7 +126,11 @@ def test_redirects_to_responses_if_change_query_param_is_true(self):
126126
}
127127
)
128128

129-
self.assertRedirects(response, reverse("questions:responses"))
129+
self.assertRedirects(
130+
response,
131+
reverse("questions:periods_when_you_stopped_smoking", query={"change": "True"})
132+
)
133+
130134

131135
def test_responds_with_422_if_the_response_fails_to_create(self):
132136
ResponseSetFactory.create(user=self.user, eligible=True)

lung_cancer_screening/questions/views/age_when_started_smoking.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django.urls import reverse_lazy
1+
from django.urls import reverse, reverse_lazy
22
from django.contrib.auth.mixins import LoginRequiredMixin
33

44
from .mixins.ensure_response_set import EnsureResponseSet
@@ -13,3 +13,12 @@ class AgeWhenStartedSmokingView(LoginRequiredMixin, EnsureResponseSet, EnsureEli
1313
model = AgeWhenStartedSmokingResponse
1414
success_url = reverse_lazy("questions:periods_when_you_stopped_smoking")
1515
back_link_url = reverse_lazy("questions:relatives_age_when_diagnosed")
16+
17+
def get_success_url(self):
18+
if self.should_redirect_to_responses(self.request):
19+
return reverse(
20+
"questions:periods_when_you_stopped_smoking",
21+
query={"change": "True"}
22+
)
23+
else:
24+
return super().get_success_url()

0 commit comments

Comments
 (0)