Skip to content

Commit 650c7fa

Browse files
committed
Run local acceptance tests as part of test unit scirpt
1 parent 87cd6e6 commit 650c7fa

File tree

11 files changed

+96
-52
lines changed

11 files changed

+96
-52
lines changed
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)
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: 6 additions & 11 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 value in (fifty_five_years_ago, seventy_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,12 @@ def date_of_birth(request):
4136
return render(
4237
request,
4338
"date_of_birth.jinja",
44-
{"participant_id": participant.unique_id},
39+
{"participant_id": request.participant.unique_id},
4540
status=422
4641
)
4742

4843
return render(
4944
request,
5045
"date_of_birth.jinja",
51-
{ "participant_id": participant.unique_id }
46+
{ "participant_id": request.participant.unique_id }
5247
)

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

lung_cancer_screening/questions/views/have_you_ever_smoked.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@
22
from django.urls import reverse
33
from django.core.exceptions import ValidationError
44

5-
from ..models.participant import Participant
6-
from ..models.boolean_response import BooleanResponse
5+
from .decorators.participant_decorators import require_participant
76

7+
from ..models.boolean_response import BooleanResponse
88

9+
@require_participant
910
def have_you_ever_smoked(request):
10-
try:
11-
participant = Participant.objects.get(
12-
unique_id=request.session['participant_id'])
13-
except Participant.DoesNotExist:
14-
return redirect(reverse("questions:start"))
15-
1611
if request.method == "POST":
1712
try:
1813
value = int(request.POST['value'])
1914

2015
if value:
2116
BooleanResponse.objects.create(
22-
participant=participant,
17+
participant=request.participant,
2318
value=value,
2419
question="Have you ever smoked?"
2520
)
@@ -32,12 +27,12 @@ def have_you_ever_smoked(request):
3227
return render(
3328
request,
3429
"have_you_ever_smoked.jinja",
35-
{"participant_id": participant.unique_id},
30+
{"participant_id": request.participant.unique_id},
3631
status=422
3732
)
3833

3934
return render(
4035
request,
4136
"have_you_ever_smoked.jinja",
42-
{"participant_id": participant.unique_id}
37+
{"participant_id": request.participant.unique_id}
4338
)

lung_cancer_screening/questions/views/non_smoker_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 non_smoker_exit(request):
47
return render(
58
request,
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
from django.shortcuts import render, redirect
2-
from django.urls import reverse
1+
from django.shortcuts import render
32

4-
from ..models.participant import Participant
3+
from .decorators.participant_decorators import require_participant
54

5+
@require_participant
66
def responses(request):
7-
try:
8-
participant = Participant.objects.get(unique_id=request.session['participant_id'])
9-
except Participant.DoesNotExist:
10-
return redirect(reverse("questions:start"))
11-
127
return render(
138
request,
149
"responses.jinja",
15-
{"responses": participant.responses()}
10+
{"responses": request.participant.responses()}
1611
)

scripts/tests/integration.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)