Skip to content

Commit fa36f54

Browse files
authored
ref(grouping): Simplify categorization test helper (#97543)
This is a slight simplification of one of the helpers in the categorization test module. It also simplifies one of the types slightly, and changes the tests so they run in alphabetical order.
1 parent 11e96af commit fa36f54

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

tests/sentry/grouping/test_categorization.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@
4646

4747
from __future__ import annotations
4848

49-
import contextlib
5049
import json # NOQA
5150
import os
5251
import uuid
5352
from collections.abc import Callable, Generator
53+
from contextlib import _GeneratorContextManager, contextmanager
5454
from typing import Any
5555

5656
import pytest
@@ -125,15 +125,17 @@ def get_stacktrace_render(data: dict[str, Any]) -> str:
125125

126126

127127
INPUTS = [
128-
CategorizationInput(fname) for fname in os.listdir(_fixture_path) if fname.endswith(".json")
128+
CategorizationInput(fname)
129+
for fname in sorted(os.listdir(_fixture_path))
130+
if fname.endswith(".json")
129131
]
130132

131133

132134
@pytest.mark.parametrize("input", INPUTS, ids=lambda x: x.filename[:-5].replace("-", "_"))
133135
def test_categorization(
134136
input: CategorizationInput,
135137
insta_snapshot: InstaSnapshotter,
136-
track_enhancers_coverage: Callable[[CategorizationInput], Any],
138+
track_enhancers_coverage: Callable[[CategorizationInput], _GeneratorContextManager[None]],
137139
) -> None:
138140
# XXX: In-process re-runs using pytest-watch or whatever will behave
139141
# wrongly because input.data is reused between tests, we do this for perf.
@@ -146,13 +148,11 @@ def test_categorization(
146148

147149
@pytest.fixture(scope="session", autouse=True)
148150
def track_enhancers_coverage() -> (
149-
Generator[
150-
Callable[[CategorizationInput], contextlib._GeneratorContextManager[None, None, None]]
151-
]
151+
Generator[Callable[[CategorizationInput], _GeneratorContextManager[None]]]
152152
):
153153
ran_tests = {}
154154

155-
@contextlib.contextmanager
155+
@contextmanager
156156
def inner(input: CategorizationInput) -> Generator[None]:
157157
ran_tests[input.filename] = True
158158
yield
@@ -225,20 +225,13 @@ def _strip_sensitive_keys(data: dict[str, Any], keep_keys: list[str]) -> bool:
225225

226226
keys_stripped = False
227227

228-
for key in list(data):
229-
if key not in keep_keys:
230-
del data[key]
231-
keys_stripped = True
232-
233-
elif data[key] is None:
234-
del data[key]
235-
keys_stripped = True
236-
237-
elif any(x in key.lower() for x in _DELETE_KEYWORDS):
238-
del data[key]
239-
keys_stripped = True
240-
241-
elif any(x in json.dumps(data[key]).lower() for x in _DELETE_KEYWORDS):
228+
for key in data:
229+
if (
230+
key not in keep_keys
231+
or data[key] is None
232+
or any(x in key.lower() for x in _DELETE_KEYWORDS)
233+
or any(x in json.dumps(data[key]).lower() for x in _DELETE_KEYWORDS)
234+
):
242235
del data[key]
243236
keys_stripped = True
244237

0 commit comments

Comments
 (0)