From 57593631a13acddb9715ef3ba6f51679ea145f6a Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Wed, 20 Jan 2021 16:10:55 -0800 Subject: [PATCH] Add unit tests --- tests/utils/decorators_tests.py | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/tests/utils/decorators_tests.py b/tests/utils/decorators_tests.py index 84812546926f4..1774925ef4bbc 100644 --- a/tests/utils/decorators_tests.py +++ b/tests/utils/decorators_tests.py @@ -14,6 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +import warnings from unittest.mock import call, Mock from superset.utils import decorators @@ -41,3 +42,81 @@ def myfunc(arg1: int, arg2: int, kwarg1: str = "abc", kwarg2: int = 2): result = myfunc(1, 0, kwarg1="haha", kwarg2=2) mock.assert_has_calls([call(1, "abc"), call(1, "haha")]) self.assertEqual(result, 3) + + def test_guard_func(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + + @decorators.guard("`x=c{#?3CakO9ObP>|Wn") + def some_function(a, b): + return a + b + + # should trigger no warnings + assert len(w) == 0 + + def test_guard_func_modified(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + + @decorators.guard("`x=c{#?3CakO9ObP>|Wn") + def some_function(a, b, c): + return a + b + + assert len(w) == 1 + + def test_guard_class(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + + @decorators.guard("x-@vK+|sloQN3GFZK8l<") + class SomeClass: + def __init__(self, a, b): + self.a = a + self.b = b + + # should trigger no warnings + assert len(w) == 0 + + def test_guard_class_change_init(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + + @decorators.guard("x-@vK+|sloQN3GFZK8l<") + class SomeClass: + def __init__(self, a, b, c): + self.a = a + self.b = b + self.c = c + + assert len(w) == 1 + + def test_guard_class_add_public_method(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + + @decorators.guard("x-@vK+|sloQN3GFZK8l<") + class SomeClass: + def __init__(self, a, b): + self.a = a + self.b = b + + def add(self): + return self.a + self.b + + assert len(w) == 1 + + def test_guard_class_add_private_method(self): + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + + @decorators.guard("x-@vK+|sloQN3GFZK8l<") + class SomeClass: + def __init__(self, a, b): + self.a = a + self.b = b + + def _add(self): + return self.a + self.b + + # should trigger no warnings + assert len(w) == 0