aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2024-09-07 18:33:19 -0400
committerLibravatarUnit 193 <unit193@unit193.net>2024-09-07 18:33:19 -0400
commit1f3ffe32342852fd9ea9e7704022488f3a1222bd (patch)
treecb255a091b73e96840de0f6f44b36dff1acab4b9 /test
parentb5e56c51e491b41f9eb6a895459c185788a377e5 (diff)
New upstream version 1.27.4.upstream/1.27.4
Diffstat (limited to 'test')
-rw-r--r--test/test_downloader.py6
-rw-r--r--test/test_formatter.py6
-rw-r--r--test/test_postprocessor.py136
-rw-r--r--test/test_util.py46
-rw-r--r--test/test_ytdl.py2
5 files changed, 194 insertions, 2 deletions
diff --git a/test/test_downloader.py b/test/test_downloader.py
index f88b2c0..35cccc4 100644
--- a/test/test_downloader.py
+++ b/test/test_downloader.py
@@ -45,11 +45,15 @@ class TestDownloaderModule(unittest.TestCase):
@classmethod
def setUpClass(cls):
# allow import of ytdl downloader module without youtube_dl installed
+ cls._orig_ytdl = sys.modules.get("youtube_dl")
sys.modules["youtube_dl"] = MagicMock()
@classmethod
def tearDownClass(cls):
- del sys.modules["youtube_dl"]
+ if cls._orig_ytdl:
+ sys.modules["youtube_dl"] = cls._orig_ytdl
+ else:
+ del sys.modules["youtube_dl"]
def tearDown(self):
downloader._cache.clear()
diff --git a/test/test_formatter.py b/test/test_formatter.py
index e00af85..75324fb 100644
--- a/test/test_formatter.py
+++ b/test/test_formatter.py
@@ -25,6 +25,7 @@ class TestFormatter(unittest.TestCase):
"b": "äöü",
"j": "げんそうきょう",
"d": {"a": "foo", "b": 0, "c": None},
+ "i": 2,
"l": ["a", "b", "c"],
"n": None,
"s": " \n\r\tSPACE ",
@@ -267,6 +268,11 @@ class TestFormatter(unittest.TestCase):
"{a:Sort-reverse}", # starts with 'S', contains 'r'
"['w', 'r', 'o', 'l', 'h', 'd', 'O', 'L', 'L', 'E', ' ']")
+ def test_specifier_arithmetic(self):
+ self._run_test("{i:A+1}", "3")
+ self._run_test("{i:A-1}", "1")
+ self._run_test("{i:A*3}", "6")
+
def test_specifier_conversions(self):
self._run_test("{a:Cl}" , "hello world")
self._run_test("{h:CHC}" , "Foo & Bar")
diff --git a/test/test_postprocessor.py b/test/test_postprocessor.py
index edd8575..dd53803 100644
--- a/test/test_postprocessor.py
+++ b/test/test_postprocessor.py
@@ -12,6 +12,7 @@ import sys
import unittest
from unittest.mock import Mock, mock_open, patch
+import shutil
import logging
import zipfile
import tempfile
@@ -239,6 +240,57 @@ class ExecTest(BasePostprocessorTest):
self.assertFalse(i.wait.called)
+class HashTest(BasePostprocessorTest):
+
+ def test_default(self):
+ self._create({})
+
+ with self.pathfmt.open() as fp:
+ fp.write(b"Foo Bar\n")
+
+ self._trigger()
+
+ kwdict = self.pathfmt.kwdict
+ self.assertEqual(
+ "35c9c9c7c90ad764bae9e2623f522c24", kwdict["md5"], "md5")
+ self.assertEqual(
+ "14d3d804494ef4e57d72de63e4cfee761240471a", kwdict["sha1"], "sha1")
+
+ def test_custom_hashes(self):
+ self._create({"hashes": "sha256:a,sha512:b"})
+
+ with self.pathfmt.open() as fp:
+ fp.write(b"Foo Bar\n")
+
+ self._trigger()
+
+ kwdict = self.pathfmt.kwdict
+ self.assertEqual(
+ "4775b55be17206445d7015a5fc7656f38a74b880670523c3b175455f885f2395",
+ kwdict["a"], "sha256")
+ self.assertEqual(
+ "6028f9e6957f4ca929941318c4bba6258713fd5162f9e33bd10e1c456d252700"
+ "3e1095b50736c4fd1e2deea152e3c8ecd5993462a747208e4d842659935a1c62",
+ kwdict["b"], "sha512")
+
+ def test_custom_hashes_dict(self):
+ self._create({"hashes": {"a": "sha256", "b": "sha512"}})
+
+ with self.pathfmt.open() as fp:
+ fp.write(b"Foo Bar\n")
+
+ self._trigger()
+
+ kwdict = self.pathfmt.kwdict
+ self.assertEqual(
+ "4775b55be17206445d7015a5fc7656f38a74b880670523c3b175455f885f2395",
+ kwdict["a"], "sha256")
+ self.assertEqual(
+ "6028f9e6957f4ca929941318c4bba6258713fd5162f9e33bd10e1c456d252700"
+ "3e1095b50736c4fd1e2deea152e3c8ecd5993462a747208e4d842659935a1c62",
+ kwdict["b"], "sha512")
+
+
class MetadataTest(BasePostprocessorTest):
def test_metadata_default(self):
@@ -585,6 +637,36 @@ class MetadataTest(BasePostprocessorTest):
self.assertTrue(not e.called)
self.assertTrue(m.called)
+ def test_metadata_option_include(self):
+ self._create(
+ {"include": ["_private", "filename", "foo"], "sort": True},
+ {"public": "hello ワールド", "_private": "foo バー"},
+ )
+
+ with patch("builtins.open", mock_open()) as m:
+ self._trigger()
+
+ self.assertEqual(self._output(m), """{
+ "_private": "foo バー",
+ "filename": "file"
+}
+""")
+
+ def test_metadata_option_exclude(self):
+ self._create(
+ {"exclude": ["category", "filename", "foo"], "sort": True},
+ {"public": "hello ワールド", "_private": "foo バー"},
+ )
+
+ with patch("builtins.open", mock_open()) as m:
+ self._trigger()
+
+ self.assertEqual(self._output(m), """{
+ "extension": "ext",
+ "public": "hello ワールド"
+}
+""")
+
@staticmethod
def _output(mock):
return "".join(
@@ -661,6 +743,60 @@ def calc(kwdict):
""")
+class RenameTest(BasePostprocessorTest):
+
+ def _prepare(self, filename):
+ path = self.pathfmt.realdirectory
+ shutil.rmtree(path, ignore_errors=True)
+ os.makedirs(path, exist_ok=True)
+
+ with open(path + filename, "w"):
+ pass
+
+ return path
+
+ def test_rename_from(self):
+ self._create({"from": "{id}.{extension}"}, {"id": 12345})
+ path = self._prepare("12345.ext")
+
+ self._trigger()
+
+ self.assertEqual(os.listdir(path), ["file.ext"])
+
+ def test_rename_to(self):
+ self._create({"to": "{id}.{extension}"}, {"id": 12345})
+ path = self._prepare("file.ext")
+
+ self._trigger(("skip",))
+
+ self.assertEqual(os.listdir(path), ["12345.ext"])
+
+ def test_rename_from_to(self):
+ self._create({"from": "name", "to": "{id}"}, {"id": 12345})
+ path = self._prepare("name")
+
+ self._trigger()
+
+ self.assertEqual(os.listdir(path), ["12345"])
+
+ def test_rename_noopt(self):
+ with self.assertRaises(ValueError):
+ self._create({})
+
+ def test_rename_skip(self):
+ self._create({"from": "{id}.{extension}"}, {"id": 12345})
+ path = self._prepare("12345.ext")
+ with open(path + "file.ext", "w"):
+ pass
+
+ with self.assertLogs("postprocessor.rename", level="WARNING") as cm:
+ self._trigger()
+ self.assertTrue(cm.output[0].startswith(
+ "WARNING:postprocessor.rename:Not renaming "
+ "'12345.ext' to 'file.ext'"))
+ self.assertEqual(sorted(os.listdir(path)), ["12345.ext", "file.ext"])
+
+
class ZipTest(BasePostprocessorTest):
def test_zip_default(self):
diff --git a/test/test_util.py b/test/test_util.py
index 4622c28..fd2ff8b 100644
--- a/test/test_util.py
+++ b/test/test_util.py
@@ -12,9 +12,11 @@ import sys
import unittest
import io
+import time
import random
import string
import datetime
+import platform
import tempfile
import itertools
import http.cookiejar
@@ -741,6 +743,9 @@ def hash(value):
self.assertFalse(obj)
self.assertEqual(len(obj), 0)
+ self.assertEqual(int(obj), 0)
+ self.assertEqual(hash(obj), 0)
+
self.assertEqual(str(obj), str(None))
self.assertEqual(repr(obj), repr(None))
self.assertEqual(format(obj), str(None))
@@ -751,6 +756,7 @@ def hash(value):
self.assertIs(obj(), obj)
self.assertIs(obj(1, "a"), obj)
self.assertIs(obj(foo="bar"), obj)
+ self.assertIs(iter(obj), obj)
self.assertEqual(util.json_dumps(obj), "null")
self.assertLess(obj, "foo")
@@ -761,9 +767,49 @@ def hash(value):
self.assertGreater(123, obj)
self.assertGreaterEqual(1.23, obj)
+ self.assertEqual(obj + 123, obj)
+ self.assertEqual(obj - 123, obj)
+ self.assertEqual(obj * 123, obj)
+ # self.assertEqual(obj @ 123, obj)
+ self.assertEqual(obj / 123, obj)
+ self.assertEqual(obj // 123, obj)
+ self.assertEqual(obj % 123, obj)
+
+ self.assertEqual(123 + obj, obj)
+ self.assertEqual(123 - obj, obj)
+ self.assertEqual(123 * obj, obj)
+ # self.assertEqual(123 @ obj, obj)
+ self.assertEqual(123 / obj, obj)
+ self.assertEqual(123 // obj, obj)
+ self.assertEqual(123 % obj, obj)
+
+ self.assertEqual(obj << 123, obj)
+ self.assertEqual(obj >> 123, obj)
+ self.assertEqual(obj & 123, obj)
+ self.assertEqual(obj ^ 123, obj)
+ self.assertEqual(obj | 123, obj)
+
+ self.assertEqual(123 << obj, obj)
+ self.assertEqual(123 >> obj, obj)
+ self.assertEqual(123 & obj, obj)
+ self.assertEqual(123 ^ obj, obj)
+ self.assertEqual(123 | obj, obj)
+
+ self.assertEqual(-obj, obj)
+ self.assertEqual(+obj, obj)
+ self.assertEqual(~obj, obj)
+ self.assertEqual(abs(obj), obj)
+
mapping = {}
mapping[obj] = 123
self.assertIn(obj, mapping)
+ self.assertEqual(mapping[obj], 123)
+
+ array = [1, 2, 3]
+ self.assertEqual(array[obj], 1)
+
+ if platform.python_implementation().lower() == "cpython":
+ self.assertTrue(time.localtime(obj))
i = 0
for _ in obj:
diff --git a/test/test_ytdl.py b/test/test_ytdl.py
index fd2e40a..f7eb671 100644
--- a/test/test_ytdl.py
+++ b/test/test_ytdl.py
@@ -22,7 +22,7 @@ class Test_CommandlineArguments(unittest.TestCase):
def setUpClass(cls):
try:
cls.module = __import__(cls.module_name)
- except ImportError:
+ except (ImportError, SyntaxError):
raise unittest.SkipTest("cannot import module '{}'".format(
cls.module_name))
cls.default = ytdl.parse_command_line(cls.module, [])