-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_views.py
185 lines (161 loc) · 6.18 KB
/
test_views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from django.test import TestCase, Client
from unittest.mock import patch
from django.urls import reverse
from caps.models import Council, Promise
class TestPageRenders(TestCase):
def setUp(self):
self.client = Client()
council = Council.objects.create(
name="Borsetshire", slug="borsetshire", country=Council.ENGLAND
)
def test_home_page(self):
url = reverse("home")
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "home.html")
def test_council_detail(self):
url = reverse("council", args=["borsetshire"])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "council_detail.html")
@patch("caps.forms.HighlightedSearchForm.search")
def test_search_results(self, search):
url = reverse("search_results")
response = self.client.get(url, {"q": "ev charging"})
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "search_results.html")
def test_council_list(self):
url = reverse("council_list")
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "council_list.html")
class TestPostcodeSearch(TestCase):
def setUp(self):
Council.objects.create(
name="Borsetshire",
slug="borsetshire",
country=Council.ENGLAND,
authority_code="BOS",
gss_code="E14000111",
)
Council.objects.create(
name="Borsetshire District",
slug="borsetshire-district",
country=Council.ENGLAND,
authority_code="BOD",
gss_code="E14000222",
)
felpersham = Council.objects.create(
name="Felpersham Combined Authority",
slug="felpersham",
country=Council.ENGLAND,
authority_code="FEL",
gss_code="E14000444",
)
Council.objects.create(
name="Ambridge",
slug="ambridge",
country=Council.ENGLAND,
authority_code="AMB",
gss_code="E14000333",
combined_authority=felpersham,
)
@patch("caps.mapit.session")
def test_postcode_to_one_council_redirects_to_council(self, mapit_session):
mapit_session.get.return_value.json.return_value = {
"areas": {
"11111": {
"id": 11111,
"codes": {"gss": "E14000111", "unit_id": "11111"},
"name": "Borsetshire Council",
"country": "E",
"type": "CTY",
}
}
}
response = self.client.get("/location/?pc=BO11AD", follow=True)
self.assertRedirects(response, "/councils/borsetshire/")
@patch("caps.mapit.session")
def test_postcode_to_two_councils_shows_results(self, mapit_session):
mapit_session.get.return_value.json.return_value = {
"areas": {
"11111": {
"id": 11111,
"codes": {"gss": "E14000111", "unit_id": "11111"},
"name": "Borsetshire Council",
"country": "E",
"type": "CTY",
},
"22222": {
"id": 22222,
"codes": {"gss": "E14000222", "unit_id": "22222"},
"name": "Borsetshire District",
"country": "E",
"type": "DIS",
},
}
}
response = self.client.get("/location/?pc=BO11AE", follow=True)
self.assertTemplateUsed(response, "location_results.html")
@patch("caps.mapit.session")
def test_postcode_to_council_and_combined_authority_shows_results(
self, mapit_session
):
mapit_session.get.return_value.json.return_value = {
"areas": {
"33333": {
"id": 33333,
"codes": {"gss": "E14000333", "unit_id": "33333"},
"name": "Ambridge",
"country": "E",
"type": "DIS",
}
}
}
response = self.client.get("/location/?pc=BO11AF", follow=True)
self.assertTemplateUsed(response, "location_results.html")
def test_empty_submission_shows_results_page(self):
response = self.client.get("/location/?pc=")
self.assertTemplateUsed(response, "location_results.html")
class TestCouncilDetailPage(TestCase):
promise = None
def setUp(self):
council = Council.objects.create(
name="Borsetshire",
slug="borsetshire",
country=Council.ENGLAND,
authority_code="BOS",
gss_code="E14000111",
)
self.promise = Promise.objects.create(
council=council,
has_promise=True,
text="this is a promise",
source_name="council website",
source="http://borsetshire.gov.uk/promise/",
target_year="2045",
)
def test_council_has_promise(self):
url = reverse("council", args=["borsetshire"])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertRegex(response.content, rb"this is a promise")
def test_council_has_no_promise(self):
self.promise.has_promise = False
self.promise.save()
url = reverse("council", args=["borsetshire"])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertRegex(
response.content,
rb"We couldn\xe2\x80\x99t find any climate pledges from this council",
)
def test_council_no_promise_data(self):
self.promise.delete()
url = reverse("council", args=["borsetshire"])
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertRegex(
response.content,
rb"checked whether this council has made any climate pledges",
)