diff --git a/lung_cancer_screening/nhsuk_forms/imperial_weight_field.py b/lung_cancer_screening/nhsuk_forms/imperial_weight_field.py index d8f94702..400534bd 100644 --- a/lung_cancer_screening/nhsuk_forms/imperial_weight_field.py +++ b/lung_cancer_screening/nhsuk_forms/imperial_weight_field.py @@ -44,24 +44,31 @@ class ImperialWeightField(forms.MultiValueField): def __init__(self, *args, **kwargs): error_messages = kwargs.get("error_messages", {}) + bounds_stone_error = "Weight must be between 4 stone and 50 stone" + stone_kwargs = { "min_value": 0, "max_value": 50, "error_messages": { - 'invalid': 'Stone must be in whole numbers', - 'min_value': 'Weight must be between 4 stone and 50 stone', - 'max_value': 'Weight must be between 4 stone and 50 stone', **error_messages, + 'invalid': 'Stone must be in whole numbers', + 'min_value': bounds_stone_error, + 'max_value': bounds_stone_error, + 'incomplete': "Stone must be between 4 and 50", }, } + + between_pounds = "Pounds must be between 0 and 13" + pounds_kwargs = { "min_value": 0, "max_value": 13, "error_messages": { - 'invalid': 'Pounds must be in whole numbers', - 'min_value': 'Pounds must be between 0 and 13', - 'max_value': 'Pounds must be between 0 and 13', **error_messages, + 'invalid': 'Pounds must be in whole numbers', + 'min_value': between_pounds, + 'max_value': between_pounds, + 'incomplete': between_pounds, }, } fields = ( diff --git a/lung_cancer_screening/questions/forms/imperial_weight_form.py b/lung_cancer_screening/questions/forms/imperial_weight_form.py index d610d647..14040784 100644 --- a/lung_cancer_screening/questions/forms/imperial_weight_form.py +++ b/lung_cancer_screening/questions/forms/imperial_weight_form.py @@ -15,8 +15,7 @@ def __init__(self, *args, **kwargs): required=True, require_all_fields=False, error_messages={ - 'required': 'Enter your weight', - 'incomplete': 'Enter your weight' + 'required': 'Enter your weight' } ) diff --git a/lung_cancer_screening/questions/tests/unit/forms/test_imperial_weight_form.py b/lung_cancer_screening/questions/tests/unit/forms/test_imperial_weight_form.py index 04b0e2a9..798ef5eb 100644 --- a/lung_cancer_screening/questions/tests/unit/forms/test_imperial_weight_form.py +++ b/lung_cancer_screening/questions/tests/unit/forms/test_imperial_weight_form.py @@ -47,14 +47,11 @@ def test_setting_weight_imperial_clears_weight_metric(self): form.save() self.assertEqual(self.response_set.weight_metric, None) - def test_is_invalid_with_missing_data(self): + def test_is_invalid_with_no_values_set(self): form = ImperialWeightForm( participant=self.participant, instance=self.response_set, - data={ - "weight_imperial_0": "5", - # missing pounds - } + data={} ) self.assertFalse(form.is_valid()) self.assertEqual( @@ -62,6 +59,7 @@ def test_is_invalid_with_missing_data(self): ["Enter your weight"] ) + def test_is_invalid_when_given_a_decimal_stone_value(self): form = ImperialWeightForm( participant=self.participant, @@ -76,3 +74,93 @@ def test_is_invalid_when_given_a_decimal_stone_value(self): form.errors["weight_imperial"], ["Stone must be in whole numbers"] ) + + def test_is_invalid_with_missing_pounds(self): + form = ImperialWeightForm( + participant=self.participant, + instance=self.response_set, + data={ + "weight_imperial_0": "5", + # missing pounds + } + ) + self.assertFalse(form.is_valid()) + self.assertEqual( + form.errors["weight_imperial"], + ["Pounds must be between 0 and 13"] + ) + + def test_is_invalid_with_missing_stone(self): + form = ImperialWeightForm( + participant=self.participant, + instance=self.response_set, + data={ + # missing stone + "weight_imperial_1": "5" + } + ) + self.assertFalse(form.is_valid()) + self.assertEqual( + form.errors["weight_imperial"], + ["Stone must be between 4 and 50"] + ) + + def test_is_invalid_when_pounds_under_0(self): + form = ImperialWeightForm( + participant=self.participant, + instance=self.response_set, + data={ + "weight_imperial_0": "5", + "weight_imperial_1": "-1" + } + ) + self.assertFalse(form.is_valid()) + self.assertEqual( + form.errors["weight_imperial"], + ["Pounds must be between 0 and 13"] + ) + + def test_is_invalid_when_pounds_over_13(self): + form = ImperialWeightForm( + participant=self.participant, + instance=self.response_set, + data={ + "weight_imperial_0": "5", + "weight_imperial_1": "14" + } + ) + self.assertFalse(form.is_valid()) + self.assertEqual( + form.errors["weight_imperial"], + ["Pounds must be between 0 and 13"] + ) + + def test_is_invalid_when_stone_under_4(self): + form = ImperialWeightForm( + participant=self.participant, + instance=self.response_set, + data={ + "weight_imperial_0": "3", + "weight_imperial_1": "10" + } + ) + self.assertFalse(form.is_valid()) + self.assertEqual( + form.errors["weight_imperial"], + ["Weight must be between 4 stone and 50 stone"] + ) + + def test_is_invalid_when_stone_over_50(self): + form = ImperialWeightForm( + participant=self.participant, + instance=self.response_set, + data={ + "weight_imperial_0": "51", + "weight_imperial_1": "0" + } + ) + self.assertFalse(form.is_valid()) + self.assertEqual( + form.errors["weight_imperial"], + ["Weight must be between 4 stone and 50 stone"] + )