-
-
Notifications
You must be signed in to change notification settings - Fork 996
/
Copy pathtest_results.py
493 lines (405 loc) · 15.2 KB
/
test_results.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2015-2023 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
import os
import sys
import unittest
import re
import json
import hashlib
import datetime
import collections
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from gallery_dl import \
extractor, util, job, config, exception, formatter # noqa E402
RESULTS = os.environ.get("GDL_TEST_RESULTS")
if RESULTS:
results = util.import_file(RESULTS)
else:
from test import results
# temporary issues, etc.
BROKEN = {
}
CONFIG = {
"cache": {
"file": None,
},
"downloader": {
"adjust-extensions": False,
"part": False,
},
}
AUTH_REQUIRED = {
"pixiv",
"nijie",
"horne",
"reddit",
"seiga",
"fantia",
"instagram",
"twitter",
}
AUTH_KEYS = (
"username",
"cookies",
"api-key",
"client-id",
"access-token",
"refresh-token",
)
class TestExtractorResults(unittest.TestCase):
def setUp(self):
setup_test_config()
def tearDown(self):
config.clear()
@classmethod
def setUpClass(cls):
cls._skipped = []
@classmethod
def tearDownClass(cls):
if cls._skipped:
print("\n\nSkipped tests:")
for url, exc in cls._skipped:
print('- {} ("{}")'.format(url, exc))
def assertRange(self, value, range, msg=None):
if range.step > 1:
self.assertIn(value, range, msg=msg)
else:
self.assertLessEqual(value, range.stop, msg=msg)
self.assertGreaterEqual(value, range.start, msg=msg)
def _run_test(self, result):
base, cat, sub = result_categories(result)
result.pop("#comment", None)
result.pop("#category", None)
auth = result.pop("#auth", None)
extr_url = extractor.find(result["#url"])
self.assertTrue(extr_url, "extractor by URL/find")
extr_cls = extr = result["#class"].from_url(result["#url"])
self.assertTrue(extr_url, "extractor by cls.from_url()")
self.assertIs(extr_url.__class__, extr_cls.__class__)
if len(result) <= 2:
return # only matching
if auth is None:
auth = (cat in AUTH_REQUIRED)
elif not auth:
# auth explicitly disabled
for key in AUTH_KEYS:
config.set((), key, None)
if auth and not any(extr.config(key) for key in AUTH_KEYS):
return self._skipped.append((result["#url"], "no auth"))
if "#options" in result:
for key, value in result["#options"].items():
key = key.split(".")
config.set(key[:-1], key[-1], value)
if "#range" in result:
config.set((), "image-range" , result["#range"])
config.set((), "chapter-range", result["#range"])
tjob = ResultJob(extr, content=("#sha1_content" in result))
if "#exception" in result:
with self.assertRaises(result["#exception"], msg="#exception"):
tjob.run()
return
try:
tjob.run()
except exception.StopExtraction:
pass
except exception.HttpError as exc:
exc = str(exc)
if re.match(r"'5\d\d ", exc) or \
re.search(r"\bRead timed out\b", exc):
self._skipped.append((result["#url"], exc))
self.skipTest(exc)
raise
if result.get("#archive", True):
self.assertEqual(
len(set(tjob.archive_list)),
len(tjob.archive_list),
msg="archive-id uniqueness")
if tjob.queue:
# test '_extractor' entries
for url, kwdict in zip(tjob.url_list, tjob.kwdict_list):
if "_extractor" in kwdict:
extr = kwdict["_extractor"].from_url(url)
if extr is None and not result.get("#extractor", True):
continue
self.assertIsInstance(extr, kwdict["_extractor"])
self.assertEqual(extr.url, url)
else:
# test 'extension' entries
for kwdict in tjob.kwdict_list:
self.assertIn("extension", kwdict, msg="#extension")
# test extraction results
if "#sha1_url" in result:
self.assertEqual(
result["#sha1_url"],
tjob.url_hash.hexdigest(),
msg="#sha1_url")
if "#sha1_content" in result:
expected = result["#sha1_content"]
digest = tjob.content_hash.hexdigest()
if isinstance(expected, str):
self.assertEqual(expected, digest, msg="#sha1_content")
else: # iterable
self.assertIn(digest, expected, msg="#sha1_content")
if "#sha1_metadata" in result:
self.assertEqual(
result["#sha1_metadata"],
tjob.kwdict_hash.hexdigest(),
"#sha1_metadata")
if "#count" in result:
count = result["#count"]
len_urls = len(tjob.url_list)
if isinstance(count, str):
self.assertRegex(
count, r"^ *(==|!=|<|<=|>|>=) *\d+ *$", msg="#count")
expr = "{} {}".format(len_urls, count)
self.assertTrue(eval(expr), msg=expr)
elif isinstance(count, range):
self.assertRange(len_urls, count, msg="#count")
else: # assume integer
self.assertEqual(len_urls, count, msg="#count")
if "#pattern" in result:
self.assertGreater(len(tjob.url_list), 0, msg="#pattern")
pattern = result["#pattern"]
if isinstance(pattern, str):
for url in tjob.url_list:
self.assertRegex(url, pattern, msg="#pattern")
else:
for url, pat in zip(tjob.url_list, pattern):
self.assertRegex(url, pat, msg="#pattern")
if "#urls" in result:
expected = result["#urls"]
if isinstance(expected, str):
self.assertTrue(tjob.url_list, msg="#urls")
self.assertEqual(tjob.url_list[0], expected, msg="#urls")
else:
self.assertSequenceEqual(tjob.url_list, expected, msg="#urls")
metadata = {k: v for k, v in result.items() if k[0] != "#"}
if metadata:
for kwdict in tjob.kwdict_list:
self._test_kwdict(kwdict, metadata)
def _test_kwdict(self, kwdict, tests, parent=None):
for key, test in tests.items():
if key.startswith("?"):
key = key[1:]
if key not in kwdict:
continue
path = "{}.{}".format(parent, key) if parent else key
self.assertIn(key, kwdict, msg=path)
value = kwdict[key]
if isinstance(test, dict):
self._test_kwdict(value, test, path)
elif isinstance(test, type):
self.assertIsInstance(value, test, msg=path)
elif isinstance(test, range):
self.assertRange(value, test, msg=path)
elif isinstance(test, set):
self.assertIn(value, test, msg=path)
elif isinstance(test, list):
subtest = False
for idx, item in enumerate(test):
if isinstance(item, dict):
subtest = True
subpath = "{}[{}]".format(path, idx)
self._test_kwdict(value[idx], item, subpath)
if not subtest:
self.assertEqual(test, value, msg=path)
elif isinstance(test, str):
if test.startswith("re:"):
self.assertRegex(value, test[3:], msg=path)
elif test.startswith("dt:"):
self.assertIsInstance(value, datetime.datetime, msg=path)
self.assertEqual(test[3:], str(value), msg=path)
elif test.startswith("type:"):
self.assertEqual(test[5:], type(value).__name__, msg=path)
elif test.startswith("len:"):
self.assertIsInstance(value, (list, tuple), msg=path)
self.assertEqual(int(test[4:]), len(value), msg=path)
else:
self.assertEqual(test, value, msg=path)
else:
self.assertEqual(test, value, msg=path)
class ResultJob(job.DownloadJob):
"""Generate test-results for extractor runs"""
def __init__(self, url, parent=None, content=False):
job.DownloadJob.__init__(self, url, parent)
self.queue = False
self.content = content
self.url_list = []
self.url_hash = hashlib.sha1()
self.kwdict_list = []
self.kwdict_hash = hashlib.sha1()
self.archive_list = []
self.archive_hash = hashlib.sha1()
self.content_hash = hashlib.sha1()
if content:
self.fileobj = TestPathfmt(self.content_hash)
else:
self._update_content = lambda url, kwdict: None
self.format_directory = TestFormatter(
"".join(self.extractor.directory_fmt)).format_map
self.format_filename = TestFormatter(
self.extractor.filename_fmt).format_map
self.format_archive = TestFormatter(
self.extractor.archive_fmt).format_map
def run(self):
self._init()
for msg in self.extractor:
self.dispatch(msg)
def handle_url(self, url, kwdict, fallback=None):
self._update_url(url)
self._update_kwdict(kwdict)
self._update_archive(kwdict)
self._update_content(url, kwdict)
self.format_filename(kwdict)
def handle_directory(self, kwdict):
self._update_kwdict(kwdict, False)
self.format_directory(kwdict)
def handle_metadata(self, kwdict):
pass
def handle_queue(self, url, kwdict):
self.queue = True
self._update_url(url)
self._update_kwdict(kwdict)
def _update_url(self, url):
self.url_list.append(url)
self.url_hash.update(url.encode())
def _update_kwdict(self, kwdict, to_list=True):
if to_list:
self.kwdict_list.append(kwdict.copy())
kwdict = util.filter_dict(kwdict)
self.kwdict_hash.update(
json.dumps(kwdict, sort_keys=True, default=str).encode())
def _update_archive(self, kwdict):
archive_id = self.format_archive(kwdict)
self.archive_list.append(archive_id)
self.archive_hash.update(archive_id.encode())
def _update_content(self, url, kwdict):
self.fileobj.kwdict = kwdict
downloader = self.get_downloader(url.partition(":")[0])
if downloader.download(url, self.fileobj):
return
for num, url in enumerate(kwdict.get("_fallback") or (), 1):
self.log.warning("Trying fallback URL #%d", num)
downloader = self.get_downloader(url.partition(":")[0])
if downloader.download(url, self.fileobj):
return
class TestPathfmt():
def __init__(self, hashobj):
self.hashobj = hashobj
self.path = ""
self.size = 0
self.kwdict = {}
self.extension = "jpg"
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
pass
def open(self, mode):
self.size = 0
return self
def write(self, content):
"""Update SHA1 hash"""
self.size += len(content)
self.hashobj.update(content)
def tell(self):
return self.size
def part_size(self):
return 0
class TestFormatter(formatter.StringFormatter):
@staticmethod
def _noop(_):
return ""
def _apply_simple(self, key, fmt):
if key == "extension" or "_parse_optional." in repr(fmt):
return self._noop
def wrap(obj):
return fmt(obj[key])
return wrap
def _apply(self, key, funcs, fmt):
if key == "extension" or "_parse_optional." in repr(fmt):
return self._noop
def wrap(obj):
obj = obj[key]
for func in funcs:
obj = func(obj)
return fmt(obj)
return wrap
def setup_test_config():
config._config.update(CONFIG)
def load_test_config():
try:
path = os.path.join(
os.path.dirname(os.path.dirname(__file__)),
"archive", "config.json")
with open(path) as fp:
CONFIG.update(json.loads(fp.read()))
except FileNotFoundError:
pass
except Exception as exc:
sys.exit("Error when loading {}: {}: {}".format(
path, exc.__class__.__name__, exc))
def result_categories(result):
categories = result.get("#category")
if categories:
return categories
cls = result["#class"]
return cls.basecategory, cls.category, cls.subcategory
def generate_tests():
"""Dynamically generate extractor unittests"""
def _generate_method(result):
def test(self):
print("\n" + result["#url"])
try:
self._run_test(result)
except KeyboardInterrupt as exc:
v = input("\n[e]xit | [f]ail | [S]kip ? ").strip().lower()
if v in ("e", "exit"):
raise
if v in ("f", "fail"):
self.fail("manual test failure")
else:
self._skipped.append((result["#url"], "manual skip"))
self.skipTest(exc)
return test
# enable selective testing for direct calls
if __name__ == "__main__" and len(sys.argv) > 1:
category, _, subcategory = sys.argv[1].partition(":")
del sys.argv[1:]
if category.startswith("+"):
basecategory = category[1:].lower()
tests = [t for t in results.all()
if result_categories(t)[0].lower() == basecategory]
else:
tests = results.category(category)
if subcategory:
if subcategory.startswith("+"):
url = subcategory[1:]
tests = [t for t in tests if url in t["#url"]]
elif subcategory.startswith("~"):
com = subcategory[1:]
tests = [t for t in tests
if "#comment" in t and com in t["#comment"].lower()]
else:
tests = [t for t in tests
if result_categories(t)[-1] == subcategory]
else:
tests = results.all()
# add 'test_...' methods
enum = collections.defaultdict(int)
for result in tests:
base, cat, sub = result_categories(result)
name = "{}_{}".format(cat, sub)
enum[name] += 1
method = _generate_method(result)
method.__doc__ = result["#url"]
method.__name__ = "test_{}_{}".format(name, enum[name])
setattr(TestExtractorResults, method.__name__, method)
generate_tests()
if __name__ == "__main__":
load_test_config()
unittest.main(warnings="ignore")