-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_basics.py
308 lines (250 loc) · 11.6 KB
/
test_basics.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import json
from os.path import dirname, join
import re
from unittest import TestCase
from mock import patch
import pytest
import six
from six import text_type
from everypolitician import EveryPolitician, NotFound
from popolo_data.importer import Popolo
class FakeResponse(object):
def __init__(self, json_data, text, status_code):
self.json_data = json_data
self.text = text
self.status_code = status_code
def json(self):
return self.json_data
def text(self):
return self.text
def raise_for_status(self):
pass
URL_DATA = {
'https://raw.githubusercontent.com/everypolitician/everypolitician-data/master/countries.json':
'example-countries.json',
'https://raw.githubusercontent.com/everypolitician/everypolitician-data/d3afadff7d5a08e1745b7e48782a869ec4979e78/data/Argentina/Diputados/term-133.csv':
'example-period.csv'
}
def fake_requests_get(url):
leafname = URL_DATA.get(url)
if not leafname:
raise Exception("The URL {0} hasn't been faked".format(url))
filename = join(dirname(__file__), 'test-data', leafname)
if leafname.endswith('.json'):
with open(filename) as f:
return FakeResponse(json.load(f), None, 200)
else:
with open(filename, 'rb') as f:
return FakeResponse(None, f.read().decode('utf-8'), 200)
@patch('everypolitician.lib.requests.get', side_effect=fake_requests_get)
class TestDataLoading(TestCase):
def test_create_ep(self, patched_requests_get):
ep = EveryPolitician()
assert str(ep) == \
'<EveryPolitician: https://raw.githubusercontent.com/everypolitician/everypolitician-data/master/countries.json>'
def test_ep_repr(self, patched_requests_get):
ep = EveryPolitician()
assert repr(ep) == 'EveryPolitician()'
def test_ep_repr_custom_url(self, patched_requests_get):
ep = EveryPolitician(countries_json_url='foobar')
assert repr(ep) == 'EveryPolitician(countries_json_url="foobar")'
def test_ep_from_local_file(self, patched_requests_get):
filename = join(
dirname(__file__), 'test-data', 'example-countries.json')
ep = EveryPolitician(countries_json_filename=filename)
assert re.search(
r'EveryPolitician\(countries_json_filename=".*example-countries.json"\)',
repr(ep))
def test_countries_from_local_file(self, patched_requests_get):
filename = join(
dirname(__file__), 'test-data', 'example-countries.json')
ep = EveryPolitician(countries_json_filename=filename)
countries = ep.countries()
assert len(countries) == 3
def test_countries(self, patched_requests_get):
ep = EveryPolitician()
countries = ep.countries()
assert len(countries) == 3
assert text_type(countries[0]) == '<Country: Åland>'
assert text_type(countries[1]) == '<Country: Argentina>'
assert text_type(countries[2]) == '<Country: British Virgin Islands>'
def test_json_only_fetched_once(self, patched_requests_get):
ep = EveryPolitician()
ep.countries()
ep.countries()
assert patched_requests_get.call_count == 1
def test_get_a_single_country_bad_case(self, patched_requests_get):
ep = EveryPolitician()
with pytest.raises(NotFound):
ep.country('argentina')
def test_get_a_single_country(self, patched_requests_get):
ep = EveryPolitician()
country = ep.country('Argentina')
assert country.name == 'Argentina'
def test_get_a_country_and_legislature(self, patched_requests_get):
ep = EveryPolitician()
country, legislature = ep.country_legislature('Argentina', 'Diputados')
assert country.name == 'Argentina'
assert legislature.name == 'Cámara de Diputados'
def test_get_a_country_legislature_c_not_found(self, patched_requests_get):
ep = EveryPolitician()
with pytest.raises(NotFound):
ep.country_legislature('Argentina', 'FOO')
def test_get_a_country_legislature_l_not_found(self, patched_requests_get):
ep = EveryPolitician()
with pytest.raises(NotFound):
ep.country_legislature('FOO', 'Diputados')
def test_get_a_country_legislature_neither_found(self, patched_requests_get):
ep = EveryPolitician()
with pytest.raises(NotFound):
ep.country_legislature('FOO', 'FOO')
class TestCountryMethods(TestCase):
def setUp(self):
with patch('everypolitician.lib.requests.get', side_effect=fake_requests_get):
self.ep = EveryPolitician()
self.country_aland = self.ep.country('Aland')
self.country_argentina = self.ep.country('Argentina')
self.country_bv = self.ep.country('British-Virgin-Islands')
def test_country_repr(self):
if six.PY2:
assert repr(self.country_aland) == b'<Country: \xc3\x85land>'
else:
assert repr(self.country_aland) == '<Country: \xc5land>'
def test_get_legislatures(self):
ls = self.country_aland.legislatures()
assert len(ls) == 1
def test_most_recent_house_no_house_matches(self):
with pytest.raises(NotFound):
print(self.country_argentina.house_most_recent('random_house'))
def test_most_recent_house_where_multiple_match_lower_house(self):
assert self.country_bv.house_most_recent('lower_house').name == 'House of Assembly'
def test_lower_house_shortcut(self):
assert self.country_argentina.lower_house().name == 'Cámara de Diputados'
def test_upper_house_shortcut(self):
assert self.country_argentina.upper_house().name == 'Cámara de Senadores'
class TestCountryHousesMethod(TestCase):
def test_finds_unicameral_legislature_for_lower_house(self):
with patch('everypolitician.lib.requests.get', side_effect=fake_requests_get):
ep = EveryPolitician()
country = ep.country('Aland')
houses = country.houses('lower house')
assert len(houses) == 1
assert houses[0].name == 'Lagting'
def test_finds_lower_house_if_present(self):
with patch('everypolitician.lib.requests.get', side_effect=fake_requests_get):
ep = EveryPolitician()
country = ep.country('Argentina')
houses = country.houses('lower house')
assert len(houses) == 1
assert houses[0].name == 'Cámara de Diputados'
def test_finds_upper_house_if_present(self):
with patch('everypolitician.lib.requests.get', side_effect=fake_requests_get):
ep = EveryPolitician()
country = ep.country('Argentina')
houses = country.houses('upper house')
assert len(houses) == 1
assert houses[0].name == 'Cámara de Senadores'
def test_no_matches_for_unknown_house_type(self):
with patch('everypolitician.lib.requests.get', side_effect=fake_requests_get):
ep = EveryPolitician()
country = ep.country('Argentina')
houses = country.houses('quiet area')
assert len(houses) == 0
class TestLeglislatureMethods(TestCase):
def setUp(self):
with patch('everypolitician.lib.requests.get', side_effect=fake_requests_get):
self.ep = EveryPolitician()
self.country = self.ep.country('Argentina')
self.legislatures = self.country.legislatures()
def test_legislature_repr(self):
if six.PY2:
assert repr(self.legislatures[0]) == b'<Legislature: C\xc3\xa1mara de Diputados in Argentina>'
else:
assert repr(self.legislatures[1]) == '<Legislature: Cámara de Senadores in Argentina>'
def test_legislature_str(self):
assert text_type(self.legislatures[1]) == '<Legislature: Cámara de Senadores in Argentina>'
def test_legislature_popolo_url(self):
l = self.legislatures[1]
assert l.popolo_url == 'https://cdn.rawgit.com/everypolitician/' \
'everypolitician-data/c323b935f2dce83fcdfcbb5c2f94614a25207d98/' \
'data/Argentina/Senado/ep-popolo-v1.0.json'
def test_directory(self):
l = self.legislatures[0]
assert l.directory() == 'Argentina/Diputados'
@patch('everypolitician.lib.Popolo')
def test_popolo_call(self, mocked_popolo_class):
mocked_popolo_class.from_url.return_value = Popolo({
'persons': [
{'name': 'Joe Bloggs'}
]
})
l = self.legislatures[0]
popolo = l.popolo()
mocked_popolo_class.from_url.assert_called_with(
u'https://cdn.rawgit.com/everypolitician/everypolitician-data/'
u'd3afadff7d5a08e1745b7e48782a869ec4979e78/data/Argentina/'
u'Diputados/ep-popolo-v1.0.json')
assert len(popolo.persons) == 1
assert popolo.persons.first.name == 'Joe Bloggs'
@patch('everypolitician.lib.Popolo')
def test_popolo_data_only_fetched_once(self, mocked_popolo_class):
mocked_popolo_class.from_url.return_value = Popolo({
'persons': [
{'name': 'Joe Bloggs'}
]
})
l = self.legislatures[0]
l.popolo()
l.popolo()
mocked_popolo_class.from_url.assert_called_once_with(
u'https://cdn.rawgit.com/everypolitician/everypolitician-data/'
u'd3afadff7d5a08e1745b7e48782a869ec4979e78/data/Argentina/'
u'Diputados/ep-popolo-v1.0.json')
@patch('everypolitician.lib.requests.get', side_effect=fake_requests_get)
class TestLegislativePeriod(TestCase):
def setUp(self):
with patch('everypolitician.lib.requests.get', side_effect=fake_requests_get):
self.ep = EveryPolitician()
self.country = self.ep.country('Argentina')
self.legislature = self.country.legislature('Diputados')
self.period = self.legislature.legislative_periods()[0]
def test_start_date(self, patched_requests_get):
assert self.period.start_date == "2015"
def test_end_date(self, patched_requests_get):
assert self.period.end_date is None
def test_csv(self, patched_requests_get):
assert self.period.csv() == \
[{u'area': u'BUENOS AIRES',
u'area_id': u'area/buenos_aires',
u'chamber': u'C\xe1mara de Diputados',
u'email': u'[email protected]',
u'end_date': u'2015-12-09',
u'facebook': u'',
u'gender': u'female',
u'group': u'FRENTE PARA LA VICTORIA - PJ',
u'group_id': u'frente_para_la_victoria_-_pj',
u'id': u'b882751f-4014-4f6f-b3cf-e0a5d6d3c605',
u'image': u'http://www4.hcdn.gob.ar/fotos/asegarra.jpg',
u'name': u'ADELA ROSA SEGARRA',
u'sort_name': u'SEGARRA, ADELA ROSA',
u'start_date': u'',
u'term': u'133',
u'twitter': u''},
{u'area': u'BUENOS AIRES',
u'area_id': u'area/buenos_aires',
u'chamber': u'C\xe1mara de Diputados',
u'email': u'[email protected]',
u'end_date': u'',
u'facebook': u'',
u'gender': u'male',
u'group': u'FRENTE RENOVADOR',
u'group_id': u'frente_renovador',
u'id': u'8efb1e0e-8454-4c6b-9f87-0d4fef875fd2',
u'image': u'http://www4.hcdn.gob.ar/fotos/aperez.jpg',
u'name': u'ADRIAN PEREZ',
u'sort_name': u'PEREZ, ADRIAN',
u'start_date': u'',
u'term': u'133',
u'twitter': u'adrianperezARG'}]