Skip to content

Commit 9ba2cd9

Browse files
committed
PPHA-528: test age when started smoking
1 parent 8f2c423 commit 9ba2cd9

19 files changed

Lines changed: 188 additions & 80 deletions
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
@AgeWhenStartedSmoking
2+
Feature: Age when started smoking
3+
Scenario: The page is accessible
4+
Given I am logged in
5+
And I am 60 years old
6+
When I go to "/age-when-started-smoking"
7+
Then there are no accessibility violations
8+
9+
Scenario: Form errors
10+
Given I am logged in
11+
And I am 60 years old
12+
When I go to "/age-when-started-smoking"
13+
And I click "Continue"
14+
Then I am on "/age-when-started-smoking"
15+
And I see a form error "Enter your age when you started smoking"
16+
When I fill in "How old were you when you started smoking?" as "0" and submit
17+
Then I see a form error "The age you started smoking must be between 1 and your current age"
18+
When I fill in "How old were you when you started smoking?" as "70" and submit
19+
Then I see a form error "The age you started smoking must be the same as, or less than your current age"
20+
And there are no accessibility violations
21+
22+
Scenario: Navigating backwards and forwards
23+
Given I am logged in
24+
And I am 60 years old
25+
When I go to "/age-when-started-smoking"
26+
Then I see a back link to "/relatives-age-when-diagnosed"
27+
When I fill in "How old were you when you started smoking?" as "18" and submit
28+
Then I am on "/check-your-answers"
29+
30+
Scenario: Checking responses and changing them
31+
Given I am logged in
32+
And I am 60 years old
33+
When I go to "/age-when-started-smoking"
34+
And I fill in "How old were you when you started smoking?" as "18" and submit
35+
When I go to "/check-your-answers"
36+
Then I see "18" as a response to "Age you started smoking" under "Smoking history"
37+
And I see "/age-when-started-smoking?change=True" as a link to change "Age you started smoking" under "Smoking history"
38+
When I click the link to change "Age you started smoking" under "Smoking History"
39+
Then I am on "/age-when-started-smoking?change=True"
40+
And I see "18" filled in for "How old were you when you started smoking?"
41+
When I fill in "How old were you when you started smoking?" as "22" and submit
42+
Then I am on "/check-your-answers"
43+
And I see "22" as a response to "Age you started smoking" under "Smoking history"

features/family_history_lung_cancer.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ Feature: Family history of lung cancer page
1717
Given I am logged in
1818
When I go to "/family-history-lung-cancer"
1919
Then I see a back link to "/cancer-diagnosis"
20-
When I fill in and submit my asbestos exposure with "No"
21-
Then I am on "/check-your-answers"
20+
When I fill in and submit my family history lung cancer with "No"
21+
Then I am on "/age-when-started-smoking"
2222
When I click "Back"
2323
When I fill in and submit my family history lung cancer with "Yes"
2424
Then I am on "/relatives-age-when-diagnosed"

features/relatives_age_when_diagnosed.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Feature: Relatives age when diagnosed page
1717
And I see a form error "Select if your relatives were younger than 60 when they were diagnosed with lung cancer"
1818
And there are no accessibility violations
1919

20-
@AgeWhenStartedSmoking
2120
Scenario: Navigating backwards and forwards
2221
Given I am logged in
2322
When I go to "/family-history-lung-cancer"

features/steps/form_steps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from behave import when
1+
from behave import when, then
22
from datetime import datetime
33
from dateutil.relativedelta import relativedelta
44

features/steps/participant_steps.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from behave import given
22
from django.utils import timezone
33

4+
from features.steps.form_steps import when_i_fill_in_and_submit_my_date_of_birth_as_x_years_ago
5+
from features.steps.navigation_steps import given_i_go_to
46
from lung_cancer_screening.questions.tests.factories.response_set_factory import ResponseSetFactory
57

68

@@ -15,3 +17,8 @@ def given_i_have_already_submitted_my_responses(context):
1517
def given_i_have_started_the_questionnaire(context):
1618
context.page.goto(f'{context.live_server_url}/start')
1719
context.page.click('text=Start')
20+
21+
@given("I am {years:d} years old")
22+
def given_i_am_years_old(context, years):
23+
given_i_go_to(context, '/date-of-birth')
24+
when_i_fill_in_and_submit_my_date_of_birth_as_x_years_ago(context, years)

lung_cancer_screening/nhsuk_forms/jinja2/input.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
{% if field.errors %}
2929
{% set error_params = {
3030
"errorMessage": {
31-
"text": field.errors | first
31+
"html": field.errors | first
3232
}
3333
} %}
3434
{% set input_params = dict(input_params, **error_params) %}
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from django import forms
2+
from django.urls import reverse_lazy
3+
from django.utils.html import format_html
4+
25
from ...nhsuk_forms.integer_field import IntegerField
36
from ..models.age_when_started_smoking_response import AgeWhenStartedSmokingResponse
47

5-
68
class AgeWhenStartedSmokingForm(forms.ModelForm):
79
def __init__(self, *args, **kwargs):
810
super().__init__(*args, **kwargs)
@@ -15,13 +17,14 @@ def __init__(self, *args, **kwargs):
1517
hint="Give an estimate if you are not sure",
1618
prefix="Age",
1719
error_messages={
18-
'required': 'Enter your age when you started smoking',
19-
'invalid': 'Enter your age when you started smoking',
20-
'zero_entered':'The age you started smoking must be between 1 and your current age',
21-
'old_current_age':'The age you started smoking must be the same as, or younger than your current age'
20+
"required": "Enter your age when you started smoking",
21+
"invalid": "Enter your age when you started smoking",
22+
"zero_entered":"The age you started smoking must be between 1 and your current age",
23+
"age_started_smoking_greater_than_current_age":"The age you started smoking must be the same as, or less than your current age",
24+
"no_date_of_birth" : format_html("<a href=\"{}\">Provide your date of birth</a> before answering this question", reverse_lazy("questions:date_of_birth"))
2225
}
2326
)
2427

2528
class Meta:
2629
model = AgeWhenStartedSmokingResponse
27-
fields = ['value']
30+
fields = ["value"]

lung_cancer_screening/questions/jinja2/responses.jinja

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
}) }}
4040
</section>
4141

42+
<section>
43+
<h2 class="nhsuk-heading-m">Smoking history</h2>
44+
45+
{{ summaryList({
46+
"rows": response_set.smoking_history_responses_items()
47+
}) }}
48+
</section>
49+
4250
<form action="{{ request.path }}" method="post">
4351
{{ csrf_input }}
4452
{{ button({
Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,32 @@
1-
import json
2-
31
from django.db import models
42
from django.forms import ValidationError
5-
from django.utils import timezone
63

74
from .base import BaseModel
85
from .response_set import ResponseSet
96

10-
from django.core.validators import MaxValueValidator, MinValueValidator
11-
12-
13-
14-
# def validate_less_than_age(self, value):
15-
# print(f"self : {self}")
16-
# print(f"value : {self.value}")
17-
# print("what")
18-
# print(f"dob : {self.response_set.date_of_birth}")
19-
# print(hasattr(self.response_set, "date_of_birth"))
20-
# if hasattr(self.response_set, "date_of_birth") :
21-
# if value < self.get_age :
22-
# raise ValidationError(
23-
# "Must be less than your age",
24-
# code="less_than_age",
25-
# )
7+
from django.core.validators import MinValueValidator
268

279
class AgeWhenStartedSmokingResponse(BaseModel):
28-
response_set = models.OneToOneField(ResponseSet, on_delete=models.CASCADE, related_name='age_when_started_smoking_response')
10+
response_set = models.OneToOneField(ResponseSet, on_delete=models.CASCADE, related_name="age_when_started_smoking_response")
2911
value = models.PositiveIntegerField(validators=[
30-
MinValueValidator(1, message="The age you started smoking must be between 1 and your current age"),
31-
# validate_less_than_age
12+
MinValueValidator(1, message="The age you started smoking must be between 1 and your current age")
3213
]
3314
)
3415

35-
# def clean(self):
36-
# super().clean()
37-
# print(f"clean 1:{self.response_set}")
38-
# if hasattr(self.response_set, "date_of_birth_response") :
39-
# print(f"clean 2:{self.response_set.date_of_birth_response.value}")
40-
# today = timezone.now()
41-
# born = self.response_set.date_of_birth_response.value
42-
# age = today.year - born.year - ((today.month, today.day) < (born.month, born.day))
43-
# print(f"value : {self.value}")
44-
# print(f"Age {age}")
45-
# if (self.value and self.value < age):
46-
# raise ValidationError(
47-
# "The age you started smoking must be the same as, or younger than your current age"
48-
# )
49-
#if submitted_response_sets_in_last_year:
50-
# raise ValidationError(
51-
# "Responses have already been submitted for this user"
52-
# )
53-
16+
def clean(self):
17+
super().clean()
18+
if hasattr(self.response_set, "date_of_birth_response") :
19+
if (self.value and self.value > self.response_set.date_of_birth_response.age_in_years()):
20+
raise ValidationError({
21+
"value":ValidationError(
22+
"age started smoking must be less than current age",
23+
code="age_started_smoking_greater_than_current_age")
24+
}
25+
)
26+
else:
27+
raise ValidationError({
28+
"value":ValidationError(
29+
"date of birth not set",
30+
code="no_date_of_birth")
31+
}
32+
)

lung_cancer_screening/questions/models/date_of_birth_response.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ def is_currently_in_age_range(self):
1515
seventy_five_years_ago = date.today() - relativedelta(years=75)
1616

1717
return seventy_five_years_ago < self.value <= fifty_five_years_ago
18+
19+
def age_in_years(self):
20+
today = date.today()
21+
born = self.value
22+
age = int(today.year - born.year - ((today.month, today.day) < (born.month, born.day)))
23+
return age

0 commit comments

Comments
 (0)