2015-12-12 15:58:07 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-02-09 21:51:35 +01:00
|
|
|
# Copyright 2015-2018 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.
|
|
|
|
|
2017-01-10 13:41:00 +01:00
|
|
|
import sys
|
2015-12-12 15:58:07 +01:00
|
|
|
import unittest
|
2017-11-12 20:51:12 +01:00
|
|
|
from gallery_dl import extractor, job, config, exception
|
2017-01-09 12:27:20 +01:00
|
|
|
|
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
|
|
|
SKIP = {
|
|
|
|
# don't work on travis-ci
|
|
|
|
"exhentai", "kissmanga", "mangafox", "dynastyscans", "nijie",
|
|
|
|
"archivedmoe", "archiveofsins", "thebarchive",
|
|
|
|
|
|
|
|
# temporary issues
|
2018-01-23 16:54:19 +01:00
|
|
|
"chronos",
|
|
|
|
"coreimg",
|
2018-02-09 21:51:35 +01:00
|
|
|
"hosturimage",
|
2018-02-03 23:14:32 +01:00
|
|
|
"yeet",
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-15 23:45:40 +01:00
|
|
|
class TestExtractors(unittest.TestCase):
|
2015-12-12 15:58:07 +01:00
|
|
|
|
2016-02-18 15:53:53 +01:00
|
|
|
def setUp(self):
|
2017-01-10 13:41:00 +01:00
|
|
|
name = "gallerydl"
|
|
|
|
email = "gallerydl@openaliasbox.org"
|
2016-03-08 18:01:35 +01:00
|
|
|
config.set(("cache", "file"), ":memory:")
|
2017-03-27 13:22:02 +02:00
|
|
|
config.set(("extractor", "username"), name)
|
|
|
|
config.set(("extractor", "password"), name)
|
2017-01-10 13:41:00 +01:00
|
|
|
config.set(("extractor", "nijie", "username"), email)
|
|
|
|
config.set(("extractor", "seiga", "username"), email)
|
2017-10-25 12:55:36 +02:00
|
|
|
config.set(("downloader", "part"), False)
|
2015-12-12 15:58:07 +01:00
|
|
|
|
2017-07-25 14:59:41 +02:00
|
|
|
def tearDown(self):
|
|
|
|
config.clear()
|
|
|
|
|
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)
|
|
|
|
content = "content" in result
|
|
|
|
else:
|
|
|
|
content = False
|
|
|
|
|
2017-10-25 12:55:36 +02:00
|
|
|
tjob = job.TestJob(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:
|
|
|
|
self.assertRaises(result["exception"], tjob.run)
|
|
|
|
return
|
2017-10-07 13:07:34 +02:00
|
|
|
|
2017-11-12 20:51:12 +01:00
|
|
|
try:
|
|
|
|
tjob.run()
|
|
|
|
except exception.HttpError as exc:
|
|
|
|
try:
|
|
|
|
if 500 <= exc.args[0].response.status_code < 600:
|
|
|
|
self.skipTest(exc)
|
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
|
|
|
except AttributeError:
|
2017-11-12 20:51:12 +01:00
|
|
|
pass
|
|
|
|
raise
|
|
|
|
|
2018-02-12 23:02:09 +01:00
|
|
|
# test archive-id uniqueness
|
|
|
|
self.assertEqual(len(set(tjob.list_archive)), len(tjob.list_archive))
|
|
|
|
|
|
|
|
# test extraction results
|
2015-12-13 03:56:29 +01:00
|
|
|
if "url" in result:
|
2017-08-25 22:01:14 +02:00
|
|
|
self.assertEqual(result["url"], tjob.hash_url.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:
|
|
|
|
self.assertEqual(result["content"], tjob.hash_content.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:
|
|
|
|
keyword = result["keyword"]
|
|
|
|
if isinstance(keyword, dict):
|
|
|
|
for kwdict in tjob.list_keyword:
|
|
|
|
self._test_kwdict(kwdict, keyword)
|
|
|
|
else: # assume SHA1 hash
|
|
|
|
self.assertEqual(keyword, tjob.hash_keyword.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+ *$")
|
|
|
|
expr = "{} {}".format(len(tjob.list_url), count)
|
|
|
|
self.assertTrue(eval(expr), msg=expr)
|
|
|
|
else: # assume integer
|
|
|
|
self.assertEqual(len(tjob.list_url), 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:
|
|
|
|
for url in tjob.list_url:
|
|
|
|
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):
|
|
|
|
self._test_kwdict(kwdict[key], test)
|
|
|
|
continue
|
|
|
|
elif isinstance(test, type):
|
|
|
|
self.assertIsInstance(value, test)
|
|
|
|
elif isinstance(test, str) and value.startswith("re:"):
|
|
|
|
self.assertRegex(value, test[3:])
|
|
|
|
else:
|
|
|
|
self.assertEqual(value, test)
|
|
|
|
|
|
|
|
|
|
|
|
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":
|
|
|
|
extractors = extractor.extractors()
|
|
|
|
else:
|
|
|
|
extractors = [
|
|
|
|
extr for extr in extractor.extractors()
|
|
|
|
if extr.category in sys.argv or
|
|
|
|
hasattr(extr, "basecategory") and extr.basecategory in sys.argv
|
|
|
|
]
|
|
|
|
del sys.argv[1:]
|
2017-07-02 08:15:12 +02:00
|
|
|
else:
|
|
|
|
extractors = [
|
|
|
|
extr for extr in extractor.extractors()
|
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 extr.category not in SKIP
|
2017-07-02 08:15:12 +02:00
|
|
|
]
|
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
|
|
|
for extr in extractors:
|
|
|
|
if not hasattr(extr, "test") or not extr.test:
|
|
|
|
continue
|
2017-01-09 12:27:20 +01:00
|
|
|
name = "test_" + extr.__name__ + "_"
|
|
|
|
for num, tcase in enumerate(extr.test, 1):
|
|
|
|
test = _generate_test(extr, tcase)
|
|
|
|
test.__name__ = name + str(num)
|
|
|
|
setattr(TestExtractors, test.__name__, 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
|
|
|
|
|
|
|
generate_tests()
|
2015-12-12 15:58:07 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(warnings='ignore')
|