-
Notifications
You must be signed in to change notification settings - Fork 0
/
polling_unit_lookup_test.py
113 lines (94 loc) · 3.98 KB
/
polling_unit_lookup_test.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
from polling_unit_lookup import app, tidy_up_pun
import unittest
import requests_mock
import json
class TidyUpPunTestCase(unittest.TestCase):
def test_none_returns_empty_string(self):
self.assertEqual('', tidy_up_pun(None))
def test_tidy_up_and_clean(self):
self.assertEqual('AB:1:23:45', tidy_up_pun('AB:01:23:45'))
self.assertEqual('AB:1:23:45', tidy_up_pun('AB--01::23 45'))
self.assertEqual('AB:1:23:45', tidy_up_pun(' AB--01::23 45 '))
def test_converting_state_number_to_code(self):
self.assertEqual('AB:1:23:45', tidy_up_pun('01:01:23:45'))
self.assertEqual('AB', tidy_up_pun('01'))
self.assertEqual('99:1:23:45', tidy_up_pun('99:01:23:45'))
class PollingUnitLookupTestCase(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
app.config['MAPIT_API_URL'] = 'http://mapit'
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
self.app = app.test_client()
self.mapit_area = {
'id': 42,
'name': 'Aba North',
}
self.states = {
2: {
'name': 'Abia',
},
}
self.federal_constituencies = {
1109: {
'name': 'Aba North/South',
},
}
self.senatorial_districts = {
811: {
'name': 'ABIA SOUTH',
},
}
def mock_mapit_response(self, m, pun):
m.get(
'http://mapit/code/poll_unit/{}'.format(pun),
headers={'Location': 'http://mapit/area/42'},
status_code=302)
m.get('http://mapit/area/42', json=self.mapit_area)
m.get('http://mapit/area/42/covered?type=STA', json=self.states)
m.get('http://mapit/area/42/covered?type=FED', json=self.federal_constituencies)
m.get('http://mapit/area/42/covered?type=SEN', json=self.senatorial_districts)
return {
'area': self.mapit_area,
'states': [
self.states[2],
],
'federal_constituencies': [
self.federal_constituencies[1109],
],
'senatorial_districts': [
self.senatorial_districts[811],
],
}
def test_polling_unit_lookup_invalid_number(self):
rv = self.app.get('/?lookup=abcd')
self.assertIn('Invalid polling unit: abcd', rv.data)
self.assertEqual(rv.status_code, 400)
def test_polling_unit_lookup_valid_number(self):
with requests_mock.mock() as m:
expected = self.mock_mapit_response(m, 'AB:1:23:45')
rv = self.app.get('/?lookup=AB%3A01%3A23%3A45')
self.assertEqual(rv.status_code, 200)
self.assertEqual(expected, json.loads(rv.data))
def test_polling_unit_lookup_valid_number_no_area(self):
with requests_mock.mock() as m:
m.get('http://mapit/code/poll_unit/ZZ', status_code=404)
rv = self.app.get('/?lookup=ZZ')
self.assertEqual(rv.status_code, 404)
self.assertIn('No areas were found that matched polling unit: ZZ', rv.data)
def test_lookup_tries_multiple_variations(self):
with requests_mock.mock() as m:
m.get('http://mapit/code/poll_unit/AB:1:23:45', status_code=404)
m.get('http://mapit/code/poll_unit/AB:1:23', status_code=404)
m.get('http://mapit/code/poll_unit/AB:1', status_code=404)
expected = self.mock_mapit_response(m, 'AB')
rv = self.app.get('/?lookup=AB%3A01%3A23%3A45')
self.assertEqual(rv.status_code, 200)
self.assertEqual(expected, json.loads(rv.data))
def test_lookup_with_slashes(self):
with requests_mock.mock() as m:
expected = self.mock_mapit_response(m, 'AB:2:3:4')
rv = self.app.get('/?lookup=01%2F02%2F03%2F04')
self.assertEqual(rv.status_code, 200)
self.assertEqual(expected, json.loads(rv.data))
if __name__ == '__main__':
unittest.main()