diff options
Diffstat (limited to 'tests/test_error.py')
| -rw-r--r-- | tests/test_error.py | 65 |
1 files changed, 33 insertions, 32 deletions
diff --git a/tests/test_error.py b/tests/test_error.py index a0ff32c31..3eb82835e 100644 --- a/tests/test_error.py +++ b/tests/test_error.py | |||
| @@ -16,7 +16,9 @@ | |||
| 16 | 16 | ||
| 17 | import inspect | 17 | import inspect |
| 18 | import pickle | 18 | import pickle |
| 19 | import unittest | 19 | from typing import Iterator, Type |
| 20 | |||
| 21 | import pytest | ||
| 20 | 22 | ||
| 21 | import command | 23 | import command |
| 22 | import error | 24 | import error |
| @@ -26,7 +28,7 @@ import project | |||
| 26 | from subcmds import all_modules | 28 | from subcmds import all_modules |
| 27 | 29 | ||
| 28 | 30 | ||
| 29 | imports = all_modules + [ | 31 | _IMPORTS = all_modules + [ |
| 30 | error, | 32 | error, |
| 31 | project, | 33 | project, |
| 32 | git_command, | 34 | git_command, |
| @@ -35,36 +37,35 @@ imports = all_modules + [ | |||
| 35 | ] | 37 | ] |
| 36 | 38 | ||
| 37 | 39 | ||
| 38 | class PickleTests(unittest.TestCase): | 40 | def get_exceptions() -> Iterator[Type[Exception]]: |
| 39 | """Make sure all our custom exceptions can be pickled.""" | 41 | """Return all our custom exceptions.""" |
| 42 | for entry in _IMPORTS: | ||
| 43 | for name in dir(entry): | ||
| 44 | cls = getattr(entry, name) | ||
| 45 | if isinstance(cls, type) and issubclass(cls, Exception): | ||
| 46 | yield cls | ||
| 47 | |||
| 40 | 48 | ||
| 41 | def getExceptions(self): | 49 | def test_exception_lookup() -> None: |
| 42 | """Return all our custom exceptions.""" | 50 | """Make sure our introspection logic works.""" |
| 43 | for entry in imports: | 51 | classes = list(get_exceptions()) |
| 44 | for name in dir(entry): | 52 | assert error.HookError in classes |
| 45 | cls = getattr(entry, name) | 53 | # Don't assert the exact number to avoid being a change-detector test. |
| 46 | if isinstance(cls, type) and issubclass(cls, Exception): | 54 | assert len(classes) > 10 |
| 47 | yield cls | ||
| 48 | 55 | ||
| 49 | def testExceptionLookup(self): | ||
| 50 | """Make sure our introspection logic works.""" | ||
| 51 | classes = list(self.getExceptions()) | ||
| 52 | self.assertIn(error.HookError, classes) | ||
| 53 | # Don't assert the exact number to avoid being a change-detector test. | ||
| 54 | self.assertGreater(len(classes), 10) | ||
| 55 | 56 | ||
| 56 | def testPickle(self): | 57 | @pytest.mark.parametrize("cls", get_exceptions()) |
| 57 | """Try to pickle all the exceptions.""" | 58 | def test_pickle(cls: Type[Exception]) -> None: |
| 58 | for cls in self.getExceptions(): | 59 | """Try to pickle all the exceptions.""" |
| 59 | args = inspect.getfullargspec(cls.__init__).args[1:] | 60 | args = inspect.getfullargspec(cls.__init__).args[1:] |
| 60 | obj = cls(*args) | 61 | obj = cls(*args) |
| 61 | p = pickle.dumps(obj) | 62 | p = pickle.dumps(obj) |
| 62 | try: | 63 | try: |
| 63 | newobj = pickle.loads(p) | 64 | newobj = pickle.loads(p) |
| 64 | except Exception as e: # pylint: disable=broad-except | 65 | except Exception as e: |
| 65 | self.fail( | 66 | pytest.fail( |
| 66 | "Class %s is unable to be pickled: %s\n" | 67 | f"Class {cls} is unable to be pickled: {e}\n" |
| 67 | "Incomplete super().__init__(...) call?" % (cls, e) | 68 | "Incomplete super().__init__(...) call?" |
| 68 | ) | 69 | ) |
| 69 | self.assertIsInstance(newobj, cls) | 70 | assert isinstance(newobj, cls) |
| 70 | self.assertEqual(str(obj), str(newobj)) | 71 | assert str(obj) == str(newobj) |
