Skip to content
Closed
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
6 changes: 3 additions & 3 deletions features/smoked_total_years.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Feature: Smoked total years page
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"
And I have answered questions showing I currently smoke "Cigarettes"
When I go to "/cigarettes-smoked-total-years"
Then there are no accessibility violations

Scenario: Form errors
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"
And I have answered questions showing I currently smoke "Cigarettes"
When I go to "/cigarettes-smoked-total-years"
And I click "Continue"
Then I am on "/cigarettes-smoked-total-years"
Expand All @@ -24,7 +24,7 @@ Feature: Smoked total years page
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"
And I have answered questions showing I currently smoke "Cigarettes"
When I go to "/cigarettes-smoked-total-years"
Then I see a back link to "/cigarettes-smoking-current"
When I fill in "Roughly how many years have you smoked cigarettes?" with "9"
Expand Down
27 changes: 23 additions & 4 deletions lung_cancer_screening/questions/forms/smoked_amount_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ def more_or_fewer_text(self):


def _normal_type_label(self):
return f"Roughly how many {self.type_string()} do you {self._currently_or_previously_text()} smoke in a normal {self.tobacco_smoking_history.frequency_singular()}?"
do_or_did = "do" if self.tobacco_smoking_history.is_current() else "did"
return (
f"Roughly how many {self.type_string()} {do_or_did} you "
f"{self._currently_or_previously_text()} smoke in a normal "
f"{self.tobacco_smoking_history.frequency_singular()}?"
)


def _changed_type_label(self):
Expand All @@ -53,11 +58,15 @@ def _type_label(self):


def _currently_or_previously_text(self):
return "currently" if self.tobacco_smoking_history.smoking_current_response.value else "previously"
return "currently" if self.tobacco_smoking_history.is_current() else "previously"


def _normal_type_required_error_message(self):
return f"Enter how many {self.type_string()} you {self._currently_or_previously_text()} smoke in a normal {self.tobacco_smoking_history.frequency_singular()}"
return (
f"Enter how many {self.type_string()} you "
f"{self._currently_or_previously_text()} {self.smoke_or_smoked_text()} "
f"in a normal {self.tobacco_smoking_history.frequency_singular()}"
)


def _changed_type_required_error_message(self):
Expand All @@ -70,9 +79,19 @@ def _type_required_error_message(self):
else:
return self._normal_type_required_error_message()

def smoke_or_smoked_text(self):
if self.tobacco_smoking_history.is_current():
return "smoke"
else:
return "smoked"


def _type_min_value_error_message(self):
return f"The number of {self.type_string()} you smoke must be at least 1"
return (
f"The number of {self.type_string()} you {self.smoke_or_smoked_text()} "
f"a {self.tobacco_smoking_history.frequency_singular()} "
"must be at least 1"
)

class Meta:
model = SmokedAmountResponse
Expand Down
60 changes: 55 additions & 5 deletions lung_cancer_screening/questions/forms/smoked_total_years_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,66 @@ def __init__(self, tobacco_smoking_history,*args, **kwargs):
required=True,
suffix="years",
error_messages={
"required": "Enter the number of years you have smoked cigarettes",
"invalid": "Years must be in whole numbers"
"required": self.required_error_message(),
"invalid": "Years must be in whole numbers",
"min_value": self.min_value_error_message(),
},
)


def normal_required_error_message(self):
return (
"Enter the number of years you have smoked "
f"{self.tobacco_smoking_history.human_type().lower()}"
)

def changed_required_error_message(self):
return (
"Enter the number of years you smoked "
f"{self.tobacco_smoking_history.to_sentence()}"
)

def required_error_message(self):
if self.tobacco_smoking_history.is_normal():
return self.normal_required_error_message()

return self.changed_required_error_message()


def normal_min_value_error_message(self):
return (
"The number of years you smoked "
f"{self.tobacco_smoking_history.human_type().lower()} "
"must be at least 1"
)


def changed_min_value_error_message(self):
return (
"The number of years you smoked "
f"{self.tobacco_smoking_history.to_sentence()} "
"must be at least 1"
)


def min_value_error_message(self):
if self.tobacco_smoking_history.is_normal():
return self.normal_min_value_error_message()

return self.changed_min_value_error_message()


def normal_label(self):
return f"Roughly how many years have you smoked {self.tobacco_smoking_history.human_type().lower()}?"

def changed_label(self):
return f"Roughly how many years did you smoke {self.tobacco_smoking_history.to_sentence()}?"

def label(self):
if self.tobacco_smoking_history.is_normal():
return f"Roughly how many years have you smoked {self.tobacco_smoking_history.human_type().lower()}?"
else:
return f"Roughly how many years did you smoke {self.tobacco_smoking_history.amount()} {self.tobacco_smoking_history.human_type().lower()} a {self.tobacco_smoking_history.frequency_singular()}?"
return self.normal_label()

return self.changed_label()

class Meta:
model = SmokedTotalYearsResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,33 @@ def _validate_age_when_started_smoking_response_exists(self):
)
})

def normal_max_value_error_message(self):
return (
"The number of years you smoked cigarettes must be fewer "
"than the total number of years you have been smoking"
)

def changed_max_value_error_message(self):
return (
"The number of years you smoked "
f"{self.tobacco_smoking_history.to_sentence()} "
"must be fewer than the total number of years you have been smoking"
)

def max_value_error_message(self):
if self.tobacco_smoking_history.is_normal():
return self.normal_max_value_error_message()

return self.changed_max_value_error_message()

def _validate_value_fewer_than_total_number_of_years_smoked(self):
if not self.value:
return None

if self.value > self.tobacco_smoking_history.response_set.age_when_started_smoking_response.years_smoked_including_stopped():
raise ValidationError({
"value": ValidationError(
"The number of years you smoked cigarettes must be fewer than the total number of years you have been smoking",
code="value_greater_than_total_number_of_years_smoked"
self.max_value_error_message(),
code="max"
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ def duration_years(self):
else:
return None

def to_sentence(self):
return f"{self.amount()} {self.human_type().lower()} a {self.frequency_singular()}"

def is_increased(self):
return self.level == self.Levels.INCREASED

Expand All @@ -139,8 +142,8 @@ def is_normal(self):
return self.level == self.Levels.NORMAL

def is_current(self):
if hasattr(self, "smoking_current_response"):
return self.smoking_current_response.value
else:
if not hasattr(self, "smoking_current_response"):
return None

return self.is_normal() and self.smoking_current_response.value

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def setUp(self):
)
self.smoking_current_response = SmokingCurrentResponseFactory.create(
tobacco_smoking_history=self.smoking_history,
value=False
value=True
)
self.frequency_response = SmokingFrequencyResponseFactory.create(
tobacco_smoking_history=self.smoking_history,
Expand Down Expand Up @@ -55,24 +55,53 @@ def test_is_invalid_with_a_non_numeric_value(self):
self.assertIn("value", form.errors)


def test_min_value_validation_has_the_correct_message(self):
self.smoking_history.type = TobaccoSmokingHistoryTypes.CIGARS.value
self.smoking_history.save()
def test_min_value_validation_with_current_smoking_history(self):
form = SmokedAmountForm(
instance=self.response,
tobacco_smoking_history=self.smoking_history,
data={"value": 0}
)
form.full_clean()
self.assertIn("value", form.errors)
self.assertIn(
"The number of cigarettes you smoke a day must be at least 1",
form.errors["value"],
)


def test_min_value_validation_has_with_previous_smoking_history(self):
self.smoking_current_response.value = False
self.smoking_current_response.save()

form = SmokedAmountForm(
instance=self.response,
tobacco_smoking_history=self.smoking_history,
data={"value": 0}
)

form.full_clean()
self.assertIn("value", form.errors)
self.assertIn(
"The number of cigars you smoke must be at least 1",
"The number of cigarettes you smoked a day must be at least 1",
form.errors["value"],
)

def test_has_a_label_for_the_normal_type_current(self):
form = SmokedAmountForm(
instance=self.response,
data={"value": 20},
tobacco_smoking_history=self.smoking_history
)

self.assertEqual(
form.fields["value"].label,
"Roughly how many cigarettes do you currently smoke in a normal day?"
)

def test_has_a_label_for_the_current_type_previously(self):
self.smoking_current_response.value = False
self.smoking_current_response.save()

def test_has_a_label_for_the_normal_type(self):
form = SmokedAmountForm(
instance=self.response,
data={"value": 20},
Expand All @@ -81,7 +110,7 @@ def test_has_a_label_for_the_normal_type(self):

self.assertEqual(
form.fields["value"].label,
"Roughly how many cigarettes do you previously smoke in a normal day?"
"Roughly how many cigarettes did you previously smoke in a normal day?"
)


Expand Down Expand Up @@ -141,10 +170,28 @@ def test_has_a_required_error_message_for_the_normal_type(self):
form.full_clean()
self.assertIn("value", form.errors)
self.assertIn(
"Enter how many cigarettes you previously smoke in a normal day",
"Enter how many cigarettes you currently smoke in a normal day",
form.errors["value"],
)

def test_has_a_required_error_message_for_the_normal_type_previously(self):
self.smoking_current_response.value = False
self.smoking_current_response.save()

form = SmokedAmountForm(
instance=self.response,
data={"value": None},
tobacco_smoking_history=self.smoking_history
)

form.full_clean()
self.assertIn("value", form.errors)
self.assertIn(
"Enter how many cigarettes you previously smoked in a normal day",
form.errors["value"],
)


def test_has_a_required_error_message_for_the_increased_type(self):
increased_smoking_history = TobaccoSmokingHistoryFactory.create(
type=TobaccoSmokingHistoryTypes.CIGARETTES.value,
Expand Down
Loading