Skip to content

Commit a9428fd

Browse files
committed
tests
1 parent a3b6fd7 commit a9428fd

8 files changed

+48
-34
lines changed

tests/extensions/test_base_extensions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ def test_nothing_found() -> None:
2525
"""
2626
config = FakeConfig()
2727

28-
r = FakeExtension(config, VerboseEcho()) # type: ignore
28+
r = FakeExtension(config, VerboseEcho())
2929
with open('tests/extensions/base_test_files/empty.foo') as f:
3030
r.search(f)
3131

3232

3333
def test_strip_single_line_comment_tokens() -> None:
3434
config = FakeConfig()
3535

36-
extension = FakeExtension(config, VerboseEcho()) # type: ignore
36+
extension = FakeExtension(config, VerboseEcho())
3737
text = """baz line1
3838
baz line2
3939
bazline3

tests/extensions/test_extension_python.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""
22
Tests for the Python static extension
33
"""
4-
from typing import List, Tuple
5-
64
import pytest
75

86
from code_annotations.base import AnnotationConfig
@@ -88,7 +86,7 @@ def test_grouping_and_choice_failures(test_file: str, expected_exit_code: int, e
8886
]
8987
),
9088
])
91-
def test_multi_line_annotations(test_file: str, annotations: List[Tuple[str, str]]) -> None:
89+
def test_multi_line_annotations(test_file: str, annotations: list[tuple[str, str]]) -> None:
9290
config = AnnotationConfig('tests/test_configurations/.annotations_test')
9391
annotator = PythonAnnotationExtension(config, VerboseEcho())
9492

tests/helpers.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
from click.testing import CliRunner
1010
from click.testing import Result as ClickTestResult
11+
from stevedore import named
1112

12-
from code_annotations.base import BaseSearch
13+
from code_annotations.base import AnnotationConfig, BaseSearch
1314
from code_annotations.cli import entry_point
1415
from code_annotations.helpers import VerboseEcho
1516

@@ -41,16 +42,30 @@
4142
""".format(DEFAULT_FAKE_SAFELIST_PATH)
4243

4344

44-
class FakeConfig:
45+
class FakeConfig(AnnotationConfig):
4546
"""
4647
Simple config for testing without reading a config file.
4748
"""
4849

49-
annotations: dict[str, t.Any] = {}
50-
annotation_regexes: list[str] = []
51-
annotation_tokens: list[str] = []
52-
groups: t.Union[list[str], dict[str, list[str]]] = []
53-
echo = VerboseEcho()
50+
# annotations: dict[str, t.Any] = {}
51+
# annotation_regexes: list[str] = []
52+
# annotation_tokens: list[str] = []
53+
# groups: t.Union[list[str], dict[str, list[str]]] = []
54+
# echo = VerboseEcho()
55+
56+
def __init__(self) -> None: # pylint: disable=super-init-not-called
57+
"""
58+
Override the base __init__ to skip reading and parsing the config.
59+
"""
60+
self.groups: dict[str, list[str]] = {}
61+
self.choices: dict[str, list[str]] = {}
62+
self.optional_groups: list[str] = []
63+
self.annotation_tokens: list[str] = []
64+
self.annotation_regexes: list[str] = []
65+
self.mgr: named.NamedExtensionManager | None = None
66+
self.coverage_target: float | None = None
67+
self.echo = VerboseEcho()
68+
5469

5570

5671
class FakeSearch(BaseSearch):
@@ -133,6 +148,7 @@ def call_script_isolated(
133148
cleared. Use this if you need access to non-report files in the temp filesystem.
134149
test_filesystem_report_cb: Callback function, called after the command is run, before the temp filesystem
135150
is cleared. Callback is called with the raw text contents of the report file.
151+
fake_safelist_data: Raw text to write to the safelist file before the command is called.
136152
137153
Returns:
138154
click.testing.Result: Result from the `CliRunner.invoke()` call.

tests/test_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
def test_get_group_for_token_missing_token() -> None:
1515
config = FakeConfig()
16-
search = FakeSearch(config) # type: ignore
16+
search = FakeSearch(config)
1717
assert search._get_group_for_token('foo') is None # pylint: disable=protected-access
1818

1919

@@ -23,7 +23,7 @@ def test_get_group_for_token_multiple_groups() -> None:
2323
'group1': ['token1'],
2424
'group2': ['token2', 'foo']
2525
}
26-
search = FakeSearch(config) # type: ignore
26+
search = FakeSearch(config)
2727
assert search._get_group_for_token('foo') == 'group2' # pylint: disable=protected-access
2828

2929

@@ -85,7 +85,7 @@ def test_format_results_for_report() -> None:
8585
'group2': ['token2', 'foo']
8686
}
8787

88-
search = FakeSearch(config) # type: ignore
88+
search = FakeSearch(config)
8989

9090
# Create a fake result set for _format_results_for_report to work on
9191
fake_results: OrderedDict[str, list[dict[str, t.Any]]] = OrderedDict()

tests/test_django_coverage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def get_models(self) -> tuple[FakeModelClass, ...]:
117117
),
118118
])
119119
def test_coverage_thresholds(
120-
local_models: t.List[FakeModelClass],
120+
local_models: list[FakeModelClass],
121121
should_succeed: bool,
122122
expected_message: str,
123123
**kwargs: t.Any

tests/test_django_generate_safelist.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
),
3737
])
3838
def test_seeding_safelist(
39-
local_models: t.List[MagicMock],
40-
non_local_models: t.List[MagicMock],
39+
local_models: list[MagicMock],
40+
non_local_models: list[MagicMock],
4141
**kwargs: t.Any
4242
) -> None:
4343
"""

tests/test_django_list_local_models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
),
3535
])
3636
def test_listing_local_models(
37-
local_model_ids: t.List[MagicMock],
38-
non_local_model_ids: t.List[MagicMock],
37+
local_model_ids: list[MagicMock],
38+
non_local_model_ids: list[MagicMock],
3939
**kwargs: t.Any
4040
) -> None:
4141
"""

tests/test_find_django.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55
import sys
66
import typing as t
7-
from unittest.mock import DEFAULT, patch
7+
from unittest.mock import DEFAULT, patch, MagicMock
88

99
from code_annotations.find_django import DjangoSearch
1010
from tests.fake_models import (
@@ -27,7 +27,7 @@
2727
'code_annotations.find_django.DjangoSearch',
2828
get_models_requiring_annotations=DEFAULT
2929
)
30-
def test_find_django_simple_success(**kwargs: t.Any) -> None:
30+
def test_find_django_simple_success(**kwargs: MagicMock) -> None:
3131
"""
3232
Tests the basic case where all models have annotations, with an empty safelist.
3333
"""
@@ -79,7 +79,7 @@ def report_callback(report_contents: str) -> None:
7979
'code_annotations.find_django.DjangoSearch',
8080
get_models_requiring_annotations=DEFAULT
8181
)
82-
def test_find_django_no_viable_models(**kwargs: t.Any) -> None:
82+
def test_find_django_no_viable_models(**kwargs: MagicMock) -> None:
8383
"""
8484
Tests the basic case where all models have annotations, with an empty safelist.
8585
"""
@@ -105,7 +105,7 @@ def test_find_django_no_viable_models(**kwargs: t.Any) -> None:
105105
'code_annotations.find_django.DjangoSearch',
106106
get_models_requiring_annotations=DEFAULT
107107
)
108-
def test_find_django_model_not_annotated(**kwargs: t.Any) -> None:
108+
def test_find_django_model_not_annotated(**kwargs: MagicMock) -> None:
109109
"""
110110
Test that a non-annotated model fails.
111111
"""
@@ -132,7 +132,7 @@ def test_find_django_model_not_annotated(**kwargs: t.Any) -> None:
132132
'code_annotations.find_django.DjangoSearch',
133133
get_models_requiring_annotations=DEFAULT
134134
)
135-
def test_find_django_model_in_safelist_not_annotated(**kwargs: t.Any) -> None:
135+
def test_find_django_model_in_safelist_not_annotated(**kwargs: MagicMock) -> None:
136136
"""
137137
Test that a safelisted model with no annotations fails.
138138
"""
@@ -166,7 +166,7 @@ def test_find_django_model_in_safelist_not_annotated(**kwargs: t.Any) -> None:
166166
'code_annotations.find_django.DjangoSearch',
167167
get_models_requiring_annotations=DEFAULT
168168
)
169-
def test_find_django_model_in_safelist_annotated(**kwargs: t.Any) -> None:
169+
def test_find_django_model_in_safelist_annotated(**kwargs: MagicMock) -> None:
170170
"""
171171
Test that a safelisted model succeeds.
172172
"""
@@ -200,7 +200,7 @@ def test_find_django_model_in_safelist_annotated(**kwargs: t.Any) -> None:
200200
'code_annotations.find_django.DjangoSearch',
201201
get_models_requiring_annotations=DEFAULT
202202
)
203-
def test_find_django_no_safelist(**kwargs: t.Any) -> None:
203+
def test_find_django_no_safelist(**kwargs: MagicMock) -> None:
204204
"""
205205
Test that we fail when there is no safelist.
206206
"""
@@ -209,7 +209,7 @@ def test_find_django_no_safelist(**kwargs: t.Any) -> None:
209209

210210
result = call_script_isolated(
211211
['django_find_annotations', '--config_file', 'test_config.yml', '--lint', '--report'],
212-
fake_safelist_data="", # Empty string, not None
212+
fake_safelist_data="",
213213
)
214214

215215
assert result.exit_code == EXIT_CODE_FAILURE
@@ -221,7 +221,7 @@ def test_find_django_no_safelist(**kwargs: t.Any) -> None:
221221
'code_annotations.find_django.DjangoSearch',
222222
get_models_requiring_annotations=DEFAULT
223223
)
224-
def test_find_django_in_safelist_and_annotated(**kwargs: t.Any) -> None:
224+
def test_find_django_in_safelist_and_annotated(**kwargs: MagicMock) -> None:
225225
"""
226226
Test that a model which is annotated and also in the safelist fails.
227227
"""
@@ -249,7 +249,7 @@ def test_find_django_in_safelist_and_annotated(**kwargs: t.Any) -> None:
249249
'code_annotations.find_django.DjangoSearch',
250250
get_models_requiring_annotations=DEFAULT
251251
)
252-
def test_find_django_no_docstring(**kwargs: t.Any) -> None:
252+
def test_find_django_no_docstring(**kwargs: MagicMock) -> None:
253253
"""
254254
Test that a model with no docstring doesn't break anything.
255255
"""
@@ -275,7 +275,7 @@ def test_find_django_no_docstring(**kwargs: t.Any) -> None:
275275
'code_annotations.find_django.DjangoSearch',
276276
get_models_requiring_annotations=DEFAULT
277277
)
278-
def test_find_django_ordering_error(**kwargs: t.Any) -> None:
278+
def test_find_django_ordering_error(**kwargs: MagicMock) -> None:
279279
"""
280280
Tests broken annotations to make sure the error paths work.
281281
"""
@@ -300,7 +300,7 @@ def test_find_django_ordering_error(**kwargs: t.Any) -> None:
300300
'code_annotations.find_django.DjangoSearch',
301301
get_models_requiring_annotations=DEFAULT
302302
)
303-
def test_find_django_without_linting(**kwargs: t.Any) -> None:
303+
def test_find_django_without_linting(**kwargs: MagicMock) -> None:
304304
"""
305305
Tests to make sure reports will be written in the case of errors, if linting is off.
306306
"""
@@ -326,7 +326,7 @@ def test_find_django_without_linting(**kwargs: t.Any) -> None:
326326
'code_annotations.find_django.DjangoSearch',
327327
get_models_requiring_annotations=DEFAULT
328328
)
329-
def test_find_django_without_report(**kwargs: t.Any) -> None:
329+
def test_find_django_without_report(**kwargs: MagicMock) -> None:
330330
"""
331331
Tests to make sure reports will be written in the case of errors, if linting is off.
332332
"""
@@ -484,7 +484,7 @@ def test_setup_django(mock_django_setup: t.Any) -> None:
484484
'code_annotations.find_django.DjangoSearch',
485485
get_models_requiring_annotations=DEFAULT
486486
)
487-
def test_find_django_no_action(**kwargs: t.Any) -> None:
487+
def test_find_django_no_action(**kwargs: MagicMock) -> None:
488488
"""
489489
Test that we fail when there is no action specified.
490490
"""

0 commit comments

Comments
 (0)