-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_utils.py
More file actions
67 lines (54 loc) · 1.88 KB
/
test_utils.py
File metadata and controls
67 lines (54 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from inspect import iscoroutinefunction
import pytest
from common.utils import apply_decorator_to_methods
@pytest.mark.parametrize(
"apply_to_protected_methods",
[
pytest.param(True, id="protected_methods"),
pytest.param(False, id="no_protected_methods"),
],
)
@pytest.mark.parametrize(
"apply_to_private_methods",
[
pytest.param(True, id="private_methods"),
pytest.param(False, id="no_private_methods"),
],
)
async def test_class_decorator(
apply_to_protected_methods: bool,
apply_to_private_methods: bool,
):
def add_ten_decorator(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return result + 10
async def async_wrapper(*args, **kwargs):
result = await func(*args, **kwargs)
return result + 10
return wrapper if not iscoroutinefunction(func) else async_wrapper
@apply_decorator_to_methods(
decorator=add_ten_decorator,
protected_methods=apply_to_protected_methods,
private_methods=apply_to_private_methods,
)
class MyClass:
def get_public(self):
return 10
def _get_protected(self):
return 10
def __get_private(self):
return 10
async def get_apublic(self):
return 10
async def _get_aprotected(self):
return 10
async def __get_aprivate(self):
return 10
c = MyClass()
assert c.get_public() == 20
assert c._get_protected() == 20 if apply_to_protected_methods else 10
assert c._MyClass__get_private() == 20 if apply_to_private_methods else 10 # type: ignore
assert await c.get_apublic() == 20
assert await c._get_aprotected() == 20 if apply_to_protected_methods else 10
assert await c._MyClass__get_aprivate() == 20 if apply_to_private_methods else 10 # type: ignore