Skip to content

Commit a062416

Browse files
committed
Implement weight collection feature with comprehensive tests
1 parent 5b05aa1 commit a062416

5 files changed

Lines changed: 84 additions & 7 deletions

File tree

lung_cancer_screening/core/tests/acceptance/helpers/user_interaction_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def fill_in_and_submit_height_imperial(page, feet, inches):
3838

3939
page.click("text=Continue")
4040

41-
def fill_in_and_submit_weight(page, kilograms):
41+
def fill_in_and_submit_weight_metric(page, kilograms):
4242
expect(page.locator("h1")).to_have_text("Enter your weight")
4343

4444
page.get_by_label("Kilograms").fill(str(kilograms))

lung_cancer_screening/core/tests/acceptance/test_questionnaire.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
fill_in_and_submit_participant_id,
1111
fill_in_and_submit_smoking_eligibility,
1212
fill_in_and_submit_date_of_birth,
13-
fill_in_and_submit_weight
13+
fill_in_and_submit_weight_metric
1414
)
1515

1616
from .helpers.assertion_helpers import expect_back_link_to_have_url
@@ -37,6 +37,9 @@ def test_full_questionnaire_user_journey(self):
3737
height = "170"
3838
feet = 5
3939
inches = 7
40+
weight_metric = 70
41+
weight_stone = 5
42+
weight_pound = 10
4043

4144
page = self.browser.new_page()
4245
page.goto(f"{self.live_server_url}/start")
@@ -69,15 +72,19 @@ def test_full_questionnaire_user_journey(self):
6972

7073
expect(page).to_have_url(f"{self.live_server_url}/weight")
7174

72-
fill_in_and_submit_weight(page, "70")
75+
fill_in_and_submit_weight_metric(page, weight_metric)
7376

7477
expect(page).to_have_url(f"{self.live_server_url}/responses")
75-
78+
page.click("text=Back")
79+
page.click("text=Switch to imperial")
80+
fill_in_and_submit_weight_imperial(page, weight_stone, weight_pound)
7681
responses = page.locator(".responses")
7782
expect(responses).to_contain_text("Have you ever smoked? Yes, I used to smoke regularly")
7883
expect(responses).to_contain_text(
7984
age.strftime("What is your date of birth? %Y-%m-%d"))
8085
expect(responses).to_contain_text(f"What is your height? {feet} feet {inches} inches")
86+
# expect(responses).to_contain_text(f"What is your weight? {weight_metric}kg")
87+
expect(responses).to_contain_text(f"What is your weight? {weight_stone} stone {weight_pound} pound")
8188

8289
page.click("text=Submit")
8390

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,19 @@ def test_is_not_valid_with_invalid_input(self):
3030
)
3131
self.assertFalse(form.is_valid())
3232

33+
# UAT: Error message when nothing is entered
34+
def test_error_message_when_weight_is_empty(self):
35+
form = MetricWeightForm(
36+
participant=self.participant,
37+
instance=self.response_set,
38+
data={
39+
"weight_metric": ""
40+
}
41+
)
42+
self.assertFalse(form.is_valid())
43+
self.assertEqual(form.errors["weight_metric"], ["Enter your weight."])
44+
3345
def test_is_not_valid_without_any_weight_value_set(self):
34-
weight = "a"
3546
form = MetricWeightForm(
3647
participant=self.participant,
3748
instance=self.response_set,
@@ -41,3 +52,51 @@ def test_is_not_valid_without_any_weight_value_set(self):
4152
)
4253
self.assertFalse(form.is_valid())
4354
self.assertEqual(form.errors, {"weight_metric": ["Enter your weight."]})
55+
56+
# UAT: Error message for weight below minimum (25.4kg)
57+
def test_error_message_when_weight_below_minimum(self):
58+
form = MetricWeightForm(
59+
participant=self.participant,
60+
instance=self.response_set,
61+
data={
62+
"weight_metric": "20.0"
63+
}
64+
)
65+
self.assertFalse(form.is_valid())
66+
self.assertIn("Weight must be between 25.4kg and 317.5kg",
67+
form.errors["weight_metric"][0])
68+
69+
# UAT: Error message for weight above maximum (317.5kg)
70+
def test_error_message_when_weight_above_maximum(self):
71+
form = MetricWeightForm(
72+
participant=self.participant,
73+
instance=self.response_set,
74+
data={
75+
"weight_metric": "320.0"
76+
}
77+
)
78+
self.assertFalse(form.is_valid())
79+
self.assertIn("Weight must be between 25.4kg and 317.5kg",
80+
form.errors["weight_metric"][0])
81+
82+
# UAT: Edge case - minimum valid weight
83+
def test_accepts_minimum_valid_weight(self):
84+
form = MetricWeightForm(
85+
participant=self.participant,
86+
instance=self.response_set,
87+
data={
88+
"weight_metric": "25.4"
89+
}
90+
)
91+
self.assertTrue(form.is_valid())
92+
93+
# UAT: Edge case - maximum valid weight
94+
def test_accepts_maximum_valid_weight(self):
95+
form = MetricWeightForm(
96+
participant=self.participant,
97+
instance=self.response_set,
98+
data={
99+
"weight_metric": "317.5"
100+
}
101+
)
102+
self.assertTrue(form.is_valid())

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_has_an_imperial_height_as_a_int(self):
5252
)
5353

5454
def test_has_an_metric_weight_as_a_int(self):
55-
self.response_set.weight_metric = 68
55+
self.response_set.weight_metric = 680
5656
self.response_set.save()
5757

5858
self.assertIsInstance(
@@ -189,3 +189,14 @@ def test_is_invalid_if_weight_metric_is_below_lower_bound(self):
189189
"Weight must be between 25.4kg and 317.5kg",
190190
context.exception.messages
191191
)
192+
193+
def test_is_invalid_if_weight_metric_is_above_upper_bound(self):
194+
self.response_set.weight_metric = ResponseSet.MAX_WEIGHT_METRIC + 1
195+
196+
with self.assertRaises(ValidationError) as context:
197+
self.response_set.full_clean()
198+
199+
self.assertIn(
200+
"Weight must be between 25.4kg and 317.5kg",
201+
context.exception.messages
202+
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_post_valid_weight_added_to_response_set(self):
8282
)
8383

8484
response_set = self.participant.responseset_set.first()
85-
self.assertEqual(response_set.weight_metric, self.valid_weight)
85+
self.assertEqual(response_set.weight_metric, self.valid_weight * 10)
8686

8787

8888
def test_post_responds_with_422_if_the_resource_is_invalid(self):

0 commit comments

Comments
 (0)