Repository navigation
Expand file tree
/
Copy pathtest_python_decorators.py
More file actions
513 lines (407 loc) · 15.1 KB
/
Copy pathtest_python_decorators.py
File metadata and controls
513 lines (407 loc) · 15.1 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
"""Test decorator parsing and extraction for Python functions, methods, and classes."""
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from codebase_rag.graph_updater import GraphUpdater
from codebase_rag.parser_loader import load_parsers
@pytest.fixture
def decorator_project(tmp_path: Path) -> Path:
"""Create a temporary project with various decorator patterns."""
project_path = tmp_path / "decorator_test"
project_path.mkdir()
# Create __init__.py
(project_path / "__init__.py").write_text("")
# Create decorators.py with comprehensive decorator examples
decorators_file = project_path / "decorators.py"
decorators_file.write_text(
'''"""Module with various decorator patterns."""
import functools
from dataclasses import dataclass
from typing import Any, Callable
# Custom decorators
def timing_decorator(func: Callable) -> Callable:
"""A simple timing decorator."""
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
def retry(attempts: int = 3):
"""Parameterized retry decorator."""
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
def wrapper(*args, **kwargs):
for attempt in range(attempts):
try:
return func(*args, **kwargs)
except Exception:
if attempt == attempts - 1:
raise
return func(*args, **kwargs)
return wrapper
return decorator
# Function decorators
@timing_decorator
def simple_decorated_function():
"""Function with single decorator."""
pass
@timing_decorator
@retry(attempts=5)
def multiple_decorated_function():
"""Function with multiple decorators."""
pass
@retry(attempts=3)
def parameterized_decorated_function(value: str) -> str:
"""Function with parameterized decorator."""
return f"processed: {value}"
# Class decorators
@dataclass
class DecoratedClass:
"""Class with decorator."""
name: str
value: int = 0
@dataclass(frozen=True)
class ParameterizedDecoratedClass:
"""Class with parameterized decorator."""
id: str
data: dict
# Property decorators and method decorators
class PropertyDecoratorExample:
"""Class demonstrating property and method decorators."""
def __init__(self, value: int):
self._value = value
self._cache = {}
@property
def value(self) -> int:
"""Property getter."""
return self._value
@value.setter
def value(self, new_value: int) -> None:
"""Property setter."""
self._value = new_value
@classmethod
def create_from_string(cls, data: str) -> "PropertyDecoratorExample":
"""Class method with decorator."""
return cls(int(data))
@staticmethod
def utility_function(x: int, y: int) -> int:
"""Static method with decorator."""
return x + y
@timing_decorator
def decorated_instance_method(self) -> str:
"""Instance method with custom decorator."""
return "decorated"
@functools.lru_cache(maxsize=128)
def cached_method(self, key: str) -> str:
"""Method with functools decorator."""
return f"cached_{key}"
# Nested function with decorator
def outer_with_decorators():
"""Function containing nested decorated function."""
@timing_decorator
def nested_decorated():
"""Nested function with decorator."""
pass
return nested_decorated
# Complex decorator combinations
@dataclass
@timing_decorator
class ComplexDecoratedClass:
"""Class with multiple decorators."""
data: str
@property
@timing_decorator
def computed_value(self) -> str:
"""Property with multiple decorators."""
return f"computed_{self.data}"
@classmethod
@retry(attempts=2)
def factory_method(cls, value: str) -> "ComplexDecoratedClass":
"""Class method with multiple decorators."""
return cls(value)
# Function with decorator that has complex arguments
@retry(attempts=5)
@functools.lru_cache(maxsize=256)
def complex_decorated_function(key: str, value: Any) -> dict:
"""Function with complex decorator arguments."""
return {"key": key, "value": value}
'''
)
return project_path
def test_simple_function_decorators(
decorator_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test that function decorators are properly extracted."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=decorator_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = decorator_project.name
# Expected functions with their decorators
expected_decorators = {
f"{project_name}.decorators.simple_decorated_function": ["timing_decorator"],
f"{project_name}.decorators.multiple_decorated_function": [
"timing_decorator",
"retry",
],
f"{project_name}.decorators.parameterized_decorated_function": ["retry"],
f"{project_name}.decorators.complex_decorated_function": [
"retry",
"functools.lru_cache",
],
}
# Get all Function node creation calls
function_calls = [
call
for call in mock_ingestor.ensure_node_batch.call_args_list
if call[0][0] == "Function"
]
# Verify decorators are extracted for functions
for call in function_calls:
func_props = call[0][1]
func_qn = func_props["qualified_name"]
if func_qn in expected_decorators:
assert "decorators" in func_props, (
f"Function {func_qn} should have decorators property"
)
expected = expected_decorators[func_qn]
actual = func_props["decorators"]
assert actual == expected, (
f"Function {func_qn}: expected decorators {expected}, got {actual}"
)
def test_class_decorators(decorator_project: Path, mock_ingestor: MagicMock) -> None:
"""Test that class decorators are properly extracted."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=decorator_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = decorator_project.name
# Expected classes with their decorators
expected_decorators = {
f"{project_name}.decorators.DecoratedClass": ["dataclass"],
f"{project_name}.decorators.ParameterizedDecoratedClass": ["dataclass"],
f"{project_name}.decorators.ComplexDecoratedClass": [
"dataclass",
"timing_decorator",
],
}
# Get all Class node creation calls
class_calls = [
call
for call in mock_ingestor.ensure_node_batch.call_args_list
if call[0][0] == "Class"
]
# Verify decorators are extracted for classes
for call in class_calls:
class_props = call[0][1]
class_qn = class_props["qualified_name"]
if class_qn in expected_decorators:
assert "decorators" in class_props, (
f"Class {class_qn} should have decorators property"
)
expected = expected_decorators[class_qn]
actual = class_props["decorators"]
assert actual == expected, (
f"Class {class_qn}: expected decorators {expected}, got {actual}"
)
def test_method_decorators(decorator_project: Path, mock_ingestor: MagicMock) -> None:
"""Test that method decorators are properly extracted."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=decorator_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = decorator_project.name
# Expected methods with their decorators
# Note: Properties create both getter and setter methods with same name
expected_decorators = {
f"{project_name}.decorators.PropertyDecoratorExample.create_from_string": [
"classmethod"
],
f"{project_name}.decorators.PropertyDecoratorExample.utility_function": [
"staticmethod"
],
f"{project_name}.decorators.PropertyDecoratorExample.decorated_instance_method": [
"timing_decorator"
],
f"{project_name}.decorators.PropertyDecoratorExample.cached_method": [
"functools.lru_cache"
],
f"{project_name}.decorators.ComplexDecoratedClass.factory_method": [
"classmethod",
"retry",
],
}
# Special handling for property methods (can be either getter or setter)
property_methods = {
f"{project_name}.decorators.PropertyDecoratorExample.value": [
["property"],
["value.setter"], # Either @property or @value.setter
],
f"{project_name}.decorators.ComplexDecoratedClass.computed_value": [
["property", "timing_decorator"],
["computed_value.setter", "timing_decorator"],
],
}
# Get all Method node creation calls
method_calls = [
call
for call in mock_ingestor.ensure_node_batch.call_args_list
if call[0][0] == "Method"
]
# Verify decorators are extracted for methods
for call in method_calls:
method_props = call[0][1]
method_qn = method_props["qualified_name"]
# Check regular methods
if method_qn in expected_decorators:
assert "decorators" in method_props, (
f"Method {method_qn} should have decorators property"
)
expected = expected_decorators[method_qn]
actual = method_props["decorators"]
assert actual == expected, (
f"Method {method_qn}: expected decorators {expected}, got {actual}"
)
# Check property methods (can be either getter or setter)
elif method_qn in property_methods:
assert "decorators" in method_props, (
f"Property method {method_qn} should have decorators property"
)
expected_variants = property_methods[method_qn]
actual = method_props["decorators"]
# Check if actual matches any of the expected variants
match_found = any(actual == expected for expected in expected_variants)
assert match_found, (
f"Property method {method_qn}: expected one of {expected_variants}, got {actual}"
)
def test_nested_function_decorators(
decorator_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test that decorators on nested functions are extracted."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=decorator_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = decorator_project.name
# Expected nested function with decorator
expected_qn = f"{project_name}.decorators.outer_with_decorators.nested_decorated"
expected_decorators = ["timing_decorator"]
# Get all Function node creation calls
function_calls = [
call
for call in mock_ingestor.ensure_node_batch.call_args_list
if call[0][0] == "Function"
]
# Find the nested decorated function
nested_func_found = False
for call in function_calls:
func_props = call[0][1]
if func_props["qualified_name"] == expected_qn:
nested_func_found = True
assert "decorators" in func_props, (
f"Nested function {expected_qn} should have decorators property"
)
actual = func_props["decorators"]
assert actual == expected_decorators, (
f"Nested function {expected_qn}: expected decorators {expected_decorators}, got {actual}"
)
assert nested_func_found, f"Nested decorated function {expected_qn} not found"
def test_decorator_with_complex_arguments(
decorator_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test that decorators with complex arguments are handled properly."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=decorator_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = decorator_project.name
# Test functions with parameterized decorators
test_cases = [
{
"qn": f"{project_name}.decorators.parameterized_decorated_function",
"expected": ["retry"], # Should extract decorator name, not full call
},
{
"qn": f"{project_name}.decorators.complex_decorated_function",
"expected": ["retry", "functools.lru_cache"],
},
]
# Get all Function node creation calls
function_calls = [
call
for call in mock_ingestor.ensure_node_batch.call_args_list
if call[0][0] == "Function"
]
for test_case in test_cases:
expected_qn = test_case["qn"]
expected_decorators = test_case["expected"]
func_found = False
for call in function_calls:
func_props = call[0][1]
if func_props["qualified_name"] == expected_qn:
func_found = True
actual = func_props["decorators"]
assert actual == expected_decorators, (
f"Function {expected_qn}: expected decorators {expected_decorators}, got {actual}"
)
assert func_found, f"Function {expected_qn} not found"
def test_empty_decorators_for_undecorated_functions(
decorator_project: Path, mock_ingestor: MagicMock
) -> None:
"""Test that functions without decorators have empty decorator lists."""
parsers, queries = load_parsers()
updater = GraphUpdater(
ingestor=mock_ingestor,
repo_path=decorator_project,
parsers=parsers,
queries=queries,
)
updater.run()
project_name = decorator_project.name
# Functions that should have no decorators
undecorated_functions = [
f"{project_name}.decorators.timing_decorator",
f"{project_name}.decorators.retry",
f"{project_name}.decorators.outer_with_decorators",
]
# Get all Function node creation calls
function_calls = [
call
for call in mock_ingestor.ensure_node_batch.call_args_list
if call[0][0] == "Function"
]
for expected_qn in undecorated_functions:
func_found = False
for call in function_calls:
func_props = call[0][1]
if func_props["qualified_name"] == expected_qn:
func_found = True
assert "decorators" in func_props, (
f"Function {expected_qn} should have decorators property"
)
actual = func_props["decorators"]
assert actual == [], (
f"Undecorated function {expected_qn} should have empty decorators, got {actual}"
)
if not func_found:
# Some functions might be nested, which is OK for this test
pass