-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
357 lines (262 loc) · 9.81 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import datetime
import re
import responses
import unittest2 as unittest
import livescrape
class BasePage(livescrape.ScrapedPage):
scrape_url = "http://fake-host/test.html"
class Test(unittest.TestCase):
def setUp(self):
responses.reset()
responses.add(
responses.GET, BasePage.scrape_url,
"""<html><body>
<h1 class="foo" data-foo=1>Heading</h1>
<h1 id="the-id">15</h1>
<span class=float>3.14</span>
<span class=int>42</span>
<span class=bool></span>
<span class=date>2016-04-23</span>
<a href="/very-fake">link</a>
<table>
<tr>test123
<th>key</th> testmore
<td>value</th></tr>
<tr><th>key2<td>value2</tr>
</table
""")
responses.start()
self.addCleanup(responses.stop)
def test_simplecss(self):
class Page(BasePage):
foo = livescrape.Css("h1.foo")
x = Page()
self.assertEqual(x.foo, 'Heading')
def test_dict(self):
class Page(BasePage):
foo = livescrape.Css("h1.foo")
x = Page()
self.assertEqual(x._dict, {"foo": 'Heading'})
def test_ambigous(self):
class Page(BasePage):
foo = livescrape.Css("h1")
x = Page()
self.assertEqual(x.foo, 'Heading')
def test_multiple(self):
class Page(BasePage):
foo = livescrape.Css("h1",
multiple=True)
x = Page()
self.assertEqual(x.foo, ['Heading', '15'])
def test_attribute(self):
class Page(BasePage):
foo = livescrape.Css("h1.foo",
attribute="data-foo")
not_there = livescrape.Css("h1.foo",
attribute="not-there")
x = Page()
self.assertEqual(x.foo, '1')
self.assertIsNone(x.not_there)
def test_link(self):
class Page(BasePage):
foo = livescrape.CssLink("a", "Page")
x = Page()
self.assertIsInstance(x.foo, Page)
self.assertEqual(x.foo.scrape_url,
"http://fake-host/very-fake")
def test_float(self):
class Page(BasePage):
foo = livescrape.CssFloat(".float")
foo_fail = livescrape.CssFloat(".date")
x = Page()
self.assertAlmostEqual(x.foo, 3.14)
self.assertIsNone(x.foo_fail)
def test_int(self):
class Page(BasePage):
foo = livescrape.CssInt(".int")
foo_fail = livescrape.CssInt(".date")
x = Page()
self.assertEqual(x.foo, 42)
self.assertIsNone(x.foo_fail)
def test_date(self):
class UTC(datetime.tzinfo):
"""UTC"""
def utcoffset(self, dt):
return datetime.timedelta(0)
def tzname(self, dt):
return "UTC"
def dst(self, dt):
return datetime.timedelta(0)
class Page(BasePage):
foo = livescrape.CssDate(".date", '%Y-%m-%d')
foo_tz = livescrape.CssDate(".date", '%Y-%m-%d', tzinfo=UTC())
foo_fail = livescrape.CssDate(".float", '%Y-%m-%d')
x = Page()
self.assertEqual(x.foo.year, 2016)
self.assertEqual(x.foo.tzinfo, None)
self.assertEqual(x.foo_tz.year, 2016)
self.assertEqual(x.foo_tz.tzinfo.tzname(None), "UTC")
self.assertIsNone(x.foo_fail)
def test_bool(self):
class Page(BasePage):
foo = livescrape.CssBoolean(".bool")
bar = livescrape.CssBoolean(".bool-not-there")
x = Page()
self.assertTrue(x.foo)
self.assertFalse(x.bar)
def test_raw(self):
class Page(BasePage):
foo = livescrape.CssRaw("table>tr")
foo_withtag = livescrape.CssRaw("table>tr", include_tag=True)
x = Page()
html = re.sub(r'\s+', " ", x.foo).strip()
self.assertEqual(html, "test123 <th>key</th> testmore <td>value</td>")
html = re.sub(r'\s+', " ", x.foo_withtag).strip()
self.assertEqual(
html,
"<tr>test123 <th>key</th> testmore <td>value</td></tr>")
def test_complex(self):
class Page(BasePage):
foo = livescrape.CssMulti(
"table tr",
key=livescrape.Css("th"),
value=livescrape.Css("td"))
x = Page()
self.assertEqual(x.foo, [{"key": "key", "value": "value"},
{"key": "key2", "value": "value2"}])
def test_group(self):
class Page(BasePage):
foo = livescrape.CssGroup("table tr", multiple=True)
foo.key = livescrape.Css("th")
foo.value = livescrape.Css("td")
x = Page()
self.assertEqual(x.foo[0]["key"], "key")
self.assertEqual(x.foo[0]["value"], "value")
self.assertEqual(x.foo[1]["key"], "key2")
self.assertEqual(x.foo[1]["value"], "value2")
self.assertEqual(x.foo[0].key, "key")
self.assertEqual(x.foo[0].value, "value")
self.assertEqual(x.foo[1].key, "key2")
self.assertEqual(x.foo[1].value, "value2")
self.assertEqual(x.foo[0]._dict(),
{"key": "key", "value": "value"})
# List members, but filter private ones
self.assertEqual([att for att in dir(x.foo[1])
if att[0] != "_"],
["key", "value"])
with self.assertRaises(AttributeError):
x.foo[0].nonexistent
def test_cleanup(self):
cleanup_args = [None]
def cleanup(x):
self.assertIsNone(cleanup_args[0])
cleanup_args[0] = x
return "TESTed"
class Page(BasePage):
foo = livescrape.Css("h1.foo",
cleanup=cleanup)
x = Page()
self.assertEqual(x.foo, "TESTed")
self.assertEqual(cleanup_args[0], "Heading")
def test_extract(self):
extract_args = [None]
def extract(x):
self.assertIsNone(extract_args[0])
extract_args[0] = x
return "TESTed"
class Page(BasePage):
foo = livescrape.Css("h1.foo",
extract=extract)
x = Page()
self.assertEqual(x.foo, "TESTed")
self.assertEqual(extract_args[0].text, "Heading")
def test_cleanup_extract(self):
cleanup_args = [None]
extract_args = [None]
def cleanup(x):
self.assertIsNone(cleanup_args[0])
cleanup_args[0] = x
return "TESTed"
def extract(x):
self.assertIsNone(extract_args[0])
extract_args[0] = x
return "Xtracted"
class Page(BasePage):
foo = livescrape.Css("h1.foo",
cleanup=cleanup,
extract=extract)
x = Page()
value = x.foo
self.assertEqual(extract_args[0].text, "Heading")
self.assertEqual(cleanup_args[0], "Xtracted")
self.assertEqual(value, "TESTed")
def test_decorator(self):
cleanup_args = [None]
extract_args = [None]
method_args = [None]
def cleanup(x):
self.assertIsNone(cleanup_args[0])
cleanup_args[0] = x
return "TESTed"
def extract(x):
self.assertIsNone(extract_args[0])
extract_args[0] = x
return "Xtracted"
class Page(BasePage):
@livescrape.Css("h1.foo",
cleanup=cleanup,
extract=extract)
def foo(self, value, element):
method_args[0] = (value, element)
return "METhod"
x = Page()
value = x.foo
self.assertEqual(extract_args[0].text, "Heading")
self.assertEqual(cleanup_args[0], "Xtracted")
self.assertEqual(method_args[0][0], "TESTed")
self.assertEqual(method_args[0][1].text, "Heading")
self.assertEqual(value, "METhod")
def test_headers(self):
class Page(BasePage):
scrape_headers = {"foo": "bar"}
Page().scrape_fetch(BasePage.scrape_url)
self.assertEqual(len(responses.calls), 1)
self.assertEqual(responses.calls[0].request.headers['Foo'],
'bar')
def test_referer(self):
class Page(BasePage):
foo = livescrape.CssLink("a", "Page")
x = Page()
self.assertIsInstance(x.foo, Page)
responses.add(
responses.GET, x.foo.scrape_url,
"<html>")
self.assertIsNone(x.foo.foo)
self.assertEqual(len(responses.calls), 2)
self.assertEqual(responses.calls[1].request.headers['Referer'],
'http://fake-host/test.html')
def test_custom_referer(self):
class Page(BasePage):
foo = livescrape.CssLink("a", "Page", referer="http://no")
x = Page()
self.assertIsInstance(x.foo, Page)
responses.add(
responses.GET, x.foo.scrape_url,
"<html>")
self.assertIsNone(x.foo.foo)
self.assertEqual(len(responses.calls), 2)
self.assertEqual(responses.calls[1].request.headers['Referer'],
'http://no')
def test_no_referer(self):
class Page(BasePage):
foo = livescrape.CssLink("a", "Page", referer=False)
x = Page()
self.assertIsInstance(x.foo, Page)
responses.add(
responses.GET, x.foo.scrape_url,
"<html>")
self.assertIsNone(x.foo.foo)
self.assertEqual(len(responses.calls), 2)
self.assertNotIn("Referer", responses.calls[1].request.headers)
if __name__ == '__main__':
unittest.main()