1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 04:02:32 +01:00

[tests] make "#category" result entries optional

use categories of the given "#class"
when "#category" is not specified
This commit is contained in:
Mike Fährmann 2024-09-21 21:45:45 +02:00
parent 8bf081d295
commit 14c57f771c
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 34 additions and 12 deletions

View File

@ -105,15 +105,23 @@ class TestExtractorModule(unittest.TestCase):
def test_categories(self): def test_categories(self):
for result in results.all(): for result in results.all():
url = result["#url"] url = result["#url"]
base, cat, sub = result["#category"] cls = result["#class"]
try: try:
extr = result["#class"].from_url(url) extr = cls.from_url(url)
except ImportError as exc: except ImportError as exc:
if exc.name in ("youtube_dl", "yt_dlp"): if exc.name in ("youtube_dl", "yt_dlp"):
print("Skipping '{}' category checks".format(cat)) print("Skipping '{}' category checks".format(cls.category))
continue continue
raise raise
self.assertTrue(extr, url) self.assertTrue(extr, url)
categories = result.get("#category")
if categories:
base, cat, sub = categories
else:
cat = cls.category
sub = cls.subcategory
base = cls.basecategory
self.assertEqual(extr.category, cat, url) self.assertEqual(extr.category, cat, url)
self.assertEqual(extr.subcategory, sub, url) self.assertEqual(extr.subcategory, sub, url)
self.assertEqual(extr.basecategory, base, url) self.assertEqual(extr.basecategory, base, url)

View File

@ -43,7 +43,8 @@ CONFIG = {
}, },
} }
AUTH = {
AUTH_REQUIRED = {
"pixiv", "pixiv",
"nijie", "nijie",
"horne", "horne",
@ -54,7 +55,7 @@ AUTH = {
"twitter", "twitter",
} }
AUTH_CONFIG = ( AUTH_KEYS = (
"username", "username",
"cookies", "cookies",
"api-key", "api-key",
@ -91,24 +92,26 @@ class TestExtractorResults(unittest.TestCase):
self.assertGreaterEqual(value, range.start, msg=msg) self.assertGreaterEqual(value, range.start, msg=msg)
def _run_test(self, result): def _run_test(self, result):
base, cat, sub = result_categories(result)
result.pop("#comment", None) result.pop("#comment", None)
result.pop("#category", None)
auth = result.pop("#auth", None) auth = result.pop("#auth", None)
extractor.find(result["#url"]) extractor.find(result["#url"])
extr = result["#class"].from_url(result["#url"]) extr = result["#class"].from_url(result["#url"])
if not extr: if not extr:
raise exception.NoExtractorError() raise exception.NoExtractorError()
if len(result) <= 3: if len(result) <= 2:
return # only matching return # only matching
if auth is None: if auth is None:
auth = (result["#category"][1] in AUTH) auth = (cat in AUTH_REQUIRED)
elif not auth: elif not auth:
# auth explicitly disabled # auth explicitly disabled
for key in AUTH_CONFIG: for key in AUTH_KEYS:
config.set((), key, None) config.set((), key, None)
if auth and not any(extr.config(key) for key in AUTH_CONFIG): if auth and not any(extr.config(key) for key in AUTH_KEYS):
return self._skipped.append((result["#url"], "no auth")) return self._skipped.append((result["#url"], "no auth"))
if "#options" in result: if "#options" in result:
@ -411,6 +414,15 @@ def load_test_config():
path, exc.__class__.__name__, exc)) 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(): def generate_tests():
"""Dynamically generate extractor unittests""" """Dynamically generate extractor unittests"""
def _generate_method(result): def _generate_method(result):
@ -437,7 +449,7 @@ def generate_tests():
if category.startswith("+"): if category.startswith("+"):
basecategory = category[1:].lower() basecategory = category[1:].lower()
tests = [t for t in results.all() tests = [t for t in results.all()
if t["#category"][0].lower() == basecategory] if result_categories(t)[0].lower() == basecategory]
else: else:
tests = results.category(category) tests = results.category(category)
@ -450,14 +462,16 @@ def generate_tests():
tests = [t for t in tests tests = [t for t in tests
if "#comment" in t and com in t["#comment"].lower()] if "#comment" in t and com in t["#comment"].lower()]
else: else:
tests = [t for t in tests if t["#category"][-1] == subcategory] tests = [t for t in tests
if result_categories(t)[-1] == subcategory]
else: else:
tests = results.all() tests = results.all()
# add 'test_...' methods # add 'test_...' methods
enum = collections.defaultdict(int) enum = collections.defaultdict(int)
for result in tests: for result in tests:
name = "{1}_{2}".format(*result["#category"]) base, cat, sub = result_categories(result)
name = "{}_{}".format(cat, sub)
enum[name] += 1 enum[name] += 1
method = _generate_method(result) method = _generate_method(result)