From 09e426350409d45e7f7a8ff369f8d8aa9eec0fe4 Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Fri, 31 Mar 2023 07:24:57 -0400 Subject: New upstream version 1.25.1. --- test/test_formatter.py | 36 +++++++++++++++++++++++++-- test/test_job.py | 61 +++++++++++++++++++++++++++++++++++++--------- test/test_postprocessor.py | 35 ++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 13 deletions(-) (limited to 'test') diff --git a/test/test_formatter.py b/test/test_formatter.py index 50e55a6..2258966 100644 --- a/test/test_formatter.py +++ b/test/test_formatter.py @@ -128,6 +128,11 @@ class TestFormatter(unittest.TestCase): self._run_test("{l[0]}" , "a") self._run_test("{a[6]}" , "w") + def test_dict_access(self): + self._run_test("{d[a]}" , "foo") + self._run_test("{d['a']}", "foo") + self._run_test('{d["a"]}', "foo") + def test_slicing(self): v = self.kwdict["a"] self._run_test("{a[1:10]}" , v[1:10]) @@ -348,6 +353,27 @@ class TestFormatter(unittest.TestCase): self._run_test("\fF foo-'\"{a.upper()}\"'-bar", """foo-'"{}"'-bar""".format(self.kwdict["a"].upper())) + @unittest.skipIf(sys.hexversion < 0x3060000, "no fstring support") + def test_template_fstring(self): + with tempfile.TemporaryDirectory() as tmpdirname: + path1 = os.path.join(tmpdirname, "tpl1") + path2 = os.path.join(tmpdirname, "tpl2") + + with open(path1, "w") as fp: + fp.write("{a}") + fmt1 = formatter.parse("\fTF " + path1) + + with open(path2, "w") as fp: + fp.write("foo-'\"{a.upper()}\"'-bar") + fmt2 = formatter.parse("\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())) + + with self.assertRaises(OSError): + formatter.parse("\fTF /") + def test_module(self): with tempfile.TemporaryDirectory() as tmpdirname: path = os.path.join(tmpdirname, "testmod.py") @@ -374,7 +400,7 @@ def noarg(): try: fmt1 = formatter.parse("\fM testmod:gentext") fmt2 = formatter.parse("\fM testmod:lengths") - fmt3 = formatter.parse("\fM testmod:noarg") + fmt0 = formatter.parse("\fM testmod:noarg") with self.assertRaises(AttributeError): formatter.parse("\fM testmod:missing") @@ -383,11 +409,17 @@ def noarg(): finally: sys.path.pop(0) + fmt3 = formatter.parse("\fM " + path + ":gentext") + fmt4 = formatter.parse("\fM " + path + ":lengths") + self.assertEqual(fmt1.format_map(self.kwdict), "'Title' by Name") self.assertEqual(fmt2.format_map(self.kwdict), "89") + self.assertEqual(fmt3.format_map(self.kwdict), "'Title' by Name") + self.assertEqual(fmt4.format_map(self.kwdict), "89") + with self.assertRaises(TypeError): - self.assertEqual(fmt3.format_map(self.kwdict), "") + self.assertEqual(fmt0.format_map(self.kwdict), "") def _run_test(self, format_string, result, default=None, fmt=format): fmt = formatter.parse(format_string, default, fmt) diff --git a/test/test_job.py b/test/test_job.py index 1bd9ccc..a6e093f 100644 --- a/test/test_job.py +++ b/test/test_job.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Copyright 2021 Mike Fährmann +# Copyright 2021-2023 Mike Fährmann # # 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 @@ -66,17 +66,36 @@ class TestKeywordJob(TestJob): jobclass = job.KeywordJob def test_default(self): - extr = TestExtractor.from_url("test:") + self.maxDiff = None + extr = TestExtractor.from_url("test:self") self.assertEqual(self._capture_stdout(extr), """\ Keywords for directory names: ----------------------------- +author['id'] + 123 +author['name'] + test +author['self'] + category test_category subcategory test_subcategory +user['id'] + 123 +user['name'] + test +user['self'] + Keywords for filenames and --filter: ------------------------------------ +author['id'] + 123 +author['name'] + test +author['self'] + category test_category extension @@ -91,10 +110,12 @@ tags[N] 0 foo 1 bar 2 テスト -user[id] +user['id'] 123 -user[name] +user['name'] test +user['self'] + """) @@ -209,6 +230,7 @@ class TestDataJob(TestJob): def test_default(self): extr = TestExtractor.from_url("test:") tjob = self.jobclass(extr, file=io.StringIO()) + user = {"id": 123, "name": "test"} tjob.run() @@ -216,6 +238,8 @@ class TestDataJob(TestJob): (Message.Directory, { "category" : "test_category", "subcategory": "test_subcategory", + "user" : user, + "author" : user, }), (Message.Url, "https://example.org/1.jpg", { "category" : "test_category", @@ -224,7 +248,8 @@ class TestDataJob(TestJob): "extension" : "jpg", "num" : 1, "tags" : ["foo", "bar", "テスト"], - "user" : {"id": 123, "name": "test"}, + "user" : user, + "author" : user, }), (Message.Url, "https://example.org/2.jpg", { "category" : "test_category", @@ -233,7 +258,8 @@ class TestDataJob(TestJob): "extension" : "jpg", "num" : 2, "tags" : ["foo", "bar", "テスト"], - "user" : {"id": 123, "name": "test"}, + "user" : user, + "author" : user, }), (Message.Url, "https://example.org/3.jpg", { "category" : "test_category", @@ -242,7 +268,8 @@ class TestDataJob(TestJob): "extension" : "jpg", "num" : 3, "tags" : ["foo", "bar", "テスト"], - "user" : {"id": 123, "name": "test"}, + "user" : user, + "author" : user, }), ]) @@ -316,7 +343,7 @@ class TestDataJob(TestJob): config.set(("output",), "num-to-str", True) with patch("gallery_dl.util.number_to_string") as nts: tjob.run() - self.assertEqual(len(nts.call_args_list), 52) + self.assertEqual(len(nts.call_args_list), 72) tjob.run() self.assertEqual(tjob.data[-1][0], Message.Url) @@ -328,18 +355,30 @@ class TestExtractor(Extractor): subcategory = "test_subcategory" directory_fmt = ("{category}",) filename_fmt = "test_{filename}.{extension}" - pattern = r"test:(child)?$" + pattern = r"test:(child|self)?$" + + def __init__(self, match): + Extractor.__init__(self, match) + self.user = {"id": 123, "name": "test"} + if match.group(1) == "self": + self.user["self"] = self.user def items(self): root = "https://example.org" + user = self.user + + yield Message.Directory, { + "user": user, + "author": user, + } - yield Message.Directory, {} for i in range(1, 4): url = "{}/{}.jpg".format(root, i) yield Message.Url, url, text.nameext_from_url(url, { "num" : i, "tags": ["foo", "bar", "テスト"], - "user": {"id": 123, "name": "test"}, + "user": user, + "author": user, "_fallback": ("{}/alt/{}.jpg".format(root, i),), }) diff --git a/test/test_postprocessor.py b/test/test_postprocessor.py index 650bf59..c78d7b0 100644 --- a/test/test_postprocessor.py +++ b/test/test_postprocessor.py @@ -428,11 +428,46 @@ class MetadataTest(BasePostprocessorTest): self.assertNotIn("baz", pdict["bar"]) self.assertEqual(kwdict["bar"], pdict["bar"]) + # no errors for deleted/undefined fields self._trigger() self.assertNotIn("foo", pdict) self.assertNotIn("baz", pdict["bar"]) self.assertEqual(kwdict["bar"], pdict["bar"]) + def test_metadata_option_skip(self): + self._create({"skip": True}) + + with patch("builtins.open", mock_open()) as m, \ + patch("os.path.exists") as e: + e.return_value = True + self._trigger() + + self.assertTrue(e.called) + self.assertTrue(not m.called) + self.assertTrue(not len(self._output(m))) + + with patch("builtins.open", mock_open()) as m, \ + patch("os.path.exists") as e: + e.return_value = False + self._trigger() + + self.assertTrue(e.called) + self.assertTrue(m.called) + self.assertGreater(len(self._output(m)), 0) + + path = self.pathfmt.realdirectory + "file.ext.json" + m.assert_called_once_with(path, "w", encoding="utf-8") + + def test_metadata_option_skip_false(self): + self._create({"skip": False}) + + with patch("builtins.open", mock_open()) as m, \ + patch("os.path.exists") as e: + self._trigger() + + self.assertTrue(not e.called) + self.assertTrue(m.called) + @staticmethod def _output(mock): return "".join( -- cgit v1.2.3