summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_config.py18
-rw-r--r--test/test_extractor.py5
-rw-r--r--test/test_postprocessor.py39
-rw-r--r--test/test_util.py146
4 files changed, 136 insertions, 72 deletions
diff --git a/test/test_config.py b/test/test_config.py
index bbe288f..1d49d77 100644
--- a/test/test_config.py
+++ b/test/test_config.py
@@ -117,6 +117,24 @@ class TestConfig(unittest.TestCase):
self.assertEqual(
config.accumulate(("c", "c"), "l"), [5, 6])
+ config.set(() , "l", 4)
+ config.set(("c",) , "l", [2, 3])
+ config.set(("c", "c"), "l", 1)
+ self.assertEqual(
+ config.accumulate((), "l") , [4])
+ self.assertEqual(
+ config.accumulate(("c",), "l") , [2, 3, 4])
+ self.assertEqual(
+ config.accumulate(("c", "c"), "l"), [1, 2, 3, 4])
+
+ config.set(("c",), "l", None)
+ self.assertEqual(
+ config.accumulate((), "l") , [4])
+ self.assertEqual(
+ config.accumulate(("c",), "l") , [4])
+ self.assertEqual(
+ config.accumulate(("c", "c"), "l"), [1, 4])
+
def test_set(self):
config.set(() , "c", [1, 2, 3])
config.set(("b",) , "c", [1, 2, 3])
diff --git a/test/test_extractor.py b/test/test_extractor.py
index e622fa8..cc85fb2 100644
--- a/test/test_extractor.py
+++ b/test/test_extractor.py
@@ -168,12 +168,17 @@ class TestExtractorModule(unittest.TestCase):
def test_init(self):
"""Test for exceptions in Extractor.initialize() and .finalize()"""
+ def fail_request(*args, **kwargs):
+ self.fail("called 'request() during initialization")
+
for cls in extractor.extractors():
if cls.category == "ytdl":
continue
extr = cls.from_url(cls.example)
if not extr and cls.basecategory and not cls.instances:
continue
+
+ extr.request = fail_request
extr.initialize()
extr.finalize()
diff --git a/test/test_postprocessor.py b/test/test_postprocessor.py
index dd53803..2941b81 100644
--- a/test/test_postprocessor.py
+++ b/test/test_postprocessor.py
@@ -120,30 +120,37 @@ class ClassifyTest(BasePostprocessorTest):
for directory, exts in pp.DEFAULT_MAPPING.items()
for ext in exts
})
+
+ self.assertEqual(pp.directory, "")
+ self._trigger(("post",))
+ self.assertEqual(pp.directory, self.pathfmt.directory)
+
self.pathfmt.set_extension("jpg")
+ self._trigger(("prepare",))
self.pathfmt.build_path()
-
- pp.prepare(self.pathfmt)
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")
- with patch("os.makedirs") as mkdirs:
- self._trigger()
- mkdirs.assert_called_once_with(path, exist_ok=True)
+ 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")
def test_classify_noop(self):
pp = self._create()
rp = self.pathfmt.realpath
- pp.prepare(self.pathfmt)
+ self.assertEqual(pp.directory, "")
+ self._trigger(("post",))
+ self._trigger(("prepare",))
+
+ self.assertEqual(pp.directory, self.pathfmt.directory)
self.assertEqual(self.pathfmt.path, rp)
self.assertEqual(self.pathfmt.realpath, rp)
- with patch("os.makedirs") as mkdirs:
- self._trigger()
- self.assertEqual(mkdirs.call_count, 0)
-
def test_classify_custom(self):
pp = self._create({"mapping": {
"foo/bar": ["foo", "bar"],
@@ -153,18 +160,18 @@ class ClassifyTest(BasePostprocessorTest):
"foo": "foo/bar",
"bar": "foo/bar",
})
+
+ self.assertEqual(pp.directory, "")
+ self._trigger(("post",))
+ self.assertEqual(pp.directory, self.pathfmt.directory)
+
self.pathfmt.set_extension("foo")
+ self._trigger(("prepare",))
self.pathfmt.build_path()
-
- pp.prepare(self.pathfmt)
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")
- with patch("os.makedirs") as mkdirs:
- self._trigger()
- mkdirs.assert_called_once_with(path, exist_ok=True)
-
class ExecTest(BasePostprocessorTest):
diff --git a/test/test_util.py b/test/test_util.py
index 888a70a..fa16c44 100644
--- a/test/test_util.py
+++ b/test/test_util.py
@@ -300,6 +300,96 @@ class TestCookiesTxt(unittest.TestCase):
)
+class TestCompileExpression(unittest.TestCase):
+
+ def test_compile_expression(self):
+ expr = util.compile_expression("1 + 2 * 3")
+ self.assertEqual(expr(), 7)
+ self.assertEqual(expr({"a": 1, "b": 2, "c": 3}), 7)
+ self.assertEqual(expr({"a": 9, "b": 9, "c": 9}), 7)
+
+ expr = util.compile_expression("a + b * c")
+ self.assertEqual(expr({"a": 1, "b": 2, "c": 3}), 7)
+ self.assertEqual(expr({"a": 9, "b": 9, "c": 9}), 90)
+
+ with self.assertRaises(SyntaxError):
+ util.compile_expression("")
+ with self.assertRaises(SyntaxError):
+ util.compile_expression("x++")
+
+ expr = util.compile_expression("1 and abort()")
+ with self.assertRaises(exception.StopExtraction):
+ expr()
+
+ def test_compile_expression_raw(self):
+ expr = util.compile_expression_raw("a + b * c")
+ with self.assertRaises(NameError):
+ expr()
+ with self.assertRaises(NameError):
+ expr({"a": 2})
+
+ expr = util.compile_expression_raw("int.param")
+ with self.assertRaises(AttributeError):
+ expr({"a": 2})
+
+ def test_compile_expression_tryexcept(self):
+ expr = util.compile_expression_tryexcept("a + b * c")
+ self.assertIs(expr(), util.NONE)
+ self.assertIs(expr({"a": 2}), util.NONE)
+
+ expr = util.compile_expression_tryexcept("int.param")
+ self.assertIs(expr({"a": 2}), util.NONE)
+
+ def test_compile_expression_defaultdict(self):
+ expr = util.compile_expression_defaultdict("a + b * c")
+ self.assertIs(expr(), util.NONE)
+ self.assertIs(expr({"a": 2}), util.NONE)
+
+ expr = util.compile_expression_defaultdict("int.param")
+ with self.assertRaises(AttributeError):
+ expr({"a": 2})
+
+ def test_compile_filter(self):
+ expr = util.compile_filter("a + b * c")
+ self.assertEqual(expr({"a": 1, "b": 2, "c": 3}), 7)
+ self.assertEqual(expr({"a": 9, "b": 9, "c": 9}), 90)
+
+ expr = util.compile_filter(["a % 2 == 0", "b % 3 == 0", "c % 5 == 0"])
+ self.assertTrue(expr({"a": 4, "b": 6, "c": 10}))
+ self.assertFalse(expr({"a": 1, "b": 2, "c": 3}))
+
+ def test_custom_globals(self):
+ value = {"v": "foobar"}
+ result = "8843d7f92416211de9ebb963ff4ce28125932878"
+
+ expr = util.compile_expression("hash_sha1(v)")
+ self.assertEqual(expr(value), result)
+
+ expr = util.compile_expression("hs(v)", globals={"hs": util.sha1})
+ self.assertEqual(expr(value), result)
+
+ with tempfile.TemporaryDirectory() as path:
+ file = path + "/module_sha1.py"
+ with open(file, "w") as fp:
+ fp.write("""
+import hashlib
+def hash(value):
+ return hashlib.sha1(value.encode()).hexdigest()
+""")
+ module = util.import_file(file)
+
+ expr = util.compile_expression("hash(v)", globals=module.__dict__)
+ self.assertEqual(expr(value), result)
+
+ GLOBALS_ORIG = util.GLOBALS
+ try:
+ util.GLOBALS = module.__dict__
+ expr = util.compile_expression("hash(v)")
+ finally:
+ util.GLOBALS = GLOBALS_ORIG
+ self.assertEqual(expr(value), result)
+
+
class TestOther(unittest.TestCase):
def test_bencode(self):
@@ -434,31 +524,6 @@ class TestOther(unittest.TestCase):
self.assertEqual(util.sha1(None),
"da39a3ee5e6b4b0d3255bfef95601890afd80709")
- def test_compile_expression(self):
- expr = util.compile_expression("1 + 2 * 3")
- self.assertEqual(expr(), 7)
- self.assertEqual(expr({"a": 1, "b": 2, "c": 3}), 7)
- self.assertEqual(expr({"a": 9, "b": 9, "c": 9}), 7)
-
- expr = util.compile_expression("a + b * c")
- self.assertEqual(expr({"a": 1, "b": 2, "c": 3}), 7)
- self.assertEqual(expr({"a": 9, "b": 9, "c": 9}), 90)
-
- expr = util.compile_expression_raw("a + b * c")
- with self.assertRaises(NameError):
- expr()
- with self.assertRaises(NameError):
- expr({"a": 2})
-
- with self.assertRaises(SyntaxError):
- util.compile_expression("")
- with self.assertRaises(SyntaxError):
- util.compile_expression("x++")
-
- expr = util.compile_expression("1 and abort()")
- with self.assertRaises(exception.StopExtraction):
- expr()
-
def test_import_file(self):
module = util.import_file("datetime")
self.assertIs(module, datetime)
@@ -478,37 +543,6 @@ value = 123
self.assertEqual(module.value, 123)
self.assertIs(module.datetime, datetime)
- def test_custom_globals(self):
- value = {"v": "foobar"}
- result = "8843d7f92416211de9ebb963ff4ce28125932878"
-
- expr = util.compile_expression("hash_sha1(v)")
- self.assertEqual(expr(value), result)
-
- expr = util.compile_expression("hs(v)", globals={"hs": util.sha1})
- self.assertEqual(expr(value), result)
-
- with tempfile.TemporaryDirectory() as path:
- file = path + "/module_sha1.py"
- with open(file, "w") as fp:
- fp.write("""
-import hashlib
-def hash(value):
- return hashlib.sha1(value.encode()).hexdigest()
-""")
- module = util.import_file(file)
-
- expr = util.compile_expression("hash(v)", globals=module.__dict__)
- self.assertEqual(expr(value), result)
-
- GLOBALS_ORIG = util.GLOBALS
- try:
- util.GLOBALS = module.__dict__
- expr = util.compile_expression("hash(v)")
- finally:
- util.GLOBALS = GLOBALS_ORIG
- self.assertEqual(expr(value), result)
-
def test_build_duration_func(self, f=util.build_duration_func):
def test_single(df, v):