Skip to content

Commit c0d18af

Browse files
Themitchellmiriam-z
authored andcommitted
PPHA-263: Validate when no value is entered for pounds
1 parent e68ea15 commit c0d18af

File tree

3 files changed

+107
-13
lines changed

3 files changed

+107
-13
lines changed

lung_cancer_screening/nhsuk_forms/imperial_weight_field.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,31 @@ class ImperialWeightField(forms.MultiValueField):
4444
def __init__(self, *args, **kwargs):
4545
error_messages = kwargs.get("error_messages", {})
4646

47+
bounds_stone_error = "Weight must be between 4 stone and 50 stone"
48+
4749
stone_kwargs = {
4850
"min_value": 0,
4951
"max_value": 50,
5052
"error_messages": {
51-
'invalid': 'Stone must be in whole numbers',
52-
'min_value': 'Weight must be between 4 stone and 50 stone',
53-
'max_value': 'Weight must be between 4 stone and 50 stone',
5453
**error_messages,
54+
'invalid': 'Stone must be in whole numbers',
55+
'min_value': bounds_stone_error,
56+
'max_value': bounds_stone_error,
57+
'incomplete': "Stone must be between 4 and 50",
5558
},
5659
}
60+
61+
between_pounds = "Pounds must be between 0 and 13"
62+
5763
pounds_kwargs = {
5864
"min_value": 0,
5965
"max_value": 13,
6066
"error_messages": {
61-
'invalid': 'Pounds must be in whole numbers',
62-
'min_value': 'Pounds must be between 0 and 13',
63-
'max_value': 'Pounds must be between 0 and 13',
6467
**error_messages,
68+
'invalid': 'Pounds must be in whole numbers',
69+
'min_value': between_pounds,
70+
'max_value': between_pounds,
71+
'incomplete': between_pounds,
6572
},
6673
}
6774
fields = (

lung_cancer_screening/questions/forms/imperial_weight_form.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ def __init__(self, *args, **kwargs):
1515
required=True,
1616
require_all_fields=False,
1717
error_messages={
18-
'required': 'Enter your weight',
19-
'incomplete': 'Enter your weight'
18+
'required': 'Enter your weight'
2019
}
2120
)
2221

lung_cancer_screening/questions/tests/unit/forms/test_imperial_weight_form.py

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,19 @@ def test_setting_weight_imperial_clears_weight_metric(self):
4747
form.save()
4848
self.assertEqual(self.response_set.weight_metric, None)
4949

50-
def test_is_invalid_with_missing_data(self):
50+
def test_is_invalid_with_no_values_set(self):
5151
form = ImperialWeightForm(
5252
participant=self.participant,
5353
instance=self.response_set,
54-
data={
55-
"weight_imperial_0": "5",
56-
# missing pounds
57-
}
54+
data={}
5855
)
5956
self.assertFalse(form.is_valid())
6057
self.assertEqual(
6158
form.errors["weight_imperial"],
6259
["Enter your weight"]
6360
)
6461

62+
6563
def test_is_invalid_when_given_a_decimal_stone_value(self):
6664
form = ImperialWeightForm(
6765
participant=self.participant,
@@ -76,3 +74,93 @@ def test_is_invalid_when_given_a_decimal_stone_value(self):
7674
form.errors["weight_imperial"],
7775
["Stone must be in whole numbers"]
7876
)
77+
78+
def test_is_invalid_with_missing_pounds(self):
79+
form = ImperialWeightForm(
80+
participant=self.participant,
81+
instance=self.response_set,
82+
data={
83+
"weight_imperial_0": "5",
84+
# missing pounds
85+
}
86+
)
87+
self.assertFalse(form.is_valid())
88+
self.assertEqual(
89+
form.errors["weight_imperial"],
90+
["Pounds must be between 0 and 13"]
91+
)
92+
93+
def test_is_invalid_with_missing_stone(self):
94+
form = ImperialWeightForm(
95+
participant=self.participant,
96+
instance=self.response_set,
97+
data={
98+
# missing stone
99+
"weight_imperial_1": "5"
100+
}
101+
)
102+
self.assertFalse(form.is_valid())
103+
self.assertEqual(
104+
form.errors["weight_imperial"],
105+
["Stone must be between 4 and 50"]
106+
)
107+
108+
def test_is_invalid_when_pounds_under_0(self):
109+
form = ImperialWeightForm(
110+
participant=self.participant,
111+
instance=self.response_set,
112+
data={
113+
"weight_imperial_0": "5",
114+
"weight_imperial_1": "-1"
115+
}
116+
)
117+
self.assertFalse(form.is_valid())
118+
self.assertEqual(
119+
form.errors["weight_imperial"],
120+
["Pounds must be between 0 and 13"]
121+
)
122+
123+
def test_is_invalid_when_pounds_over_13(self):
124+
form = ImperialWeightForm(
125+
participant=self.participant,
126+
instance=self.response_set,
127+
data={
128+
"weight_imperial_0": "5",
129+
"weight_imperial_1": "14"
130+
}
131+
)
132+
self.assertFalse(form.is_valid())
133+
self.assertEqual(
134+
form.errors["weight_imperial"],
135+
["Pounds must be between 0 and 13"]
136+
)
137+
138+
def test_is_invalid_when_stone_under_4(self):
139+
form = ImperialWeightForm(
140+
participant=self.participant,
141+
instance=self.response_set,
142+
data={
143+
"weight_imperial_0": "3",
144+
"weight_imperial_1": "10"
145+
}
146+
)
147+
self.assertFalse(form.is_valid())
148+
self.assertEqual(
149+
form.errors["weight_imperial"],
150+
["Weight must be between 4 stone and 50 stone"]
151+
)
152+
153+
def test_is_invalid_when_stone_over_50(self):
154+
form = ImperialWeightForm(
155+
participant=self.participant,
156+
instance=self.response_set,
157+
data={
158+
"weight_imperial_0": "51",
159+
"weight_imperial_1": "0"
160+
}
161+
)
162+
self.assertFalse(form.is_valid())
163+
self.assertEqual(
164+
form.errors["weight_imperial"],
165+
["Weight must be between 4 stone and 50 stone"]
166+
)

0 commit comments

Comments
 (0)