/
test_async_yield_magic_methods.py
148 lines (122 loc) · 3.17 KB
/
test_async_yield_magic_methods.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import pytest
from wemake_python_styleguide.violations.oop import AsyncMagicMethodViolation
from wemake_python_styleguide.visitors.ast.classes import WrongMethodVisitor
sync_method = """
class Example(object):
def {0}(self, *args, **kwargs):
{1}
"""
async_method = """
class Example(object):
async def {0}(self, *args, **kwargs):
{1}
"""
@pytest.mark.parametrize('template', [
sync_method,
async_method,
])
@pytest.mark.parametrize('method', [
'__aiter__',
])
@pytest.mark.parametrize('statement', [
'yield',
'yield 1',
])
def test_yield_is_always_allowed_in_aiter(
assert_errors,
parse_ast_tree,
default_options,
template,
method,
statement,
):
"""Testing that the `__aiter__` can always have `yield`."""
tree = parse_ast_tree(template.format(method, statement))
visitor = WrongMethodVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [])
@pytest.mark.parametrize('method', [
'__aiter__',
])
@pytest.mark.parametrize('statement', [
'return some_async_iterator()',
])
def test_wrong_async_magic_used(
assert_errors,
assert_error_text,
parse_ast_tree,
default_options,
method,
statement,
):
"""Testing that the method cannot be a coroutine."""
tree = parse_ast_tree(async_method.format(method, statement))
visitor = WrongMethodVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [AsyncMagicMethodViolation])
assert_error_text(visitor, method)
@pytest.mark.parametrize('method', [
'__aiter__',
])
@pytest.mark.parametrize('statement', [
'yield',
'yield 1',
])
def test_correct_async_yield_magic_used(
assert_errors,
parse_ast_tree,
default_options,
method,
statement,
):
"""Testing that the method can be an async generator."""
tree = parse_ast_tree(async_method.format(method, statement))
visitor = WrongMethodVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [])
@pytest.mark.parametrize('method', [
'__aiter__',
])
@pytest.mark.parametrize('statement', [
'return some_async_iterator()',
])
def test_correct_sync_magic_used(
assert_errors,
parse_ast_tree,
default_options,
method,
statement,
):
"""Testing that the method can be a normal method."""
tree = parse_ast_tree(sync_method.format(method, statement))
visitor = WrongMethodVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [])
# Examples:
correct_nested_example = """
class Some:
{0}def __aiter__(self):
async def inner():
yield 1
return inner()
"""
@pytest.mark.parametrize('example', [
correct_nested_example,
])
@pytest.mark.parametrize('mode', [
# We don't use `mode()` fixture here, because we have a nested func.
'', # sync
'async ',
])
def test_correct_examples(
assert_errors,
parse_ast_tree,
default_options,
example,
mode,
):
"""Testing specific real-life examples that should be working."""
tree = parse_ast_tree(example.format(mode))
visitor = WrongMethodVisitor(default_options, tree=tree)
visitor.run()
assert_errors(visitor, [])