aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2025-08-16 07:00:33 -0400
committerLibravatarUnit 193 <unit193@unit193.net>2025-08-16 07:00:33 -0400
commit3d18761f620a294ea6c5bff13c5994b93b29f3ed (patch)
tree092fa6f8128bc187512be532801670417f215986 /test
parenta6e995c093de8aae2e91a0787281bb34c0b871eb (diff)
New upstream version 1.30.3.upstream/1.30.3
Diffstat (limited to 'test')
-rw-r--r--test/test_config.py2
-rw-r--r--test/test_cookies.py8
-rw-r--r--test/test_downloader.py28
-rw-r--r--test/test_extractor.py21
-rw-r--r--test/test_formatter.py53
-rw-r--r--test/test_job.py6
-rw-r--r--test/test_postprocessor.py89
-rw-r--r--test/test_results.py88
-rw-r--r--test/test_util.py4
-rw-r--r--test/test_ytdl.py4
10 files changed, 176 insertions, 127 deletions
diff --git a/test/test_config.py b/test/test_config.py
index 5c94b1b..064d8e7 100644
--- a/test/test_config.py
+++ b/test/test_config.py
@@ -229,7 +229,7 @@ class TestConfigFiles(unittest.TestCase):
with open(path) as fp:
return util.json_loads(fp.read())
except FileNotFoundError:
- raise unittest.SkipTest(path + " not available")
+ raise unittest.SkipTest(f"{path} not available")
if __name__ == "__main__":
diff --git a/test/test_cookies.py b/test/test_cookies.py
index 5900473..9721d10 100644
--- a/test/test_cookies.py
+++ b/test/test_cookies.py
@@ -91,7 +91,7 @@ class TestCookiedict(unittest.TestCase):
self.assertEqual(sorted(cookies.values()), sorted(self.cdict.values()))
def test_domain(self):
- for category in ["exhentai", "idolcomplex", "nijie", "horne"]:
+ for category in ["exhentai", "nijie", "horne"]:
extr = _get_extractor(category)
cookies = extr.cookies
for key in self.cdict:
@@ -108,7 +108,6 @@ class TestCookieLogin(unittest.TestCase):
def test_cookie_login(self):
extr_cookies = {
"exhentai" : ("ipb_member_id", "ipb_pass_hash"),
- "idolcomplex": ("login", "pass_hash"),
"nijie" : ("nijie_tok",),
"horne" : ("horne_tok",),
}
@@ -159,7 +158,7 @@ class TestCookieUtils(unittest.TestCase):
extr.cookies.set("cd_a", "1", domain=extr.cookies_domain)
self.assertTrue(extr.cookies_check(("cd_a",)))
- extr.cookies.set("wd_a", "1", domain="www" + extr.cookies_domain)
+ extr.cookies.set("wd_a", "1", domain=f"www{extr.cookies_domain}")
self.assertFalse(extr.cookies_check(("wd_a",)))
self.assertEqual(len(extr.cookies), 3)
@@ -184,7 +183,7 @@ class TestCookieUtils(unittest.TestCase):
extr.cookies.set("cd_a", "1", domain=extr.cookies_domain)
self.assertTrue(extr.cookies_check(("cd_a",), subdomains=True))
- extr.cookies.set("wd_a", "1", domain="www" + extr.cookies_domain)
+ extr.cookies.set("wd_a", "1", domain=f"www{extr.cookies_domain}")
self.assertTrue(extr.cookies_check(("wd_a",), subdomains=True))
extr.cookies.set("cd_b", "2", domain=extr.cookies_domain)
@@ -244,7 +243,6 @@ def _get_extractor(category):
URLS = {
"exhentai" : "https://exhentai.org/g/1200119/d55c44d3d0/",
- "idolcomplex": "https://idol.sankakucomplex.com/post/show/1",
"nijie" : "https://nijie.info/view.php?id=1",
"horne" : "https://horne.red/view.php?id=1",
"test" : "generic:https://example.org/",
diff --git a/test/test_downloader.py b/test/test_downloader.py
index 3e5bf84..ecd8b85 100644
--- a/test/test_downloader.py
+++ b/test/test_downloader.py
@@ -183,7 +183,7 @@ class TestDownloaderBase(unittest.TestCase):
@classmethod
def _prepare_destination(cls, content=None, part=True, extension=None):
- name = "file-{}".format(cls.fnum)
+ name = f"file-{cls.fnum}"
cls.fnum += 1
kwdict = {
@@ -199,7 +199,7 @@ class TestDownloaderBase(unittest.TestCase):
pathfmt.build_path()
if content:
- mode = "w" + ("b" if isinstance(content, bytes) else "")
+ mode = "wb" if isinstance(content, bytes) else "w"
with pathfmt.open(mode) as fp:
fp.write(content)
@@ -211,10 +211,10 @@ class TestDownloaderBase(unittest.TestCase):
success = self.downloader.download(url, pathfmt)
# test successful download
- self.assertTrue(success, "downloading '{}' failed".format(url))
+ self.assertTrue(success, f"downloading '{url}' failed")
# test content
- mode = "r" + ("b" if isinstance(output, bytes) else "")
+ mode = "rb" if isinstance(output, bytes) else "r"
with pathfmt.open(mode) as fp:
content = fp.read()
self.assertEqual(content, output)
@@ -245,16 +245,16 @@ class TestHTTPDownloader(TestDownloaderBase):
server = http.server.HTTPServer((host, port), HttpRequestHandler)
except OSError as exc:
raise unittest.SkipTest(
- "cannot spawn local HTTP server ({})".format(exc))
+ f"cannot spawn local HTTP server ({exc})")
host, port = server.server_address
- cls.address = "http://{}:{}".format(host, port)
+ cls.address = f"http://{host}:{port}"
threading.Thread(target=server.serve_forever, daemon=True).start()
def _run_test(self, ext, input, output,
extension, expected_extension=None):
TestDownloaderBase._run_test(
- self, self.address + "/" + ext, input, output,
+ self, f"{self.address}/{ext}", input, output,
extension, expected_extension)
def tearDown(self):
@@ -281,7 +281,7 @@ class TestHTTPDownloader(TestDownloaderBase):
self._run_test("gif", None, DATA["gif"], "jpg", "gif")
def test_http_filesize_min(self):
- url = self.address + "/gif"
+ url = f"{self.address}/gif"
pathfmt = self._prepare_destination(None, extension=None)
self.downloader.minsize = 100
with self.assertLogs(self.downloader.log, "WARNING"):
@@ -290,7 +290,7 @@ class TestHTTPDownloader(TestDownloaderBase):
self.assertEqual(pathfmt.temppath, "")
def test_http_filesize_max(self):
- url = self.address + "/jpg"
+ url = f"{self.address}/jpg"
pathfmt = self._prepare_destination(None, extension=None)
self.downloader.maxsize = 100
with self.assertLogs(self.downloader.log, "WARNING"):
@@ -334,8 +334,8 @@ class HttpRequestHandler(http.server.BaseHTTPRequestHandler):
match = re.match(r"bytes=(\d+)-", self.headers["Range"])
start = int(match[1])
- headers["Content-Range"] = "bytes {}-{}/{}".format(
- start, len(output)-1, len(output))
+ headers["Content-Range"] = \
+ f"bytes {start}-{len(output) - 1}/{len(output)}"
output = output[start:]
else:
status = 200
@@ -408,7 +408,7 @@ for ext, content in SAMPLES:
DATA[ext] = content
for idx, (_, content) in enumerate(SAMPLES):
- DATA["S{:>02}".format(idx)] = content
+ DATA[f"S{idx:>02}"] = content
# reverse mime types mapping
@@ -421,8 +421,8 @@ MIME_TYPES = {
def generate_tests():
def generate_test(idx, ext, content):
def test(self):
- self._run_test("S{:>02}".format(idx), None, content, "bin", ext)
- test.__name__ = "test_http_ext_{:>02}_{}".format(idx, ext)
+ self._run_test(f"S{idx:>02}", None, content, "bin", ext)
+ test.__name__ = f"test_http_ext_{idx:>02}_{ext}"
return test
for idx, (ext, content) in enumerate(SAMPLES):
diff --git a/test/test_extractor.py b/test/test_extractor.py
index bf4aa07..f8b8f09 100644
--- a/test/test_extractor.py
+++ b/test/test_extractor.py
@@ -110,7 +110,7 @@ class TestExtractorModule(unittest.TestCase):
except AssertionError:
pass
else:
- self.fail(result["#url"] + ": Test did not fail")
+ self.fail(f"{result['#url']}: Test did not fail")
else:
self.assertCategories(result)
@@ -167,8 +167,7 @@ class TestExtractorModule(unittest.TestCase):
extr.finalize()
except ImportError as exc:
if exc.name in ("youtube_dl", "yt_dlp"):
- raise unittest.SkipTest("cannot import module '{}'".format(
- exc.name))
+ raise unittest.SkipTest(f"cannot import module '{exc.name}'")
raise
def test_docstrings(self):
@@ -179,7 +178,7 @@ class TestExtractorModule(unittest.TestCase):
self.assertNotEqual(
extr1.__doc__,
extr2.__doc__,
- "{} <-> {}".format(extr1, extr2),
+ f"{extr1} <-> {extr2}",
)
def test_names(self):
@@ -191,12 +190,10 @@ class TestExtractorModule(unittest.TestCase):
for extr in extractor.extractors():
if extr.category not in ("", "oauth", "ytdl"):
- expected = "{}{}Extractor".format(
- capitalize(extr.category),
- capitalize(extr.subcategory),
- )
+ expected = (f"{capitalize(extr.category)}"
+ f"{capitalize(extr.subcategory)}Extractor")
if expected[0].isdigit():
- expected = "_" + expected
+ expected = f"_{expected}"
self.assertEqual(expected, extr.__name__)
@@ -225,7 +222,7 @@ class TestExtractorWait(unittest.TestCase):
calls = sleep.mock_calls
self.assertEqual(len(calls), 1)
- self.assertAlmostEqual(calls[0][1][0], 6.0, places=1)
+ self.assertAlmostEqual(calls[0][1][0], 6.0, places=0)
calls = log.info.mock_calls
self.assertEqual(len(calls), 1)
@@ -266,7 +263,7 @@ class TextExtractorOAuth(unittest.TestCase):
def test_oauth1(self):
for category in ("flickr", "smugmug", "tumblr"):
- extr = extractor.find("oauth:" + category)
+ extr = extractor.find(f"oauth:{category}")
with patch.object(extr, "_oauth1_authorization_flow") as m:
for msg in extr:
@@ -275,7 +272,7 @@ class TextExtractorOAuth(unittest.TestCase):
def test_oauth2(self):
for category in ("deviantart", "reddit"):
- extr = extractor.find("oauth:" + category)
+ extr = extractor.find(f"oauth:{category}")
with patch.object(extr, "_oauth2_authorization_code_grant") as m:
for msg in extr:
diff --git a/test/test_formatter.py b/test/test_formatter.py
index 3305983..f3ed9dd 100644
--- a/test/test_formatter.py
+++ b/test/test_formatter.py
@@ -73,8 +73,8 @@ class TestFormatter(unittest.TestCase):
self._run_test("{u!H}", "'< / >'")
self._run_test("{n!H}", "")
self._run_test("{a!s}", self.kwdict["a"])
- self._run_test("{a!r}", "'" + self.kwdict["a"] + "'")
- self._run_test("{a!a}", "'" + self.kwdict["a"] + "'")
+ self._run_test("{a!r}", f"'{self.kwdict['a']}'")
+ self._run_test("{a!a}", f"'{self.kwdict['a']}'")
self._run_test("{b!a}", "'\\xe4\\xf6\\xfc'")
self._run_test("{a!S}", self.kwdict["a"])
self._run_test("{l!S}", "a, b, c")
@@ -139,7 +139,7 @@ class TestFormatter(unittest.TestCase):
self._run_test("{missing}" , replacement, default)
self._run_test("{missing.attr}", replacement, default)
self._run_test("{missing[key]}", replacement, default)
- self._run_test("{missing:?a//}", "a" + default, default)
+ self._run_test("{missing:?a//}", f"a{default}", default)
def test_fmt_func(self):
self._run_test("{t}" , self.kwdict["t"] , None, int)
@@ -444,11 +444,11 @@ class TestFormatter(unittest.TestCase):
with open(path1, "w") as fp:
fp.write("{a}")
- fmt1 = formatter.parse("\fT " + path1)
+ fmt1 = formatter.parse(f"\fT {path1}")
with open(path2, "w") as fp:
fp.write("{a!u:Rh/C/}\nFooBar")
- fmt2 = formatter.parse("\fT " + path2)
+ fmt2 = formatter.parse(f"\fT {path2}")
self.assertEqual(fmt1.format_map(self.kwdict), self.kwdict["a"])
self.assertEqual(fmt2.format_map(self.kwdict), "HELLO WORLD\nFooBar")
@@ -458,15 +458,18 @@ class TestFormatter(unittest.TestCase):
def test_expression(self):
self._run_test("\fE a", self.kwdict["a"])
- self._run_test("\fE name * 2 + ' ' + a", "{}{} {}".format(
- self.kwdict["name"], self.kwdict["name"], self.kwdict["a"]))
+ self._run_test(
+ "\fE name * 2 + ' ' + a",
+ f"{self.kwdict['name']}{self.kwdict['name']} {self.kwdict['a']}")
def test_fstring(self):
self._run_test("\fF {a}", self.kwdict["a"])
- self._run_test("\fF {name}{name} {a}", "{}{} {}".format(
- self.kwdict["name"], self.kwdict["name"], self.kwdict["a"]))
- self._run_test("\fF foo-'\"{a.upper()}\"'-bar",
- """foo-'"{}"'-bar""".format(self.kwdict["a"].upper()))
+ self._run_test(
+ "\fF {name}{name} {a}",
+ f"{self.kwdict['name']}{self.kwdict['name']} {self.kwdict['a']}")
+ self._run_test(
+ "\fF foo-'\"{a.upper()}\"'-bar",
+ f"""foo-'"{self.kwdict['a'].upper()}"'-bar""")
def test_template_fstring(self):
with tempfile.TemporaryDirectory() as tmpdirname:
@@ -475,15 +478,15 @@ class TestFormatter(unittest.TestCase):
with open(path1, "w") as fp:
fp.write("{a}")
- fmt1 = formatter.parse("\fTF " + path1)
+ fmt1 = formatter.parse(f"\fTF {path1}")
with open(path2, "w") as fp:
fp.write("foo-'\"{a.upper()}\"'-bar")
- fmt2 = formatter.parse("\fTF " + path2)
+ fmt2 = formatter.parse(f"\fTF {path2}")
self.assertEqual(fmt1.format_map(self.kwdict), self.kwdict["a"])
self.assertEqual(fmt2.format_map(self.kwdict),
- """foo-'"{}"'-bar""".format(self.kwdict["a"].upper()))
+ f"""foo-'"{self.kwdict['a'].upper()}"'-bar""")
with self.assertRaises(OSError):
formatter.parse("\fTF /")
@@ -493,10 +496,12 @@ class TestFormatter(unittest.TestCase):
formatter.JinjaFormatter.env = None
self._run_test("\fJ {{a}}", self.kwdict["a"])
- self._run_test("\fJ {{name}}{{name}} {{a}}", "{}{} {}".format(
- self.kwdict["name"], self.kwdict["name"], self.kwdict["a"]))
- self._run_test("\fJ foo-'\"{{a | upper}}\"'-bar",
- """foo-'"{}"'-bar""".format(self.kwdict["a"].upper()))
+ self._run_test(
+ "\fJ {{name}}{{name}} {{a}}",
+ f"{self.kwdict['name']}{self.kwdict['name']} {self.kwdict['a']}")
+ self._run_test(
+ "\fJ foo-'\"{{a | upper}}\"'-bar",
+ f"""foo-'"{self.kwdict['a'].upper()}"'-bar""")
@unittest.skipIf(jinja2 is None, "no jinja2")
def test_template_jinja(self):
@@ -508,15 +513,15 @@ class TestFormatter(unittest.TestCase):
with open(path1, "w") as fp:
fp.write("{{a}}")
- fmt1 = formatter.parse("\fTJ " + path1)
+ fmt1 = formatter.parse(f"\fTJ {path1}")
with open(path2, "w") as fp:
fp.write("foo-'\"{{a | upper}}\"'-bar")
- fmt2 = formatter.parse("\fTJ " + path2)
+ fmt2 = formatter.parse(f"\fTJ {path2}")
self.assertEqual(fmt1.format_map(self.kwdict), self.kwdict["a"])
self.assertEqual(fmt2.format_map(self.kwdict),
- """foo-'"{}"'-bar""".format(self.kwdict["a"].upper()))
+ f"""foo-'"{self.kwdict['a'].upper()}"'-bar""")
with self.assertRaises(OSError):
formatter.parse("\fTJ /")
@@ -562,7 +567,7 @@ Present Time is ((( dt | dt_fmt("%H:%M:%S") )))
Hello ((( s | sanitize_whitespace ))).
I hope there is enough "(((S|sanitize_whitespace)))" for you.
""")
- fmt = formatter.parse("\fTJ " + path_template)
+ fmt = formatter.parse(f"\fTJ {path_template}")
self.assertEqual(fmt.format_map(self.kwdict), """\
Present Day is January 01, 2010
@@ -607,8 +612,8 @@ def noarg():
finally:
sys.path.pop(0)
- fmt3 = formatter.parse("\fM " + path + ":gentext")
- fmt4 = formatter.parse("\fM " + path + ":lengths")
+ fmt3 = formatter.parse(f"\fM {path}:gentext")
+ fmt4 = formatter.parse(f"\fM {path}:lengths")
self.assertEqual(fmt1.format_map(self.kwdict), "'Title' by Name")
self.assertEqual(fmt2.format_map(self.kwdict), "168")
diff --git a/test/test_job.py b/test/test_job.py
index 3aa28e8..0a533ea 100644
--- a/test/test_job.py
+++ b/test/test_job.py
@@ -299,7 +299,7 @@ class TestDataJob(TestJob):
for i in range(1, 4):
self.assertEqual(
tjob.data[i][2]["_fallback"],
- ("https://example.org/alt/{}.jpg".format(i),),
+ (f"https://example.org/alt/{i}.jpg",),
)
def test_sleep(self):
@@ -382,13 +382,13 @@ class TestExtractor(Extractor):
}
for i in range(1, 4):
- url = "{}/{}.jpg".format(root, i)
+ url = f"{root}/{i}.jpg"
yield Message.Url, url, text.nameext_from_url(url, {
"num" : i,
"tags": ["foo", "bar", "ใƒ†ใ‚นใƒˆ"],
"user": user,
"author": user,
- "_fallback": ("{}/alt/{}.jpg".format(root, i),),
+ "_fallback": (f"{root}/alt/{i}.jpg",),
})
diff --git a/test/test_postprocessor.py b/test/test_postprocessor.py
index 2e39cc7..07bd348 100644
--- a/test/test_postprocessor.py
+++ b/test/test_postprocessor.py
@@ -52,7 +52,7 @@ class TestPostprocessorModule(unittest.TestCase):
def test_find(self):
for name in (postprocessor.modules):
cls = postprocessor.find(name)
- self.assertEqual(cls.__name__, name.capitalize() + "PP")
+ self.assertEqual(cls.__name__, f"{name.capitalize()}PP")
self.assertIs(cls.__base__, PostProcessor)
self.assertEqual(postprocessor.find("foo"), None)
@@ -129,15 +129,15 @@ class ClassifyTest(BasePostprocessorTest):
self._trigger(("prepare",))
self.pathfmt.build_path()
path = os.path.join(self.dir.name, "test", "Pictures")
- self.assertEqual(self.pathfmt.path, path + "/file.jpg")
- self.assertEqual(self.pathfmt.realpath, path + "/file.jpg")
+ self.assertEqual(self.pathfmt.path, f"{path}/file.jpg")
+ self.assertEqual(self.pathfmt.realpath, f"{path}/file.jpg")
self.pathfmt.set_extension("mp4")
self._trigger(("prepare",))
self.pathfmt.build_path()
path = os.path.join(self.dir.name, "test", "Video")
- self.assertEqual(self.pathfmt.path, path + "/file.mp4")
- self.assertEqual(self.pathfmt.realpath, path + "/file.mp4")
+ self.assertEqual(self.pathfmt.path, f"{path}/file.mp4")
+ self.assertEqual(self.pathfmt.realpath, f"{path}/file.mp4")
def test_classify_noop(self):
pp = self._create()
@@ -169,8 +169,8 @@ class ClassifyTest(BasePostprocessorTest):
self._trigger(("prepare",))
self.pathfmt.build_path()
path = os.path.join(self.dir.name, "test", "foo", "bar")
- self.assertEqual(self.pathfmt.path, path + "/file.foo")
- self.assertEqual(self.pathfmt.realpath, path + "/file.foo")
+ self.assertEqual(self.pathfmt.path, f"{path}/file.foo")
+ self.assertEqual(self.pathfmt.realpath, f"{path}/file.foo")
class DirectoryTest(BasePostprocessorTest):
@@ -179,16 +179,16 @@ class DirectoryTest(BasePostprocessorTest):
self._create()
path = os.path.join(self.dir.name, "test")
- self.assertEqual(self.pathfmt.realdirectory, path + "/")
- self.assertEqual(self.pathfmt.realpath, path + "/file.ext")
+ self.assertEqual(self.pathfmt.realdirectory, f"{path}/")
+ self.assertEqual(self.pathfmt.realpath, f"{path}/file.ext")
self.pathfmt.kwdict["category"] = "custom"
self._trigger()
path = os.path.join(self.dir.name, "custom")
- self.assertEqual(self.pathfmt.realdirectory, path + "/")
+ self.assertEqual(self.pathfmt.realdirectory, f"{path}/")
self.pathfmt.build_path()
- self.assertEqual(self.pathfmt.realpath, path + "/file.ext")
+ self.assertEqual(self.pathfmt.realpath, f"{path}/file.ext")
class ExecTest(BasePostprocessorTest):
@@ -205,10 +205,12 @@ class ExecTest(BasePostprocessorTest):
self._trigger(("after",))
p.assert_called_once_with(
- "echo {0} {0} {1} {2} && rm {0};".format(
- self.pathfmt.realpath,
- self.pathfmt.realdirectory,
- self.pathfmt.filename),
+ (f"echo "
+ f"{self.pathfmt.realpath} "
+ f"{self.pathfmt.realpath} "
+ f"{self.pathfmt.realdirectory} "
+ f"{self.pathfmt.filename} "
+ f"&& rm {self.pathfmt.realpath};"),
shell=True,
creationflags=0,
start_new_session=False,
@@ -254,10 +256,12 @@ class ExecTest(BasePostprocessorTest):
self.assertEqual(p.call_args_list, [
call(
- "echo {0} {0} {1} {2} && rm {0};".format(
- self.pathfmt.realpath,
- self.pathfmt.realdirectory,
- self.pathfmt.filename),
+ (f"echo "
+ f"{self.pathfmt.realpath} "
+ f"{self.pathfmt.realpath} "
+ f"{self.pathfmt.realdirectory} "
+ f"{self.pathfmt.filename} "
+ f"&& rm {self.pathfmt.realpath};"),
shell=True,
creationflags=0,
start_new_session=False,
@@ -287,8 +291,9 @@ class ExecTest(BasePostprocessorTest):
with self.assertLogs() as log:
self._trigger(("after",))
- msg = ("WARNING:postprocessor.exec:'echo {}' returned with "
- "non-zero exit status (123)".format(self.pathfmt.realpath))
+ msg = (f"WARNING:postprocessor.exec:"
+ f"'echo {self.pathfmt.realpath}' "
+ f"returned with non-zero exit status (123)")
self.assertEqual(log.output[0], msg)
def test_async(self):
@@ -426,7 +431,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
- path = self.pathfmt.realpath + ".JSON"
+ path = f"{self.pathfmt.realpath}.JSON"
m.assert_called_once_with(path, "w", encoding="utf-8")
self.assertEqual(self._output(m), """{
@@ -460,7 +465,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
- path = self.pathfmt.realpath + ".JSON"
+ path = f"{self.pathfmt.realpath}.JSON"
m.assert_called_once_with(path, "a", encoding="UTF-8")
self.assertEqual(self._output(m), """{\
"_private" : "foo \\u30d0\\u30fc",\
@@ -481,7 +486,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
- path = self.pathfmt.realpath + ".txt"
+ path = f"{self.pathfmt.realpath}.txt"
m.assert_called_once_with(path, "w", encoding="utf-8")
self.assertEqual(self._output(m), "foo\nbar\nbaz\n")
@@ -561,7 +566,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
- path = self.pathfmt.realdirectory + "file.json"
+ path = f"{self.pathfmt.realdirectory}file.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_extfmt_2(self):
@@ -573,7 +578,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
- path = self.pathfmt.realdirectory + "file.2.EXT-data:tESt"
+ path = f"{self.pathfmt.realdirectory}file.2.EXT-data:tESt"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_directory(self):
@@ -584,7 +589,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
- path = self.pathfmt.realdirectory + "metadata/file.ext.json"
+ path = f"{self.pathfmt.realdirectory}metadata/file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_directory_2(self):
@@ -596,7 +601,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
- path = self.pathfmt.realdirectory + "metadata/file.json"
+ path = f"{self.pathfmt.realdirectory}metadata/file.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_directory_format(self):
@@ -608,7 +613,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
- path = self.pathfmt.realdirectory + "../json/12500/file.ext.json"
+ path = f"{self.pathfmt.realdirectory}../json/12500/file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_directory_empty(self):
@@ -619,7 +624,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
- path = self.pathfmt.realdirectory + "./file.ext.json"
+ path = f"{self.pathfmt.realdirectory}./file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_basedirectory(self):
@@ -628,7 +633,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
- path = self.pathfmt.basedirectory + "file.ext.json"
+ path = f"{self.pathfmt.basedirectory}file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_basedirectory_custom(self):
@@ -652,7 +657,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
- path = self.pathfmt.realdirectory + "test_file__meta_.data"
+ path = f"{self.pathfmt.realdirectory}test_file__meta_.data"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_meta_path(self):
@@ -663,7 +668,7 @@ class MetadataTest(BasePostprocessorTest):
self._trigger()
self.assertEqual(self.pathfmt.kwdict["_meta_path"],
- self.pathfmt.realpath + ".json")
+ f"{self.pathfmt.realpath}.json")
def test_metadata_stdout(self):
self._create({"filename": "-", "indent": None, "sort": True})
@@ -752,7 +757,7 @@ class MetadataTest(BasePostprocessorTest):
self.assertTrue(m.called)
self.assertGreater(len(self._output(m)), 0)
- path = self.pathfmt.realdirectory + "file.ext.json"
+ path = f"{self.pathfmt.realdirectory}file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_option_skip_false(self):
@@ -856,7 +861,7 @@ class PythonTest(BasePostprocessorTest):
path = os.path.join(self.dir.name, "module.py")
self._write_module(path)
- self._create({"function": path + ":calc"}, {"_value": 12})
+ self._create({"function": f"{path}:calc"}, {"_value": 12})
self.assertNotIn("_result", self.pathfmt.kwdict)
self._trigger()
@@ -913,7 +918,7 @@ class RenameTest(BasePostprocessorTest):
def test_rename_skip(self):
self._create({"from": "{id}.{extension}"}, {"id": 12345})
path = self._prepare("12345.ext")
- with open(path + "file.ext", "w"):
+ with open(f"{path}file.ext", "w"):
pass
with self.assertLogs("postprocessor.rename", level="WARNING") as cm:
@@ -932,7 +937,7 @@ class ZipTest(BasePostprocessorTest):
self.assertEqual(pp.path, self.pathfmt.realdirectory[:-1])
self.assertEqual(pp.delete, True)
self.assertEqual(pp.args, (
- pp.path + ".zip", "a", zipfile.ZIP_STORED, True,
+ f"{pp.path}.zip", "a", zipfile.ZIP_STORED, True,
))
self.assertTrue(pp.args[0].endswith("/test.zip"))
@@ -942,7 +947,7 @@ class ZipTest(BasePostprocessorTest):
self.assertEqual(pp.path, self.pathfmt.realdirectory[:-1])
self.assertEqual(pp.delete, True)
self.assertEqual(pp.args, (
- pp.path + ".zip", "a", zipfile.ZIP_STORED, True,
+ f"{pp.path}.zip", "a", zipfile.ZIP_STORED, True,
))
self.assertTrue(pp.args[0].endswith("/test.zip"))
@@ -954,7 +959,7 @@ class ZipTest(BasePostprocessorTest):
})
self.assertEqual(pp.delete, False)
self.assertEqual(pp.args, (
- pp.path + ".cbz", "a", zipfile.ZIP_DEFLATED, True,
+ f"{pp.path}.cbz", "a", zipfile.ZIP_DEFLATED, True,
))
self.assertTrue(pp.args[0].endswith("/test.cbz"))
@@ -968,7 +973,7 @@ class ZipTest(BasePostprocessorTest):
# write dummy file with 3 different names
for i in range(3):
- name = "file{}.ext".format(i)
+ name = f"file{i}.ext"
self.pathfmt.temppath = file.name
self.pathfmt.filename = name
@@ -1015,8 +1020,8 @@ class ZipTest(BasePostprocessorTest):
# write 3 files
for i in range(3):
- self.pathfmt.temppath = self.pathfmt.realdirectory + "file.ext"
- self.pathfmt.filename = "file{}.ext".format(i)
+ self.pathfmt.temppath = f"{self.pathfmt.realdirectory}file.ext"
+ self.pathfmt.filename = f"file{i}.ext"
self._trigger()
# write the last file a second time (should be skipped)
diff --git a/test/test_results.py b/test/test_results.py
index 4b1c4c1..05b98bf 100644
--- a/test/test_results.py
+++ b/test/test_results.py
@@ -145,7 +145,8 @@ class TestExtractorResults(unittest.TestCase):
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"))
+ self._skipped.append((result["#url"], "no auth"))
+ self.skipTest("no auth")
if "#options" in result:
for key, value in result["#options"].items():
@@ -155,11 +156,16 @@ class TestExtractorResults(unittest.TestCase):
config.set((), "image-range" , result["#range"])
config.set((), "chapter-range", result["#range"])
- tjob = ResultJob(extr, content=("#sha1_content" in result))
+ tjob = ResultJob(extr,
+ content=("#sha1_content" in result),
+ format=(result.get("#metadata") != "post"))
if "#exception" in result:
- with self.assertRaises(result["#exception"], msg="#exception"):
+ with self.assertRaises(result["#exception"], msg="#exception"), \
+ self.assertLogs() as log_info:
tjob.run()
+ if "#log" in result:
+ self.assertLogEqual(result["#log"], log_info.output)
return
try:
@@ -228,7 +234,7 @@ class TestExtractorResults(unittest.TestCase):
if isinstance(count, str):
self.assertRegex(
count, r"^ *(==|!=|<|<=|>|>=) *\d+ *$", msg="#count")
- expr = "{} {}".format(len_urls, count)
+ expr = f"{len_urls} {count}"
self.assertTrue(eval(expr), msg=expr)
elif isinstance(count, range):
self.assertRange(len_urls, count, msg="#count")
@@ -257,7 +263,11 @@ class TestExtractorResults(unittest.TestCase):
metadata = {k: v for k, v in result.items() if k[0] != "#"}
if metadata:
- for kwdict in tjob.kwdict_list:
+ if result.get("#metadata") == "post":
+ kwdicts = tjob.kwdict_post
+ else:
+ kwdicts = tjob.kwdict_list
+ for kwdict in kwdicts:
self._test_kwdict(kwdict, metadata)
def _test_kwdict(self, kwdict, tests, parent=None):
@@ -274,7 +284,7 @@ class TestExtractorResults(unittest.TestCase):
else:
subtest = False
- path = "{}.{}".format(parent, key) if parent else key
+ path = f"{parent}.{key}" if parent else key
if key.startswith("!"):
self.assertNotIn(key[1:], kwdict, msg=path)
@@ -286,7 +296,7 @@ class TestExtractorResults(unittest.TestCase):
if subtest:
self.assertNotIsInstance(value, str, msg=path)
for idx, item in enumerate(value):
- subpath = "{}[{}]".format(path, idx)
+ subpath = f"{path}[{idx}]"
self._test_kwdict_value(item, test, subpath)
else:
self._test_kwdict_value(value, test, path)
@@ -308,12 +318,18 @@ class TestExtractorResults(unittest.TestCase):
for idx, item in enumerate(test):
if isinstance(item, dict):
subtest = True
- subpath = "{}[{}]".format(path, idx)
- self._test_kwdict(value[idx], item, subpath)
+ subpath = f"{path}[{idx}]"
+ try:
+ obj = value[idx]
+ except Exception as exc:
+ self.fail(f"'{exc.__class__.__name__}: {exc}' "
+ f"when accessing {subpath}")
+ self._test_kwdict(obj, item, subpath)
if not subtest:
self.assertEqual(test, value, msg=path)
elif isinstance(test, str):
if test.startswith("re:"):
+ self.assertIsInstance(value, str, msg=path)
self.assertRegex(value, test[3:], msg=path)
elif test.startswith("dt:"):
self.assertIsInstance(value, datetime.datetime, msg=path)
@@ -324,8 +340,29 @@ class TestExtractorResults(unittest.TestCase):
cls, _, length = test[4:].rpartition(":")
if cls:
self.assertEqual(
- cls, type(value).__name__, msg=path + "/type")
- self.assertEqual(int(length), len(value), msg=path)
+ cls, type(value).__name__, msg=f"{path}/type")
+ try:
+ len_value = len(value)
+ except Exception:
+ len_value = 0
+ for _ in value:
+ len_value += 1
+ self.assertEqual(int(length), len_value, msg=path)
+ elif test.startswith("iso:"):
+ iso = test[4:]
+ if iso in ("dt", "datetime", "8601"):
+ msg = f"{path} / ISO 8601"
+ try:
+ dt = datetime.datetime.fromisoformat(value)
+ except Exception as exc:
+ self.fail(f"Invalid datetime '{value}': {exc} {msg}")
+ self.assertIsInstance(dt, datetime.datetime, msg=msg)
+ elif iso in ("lang", "639", "639-1"):
+ msg = f"{path} / ISO 639-1"
+ self.assertIsInstance(value, str, msg=msg)
+ self.assertRegex(value, r"^[a-z]{2}(-\w+)?$", msg=msg)
+ else:
+ self.fail(f"Unsupported ISO test '{test}'")
else:
self.assertEqual(test, value, msg=path)
else:
@@ -335,7 +372,7 @@ class TestExtractorResults(unittest.TestCase):
class ResultJob(job.DownloadJob):
"""Generate test-results for extractor runs"""
- def __init__(self, url, parent=None, content=False):
+ def __init__(self, url, parent=None, content=False, format=True):
job.DownloadJob.__init__(self, url, parent)
self.queue = False
self.content = content
@@ -343,6 +380,7 @@ class ResultJob(job.DownloadJob):
self.url_list = []
self.url_hash = hashlib.sha1()
self.kwdict_list = []
+ self.kwdict_post = []
self.kwdict_hash = hashlib.sha1()
self.archive_list = []
self.archive_hash = hashlib.sha1()
@@ -353,12 +391,17 @@ class ResultJob(job.DownloadJob):
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
+ if format:
+ 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
+ else:
+ self.format_directory = \
+ self.format_filename = \
+ self.format_archive = lambda kwdict: ""
def run(self):
self._init()
@@ -391,6 +434,8 @@ class ResultJob(job.DownloadJob):
def _update_kwdict(self, kwdict, to_list=True):
if to_list:
self.kwdict_list.append(kwdict.copy())
+ else:
+ self.kwdict_post.append(kwdict.copy())
kwdict = util.filter_dict(kwdict)
self.kwdict_hash.update(
json.dumps(kwdict, sort_keys=True, default=str).encode())
@@ -489,8 +534,7 @@ def load_test_config():
except FileNotFoundError:
pass
except Exception as exc:
- sys.exit("Error when loading {}: {}: {}".format(
- path, exc.__class__.__name__, exc))
+ sys.exit(f"Error when loading {path}: {exc.__class__.__name__}: {exc}")
def result_categories(result):
@@ -553,12 +597,12 @@ def generate_tests():
enum = collections.defaultdict(int)
for result in tests:
base, cat, sub = result_categories(result)
- name = "{}_{}".format(cat, sub)
+ name = f"{cat}_{sub}"
enum[name] += 1
method = _generate_method(result)
method.__doc__ = result["#url"]
- method.__name__ = "test_{}_{}".format(name, enum[name])
+ method.__name__ = f"test_{name}_{enum[name]}"
setattr(TestExtractorResults, method.__name__, method)
diff --git a/test/test_util.py b/test/test_util.py
index 00e8c4b..4a76769 100644
--- a/test/test_util.py
+++ b/test/test_util.py
@@ -385,7 +385,7 @@ class TestCompileExpression(unittest.TestCase):
self.assertEqual(expr(value), result)
with tempfile.TemporaryDirectory() as path:
- file = path + "/module_sha1.py"
+ file = f"{path}/module_sha1.py"
with open(file, "w") as fp:
fp.write("""
import hashlib
@@ -638,7 +638,7 @@ class TestOther(unittest.TestCase):
self.assertIs(module, datetime)
with tempfile.TemporaryDirectory() as path:
- file = path + "/module_test.py"
+ file = f"{path}/module_test.py"
with open(file, "w") as fp:
fp.write("""
import datetime
diff --git a/test/test_ytdl.py b/test/test_ytdl.py
index ecc6d2f..88933e4 100644
--- a/test/test_ytdl.py
+++ b/test/test_ytdl.py
@@ -23,8 +23,8 @@ class Test_CommandlineArguments(unittest.TestCase):
try:
cls.module = __import__(cls.module_name)
except (ImportError, SyntaxError):
- raise unittest.SkipTest("cannot import module '{}'".format(
- cls.module_name))
+ raise unittest.SkipTest(
+ f"cannot import module '{cls.module_name}'")
cls.default = ytdl.parse_command_line(cls.module, [])
cls.ytdlp = hasattr(cls.module, "cookies")