summaryrefslogtreecommitdiff
path: root/test/test_deprecation.py
diff options
context:
space:
mode:
authorLaurent Peuch <cortex@worlddomination.be>2020-08-25 22:11:27 +0200
committerLaurent Peuch <cortex@worlddomination.be>2020-08-25 22:11:27 +0200
commit0aedd28cb0eca8ba51f54db4fd2e171971569c7d (patch)
treeca1f9456c538e32523f64924ff5fc45605024858 /test/test_deprecation.py
parent34e17836ea4c00f200941a07d0b50147b061dd12 (diff)
downloadlogilab-common-0aedd28cb0eca8ba51f54db4fd2e171971569c7d.tar.gz
feat(deprecation): add structured informations to deprecation warnings
Diffstat (limited to 'test/test_deprecation.py')
-rw-r--r--test/test_deprecation.py241
1 files changed, 234 insertions, 7 deletions
diff --git a/test/test_deprecation.py b/test/test_deprecation.py
index 2508208..93fc0eb 100644
--- a/test/test_deprecation.py
+++ b/test/test_deprecation.py
@@ -31,7 +31,7 @@ class RawInputTC(TestCase):
# instead we just make sure it does not crash
def mock_warn(self, *args, **kwargs):
- self.messages.append(args[0])
+ self.messages.append(str(args[0]))
def setUp(self):
self.messages = []
@@ -83,7 +83,10 @@ class RawInputTC(TestCase):
OldClass()
self.assertEqual(
self.messages,
- ["[test_deprecation] class OldName is now available as test_deprecation.AnyClass"],
+ [
+ "[test_deprecation] class test_deprecation.OldName is now available as "
+ "test_deprecation.AnyClass"
+ ],
)
self.messages = []
@@ -93,7 +96,10 @@ class RawInputTC(TestCase):
AnyClass()
self.assertEqual(
self.messages,
- ["[test_deprecation] class AnyClass is now available as test_deprecation.AnyClass"],
+ [
+ "[test_deprecation] class test_deprecation.AnyClass is now available as "
+ "test_deprecation.AnyClass"
+ ],
)
def test_deprecated_func(self):
@@ -242,15 +248,236 @@ class RawInputTC(TestCase):
],
)
- def test_moved(self):
+ def test_callable_moved(self):
module = "data.deprecation"
- any_func = deprecation.callable_moved(module, "moving_target")
- any_func()
+ moving_target = deprecation.callable_moved(module, "moving_target")
+ moving_target()
self.assertEqual(
self.messages,
- ["[test_deprecation] object moving_target has been moved to module data.deprecation"],
+ [
+ "[test_deprecation] object test_deprecation.moving_target has been moved to "
+ "data.deprecation.moving_target"
+ ],
)
+class StructuredDeprecatedWarningsTest(TestCase):
+ def mock_warn(self, *args, **kwargs):
+ self.collected_warnings.append(args[0])
+
+ def setUp(self):
+ self.collected_warnings = []
+ deprecation.warn = self.mock_warn
+
+ def tearDown(self):
+ deprecation.warn = warnings.warn
+
+ def mk_func(self):
+ def any_func():
+ pass
+
+ return any_func
+
+ def test_class_deprecated(self):
+ class AnyClass(metaclass=deprecation.class_deprecated):
+ pass
+
+ AnyClass()
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.DEPRECATED)
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CLASS)
+
+ def test_class_renamed(self):
+ class AnyClass:
+ pass
+
+ OldClass = deprecation.class_renamed("OldClass", AnyClass)
+
+ OldClass()
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.RENAMED)
+ self.assertEqual(warning.old_name, "OldClass")
+ self.assertEqual(warning.new_name, "AnyClass")
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CLASS)
+
+ def test_class_moved(self):
+ class AnyClass:
+ pass
+
+ OldClass = deprecation.class_moved(new_class=AnyClass, old_name="OldName")
+ OldClass()
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.MOVED)
+ self.assertEqual(warning.old_module, "test_deprecation")
+ self.assertEqual(warning.new_module, "test_deprecation")
+ self.assertEqual(warning.old_name, "OldName")
+ self.assertEqual(warning.new_name, "AnyClass")
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CLASS)
+
+ self.collected_warnings = []
+
+ AnyClass = deprecation.class_moved(new_class=AnyClass)
+
+ AnyClass()
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.MOVED)
+ self.assertEqual(warning.old_module, "test_deprecation")
+ self.assertEqual(warning.new_module, "test_deprecation")
+ self.assertEqual(warning.old_name, "AnyClass")
+ self.assertEqual(warning.new_name, "AnyClass")
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CLASS)
+
+ def test_deprecated_func(self):
+ any_func = deprecation.callable_deprecated()(self.mk_func())
+ any_func()
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.DEPRECATED)
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CALLABLE)
+
+ any_func = deprecation.callable_deprecated("message")(self.mk_func())
+ any_func()
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.DEPRECATED)
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CALLABLE)
+
+ def test_deprecated_decorator(self):
+ @deprecation.callable_deprecated()
+ def any_func():
+ pass
+
+ any_func()
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.DEPRECATED)
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CALLABLE)
+
+ @deprecation.callable_deprecated("message")
+ def any_func():
+ pass
+
+ any_func()
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.DEPRECATED)
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CALLABLE)
+
+ def test_attribute_renamed(self):
+ @deprecation.attribute_renamed(old_name="old", new_name="new")
+ class SomeClass:
+ def __init__(self):
+ self.new = 42
+
+ some_class = SomeClass()
+
+ some_class.old == some_class.new
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.RENAMED)
+ self.assertEqual(warning.old_name, "old")
+ self.assertEqual(warning.new_name, "new")
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.ATTRIBUTE)
+
+ some_class.old = 43
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.RENAMED)
+ self.assertEqual(warning.old_name, "old")
+ self.assertEqual(warning.new_name, "new")
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.ATTRIBUTE)
+
+ del some_class.old
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.RENAMED)
+ self.assertEqual(warning.old_name, "old")
+ self.assertEqual(warning.new_name, "new")
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.ATTRIBUTE)
+
+ def test_argument_renamed(self):
+ @deprecation.argument_renamed(old_name="old", new_name="new")
+ def some_function(new):
+ return new
+
+ some_function(old=42)
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.RENAMED)
+ self.assertEqual(warning.old_name, "old")
+ self.assertEqual(warning.new_name, "new")
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.ARGUMENT)
+
+ def test_argument_removed(self):
+ @deprecation.argument_removed("old")
+ def some_function(new):
+ return new
+
+ some_function(new=10, old=20)
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.REMOVED)
+ self.assertEqual(warning.name, "old")
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.ARGUMENT)
+
+ def test_callable_renamed(self):
+ def any_func():
+ pass
+
+ old_func = deprecation.callable_renamed("old_func", any_func)
+ old_func()
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.RENAMED)
+ self.assertEqual(warning.old_name, "old_func")
+ self.assertEqual(warning.new_name, "any_func")
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CALLABLE)
+
+ def test_callable_moved(self):
+ module = "data.deprecation"
+ moving_target = deprecation.callable_moved(module, "moving_target")
+ moving_target()
+
+ self.assertEqual(len(self.collected_warnings), 1)
+ warning = self.collected_warnings.pop()
+
+ self.assertEqual(warning.operation, deprecation.DeprecationWarningOperation.MOVED)
+ self.assertEqual(warning.old_module, "test_deprecation")
+ self.assertEqual(warning.new_module, "data.deprecation")
+ self.assertEqual(warning.old_name, "moving_target")
+ self.assertEqual(warning.new_name, "moving_target")
+ self.assertEqual(warning.kind, deprecation.DeprecationWarningKind.CALLABLE)
+
+
if __name__ == "__main__":
unittest_main()