[tests:results] split '_test_kwdict()'

This commit is contained in:
Mike Fährmann
2025-06-07 08:58:00 +02:00
parent 6e120f2551
commit fc01d85823

View File

@@ -235,56 +235,60 @@ class TestExtractorResults(unittest.TestCase):
def _test_kwdict(self, kwdict, tests, parent=None): def _test_kwdict(self, kwdict, tests, parent=None):
for key, test in tests.items(): for key, test in tests.items():
if key.startswith("?"): if key.startswith("?"):
key = key[1:] key = key[1:]
if key not in kwdict: if key not in kwdict:
continue continue
path = "{}.{}".format(parent, key) if parent else key path = "{}.{}".format(parent, key) if parent else key
if key.startswith("!"): if key.startswith("!"):
self.assertNotIn(key[1:], kwdict, msg=path) self.assertNotIn(key[1:], kwdict, msg=path)
continue continue
self.assertIn(key, kwdict, msg=path)
value = kwdict[key]
if isinstance(test, dict): self.assertIn(key, kwdict, msg=path)
self._test_kwdict(value, test, path) self._test_kwdict_value(kwdict[key], test, path)
elif isinstance(test, type):
self.assertIsInstance(value, test, msg=path) def _test_kwdict_value(self, value, test, path):
elif isinstance(test, range): if isinstance(test, dict):
self.assertRange(value, test, msg=path) self._test_kwdict(value, test, path)
elif isinstance(test, set): elif isinstance(test, type):
try: self.assertIsInstance(value, test, msg=path)
self.assertIn(value, test, msg=path) elif isinstance(test, range):
except AssertionError: self.assertRange(value, test, msg=path)
self.assertIn(type(value), test, msg=path) elif isinstance(test, set):
elif isinstance(test, list): try:
subtest = False self.assertIn(value, test, msg=path)
for idx, item in enumerate(test): except AssertionError:
if isinstance(item, dict): self.assertIn(type(value), test, msg=path)
subtest = True elif isinstance(test, list):
subpath = "{}[{}]".format(path, idx) subtest = False
self._test_kwdict(value[idx], item, subpath) for idx, item in enumerate(test):
if not subtest: if isinstance(item, dict):
self.assertEqual(test, value, msg=path) subtest = True
elif isinstance(test, str): subpath = "{}[{}]".format(path, idx)
if test.startswith("re:"): self._test_kwdict(value[idx], item, subpath)
self.assertRegex(value, test[3:], msg=path) if not subtest:
elif test.startswith("dt:"): self.assertEqual(test, value, msg=path)
self.assertIsInstance(value, datetime.datetime, msg=path) elif isinstance(test, str):
self.assertEqual(test[3:], str(value), msg=path) if test.startswith("re:"):
elif test.startswith("type:"): self.assertRegex(value, test[3:], msg=path)
self.assertEqual(test[5:], type(value).__name__, msg=path) elif test.startswith("dt:"):
elif test.startswith("len:"): self.assertIsInstance(value, datetime.datetime, msg=path)
cls, _, length = test[4:].rpartition(":") self.assertEqual(test[3:], str(value), msg=path)
if cls: elif test.startswith("type:"):
self.assertEqual( self.assertEqual(test[5:], type(value).__name__, msg=path)
cls, type(value).__name__, msg=path + "/type") elif test.startswith("len:"):
self.assertEqual(int(length), len(value), msg=path) cls, _, length = test[4:].rpartition(":")
else: if cls:
self.assertEqual(test, value, msg=path) self.assertEqual(
cls, type(value).__name__, msg=path + "/type")
self.assertEqual(int(length), len(value), msg=path)
else: else:
self.assertEqual(test, value, msg=path) self.assertEqual(test, value, msg=path)
else:
self.assertEqual(test, value, msg=path)
class ResultJob(job.DownloadJob): class ResultJob(job.DownloadJob):