Skip to content

Commit f350711

Browse files
committed
WIP
1 parent 70a5065 commit f350711

8 files changed

Lines changed: 67 additions & 15 deletions

File tree

lung_cancer_screening/core/tests/acceptance/test_questionnaire_validation_errors.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,22 @@ def test_height_validation_errors(self):
8383
expect(page.locator(".nhsuk-error-message")).to_contain_text(
8484
"Inches must be in whole numbers"
8585
)
86+
87+
def test_weight_validation_errors(self):
88+
participant_id = '123'
89+
90+
page = self.browser.new_page()
91+
page.goto(f"{self.live_server_url}/start")
92+
fill_in_and_submit_participant_id(page, participant_id)
93+
page.goto(f"{self.live_server_url}/weight")
94+
95+
page.click("text=Continue")
96+
expect(page.locator(".nhsuk-error-message")).to_contain_text(
97+
"Enter your weight."
98+
)
99+
# Test weight below minimum
100+
page.get_by_label("Kilograms").fill('25.3')
101+
page.click('text=Continue')
102+
expect(page.locator(".nhsuk-error-message")).to_contain_text(
103+
"Weight must be between 25.4kg and 317.5kg"
104+
)

lung_cancer_screening/questions/forms/metric_weight_form.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def __init__(self, *args, **kwargs):
1313
self.fields["weight_metric"] = DecimalField(
1414
label="Kilograms",
1515
classes="nhsuk-input--width-4",
16+
required=True,
1617
error_messages={
1718
'required': 'Enter your weight.',
1819
}

lung_cancer_screening/questions/jinja2/weight.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
{% block page_content %}
1616
<div class="nhsuk-grid-row">
1717
<div class="nhsuk-grid-column-two-thirds">
18-
<form action="weight" method="POST" novalidate>
18+
<form action="{{request.path}}" method="POST" novalidate>
1919
{{ csrf_input }}
2020
<h1 class="nhsuk-heading-l">Enter your weight</h1>
2121

lung_cancer_screening/questions/models/response_set.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ class ResponseSet(BaseModel):
3939
MAX_HEIGHT_IMPERIAL, message="Height must be between 4 feet 7 inches and 8 feet"),
4040
])
4141

42-
weight_metric = models.PositiveIntegerField(null=True, blank=True
43-
# validators=[
44-
# MinValueValidator(MIN_HEIGHT_METRIC, message="Height must be between 139.7cm and 243.8 cm"),
45-
# MaxValueValidator(MAX_HEIGHT_METRIC, message="Height must be between 139.7cm and 243.8 cm"),
46-
# ]
47-
)
42+
MIN_WEIGHT_METRIC = 254
43+
44+
weight_metric = models.PositiveIntegerField(null=True, blank=True, validators=[
45+
MinValueValidator(MIN_WEIGHT_METRIC, message="Weight must be between 25.4kg and 317.5kg"),
46+
# MaxValueValidator(MAX_WEIGHT_METRIC, message="Height must be between 139.7cm and 243.8 cm"),
47+
])
4848

4949
submitted_at = models.DateTimeField(null=True, blank=True)
5050

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_is_valid_with_valid_input(self):
1919
)
2020
self.assertTrue(form.is_valid())
2121

22-
def test_is_valid_with_invalid_input(self):
22+
def test_is_not_valid_with_invalid_input(self):
2323
weight = "a"
2424
form = MetricWeightForm(
2525
participant=self.participant,
@@ -28,4 +28,16 @@ def test_is_valid_with_invalid_input(self):
2828
"weight_metric": weight
2929
}
3030
)
31-
self.assertFalse(form.is_valid())
31+
self.assertFalse(form.is_valid())
32+
33+
def test_is_not_valid_without_any_weight_value_set(self):
34+
weight = "a"
35+
form = MetricWeightForm(
36+
participant=self.participant,
37+
instance=self.response_set,
38+
data={
39+
"weight_metric": None
40+
}
41+
)
42+
self.assertFalse(form.is_valid())
43+
self.assertEqual(form.errors, {"weight_metric": ["Enter your weight."]})

lung_cancer_screening/questions/tests/unit/models/test_response_set.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,14 @@ def test_formatted_height_returns_imperial_height_if_set(self):
178178
self.response_set.formatted_height,
179179
"5 feet 8 inches"
180180
)
181+
182+
def test_is_invalid_if_weight_metric_is_below_lower_bound(self):
183+
self.response_set.weight_metric = ResponseSet.MIN_WEIGHT_METRIC - 1
184+
185+
with self.assertRaises(ValidationError) as context:
186+
self.response_set.full_clean()
187+
188+
self.assertIn(
189+
"Weight must be between 25.4kg and 317.5kg",
190+
context.exception.messages
191+
)

lung_cancer_screening/questions/tests/unit/views/test_weight.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ def test_post_valid_weight_added_to_response_set(self):
8585
self.assertEqual(response_set.weight_metric, self.valid_weight)
8686

8787

88-
# def test_post_redirects_if_the_weight_is_invalid(self):
89-
# response = self.client.post(
90-
# reverse("questions:weight"),
91-
# {"weight": "not a valid weight"}
92-
# )
88+
def test_post_responds_with_422_if_the_resource_is_invalid(self):
89+
response = self.client.post(
90+
reverse("questions:weight"),
91+
{"weight": "not a valid weight"}
92+
)
9393

94-
# self.assertEqual(response.status_code, 422)
94+
self.assertEqual(response.status_code, 422)

lung_cancer_screening/questions/views/weight.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ def weight(request):
1515
if form.is_valid():
1616
form.save()
1717
return redirect("questions:responses")
18+
else:
19+
return render(
20+
request,
21+
"weight.jinja",
22+
{
23+
"form": form
24+
},
25+
status=422
26+
)
1827
return render(
1928
request,
2029
"weight.jinja",

0 commit comments

Comments
 (0)