Navegador

navegador / tests / test_enrichment_django.py
Blame History Raw 464 lines
1
"""Tests for navegador.enrichment.django — DjangoEnricher."""
2
3
from unittest.mock import MagicMock, call
4
5
import pytest
6
7
from navegador.enrichment.base import EnrichmentResult, FrameworkEnricher
8
from navegador.enrichment.django import DjangoEnricher
9
from navegador.graph.store import GraphStore
10
11
12
# ── Helpers ───────────────────────────────────────────────────────────────────
13
14
15
def _mock_store(result_set=None):
16
"""Return a GraphStore backed by a mock FalkorDB graph."""
17
client = MagicMock()
18
graph = MagicMock()
19
graph.query.return_value = MagicMock(result_set=result_set)
20
client.select_graph.return_value = graph
21
return GraphStore(client)
22
23
24
def _store_with_side_effect(side_effect):
25
"""Return a GraphStore whose graph.query uses the given side_effect callable."""
26
client = MagicMock()
27
graph = MagicMock()
28
graph.query.side_effect = side_effect
29
client.select_graph.return_value = graph
30
return GraphStore(client)
31
32
33
# ── Metadata ──────────────────────────────────────────────────────────────────
34
35
36
class TestDjangoEnricherMetadata:
37
def test_framework_name(self):
38
enricher = DjangoEnricher(_mock_store())
39
assert enricher.framework_name == "django"
40
41
def test_detection_patterns_contains_django(self):
42
enricher = DjangoEnricher(_mock_store())
43
assert "django" in enricher.detection_patterns
44
45
def test_detection_patterns_contains_django_conf(self):
46
enricher = DjangoEnricher(_mock_store())
47
assert "django.conf" in enricher.detection_patterns
48
49
def test_detection_patterns_contains_django_db(self):
50
enricher = DjangoEnricher(_mock_store())
51
assert "django.db" in enricher.detection_patterns
52
53
def test_detection_patterns_contains_django_http(self):
54
enricher = DjangoEnricher(_mock_store())
55
assert "django.http" in enricher.detection_patterns
56
57
def test_detection_patterns_is_list_of_strings(self):
58
enricher = DjangoEnricher(_mock_store())
59
patterns = enricher.detection_patterns
60
assert isinstance(patterns, list)
61
assert all(isinstance(p, str) for p in patterns)
62
63
def test_is_subclass_of_framework_enricher(self):
64
assert issubclass(DjangoEnricher, FrameworkEnricher)
65
66
def test_enrich_returns_enrichment_result(self):
67
store = _mock_store(result_set=[])
68
enricher = DjangoEnricher(store)
69
result = enricher.enrich()
70
assert isinstance(result, EnrichmentResult)
71
72
73
# ── detect() ──────────────────────────────────────────────────────────────────
74
75
76
class TestDjangoEnricherDetect:
77
def test_detect_returns_true_when_urls_py_present(self):
78
store = _mock_store(result_set=[[1]])
79
enricher = DjangoEnricher(store)
80
assert enricher.detect() is True
81
82
def test_detect_returns_false_when_no_patterns_match(self):
83
store = _mock_store(result_set=[[0]])
84
enricher = DjangoEnricher(store)
85
assert enricher.detect() is False
86
87
def test_detect_returns_false_when_result_set_empty(self):
88
store = _mock_store(result_set=[])
89
enricher = DjangoEnricher(store)
90
assert enricher.detect() is False
91
92
def test_detect_returns_false_when_result_set_none(self):
93
store = _mock_store(result_set=None)
94
enricher = DjangoEnricher(store)
95
assert enricher.detect() is False
96
97
def test_detect_short_circuits_on_first_match(self):
98
store = _mock_store(result_set=[[5]])
99
enricher = DjangoEnricher(store)
100
assert enricher.detect() is True
101
# Should only query once (short-circuit)
102
assert store._graph.query.call_count == 1
103
104
def test_detect_tries_all_patterns_before_giving_up(self):
105
store = _mock_store(result_set=[[0]])
106
enricher = DjangoEnricher(store)
107
enricher.detect()
108
# All detection_patterns are checked, then all detection_files
109
expected = len(enricher.detection_patterns) + len(enricher.detection_files)
110
assert store._graph.query.call_count == expected
111
112
113
# ── enrich() — views ──────────────────────────────────────────────────────────
114
115
116
class TestDjangoEnricherViews:
117
def _make_store_for_views(self, view_rows):
118
"""
119
Return a store that yields view_rows for the views query and [] for everything else.
120
"""
121
call_count = [0]
122
123
def side_effect(cypher, params=None):
124
call_count[0] += 1
125
# First enrich query targets views.py functions
126
if "views.py" in cypher and "Function" in cypher and call_count[0] == 1:
127
return MagicMock(result_set=view_rows)
128
return MagicMock(result_set=[])
129
130
return _store_with_side_effect(side_effect)
131
132
def test_promotes_functions_in_views_py(self):
133
view_rows = [["my_view", "app/views.py"], ["another_view", "app/views.py"]]
134
store = self._make_store_for_views(view_rows)
135
enricher = DjangoEnricher(store)
136
result = enricher.enrich()
137
assert result.patterns_found["views"] == 2
138
assert result.promoted >= 2
139
140
def test_no_views_produces_zero_count(self):
141
store = _mock_store(result_set=[])
142
enricher = DjangoEnricher(store)
143
result = enricher.enrich()
144
assert result.patterns_found["views"] == 0
145
146
def test_view_promote_node_called_with_semantic_type_view(self):
147
view_rows = [["index_view", "myapp/views.py"]]
148
149
call_count = [0]
150
151
def side_effect(cypher, params=None):
152
call_count[0] += 1
153
if "views.py" in cypher and "Function" in cypher and call_count[0] == 1:
154
return MagicMock(result_set=view_rows)
155
# _promote_node calls also go through store.query — return empty
156
return MagicMock(result_set=[])
157
158
store = _store_with_side_effect(side_effect)
159
enricher = DjangoEnricher(store)
160
enricher.enrich()
161
162
# Find the _promote_node call for "index_view"
163
promote_calls = [
164
c for c in store._graph.query.call_args_list
165
if "SET n.semantic_type" in c[0][0]
166
and c[0][1].get("name") == "index_view"
167
]
168
assert len(promote_calls) == 1
169
assert promote_calls[0][0][1]["semantic_type"] == "View"
170
171
172
# ── enrich() — models ─────────────────────────────────────────────────────────
173
174
175
class TestDjangoEnricherModels:
176
def _make_store_for_models(self, model_rows):
177
call_count = [0]
178
179
def side_effect(cypher, params=None):
180
call_count[0] += 1
181
# Second enrich query targets Model-inheriting classes
182
if "INHERITS" in cypher and call_count[0] == 2:
183
return MagicMock(result_set=model_rows)
184
return MagicMock(result_set=[])
185
186
return _store_with_side_effect(side_effect)
187
188
def test_promotes_classes_inheriting_from_model(self):
189
model_rows = [["UserProfile", "app/models.py"], ["Post", "blog/models.py"]]
190
store = self._make_store_for_models(model_rows)
191
enricher = DjangoEnricher(store)
192
result = enricher.enrich()
193
assert result.patterns_found["models"] == 2
194
assert result.promoted >= 2
195
196
def test_no_models_produces_zero_count(self):
197
store = _mock_store(result_set=[])
198
enricher = DjangoEnricher(store)
199
result = enricher.enrich()
200
assert result.patterns_found["models"] == 0
201
202
def test_model_promote_node_called_with_semantic_type_model(self):
203
model_rows = [["Article", "news/models.py"]]
204
205
call_count = [0]
206
207
def side_effect(cypher, params=None):
208
call_count[0] += 1
209
if "INHERITS" in cypher and call_count[0] == 2:
210
return MagicMock(result_set=model_rows)
211
return MagicMock(result_set=[])
212
213
store = _store_with_side_effect(side_effect)
214
enricher = DjangoEnricher(store)
215
enricher.enrich()
216
217
promote_calls = [
218
c for c in store._graph.query.call_args_list
219
if "SET n.semantic_type" in c[0][0]
220
and c[0][1].get("name") == "Article"
221
]
222
assert len(promote_calls) == 1
223
assert promote_calls[0][0][1]["semantic_type"] == "Model"
224
225
226
# ── enrich() — serializers ────────────────────────────────────────────────────
227
228
229
class TestDjangoEnricherSerializers:
230
def _make_store_for_serializers(self, serializer_rows):
231
call_count = [0]
232
233
def side_effect(cypher, params=None):
234
call_count[0] += 1
235
# Fourth enrich query targets serializers.py classes
236
if "serializers.py" in cypher and call_count[0] == 4:
237
return MagicMock(result_set=serializer_rows)
238
return MagicMock(result_set=[])
239
240
return _store_with_side_effect(side_effect)
241
242
def test_promotes_classes_in_serializers_py(self):
243
serializer_rows = [["UserSerializer", "api/serializers.py"]]
244
store = self._make_store_for_serializers(serializer_rows)
245
enricher = DjangoEnricher(store)
246
result = enricher.enrich()
247
assert result.patterns_found["serializers"] == 1
248
assert result.promoted >= 1
249
250
def test_no_serializers_produces_zero_count(self):
251
store = _mock_store(result_set=[])
252
enricher = DjangoEnricher(store)
253
result = enricher.enrich()
254
assert result.patterns_found["serializers"] == 0
255
256
def test_serializer_promote_node_called_with_semantic_type_serializer(self):
257
serializer_rows = [["PostSerializer", "blog/serializers.py"]]
258
259
call_count = [0]
260
261
def side_effect(cypher, params=None):
262
call_count[0] += 1
263
if "serializers.py" in cypher and call_count[0] == 4:
264
return MagicMock(result_set=serializer_rows)
265
return MagicMock(result_set=[])
266
267
store = _store_with_side_effect(side_effect)
268
enricher = DjangoEnricher(store)
269
enricher.enrich()
270
271
promote_calls = [
272
c for c in store._graph.query.call_args_list
273
if "SET n.semantic_type" in c[0][0]
274
and c[0][1].get("name") == "PostSerializer"
275
]
276
assert len(promote_calls) == 1
277
assert promote_calls[0][0][1]["semantic_type"] == "Serializer"
278
279
280
# ── enrich() — tasks ──────────────────────────────────────────────────────────
281
282
283
class TestDjangoEnricherTasks:
284
def _make_store_for_tasks(self, task_rows):
285
call_count = [0]
286
287
def side_effect(cypher, params=None):
288
call_count[0] += 1
289
# Fifth enrich query targets tasks.py functions or @task decorator
290
if "tasks.py" in cypher and call_count[0] == 5:
291
return MagicMock(result_set=task_rows)
292
return MagicMock(result_set=[])
293
294
return _store_with_side_effect(side_effect)
295
296
def test_promotes_functions_in_tasks_py(self):
297
task_rows = [["send_email", "myapp/tasks.py"], ["process_order", "shop/tasks.py"]]
298
store = self._make_store_for_tasks(task_rows)
299
enricher = DjangoEnricher(store)
300
result = enricher.enrich()
301
assert result.patterns_found["tasks"] == 2
302
assert result.promoted >= 2
303
304
def test_no_tasks_produces_zero_count(self):
305
store = _mock_store(result_set=[])
306
enricher = DjangoEnricher(store)
307
result = enricher.enrich()
308
assert result.patterns_found["tasks"] == 0
309
310
def test_task_cypher_includes_decorator_check(self):
311
store = _mock_store(result_set=[])
312
enricher = DjangoEnricher(store)
313
enricher.enrich()
314
315
# Find the tasks query
316
tasks_queries = [
317
c[0][0] for c in store._graph.query.call_args_list
318
if "tasks.py" in c[0][0]
319
]
320
assert len(tasks_queries) == 1
321
assert "decorators" in tasks_queries[0]
322
assert "task" in tasks_queries[0]
323
324
def test_task_promote_node_called_with_semantic_type_task(self):
325
task_rows = [["send_welcome_email", "users/tasks.py"]]
326
327
call_count = [0]
328
329
def side_effect(cypher, params=None):
330
call_count[0] += 1
331
if "tasks.py" in cypher and call_count[0] == 5:
332
return MagicMock(result_set=task_rows)
333
return MagicMock(result_set=[])
334
335
store = _store_with_side_effect(side_effect)
336
enricher = DjangoEnricher(store)
337
enricher.enrich()
338
339
promote_calls = [
340
c for c in store._graph.query.call_args_list
341
if "SET n.semantic_type" in c[0][0]
342
and c[0][1].get("name") == "send_welcome_email"
343
]
344
assert len(promote_calls) == 1
345
assert promote_calls[0][0][1]["semantic_type"] == "Task"
346
347
348
# ── enrich() — URL patterns ───────────────────────────────────────────────────
349
350
351
class TestDjangoEnricherURLPatterns:
352
def test_url_patterns_count_tracked(self):
353
call_count = [0]
354
355
def side_effect(cypher, params=None):
356
call_count[0] += 1
357
if "urls.py" in cypher and call_count[0] == 3:
358
return MagicMock(result_set=[["urlconf", "myapp/urls.py"]])
359
return MagicMock(result_set=[])
360
361
store = _store_with_side_effect(side_effect)
362
enricher = DjangoEnricher(store)
363
result = enricher.enrich()
364
assert result.patterns_found["url_patterns"] == 1
365
366
def test_url_pattern_promoted_with_correct_semantic_type(self):
367
call_count = [0]
368
369
def side_effect(cypher, params=None):
370
call_count[0] += 1
371
if "urls.py" in cypher and call_count[0] == 3:
372
return MagicMock(result_set=[["urlconf", "myapp/urls.py"]])
373
return MagicMock(result_set=[])
374
375
store = _store_with_side_effect(side_effect)
376
enricher = DjangoEnricher(store)
377
enricher.enrich()
378
379
promote_calls = [
380
c for c in store._graph.query.call_args_list
381
if "SET n.semantic_type" in c[0][0]
382
and c[0][1].get("name") == "urlconf"
383
]
384
assert len(promote_calls) == 1
385
assert promote_calls[0][0][1]["semantic_type"] == "URLPattern"
386
387
388
# ── enrich() — admin ──────────────────────────────────────────────────────────
389
390
391
class TestDjangoEnricherAdmin:
392
def test_admin_count_tracked(self):
393
call_count = [0]
394
395
def side_effect(cypher, params=None):
396
call_count[0] += 1
397
if "admin.py" in cypher and call_count[0] == 6:
398
return MagicMock(result_set=[["UserAdmin", "myapp/admin.py"]])
399
return MagicMock(result_set=[])
400
401
store = _store_with_side_effect(side_effect)
402
enricher = DjangoEnricher(store)
403
result = enricher.enrich()
404
assert result.patterns_found["admin"] == 1
405
406
def test_admin_promoted_with_correct_semantic_type(self):
407
call_count = [0]
408
409
def side_effect(cypher, params=None):
410
call_count[0] += 1
411
if "admin.py" in cypher and call_count[0] == 6:
412
return MagicMock(result_set=[["PostAdmin", "blog/admin.py"]])
413
return MagicMock(result_set=[])
414
415
store = _store_with_side_effect(side_effect)
416
enricher = DjangoEnricher(store)
417
enricher.enrich()
418
419
promote_calls = [
420
c for c in store._graph.query.call_args_list
421
if "SET n.semantic_type" in c[0][0]
422
and c[0][1].get("name") == "PostAdmin"
423
]
424
assert len(promote_calls) == 1
425
assert promote_calls[0][0][1]["semantic_type"] == "Admin"
426
427
428
# ── enrich() — aggregate result ───────────────────────────────────────────────
429
430
431
class TestDjangoEnricherAggregateResult:
432
def test_patterns_found_has_all_expected_keys(self):
433
store = _mock_store(result_set=[])
434
enricher = DjangoEnricher(store)
435
result = enricher.enrich()
436
expected_keys = {"views", "models", "url_patterns", "serializers", "tasks", "admin"}
437
assert expected_keys == set(result.patterns_found.keys())
438
439
def test_promoted_count_is_sum_of_all_pattern_counts(self):
440
"""With no matches, promoted should be 0."""
441
store = _mock_store(result_set=[])
442
enricher = DjangoEnricher(store)
443
result = enricher.enrich()
444
assert result.promoted == 0
445
assert sum(result.patterns_found.values()) == 0
446
447
def test_promoted_accumulates_across_all_patterns(self):
448
"""Each query returns one row — promoted should equal 6 (one per pattern)."""
449
call_count = [0]
450
451
def side_effect(cypher, params=None):
452
call_count[0] += 1
453
# The 6 SELECT queries are calls 1, 2, 3, 4, 5, 6 (interleaved with SET calls)
454
# We track by call_count on the non-SET queries
455
if "SET n.semantic_type" not in cypher:
456
return MagicMock(result_set=[["some_node", "some_file.py"]])
457
return MagicMock(result_set=[])
458
459
store = _store_with_side_effect(side_effect)
460
enricher = DjangoEnricher(store)
461
result = enricher.enrich()
462
assert result.promoted == 6
463
assert sum(result.patterns_found.values()) == 6
464

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button