File: run_tests.py

package info (click to toggle)
flake8-deprecated 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 132 kB
  • sloc: python: 247; sh: 5; makefile: 3
file content (187 lines) | stat: -rw-r--r-- 4,579 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from flake8_deprecated import Flake8Deprecated

import ast
import pytest
import textwrap


def check_code(source, expected_codes=None):
    """Check if the given source code generates the given flake8 errors

    If `expected_codes` is a string is converted to a list,
    if it is not given, then it is expected to **not** generate any error.
    """
    if isinstance(expected_codes, str):
        expected_codes = [expected_codes]
    elif expected_codes is None:
        expected_codes = []

    tree = ast.parse(textwrap.dedent(source))
    checker = Flake8Deprecated(tree)
    return_statements = list(checker.run())

    assert len(return_statements) == len(expected_codes)
    for item, code in zip(return_statements, expected_codes):
        assert item[2].startswith(f'{code} ')


def test_all_good():
    source = 'a = 3'
    check_code(source)


def test_ignore_comments():
    source = """
    a = 3
    # fortunately we remove all failIfEqual !
    """
    check_code(source)


def test_error():
    source = 'self.assertEquals()'
    check_code(source, 'D001')


def test_error_line():
    source = """
        import unittest
        unittest.assertEquals()
    """
    tree = ast.parse(textwrap.dedent(source))
    checker = Flake8Deprecated(tree)
    return_statements = list(checker.run())

    assert len(return_statements) == 1
    assert return_statements[0][0] == 3


def test_handle_subscripts():
    source = """
    funcs = [print]
    funcs[0]("Hello World")
    """
    check_code(source)


def test_error_column():
    source = """
        class Foo:
            def test_all_good(self):
                self.assertEquals()
    """
    tree = ast.parse(textwrap.dedent(source))
    checker = Flake8Deprecated(tree)
    return_statements = list(checker.run())

    assert len(return_statements) == 1
    assert return_statements[0][1] == 8


def test_nested_method_call():
    source = 'self.context.deep.chained.element.with_deprecated.assertEquals()'
    check_code(source, 'D001')


test_aliases = [
    {
        'name': old_alias.lower(),
        'code': old_alias,
    }
    for old_alias, _ in Flake8Deprecated(None).old_aliases.items()
]


@pytest.mark.parametrize(
    'example',
    test_aliases,
    ids=[t['name'] for t in test_aliases],
)
def test_code_suggestions(example):
    source = f'self.{example["code"]}()'
    check_code(source, 'D001')


decorator_test_cases = [
    {
        'name': 'decorator-basic',
        'code': '@assertEquals()',
    },
    {
        'name': 'decorator-basic-no-parenthesis',
        'code': '@assertEquals',
    },
    {
        'name': 'decorator-method-chain-basic',
        'code': '@obj.assertEquals()',
    },
    {
        'name': 'decorator-method-chain-deep',
        'code': '@obj.another.yet_anoher.last_one.assertEquals()',
    },
    {
        'name': 'decorator-method-chain-basic-no-parenthesis',
        'code': '@obj.assertEquals',
    },
    {
        'name': 'decorator-method-chain-deep-no-parenthesis',
        'code': '@obj.another.yet_anoher.last_one.assertEquals',
    },
    {
        'name': 'decorator-nested-first',
        'code': """
            @obj.assertEquals()
            @another_decorator()
        """,
    },
    {
        'name': 'decorator-nested-last',
        'code': """
            @another_decorator()
            @obj.assertEquals()
        """,
    },
    {
        'name': 'decorator-nested-in-between',
        'code': """
            @another_decorator()
            @obj.assertEquals
            @yet_another_decorator()
        """,
    },
    {
        'name': 'full-example-decorator-chained-in-method',
        'code': """
            class Foo:
                @another_decorator()
                @obj.another_one.yet_one_more.assertEquals
                def my_method(self): ...
        """,
    },
    {
        'name': 'full-example-decorator-basic-in-method',
        'code': """
            class Foo:
                @another_decorator()
                @assertEquals
                def my_method(self): ...
        """,
    },
]


@pytest.mark.parametrize(
    'testcase', decorator_test_cases, ids=[t['name'] for t in decorator_test_cases]
)
def test_decorators(testcase):
    if 'nested' in testcase['name']:
        indentation = ' ' * 4 * 3
        source = f'{testcase["code"]}\n{indentation}def my_function(): ...'
    elif 'full-example' in testcase['name']:
        source = testcase['code']
    else:
        source = f"""
        {testcase['code']}
        def my_function(): ...
        """
    check_code(source, 'D001')