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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, *args, **kwargs):

self.fields["duration_years"] = IntegerField(
label=self.duration_years_label(),
label_classes="nhsuk-fieldset__legend--s",
label_classes="nhsuk-label--s",
classes="nhsuk-input--width-4",
hint=self.duration_years_hint(),
required=False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ def _validate_duration_years_is_less_than_time_they_have_smoked(self):
return None

if self.duration_years > self.response_set.age_when_started_smoking_response.years_smoked_including_stopped():
raise ValidationError(
{
"duration_years": "The number of years you stopped smoking must be fewer than the total number of years you have been smoking"
}
)
if self.response_set.current_smoker():
message = "The number of years you stopped smoking must be fewer than the total number of years you have been smoking"
else:
message = "The number of years you stopped or quit smoking must be fewer than the total number of years you smoked"

raise ValidationError({
"duration_years": message
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from ...factories.periods_when_you_stopped_smoking_response_factory import PeriodsWhenYouStoppedSmokingResponseFactory
from ...factories.date_of_birth_response_factory import DateOfBirthResponseFactory
from ...factories.age_when_started_smoking_response_factory import AgeWhenStartedSmokingResponseFactory
from ...factories.have_you_ever_smoked_response_factory import HaveYouEverSmokedResponseFactory

from ....models.periods_when_you_stopped_smoking_response import PeriodsWhenYouStoppedSmokingResponse


Expand Down Expand Up @@ -135,12 +137,17 @@ def test_is_invalid_if_they_havent_answered_age_started_smoking(self):
)


def test_is_invalid_if_duration_years_is_longer_than_time_they_have_smoked(self):
def test_is_invalid_if_duration_years_is_longer_than_time_they_have_smoked_and_they_are_a_current_smoker(self):
self.date_of_birth_response.value = datetime.today() - relativedelta(years=55)
self.date_of_birth_response.save()
self.age_when_started_smoking_response.value = 18
self.age_when_started_smoking_response.save()

HaveYouEverSmokedResponseFactory.create(
response_set=self.response_set,
current_smoker=True,
)

response = PeriodsWhenYouStoppedSmokingResponseFactory.build(
response_set=self.response_set,
value=True,
Expand All @@ -153,3 +160,28 @@ def test_is_invalid_if_duration_years_is_longer_than_time_they_have_smoked(self)
context.exception.messages[0],
"The number of years you stopped smoking must be fewer than the total number of years you have been smoking",
)

@tag("wip")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wip tag

def test_is_invalid_if_duration_years_is_longer_than_time_they_have_smoked_and_they_are_a_former_smoker(self):
self.date_of_birth_response.value = datetime.today() - relativedelta(years=55)
self.date_of_birth_response.save()
self.age_when_started_smoking_response.value = 18
self.age_when_started_smoking_response.save()

HaveYouEverSmokedResponseFactory.create(
response_set=self.response_set,
former_smoker=True,
)

response = PeriodsWhenYouStoppedSmokingResponseFactory.build(
response_set=self.response_set,
value=True,
duration_years=self.date_of_birth_response.age_in_years() - self.age_when_started_smoking_response.value + 1
)
with self.assertRaises(ValidationError) as context:
response.full_clean()

self.assertEqual(
context.exception.messages[0],
"The number of years you stopped or quit smoking must be fewer than the total number of years you smoked",
)
Loading