Skip to content

Commit 8b8f985

Browse files
committed
Run local acceptance tests as part of test unit scirpt
1 parent 1b58815 commit 8b8f985

File tree

13 files changed

+93
-53
lines changed

13 files changed

+93
-53
lines changed

lung_cancer_screening/questions/jinja2/date_of_birth.jinja

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
]
3737
}) }}
3838

39-
<input type="hidden" name="participant_id" value="{{ participant_id }}">
40-
4139
{{ button({
4240
"text": "Continue"
4341
}) }}

lung_cancer_screening/questions/jinja2/have_you_ever_smoked.jinja

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
]
3030
}) }}
3131

32-
<input type="hidden" name="participant_id" value="{{ participant_id }}">
33-
3432
{{ button({
3533
"text": "Continue"
3634
}) }}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from django.test import TestCase
2+
from django.urls import reverse
3+
4+
from lung_cancer_screening.questions.models.participant import Participant
5+
6+
class TestPostAgeRangeExit(TestCase):
7+
def setUp(self):
8+
participant = Participant.objects.create(unique_id="12345")
9+
10+
session = self.client.session
11+
session['participant_id'] = participant.unique_id
12+
session.save()
13+
14+
def test_get_redirects_if_the_particpant_does_not_exist(self):
15+
session = self.client.session
16+
session['participant_id'] = "somebody none existant participant"
17+
session.save()
18+
19+
response = self.client.get(
20+
reverse("questions:age_range_exit")
21+
)
22+
23+
self.assertRedirects(response, reverse("questions:start"))
24+
25+
def test_get_responds_successfully(self):
26+
response = self.client.get(reverse("questions:age_range_exit"))
27+
28+
self.assertEqual(response.status_code, 200)

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ def test_get_responds_successfully(self):
4343

4444
self.assertEqual(response.status_code, 200)
4545

46-
def test_get_contains_the_participant_id_in_the_form(self):
47-
response = self.client.get(reverse("questions:date_of_birth"))
48-
49-
self.assertContains(
50-
response,
51-
f"<input type=\"hidden\" name=\"participant_id\" value=\"{self.participant.unique_id}\">"
52-
)
53-
5446
def test_post_redirects_if_the_particpant_does_not_exist(self):
5547
session = self.client.session
5648
session['participant_id'] = "somebody none existant participant"

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ def test_get_responds_successfully(self):
2929

3030
self.assertEqual(response.status_code, 200)
3131

32-
def test_get_contains_the_participant_id_in_the_form(self):
33-
response = self.client.get(reverse("questions:have_you_ever_smoked"))
34-
35-
self.assertContains(
36-
response,
37-
f"<input type=\"hidden\" name=\"participant_id\" value=\"{self.participant.unique_id}\">"
38-
)
39-
4032
def test_post_redirects_if_the_particpant_does_not_exist(self):
4133
session = self.client.session
4234
session['participant_id'] = "somebody none existant participant"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from django.test import TestCase
2+
from django.urls import reverse
3+
4+
from lung_cancer_screening.questions.models.participant import Participant
5+
6+
class TestPostNonSmokerExit(TestCase):
7+
def setUp(self):
8+
participant = Participant.objects.create(unique_id="12345")
9+
10+
session = self.client.session
11+
session['participant_id'] = participant.unique_id
12+
session.save()
13+
14+
def test_get_redirects_if_the_particpant_does_not_exist(self):
15+
session = self.client.session
16+
session['participant_id'] = "somebody none existant participant"
17+
session.save()
18+
19+
response = self.client.get(
20+
reverse("questions:non_smoker_exit")
21+
)
22+
23+
self.assertRedirects(response, reverse("questions:start"))
24+
25+
def test_get_responds_successfully(self):
26+
response = self.client.get(reverse("questions:non_smoker_exit"))
27+
28+
self.assertEqual(response.status_code, 200)

lung_cancer_screening/questions/views/age_range_exit.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from django.shortcuts import render
22

3+
from .decorators.participant_decorators import require_participant
4+
5+
@require_participant
36
def age_range_exit(request):
47
return render(
58
request,

lung_cancer_screening/questions/views/date_of_birth.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
from dateutil.relativedelta import relativedelta
55
from django.core.exceptions import ValidationError
66

7-
from ..models.participant import Participant
8-
from ..models.date_response import DateResponse
7+
from .decorators.participant_decorators import require_participant
98

9+
from ..models.date_response import DateResponse
1010

11+
@require_participant
1112
def date_of_birth(request):
12-
try:
13-
participant = Participant.objects.get(
14-
unique_id=request.session['participant_id'])
15-
except Participant.DoesNotExist:
16-
return redirect(reverse("questions:start"))
17-
1813
if request.method == "POST":
1914
try:
2015
value = date(
@@ -28,7 +23,7 @@ def date_of_birth(request):
2823

2924
if (seventy_five_years_ago < value <= fifty_five_years_ago):
3025
DateResponse.objects.create(
31-
participant=participant,
26+
participant=request.participant,
3227
value=value,
3328
question="What is your date of birth?"
3429
)
@@ -41,12 +36,10 @@ def date_of_birth(request):
4136
return render(
4237
request,
4338
"date_of_birth.jinja",
44-
{"participant_id": participant.unique_id},
4539
status=422
4640
)
4741

4842
return render(
4943
request,
50-
"date_of_birth.jinja",
51-
{ "participant_id": participant.unique_id }
44+
"date_of_birth.jinja"
5245
)

lung_cancer_screening/questions/views/decorators/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from functools import wraps
2+
from django.shortcuts import redirect
3+
from django.urls import reverse
4+
5+
from lung_cancer_screening.questions.models.participant import Participant
6+
7+
def require_participant(function):
8+
@wraps(function)
9+
def wrap(request, *args, **kwargs):
10+
try:
11+
request.participant = Participant.objects.get(
12+
unique_id=request.session['participant_id'])
13+
except Participant.DoesNotExist:
14+
return redirect(reverse("questions:start"))
15+
16+
return function(request, *args, **kwargs)
17+
return wrap

0 commit comments

Comments
 (0)