-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
210 lines (159 loc) · 8.32 KB
/
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
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
#!/usr/bin/env python3
import os
import json
import unittest
import parse
import scrapeutils
BASE_DIR = os.path.dirname(__file__)
FIXTURES_DIR = os.path.join(BASE_DIR, 'fixtures')
def load_samples(filename):
"""Return JSON data from file with the given filename."""
with open('%s/%s.json' % (FIXTURES_DIR, filename), encoding='utf-8') as f:
return json.load(f)
class MaxDiffTestCase(unittest.TestCase):
maxDiff = None
class ParseMpList(MaxDiffTestCase):
def test_sample_mp_lists(self):
"""parse.mp_list should give expected result on sample MP lists"""
for sample in load_samples('mp_list'):
result = parse.mp_list(sample['term'])
result = result['_items'][sample['index']]
self.assertEqual(result, sample['expected'])
def test_nonexistent_term(self):
"""parse.mp_list should fail for a term that does not exist"""
self.assertRaises(ValueError, parse.mp_list, '999')
class ParseMp(MaxDiffTestCase):
def test_sample_mp(self):
"""parse.mp should give expected result on sample MPs"""
for sample in load_samples('mp'):
result = parse.mp(sample['id'], sample['term'])
if sample.get('memb_index') is not None:
result['členstvo'] = result['členstvo'][sample['memb_index']:sample['memb_index']+1]
self.assertEqual(result, sample['expected'])
def test_nonexistent_mp_id(self):
"""parse.mp should fail for an id that does not exist"""
self.assertRaises(RuntimeError, parse.mp, '9999', '6')
def test_not_member_in_term(self):
"""parse.mp should fail if MP was not a member of parliament in given term"""
self.assertRaises(RuntimeError, parse.mp, '226', '2')
def test_nonexistent_term(self):
"""parse.mp should fail for a term that does not exist"""
self.assertRaises(ValueError, parse.mp, '226', '999')
class ParseGroupList(MaxDiffTestCase):
def test_sample_group_lists(self):
"""parse.group_list should give expected result on sample group lists"""
for sample in load_samples('group_list'):
result = parse.group_list(sample['type'], sample['term'])
result = result['_items'][sample['index']]
self.assertEqual(result, sample['expected'])
def test_nonexistent_group_type(self):
"""parse.group_list should fail for an unknown type"""
self.assertRaises(ValueError, parse.group_list, 'foo', '1')
def test_nonexistent_term(self):
"""parse.group_list should fail for a term that does not exist"""
self.assertRaises(ValueError, parse.group_list, 'committee', '999')
class ParseGroup(MaxDiffTestCase):
def test_sample_groups(self):
"""parse.group should give expected result on sample groups"""
for sample in load_samples('group'):
result = parse.group(sample['type'], sample['id'])
result['členovia'] = result['členovia'][0:1]
if 'opis' in result:
result['opis'] = result['opis'][:50]
self.assertEqual(result, sample['expected'])
def test_nonexistent_group_type(self):
"""parse.group should fail for an unknown type"""
self.assertRaises(ValueError, parse.group, 'foo', '1')
def test_nonexistent_group_id(self):
"""parse.group should fail for an id that does not exist"""
self.assertRaises(RuntimeError, parse.group, 'committee', '9999')
class ParseChangeList(MaxDiffTestCase):
def test_sample_changes(self):
"""parse.change_list should give expected result on sample changes"""
for sample in load_samples('change_list'):
result = parse.change_list(sample['term'])
result = result['_items'][sample['index']]
self.assertEqual(result, sample['expected'])
def test_nonexistent_term(self):
"""parse.change_list should fail for a term that does not exist"""
self.assertRaises(ValueError, parse.change_list, 'abc')
@unittest.skip("Speaker is not used in scraping.")
class ParseSpeaker(MaxDiffTestCase):
def test_sample_speaker(self):
"""parse.speaker should give expected result on sample speaker"""
for sample in load_samples('speaker'):
result = parse.speaker()
if 'životopis' in result:
result['životopis'] = result['životopis'][:50]
self.assertEqual(result, sample['expected'])
@unittest.skip("Deputy speakers are not used in scraping.")
class ParseDeputySpeakers(MaxDiffTestCase):
def test_sample_deputy_speakers(self):
"""parse.deputy_speakers should give expected result on sample deputy speakers"""
for sample in load_samples('deputy_speakers'):
result = parse.deputy_speakers()
result = result[sample['index']]
self.assertEqual(result, sample['expected'])
class ParseSessionList(MaxDiffTestCase):
def test_sample_session_lists(self):
"""parse.session_list should give expected result on sample session lists"""
for sample in load_samples('session_list'):
result = parse.session_list(sample['term'])
result = result['_items'][sample['index']]
self.assertEqual(result, sample['expected'])
def test_nonexistent_term(self):
"""parse.session_list should fail for a term that does not exist"""
self.assertRaises(ValueError, parse.session_list, '999')
class ParseSession(MaxDiffTestCase):
def test_sample_sessions(self):
"""parse.session should give expected result on sample session"""
for sample in load_samples('session'):
result = parse.session(sample['session_number'], sample['term'])
result = result['_items'][sample['index']]
self.assertEqual(result, sample['expected'])
def test_invalid_session_number(self):
"""parse.session should fail for a session number that is not integer"""
self.assertRaises(ValueError, parse.session, 'abc')
def test_nonexistent_session_number(self):
"""parse.session should return empty result for a session number that does not exist"""
result = parse.session('999')
self.assertEqual(result['_items'], [])
def test_nonexistent_term(self):
"""parse.session should fail for a term that does not exist"""
self.assertRaises(ValueError, parse.session, '1', '999')
class ParseMotion(MaxDiffTestCase):
def test_sample_motions(self):
"""parse.motion should give expected result on sample motions"""
for sample in load_samples('motion'):
result = parse.motion(sample['id'])
if sample.get('mp_index') is not None:
result['hlasy'] = result['hlasy'][sample['mp_index']:sample['mp_index']+1]
self.assertEqual(result, sample['expected'])
def test_nonexistent_motion_id(self):
"""parse.motion should fail for an id that does not exist"""
self.assertRaises(RuntimeError, parse.motion, '0')
class ParseNewDebatesList(MaxDiffTestCase):
def test_sample_lists(self):
"""parse.new_debates_list should give expected result on sample debate lists"""
for sample in load_samples('new_debates_list'):
result = parse.new_debates_list(sample['term'],
since_date=sample['since_date'], until_date=sample['until_date'])
self.assertEqual(result['_items'][sample['index']], sample['expected'])
def test_wrong_term(self):
"""parse.new_debates_list should fail for a term before 5th one"""
self.assertRaises(ValueError, parse.new_debates_list, '4')
class ParseDebateOfTerms56(MaxDiffTestCase):
def test_sample_debates(self):
"""parse.debates_of_terms56 should give expected result on sample debates"""
for sample in load_samples('debate_of_terms_56'):
result = parse.debate_of_terms56(sample['id'])
result['riadky'] = [result['riadky'][sample['line_index']]]
self.assertEqual(result, sample['expected'])
def test_nonexistent_debate(self):
"""parse.debates_of_terms56 should return empty result for debate that does not exist"""
result = parse.debate_of_terms56('1')
self.assertEqual(result, {'riadky': []})
# no tests for scraping of old debates as they are no more scraped after initial load
if __name__ == '__main__':
scrapeutils.USE_WEBCACHE = True
unittest.main()