From a5b49d952fedec881d593e2b6be2ffc9bbd9a01a Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Tue, 24 Oct 2023 14:53:40 -0400 Subject: [PATCH] Improve tests --- av/__main__.py | 2 - av/datasets.py | 2 - tests/common.py | 86 +++++++++++++++++++----------------------- tests/test_doctests.py | 59 ----------------------------- 4 files changed, 38 insertions(+), 111 deletions(-) delete mode 100644 tests/test_doctests.py diff --git a/av/__main__.py b/av/__main__.py index 8c57e2dd9..b5718ba8b 100644 --- a/av/__main__.py +++ b/av/__main__.py @@ -2,7 +2,6 @@ def main(): - parser = argparse.ArgumentParser() parser.add_argument("--codecs", action="store_true") parser.add_argument("--version", action="store_true") @@ -11,7 +10,6 @@ def main(): # --- if args.version: - import av import av._core diff --git a/av/datasets.py b/av/datasets.py index bf610b89b..5c189365a 100644 --- a/av/datasets.py +++ b/av/datasets.py @@ -9,7 +9,6 @@ def iter_data_dirs(check_writable=False): - try: yield os.environ["PYAV_TESTDATA_DIR"] except KeyError: @@ -45,7 +44,6 @@ def iter_data_dirs(check_writable=False): def cached_download(url, name): - """Download the data at a URL, and cache it under the given name. The file is stored under `pyav/test` with the given name in the directory diff --git a/tests/common.py b/tests/common.py index e53537471..38ee0a0fb 100644 --- a/tests/common.py +++ b/tests/common.py @@ -3,7 +3,6 @@ import errno import functools import os -import sys import types from av.datasets import fate as fate_suite @@ -147,7 +146,7 @@ def assertNdarraysEqual(self, a, b): a[it.multi_index], b[it.multi_index], ) - self.fail("ndarrays contents differ\n%s" % msg) + self.fail(f"ndarrays contents differ\n{msg}") def assertImagesAlmostEqual(self, a, b, epsilon=0.1, *args): self.assertEqual(a.size, b.size, "sizes dont match") @@ -156,49 +155,40 @@ def assertImagesAlmostEqual(self, a, b, epsilon=0.1, *args): for i, ax, bx in zip(range(len(a)), a, b): diff = sum(abs(ac / 256 - bc / 256) for ac, bc in zip(ax, bx)) / 3 if diff > epsilon: - self.fail( - "images differed by %s at index %d; %s %s" % (diff, i, ax, bx) - ) - - # Add some of the unittest methods that we love from 2.7. - if sys.version_info < (2, 7): - - def assertIs(self, a, b, msg=None): - if a is not b: - self.fail( - msg - or "%r at 0x%x is not %r at 0x%x; %r is not %r" - % (type(a), id(a), type(b), id(b), a, b) - ) - - def assertIsNot(self, a, b, msg=None): - if a is b: - self.fail( - msg or "both are {!r} at 0x{:x}; {!r}".format(type(a), id(a), a) - ) - - def assertIsNone(self, x, msg=None): - if x is not None: - self.fail(msg or "is not None; %r" % x) - - def assertIsNotNone(self, x, msg=None): - if x is None: - self.fail(msg or "is None; %r" % x) - - def assertIn(self, a, b, msg=None): - if a not in b: - self.fail(msg or "{!r} not in {!r}".format(a, b)) - - def assertNotIn(self, a, b, msg=None): - if a in b: - self.fail(msg or "{!r} in {!r}".format(a, b)) - - def assertIsInstance(self, instance, types, msg=None): - if not isinstance(instance, types): - self.fail( - msg or "not an instance of {!r}; {!r}".format(types, instance) - ) - - def assertNotIsInstance(self, instance, types, msg=None): - if isinstance(instance, types): - self.fail(msg or "is an instance of {!r}; {!r}".format(types, instance)) + self.fail(f"images differed by {diff} at index {i}; {ax} {bx}") + + def assertIs(self, a, b, msg=None): + if a is not b: + self.fail( + msg + or "%r at 0x%x is not %r at 0x%x; %r is not %r" + % (type(a), id(a), type(b), id(b), a, b) + ) + + def assertIsNot(self, a, b, msg=None): + if a is b: + self.fail(msg or f"both are {type(a)!r} at 0x{id(a):x}; {a!r}") + + def assertIsNone(self, x, msg=None): + if x is not None: + self.fail(msg or f"is not None; {x!r}") + + def assertIsNotNone(self, x, msg=None): + if x is None: + self.fail(msg or f"is None; {x!r}") + + def assertIn(self, a, b, msg=None): + if a not in b: + self.fail(msg or f"{a!r} not in {b!r}") + + def assertNotIn(self, a, b, msg=None): + if a in b: + self.fail(msg or f"{a!r} in {b!r}") + + def assertIsInstance(self, instance, types, msg=None): + if not isinstance(instance, types): + self.fail(msg or f"not an instance of {types!r}; {instance!r}") + + def assertNotIsInstance(self, instance, types, msg=None): + if isinstance(instance, types): + self.fail(msg or f"is an instance of {types!r}; {instance!r}") diff --git a/tests/test_doctests.py b/tests/test_doctests.py deleted file mode 100644 index 07cfdd574..000000000 --- a/tests/test_doctests.py +++ /dev/null @@ -1,59 +0,0 @@ -from unittest import TestCase -import doctest -import pkgutil -import re - -import av - - -def fix_doctests(suite): - for case in suite._tests: - # Add some more flags. - case._dt_optionflags = ( - (case._dt_optionflags or 0) - | doctest.IGNORE_EXCEPTION_DETAIL - | doctest.ELLIPSIS - | doctest.NORMALIZE_WHITESPACE - ) - - case._dt_test.globs["av"] = av - case._dt_test.globs["video_path"] = av.datasets.curated( - "pexels/time-lapse-video-of-night-sky-857195.mp4" - ) - - for example in case._dt_test.examples: - # Remove b prefix from strings. - if example.want.startswith("b'"): - example.want = example.want[1:] - - -def register_doctests(mod): - if isinstance(mod, str): - mod = __import__(mod, fromlist=[""]) - - try: - suite = doctest.DocTestSuite(mod) - except ValueError: - return - - fix_doctests(suite) - - cls_name = "Test" + "".join(x.title() for x in mod.__name__.split(".")) - cls = type(cls_name, (TestCase,), {}) - - for test in suite._tests: - - def func(self): - return test.runTest() - - name = str("test_" + re.sub("[^a-zA-Z0-9]+", "_", test.id()).strip("_")) - func.__name__ = name - setattr(cls, name, func) - - globals()[cls_name] = cls - - -for importer, mod_name, ispkg in pkgutil.walk_packages( - path=av.__path__, prefix=av.__name__ + ".", onerror=lambda x: None -): - register_doctests(mod_name)