77from ...factories .periods_when_you_stopped_smoking_response_factory import PeriodsWhenYouStoppedSmokingResponseFactory
88from ...factories .date_of_birth_response_factory import DateOfBirthResponseFactory
99from ...factories .age_when_started_smoking_response_factory import AgeWhenStartedSmokingResponseFactory
10+ from ...factories .have_you_ever_smoked_response_factory import HaveYouEverSmokedResponseFactory
11+
1012from ....models .periods_when_you_stopped_smoking_response import PeriodsWhenYouStoppedSmokingResponse
1113
1214
@@ -135,12 +137,17 @@ def test_is_invalid_if_they_havent_answered_age_started_smoking(self):
135137 )
136138
137139
138- def test_is_invalid_if_duration_years_is_longer_than_time_they_have_smoked (self ):
140+ def test_is_invalid_if_duration_years_is_longer_than_time_they_have_smoked_and_they_are_a_current_smoker (self ):
139141 self .date_of_birth_response .value = datetime .today () - relativedelta (years = 55 )
140142 self .date_of_birth_response .save ()
141143 self .age_when_started_smoking_response .value = 18
142144 self .age_when_started_smoking_response .save ()
143145
146+ HaveYouEverSmokedResponseFactory .create (
147+ response_set = self .response_set ,
148+ current_smoker = True ,
149+ )
150+
144151 response = PeriodsWhenYouStoppedSmokingResponseFactory .build (
145152 response_set = self .response_set ,
146153 value = True ,
@@ -153,3 +160,28 @@ def test_is_invalid_if_duration_years_is_longer_than_time_they_have_smoked(self)
153160 context .exception .messages [0 ],
154161 "The number of years you stopped smoking must be fewer than the total number of years you have been smoking" ,
155162 )
163+
164+
165+ def test_is_invalid_if_duration_years_is_longer_than_time_they_have_smoked_and_they_are_a_former_smoker (self ):
166+ self .date_of_birth_response .value = datetime .today () - relativedelta (years = 55 )
167+ self .date_of_birth_response .save ()
168+ self .age_when_started_smoking_response .value = 18
169+ self .age_when_started_smoking_response .save ()
170+
171+ HaveYouEverSmokedResponseFactory .create (
172+ response_set = self .response_set ,
173+ former_smoker = True ,
174+ )
175+
176+ response = PeriodsWhenYouStoppedSmokingResponseFactory .build (
177+ response_set = self .response_set ,
178+ value = True ,
179+ duration_years = self .date_of_birth_response .age_in_years () - self .age_when_started_smoking_response .value + 1
180+ )
181+ with self .assertRaises (ValidationError ) as context :
182+ response .full_clean ()
183+
184+ self .assertEqual (
185+ context .exception .messages [0 ],
186+ "The number of years you stopped or quit smoking must be fewer than the total number of years you smoked" ,
187+ )
0 commit comments