2015-12-12 15:58:07 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-09 14:29:53 +01:00
|
|
|
# Copyright 2015-2019 Mike Fährmann
|
2015-12-12 15:58:07 +01:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2018-03-13 13:11:10 +01:00
|
|
|
import os
|
2017-01-10 13:41:00 +01:00
|
|
|
import sys
|
2018-07-19 18:47:23 +02:00
|
|
|
import re
|
2019-02-17 18:15:40 +01:00
|
|
|
import json
|
|
|
|
import hashlib
|
2015-12-12 15:58:07 +01:00
|
|
|
import unittest
|
2019-06-25 18:11:13 +02:00
|
|
|
from gallery_dl import extractor, util, job, config, exception
|
2017-01-09 12:27:20 +01:00
|
|
|
|
2015-12-12 15:58:07 +01:00
|
|
|
|
2018-08-15 20:41:53 +02:00
|
|
|
# these don't work on Travis CI
|
2018-03-13 13:11:10 +01:00
|
|
|
TRAVIS_SKIP = {
|
2019-01-09 14:29:53 +01:00
|
|
|
"exhentai", "kissmanga", "mangafox", "dynastyscans", "nijie", "bobx",
|
2019-03-05 22:33:37 +01:00
|
|
|
"archivedmoe", "archiveofsins", "thebarchive", "fireden", "4plebs",
|
2019-04-11 20:43:08 +02:00
|
|
|
"sankaku", "idolcomplex", "mangahere", "readcomiconline", "mangadex",
|
2019-09-28 20:48:15 +02:00
|
|
|
"sankakucomplex", "warosu", "fuskator",
|
2018-03-13 13:11:10 +01:00
|
|
|
}
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
|
2018-03-13 13:11:10 +01:00
|
|
|
# temporary issues, etc.
|
|
|
|
BROKEN = {
|
2019-11-08 21:44:12 +01:00
|
|
|
"hentaifox",
|
2019-02-19 13:30:39 +01:00
|
|
|
"mangapark",
|
2019-11-10 17:52:51 +01:00
|
|
|
"pixhost",
|
2019-11-08 20:58:53 +01:00
|
|
|
"yaplog",
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-19 17:57:32 +01:00
|
|
|
class TestExtractorResults(unittest.TestCase):
|
2015-12-12 15:58:07 +01:00
|
|
|
|
2016-02-18 15:53:53 +01:00
|
|
|
def setUp(self):
|
2018-11-15 14:24:18 +01:00
|
|
|
setup_test_config()
|
2015-12-12 15:58:07 +01:00
|
|
|
|
2017-07-25 14:59:41 +02:00
|
|
|
def tearDown(self):
|
|
|
|
config.clear()
|
|
|
|
|
2019-06-01 17:15:32 +02:00
|
|
|
@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))
|
|
|
|
|
2017-01-09 12:27:20 +01:00
|
|
|
def _run_test(self, extr, url, result):
|
2017-10-07 13:07:34 +02:00
|
|
|
if result:
|
|
|
|
if "options" in result:
|
|
|
|
for key, value in result["options"]:
|
|
|
|
config.set(key.split("."), value)
|
2018-08-15 20:41:53 +02:00
|
|
|
if "range" in result:
|
2018-10-08 23:30:06 +02:00
|
|
|
config.set(("image-range",), result["range"])
|
2018-11-25 18:54:05 +01:00
|
|
|
config.set(("chapter-range",), result["range"])
|
2017-10-07 13:07:34 +02:00
|
|
|
content = "content" in result
|
|
|
|
else:
|
|
|
|
content = False
|
|
|
|
|
2019-02-17 18:15:40 +01:00
|
|
|
tjob = ResultJob(url, content=content)
|
2017-02-26 02:06:56 +01:00
|
|
|
self.assertEqual(extr, tjob.extractor.__class__)
|
2017-10-07 13:07:34 +02:00
|
|
|
|
2017-06-13 23:10:42 +02:00
|
|
|
if not result:
|
|
|
|
return
|
2017-02-27 23:05:08 +01:00
|
|
|
if "exception" in result:
|
2019-05-13 11:48:20 +02:00
|
|
|
with self.assertRaises(result["exception"]):
|
|
|
|
tjob.run()
|
2017-02-27 23:05:08 +01:00
|
|
|
return
|
2017-11-12 20:51:12 +01:00
|
|
|
try:
|
|
|
|
tjob.run()
|
2018-08-15 20:41:53 +02:00
|
|
|
except exception.StopExtraction:
|
|
|
|
pass
|
2017-11-12 20:51:12 +01:00
|
|
|
except exception.HttpError as exc:
|
2019-06-01 17:15:32 +02:00
|
|
|
exc = str(exc)
|
|
|
|
if re.match(r"5\d\d: ", exc) or \
|
|
|
|
re.search(r"\bRead timed out\b", exc):
|
|
|
|
self._skipped.append((url, exc))
|
2018-07-19 18:47:23 +02:00
|
|
|
self.skipTest(exc)
|
2017-11-12 20:51:12 +01:00
|
|
|
raise
|
|
|
|
|
2019-11-10 17:03:38 +01:00
|
|
|
if result.get("archive", True):
|
|
|
|
self.assertEqual(
|
|
|
|
len(set(tjob.archive_list)),
|
|
|
|
len(tjob.archive_list),
|
|
|
|
"archive-id uniqueness",
|
|
|
|
)
|
2018-02-12 23:02:09 +01:00
|
|
|
|
2019-02-17 18:15:40 +01:00
|
|
|
if tjob.queue:
|
2019-08-14 12:28:21 +02:00
|
|
|
# test '_extractor' entries
|
2019-10-29 15:46:35 +01:00
|
|
|
for url, kwdict in zip(tjob.url_list, tjob.kwdict_list):
|
2019-02-17 18:15:40 +01:00
|
|
|
if "_extractor" in kwdict:
|
|
|
|
extr = kwdict["_extractor"].from_url(url)
|
|
|
|
self.assertIsInstance(extr, kwdict["_extractor"])
|
|
|
|
self.assertEqual(extr.url, url)
|
2019-08-14 12:28:21 +02:00
|
|
|
else:
|
|
|
|
# test 'extension' entries
|
2019-10-29 15:46:35 +01:00
|
|
|
for kwdict in tjob.kwdict_list:
|
2019-08-14 12:28:21 +02:00
|
|
|
self.assertIn("extension", kwdict)
|
2019-02-17 18:15:40 +01:00
|
|
|
|
2018-02-12 23:02:09 +01:00
|
|
|
# test extraction results
|
2015-12-13 03:56:29 +01:00
|
|
|
if "url" in result:
|
2019-10-29 15:46:35 +01:00
|
|
|
self.assertEqual(result["url"], tjob.url_hash.hexdigest())
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
|
2017-10-25 12:55:36 +02:00
|
|
|
if "content" in result:
|
2019-10-29 15:46:35 +01:00
|
|
|
self.assertEqual(result["content"], tjob.content_hash.hexdigest())
|
2015-12-12 15:58:07 +01:00
|
|
|
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
if "keyword" in result:
|
2019-10-29 15:46:35 +01:00
|
|
|
expected = result["keyword"]
|
|
|
|
if isinstance(expected, dict):
|
|
|
|
for kwdict in tjob.kwdict_list:
|
|
|
|
self._test_kwdict(kwdict, expected)
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
else: # assume SHA1 hash
|
2019-10-29 15:46:35 +01:00
|
|
|
self.assertEqual(expected, tjob.kwdict_hash.hexdigest())
|
2016-02-18 15:53:53 +01:00
|
|
|
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
if "count" in result:
|
|
|
|
count = result["count"]
|
|
|
|
if isinstance(count, str):
|
|
|
|
self.assertRegex(count, r"^ *(==|!=|<|<=|>|>=) *\d+ *$")
|
2019-10-29 15:46:35 +01:00
|
|
|
expr = "{} {}".format(len(tjob.url_list), count)
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
self.assertTrue(eval(expr), msg=expr)
|
|
|
|
else: # assume integer
|
2019-10-29 15:46:35 +01:00
|
|
|
self.assertEqual(len(tjob.url_list), count)
|
2016-02-18 15:53:53 +01:00
|
|
|
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
if "pattern" in result:
|
2019-10-29 15:46:35 +01:00
|
|
|
self.assertGreater(len(tjob.url_list), 0)
|
|
|
|
for url in tjob.url_list:
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
self.assertRegex(url, result["pattern"])
|
2017-01-30 19:40:15 +01:00
|
|
|
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
def _test_kwdict(self, kwdict, tests):
|
|
|
|
for key, test in tests.items():
|
|
|
|
if key.startswith("?"):
|
|
|
|
key = key[1:]
|
|
|
|
if key not in kwdict:
|
|
|
|
continue
|
|
|
|
self.assertIn(key, kwdict)
|
|
|
|
value = kwdict[key]
|
|
|
|
|
|
|
|
if isinstance(test, dict):
|
2019-01-01 15:39:34 +01:00
|
|
|
self._test_kwdict(value, test)
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
elif isinstance(test, type):
|
2019-01-01 15:39:34 +01:00
|
|
|
self.assertIsInstance(value, test, msg=key)
|
2019-04-29 17:27:59 +02:00
|
|
|
elif isinstance(test, str):
|
|
|
|
if test.startswith("re:"):
|
|
|
|
self.assertRegex(value, test[3:], msg=key)
|
|
|
|
elif test.startswith("type:"):
|
|
|
|
self.assertEqual(type(value).__name__, test[5:], msg=key)
|
|
|
|
else:
|
|
|
|
self.assertEqual(value, test, msg=key)
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
else:
|
2019-01-01 15:39:34 +01:00
|
|
|
self.assertEqual(value, test, msg=key)
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
|
|
|
|
|
2019-02-17 18:15:40 +01:00
|
|
|
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
|
2019-10-29 15:46:35 +01:00
|
|
|
|
|
|
|
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()
|
2019-06-29 15:39:52 +02:00
|
|
|
|
2019-02-17 18:15:40 +01:00
|
|
|
if content:
|
2019-10-29 15:46:35 +01:00
|
|
|
self.fileobj = TestPathfmt(self.content_hash)
|
2019-02-17 18:15:40 +01:00
|
|
|
|
2019-06-25 18:11:13 +02:00
|
|
|
self.format_directory = TestFormatter(
|
2019-10-29 15:46:35 +01:00
|
|
|
"".join(self.extractor.directory_fmt)).format_map
|
|
|
|
self.format_filename = TestFormatter(
|
|
|
|
self.extractor.filename_fmt).format_map
|
2019-06-25 18:11:13 +02:00
|
|
|
|
2019-02-17 18:15:40 +01:00
|
|
|
def run(self):
|
|
|
|
for msg in self.extractor:
|
|
|
|
self.dispatch(msg)
|
|
|
|
|
2019-10-29 15:46:35 +01:00
|
|
|
def handle_url(self, url, kwdict, fallback=None):
|
|
|
|
self._update_url(url)
|
|
|
|
self._update_kwdict(kwdict)
|
|
|
|
self._update_archive(kwdict)
|
|
|
|
self._update_content(url)
|
|
|
|
self.format_filename(kwdict)
|
2019-02-17 18:15:40 +01:00
|
|
|
|
2019-10-29 15:46:35 +01:00
|
|
|
def handle_directory(self, kwdict):
|
|
|
|
self._update_kwdict(kwdict, False)
|
|
|
|
self.format_directory(kwdict)
|
2019-02-17 18:15:40 +01:00
|
|
|
|
2019-10-29 15:46:35 +01:00
|
|
|
def handle_queue(self, url, kwdict):
|
2019-02-17 18:15:40 +01:00
|
|
|
self.queue = True
|
2019-10-29 15:46:35 +01:00
|
|
|
self._update_url(url)
|
|
|
|
self._update_kwdict(kwdict)
|
2019-02-17 18:15:40 +01:00
|
|
|
|
2019-10-29 15:46:35 +01:00
|
|
|
def _update_url(self, url):
|
|
|
|
self.url_list.append(url)
|
|
|
|
self.url_hash.update(url.encode())
|
2019-02-17 18:15:40 +01:00
|
|
|
|
2019-10-29 15:46:35 +01:00
|
|
|
def _update_kwdict(self, kwdict, to_list=True):
|
2019-02-17 18:15:40 +01:00
|
|
|
if to_list:
|
2019-10-29 15:46:35 +01:00
|
|
|
self.kwdict_list.append(kwdict.copy())
|
2019-02-17 18:15:40 +01:00
|
|
|
kwdict = self._filter(kwdict)
|
2019-10-29 15:46:35 +01:00
|
|
|
self.kwdict_hash.update(
|
2019-02-17 18:15:40 +01:00
|
|
|
json.dumps(kwdict, sort_keys=True, default=str).encode())
|
|
|
|
|
2019-10-29 15:46:35 +01:00
|
|
|
def _update_archive(self, kwdict):
|
2019-02-17 18:15:40 +01:00
|
|
|
archive_id = self.extractor.archive_fmt.format_map(kwdict)
|
2019-10-29 15:46:35 +01:00
|
|
|
self.archive_list.append(archive_id)
|
|
|
|
self.archive_hash.update(archive_id.encode())
|
2019-02-17 18:15:40 +01:00
|
|
|
|
2019-10-29 15:46:35 +01:00
|
|
|
def _update_content(self, url):
|
2019-02-17 18:15:40 +01:00
|
|
|
if self.content:
|
|
|
|
scheme = url.partition(":")[0]
|
|
|
|
self.get_downloader(scheme).download(url, self.fileobj)
|
|
|
|
|
|
|
|
|
2019-06-25 18:11:13 +02:00
|
|
|
class TestPathfmt():
|
2019-02-17 18:15:40 +01:00
|
|
|
|
|
|
|
def __init__(self, hashobj):
|
|
|
|
self.hashobj = hashobj
|
|
|
|
self.path = ""
|
|
|
|
self.size = 0
|
2019-08-12 21:40:37 +02:00
|
|
|
self.kwdict = {}
|
|
|
|
self.extension = "jpg"
|
2019-02-17 18:15:40 +01:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-06-25 18:11:13 +02:00
|
|
|
class TestFormatter(util.Formatter):
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _noop(_):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def _apply_simple(self, key, fmt):
|
|
|
|
if key == "extension" or "._format_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 "._format_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
|
|
|
|
|
|
|
|
|
2019-02-17 18:15:40 +01:00
|
|
|
def setup_test_config():
|
|
|
|
name = "gallerydl"
|
|
|
|
email = "gallerydl@openaliasbox.org"
|
|
|
|
|
|
|
|
config.clear()
|
|
|
|
config.set(("cache", "file"), ":memory:")
|
|
|
|
config.set(("downloader", "part"), False)
|
2019-08-07 22:52:29 +02:00
|
|
|
config.set(("downloader", "adjust-extensions"), False)
|
2019-02-17 18:15:40 +01:00
|
|
|
config.set(("extractor", "timeout"), 60)
|
|
|
|
config.set(("extractor", "username"), name)
|
|
|
|
config.set(("extractor", "password"), name)
|
2019-09-08 11:33:35 +02:00
|
|
|
config.set(("extractor", "nijie" , "username"), email)
|
|
|
|
config.set(("extractor", "seiga" , "username"), email)
|
2019-10-15 22:17:31 +02:00
|
|
|
|
2019-11-15 23:54:07 +01:00
|
|
|
config.set(("extractor", "danbooru" , "username"), None)
|
|
|
|
config.set(("extractor", "instagram" , "username"), None)
|
|
|
|
config.set(("extractor", "imgur" , "username"), None)
|
|
|
|
config.set(("extractor", "newgrounds", "username"), None)
|
|
|
|
config.set(("extractor", "twitter" , "username"), None)
|
2019-09-08 11:33:35 +02:00
|
|
|
|
|
|
|
config.set(("extractor", "mangoxo" , "username"), "LiQiang3")
|
|
|
|
config.set(("extractor", "mangoxo" , "password"), "5zbQF10_5u25259Ma")
|
2019-02-17 18:15:40 +01:00
|
|
|
|
|
|
|
config.set(("extractor", "deviantart", "client-id"), "7777")
|
|
|
|
config.set(("extractor", "deviantart", "client-secret"),
|
|
|
|
"ff14994c744d9208e5caeec7aab4a026")
|
|
|
|
|
|
|
|
config.set(("extractor", "tumblr", "api-key"),
|
|
|
|
"0cXoHfIqVzMQcc3HESZSNsVlulGxEXGDTTZCDrRrjaa0jmuTc6")
|
|
|
|
config.set(("extractor", "tumblr", "api-secret"),
|
|
|
|
"6wxAK2HwrXdedn7VIoZWxGqVhZ8JdYKDLjiQjL46MLqGuEtyVj")
|
|
|
|
config.set(("extractor", "tumblr", "access-token"),
|
|
|
|
"N613fPV6tOZQnyn0ERTuoEZn0mEqG8m2K8M3ClSJdEHZJuqFdG")
|
|
|
|
config.set(("extractor", "tumblr", "access-token-secret"),
|
|
|
|
"sgOA7ZTT4FBXdOGGVV331sSp0jHYp4yMDRslbhaQf7CaS71i4O")
|
|
|
|
|
|
|
|
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
def generate_tests():
|
|
|
|
"""Dynamically generate extractor unittests"""
|
|
|
|
def _generate_test(extr, tcase):
|
|
|
|
def test(self):
|
|
|
|
url, result = tcase
|
|
|
|
print("\n", url, sep="")
|
|
|
|
self._run_test(extr, url, result)
|
|
|
|
return test
|
|
|
|
|
|
|
|
# enable selective testing for direct calls
|
|
|
|
if __name__ == '__main__' and len(sys.argv) > 1:
|
|
|
|
if sys.argv[1].lower() == "all":
|
2018-03-13 13:11:10 +01:00
|
|
|
fltr = lambda c, bc: True # noqa: E731
|
|
|
|
elif sys.argv[1].lower() == "broken":
|
|
|
|
fltr = lambda c, bc: c in BROKEN # noqa: E731
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
else:
|
2018-03-13 13:11:10 +01:00
|
|
|
argv = sys.argv[1:]
|
|
|
|
fltr = lambda c, bc: c in argv or bc in argv # noqa: E731
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
del sys.argv[1:]
|
2017-07-02 08:15:12 +02:00
|
|
|
else:
|
2018-04-29 22:37:13 +02:00
|
|
|
skip = set(BROKEN)
|
2018-03-13 13:11:10 +01:00
|
|
|
if "CI" in os.environ and "TRAVIS" in os.environ:
|
2018-04-29 22:37:13 +02:00
|
|
|
skip |= set(TRAVIS_SKIP)
|
2019-02-17 18:15:40 +01:00
|
|
|
if skip:
|
|
|
|
print("skipping:", ", ".join(skip))
|
2018-03-13 13:11:10 +01:00
|
|
|
fltr = lambda c, bc: c not in skip # noqa: E731
|
|
|
|
|
|
|
|
# filter available extractor classes
|
|
|
|
extractors = [
|
|
|
|
extr for extr in extractor.extractors()
|
2019-02-17 18:15:40 +01:00
|
|
|
if fltr(extr.category, getattr(extr, "basecategory", None))
|
2018-03-13 13:11:10 +01:00
|
|
|
]
|
|
|
|
|
2018-03-19 17:57:32 +01:00
|
|
|
# add 'test_...' methods
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
for extr in extractors:
|
2017-01-09 12:27:20 +01:00
|
|
|
name = "test_" + extr.__name__ + "_"
|
2019-02-06 17:24:44 +01:00
|
|
|
for num, tcase in enumerate(extr._get_tests(), 1):
|
2017-01-09 12:27:20 +01:00
|
|
|
test = _generate_test(extr, tcase)
|
|
|
|
test.__name__ = name + str(num)
|
2018-03-19 17:57:32 +01:00
|
|
|
setattr(TestExtractorResults, test.__name__, test)
|
2017-01-09 12:27:20 +01:00
|
|
|
|
update extractor-unittest capabilities
- "count" can now be a string defining a comparison in the form of
'<operator> <value>', for example: '> 12' or '!= 1'. If its value
is not a string, it is assumed to be a concrete integer as before.
- "keyword" can now be a dictionary defining tests for individual keys.
These tests can either be a type, a concrete value or a regex
starting with "re:". Dictionaries can be stacked inside each other.
Optional keys can be indicated with a "?" before its name.
For example:
"keyword:" {
"image_id": int,
"gallery_id", 123,
"name": "re:pattern",
"user": {
"id": 321,
},
"?optional": None,
}
2017-12-30 19:05:37 +01:00
|
|
|
|
|
|
|
generate_tests()
|
2015-12-12 15:58:07 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(warnings='ignore')
|