forked from FSoft-AI4Code/CodeWiki
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_clustering_integration.py
More file actions
executable file
·775 lines (645 loc) · 23.1 KB
/
Copy pathtest_clustering_integration.py
File metadata and controls
executable file
·775 lines (645 loc) · 23.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
#!/usr/bin/env python3
"""
Comprehensive integration tests for CodeWiki ID-based clustering system.
Tests validation fixes, normalization, and error handling.
"""
import json
import sys
import logging
from typing import List, Dict, Any
from pathlib import Path
from io import StringIO
# Add CodeWiki to path
sys.path.insert(0, str(Path(__file__).parent))
from codewiki.src.be.cluster_modules import (
create_component_id_map,
normalize_component_ids_by_lookup,
)
# Configure logging to capture warnings
logging.basicConfig(level=logging.WARNING, format='%(message)s')
class TestResults:
def __init__(self):
self.passed = 0
self.failed = 0
self.tests = []
def add_test(self, name: str, passed: bool, details: str = ""):
self.tests.append({
"name": name,
"passed": passed,
"details": details
})
if passed:
self.passed += 1
else:
self.failed += 1
def print_summary(self):
print("\n" + "="*80)
print("TEST SUMMARY")
print("="*80)
for test in self.tests:
status = "✅ PASS" if test["passed"] else "❌ FAIL"
print(f"{status}: {test['name']}")
if test["details"]:
print(f" → {test['details']}")
print("="*80)
print(f"FINAL SCORE: {self.passed}/{self.passed + self.failed} tests passed")
print("="*80)
return self.failed == 0
# Sample OpenFrame data structure (mimicking Node objects)
class MockNode:
"""Mock Node object for testing"""
def __init__(self, fqdn, name, file_path):
self.fqdn = fqdn
self.name = name
self.file_path = file_path
SAMPLE_COMPONENTS = {
"openframe.auth.service::AuthService": MockNode(
"openframe.auth.service::AuthService",
"AuthService",
"auth/service.py"
),
"openframe.api.models::CountedGenericQueryResult": MockNode(
"openframe.api.models::CountedGenericQueryResult",
"CountedGenericQueryResult",
"api/models.py"
),
"openframe.controllers.user::UserController": MockNode(
"openframe.controllers.user::UserController",
"UserController",
"controllers/user.py"
),
"openframe.db.connection::DatabaseConnection": MockNode(
"openframe.db.connection::DatabaseConnection",
"DatabaseConnection",
"db/connection.py"
),
"openframe.middleware.logging::LoggingMiddleware": MockNode(
"openframe.middleware.logging::LoggingMiddleware",
"LoggingMiddleware",
"middleware/logging.py"
)
}
def capture_log_warnings(func):
"""Decorator to capture log warnings"""
def wrapper(*args, **kwargs):
# Capture logging output
log_capture = StringIO()
handler = logging.StreamHandler(log_capture)
handler.setLevel(logging.WARNING)
logger = logging.getLogger('codewiki.src.be.cluster_modules')
logger.addHandler(handler)
try:
result = func(*args, **kwargs)
log_output = log_capture.getvalue()
return result, log_output
finally:
logger.removeHandler(handler)
return wrapper
def test_component_id_map_creation(results: TestResults):
"""Test 1: Create component ID map from real data"""
print("\n[TEST 1] Creating component ID map from real data...")
try:
id_to_fqdn, id_descriptions = create_component_id_map(SAMPLE_COMPONENTS)
# Verify map structure
if not isinstance(id_to_fqdn, dict):
results.add_test(
"Component ID Map Creation",
False,
f"Expected dict for id_to_fqdn, got {type(id_to_fqdn)}"
)
return
# Verify all components are mapped
if len(id_to_fqdn) != len(SAMPLE_COMPONENTS):
results.add_test(
"Component ID Map Creation",
False,
f"Expected {len(SAMPLE_COMPONENTS)} components, got {len(id_to_fqdn)}"
)
return
# Verify IDs are sequential
expected_ids = set(range(len(SAMPLE_COMPONENTS)))
actual_ids = set(id_to_fqdn.keys())
if expected_ids != actual_ids:
results.add_test(
"Component ID Map Creation",
False,
f"IDs not sequential: expected {expected_ids}, got {actual_ids}"
)
return
# Verify FQDNs
expected_fqdns = set(SAMPLE_COMPONENTS.keys())
actual_fqdns = set(id_to_fqdn.values())
if expected_fqdns != actual_fqdns:
results.add_test(
"Component ID Map Creation",
False,
f"FQDN mismatch"
)
return
print(f"✓ Created map with {len(id_to_fqdn)} components")
print(f" Sample mapping: 0 → {id_to_fqdn[0]}")
print(f" Description: {id_descriptions.get('0', 'N/A')}")
results.add_test(
"Component ID Map Creation",
True,
f"Successfully created map with {len(id_to_fqdn)} components"
)
except Exception as e:
results.add_test(
"Component ID Map Creation",
False,
f"Exception: {str(e)}"
)
def test_valid_integer_ids(results: TestResults):
"""Test 2: Valid integer IDs (should pass without warnings)"""
print("\n[TEST 2] Testing valid integer IDs...")
try:
id_to_fqdn, _ = create_component_id_map(SAMPLE_COMPONENTS)
# Create module tree with valid IDs
module_tree = {
"Auth Module": {
"components": [0, 1, 2],
"description": "Authentication components"
}
}
# Normalize
@capture_log_warnings
def normalize_test():
return normalize_component_ids_by_lookup(module_tree, id_to_fqdn)
normalized, log_output = normalize_test()
# Check for warnings
if "❌" in log_output or "⚠️" in log_output:
results.add_test(
"Valid Integer IDs",
False,
f"Unexpected warnings for valid IDs: {log_output}"
)
return
# Verify normalization
components = normalized["Auth Module"]["components"]
if len(components) != 3:
results.add_test(
"Valid Integer IDs",
False,
f"Expected 3 components, got {len(components)}"
)
return
# Verify FQDNs
for comp in components:
if not isinstance(comp, str) or "::" not in comp:
results.add_test(
"Valid Integer IDs",
False,
f"Invalid FQDN format: {comp}"
)
return
print(f"✓ Valid IDs normalized successfully: [0, 1, 2] → 3 FQDNs")
results.add_test(
"Valid Integer IDs",
True,
f"Correctly normalized {len(components)} valid IDs"
)
except Exception as e:
results.add_test(
"Valid Integer IDs",
False,
f"Exception: {str(e)}"
)
def test_invalid_quoted_integers(results: TestResults):
"""Test 3: Quoted integer strings (should ACCEPT via int() conversion)"""
print("\n[TEST 3] Testing quoted integer strings (resilient handling)...")
try:
id_to_fqdn, _ = create_component_id_map(SAMPLE_COMPONENTS)
# Create module tree with quoted integers (Python's int() converts these)
module_tree = {
"Auth Module": {
"components": ["0", "1", "2"],
"description": "Authentication components"
}
}
# Normalize
@capture_log_warnings
def normalize_test():
return normalize_component_ids_by_lookup(module_tree, id_to_fqdn)
normalized, log_output = normalize_test()
# Should NOT have warnings (int("0") works in Python)
if "❌" in log_output or "⚠️" in log_output:
results.add_test(
"Quoted Integers (Resilient)",
False,
f"Unexpected warnings for quoted integers: {log_output}"
)
return
# Components should be successfully normalized (3 components)
components = normalized["Auth Module"]["components"]
if len(components) != 3:
results.add_test(
"Quoted Integers (Resilient)",
False,
f"Expected 3 components, got {len(components)}"
)
return
# Verify FQDNs
for comp in components:
if not isinstance(comp, str) or "::" not in comp:
results.add_test(
"Quoted Integers (Resilient)",
False,
f"Invalid FQDN format: {comp}"
)
return
print(f"✓ Quoted integers correctly converted: [\"0\", \"1\", \"2\"] → 3 FQDNs")
results.add_test(
"Quoted Integers (Resilient)",
True,
"Correctly converted quoted integers to IDs (resilient behavior)"
)
except Exception as e:
results.add_test(
"Quoted Integers (Resilient)",
False,
f"Exception: {str(e)}"
)
def test_invalid_class_names(results: TestResults):
"""Test 4: Invalid class name strings (should log warnings)"""
print("\n[TEST 4] Testing invalid class name strings...")
try:
id_to_fqdn, _ = create_component_id_map(SAMPLE_COMPONENTS)
# Create module tree with class names (INVALID)
module_tree = {
"Auth Module": {
"components": ["AuthService", "CountedGenericQueryResult"],
"description": "Authentication components"
}
}
# Normalize
@capture_log_warnings
def normalize_test():
return normalize_component_ids_by_lookup(module_tree, id_to_fqdn)
normalized, log_output = normalize_test()
# Should have warnings
if "❌" not in log_output:
results.add_test(
"Invalid Class Names",
False,
"No warnings logged for class names (should warn)"
)
return
# Should have "Non-integer ID" warnings
if "Non-integer ID" not in log_output:
results.add_test(
"Invalid Class Names",
False,
f"Missing 'Non-integer ID' warning. Got: {log_output}"
)
return
# Components should be empty (all failed)
components = normalized["Auth Module"]["components"]
if len(components) != 0:
results.add_test(
"Invalid Class Names",
False,
f"Expected 0 components (all failed), got {len(components)}"
)
return
print(f"✓ Class names correctly rejected with warnings")
results.add_test(
"Invalid Class Names",
True,
"Correctly rejected class names with warnings"
)
except Exception as e:
results.add_test(
"Invalid Class Names",
False,
f"Exception: {str(e)}"
)
def test_mixed_invalid_ids(results: TestResults):
"""Test 5: Mixed valid/invalid IDs (should partially normalize)"""
print("\n[TEST 5] Testing mixed valid/invalid IDs...")
try:
id_to_fqdn, _ = create_component_id_map(SAMPLE_COMPONENTS)
# Create module tree with mixed IDs
# Note: "1" will be converted to int(1) successfully by Python
module_tree = {
"Auth Module": {
"components": [0, "1", "AuthService", 999],
"description": "Mixed components"
}
}
# Normalize
@capture_log_warnings
def normalize_test():
return normalize_component_ids_by_lookup(module_tree, id_to_fqdn)
normalized, log_output = normalize_test()
# Should have warnings for invalid IDs (AuthService and 999)
if "❌" not in log_output:
results.add_test(
"Mixed Invalid IDs",
False,
"No warnings logged for mixed invalid IDs (should warn)"
)
return
# Should have exactly 2 valid components (ID 0 and "1" converted to 1)
components = normalized["Auth Module"]["components"]
if len(components) != 2:
results.add_test(
"Mixed Invalid IDs",
False,
f"Expected 2 valid components (0 and \"1\"), got {len(components)}"
)
return
# Verify the two valid components are IDs 0 and 1
expected_fqdns = [id_to_fqdn[0], id_to_fqdn[1]]
if components != expected_fqdns:
results.add_test(
"Mixed Invalid IDs",
False,
f"Expected FQDNs for IDs 0 and 1, got {components}"
)
return
print(f"✓ Mixed IDs: 2 valid normalized (0, \"1\"), 2 invalid rejected (AuthService, 999)")
results.add_test(
"Mixed Invalid IDs",
True,
"Correctly normalized valid IDs and rejected invalid IDs"
)
except Exception as e:
results.add_test(
"Mixed Invalid IDs",
False,
f"Exception: {str(e)}"
)
def test_out_of_range_ids(results: TestResults):
"""Test 6: Out-of-range valid integer IDs (should log warnings)"""
print("\n[TEST 6] Testing out-of-range integer IDs...")
try:
id_to_fqdn, _ = create_component_id_map(SAMPLE_COMPONENTS)
# Create module tree with out-of-range IDs
module_tree = {
"Auth Module": {
"components": [0, 1, 999],
"description": "Out of range components"
}
}
# Normalize
@capture_log_warnings
def normalize_test():
return normalize_component_ids_by_lookup(module_tree, id_to_fqdn)
normalized, log_output = normalize_test()
# Should have warnings for out-of-range
if "❌" not in log_output or "Invalid ID 999" not in log_output:
results.add_test(
"Out-of-Range IDs",
False,
"Missing warning for out-of-range ID 999"
)
return
# Should have "Valid range" in warning
if "Valid range" not in log_output:
results.add_test(
"Out-of-Range IDs",
False,
f"Missing 'Valid range' in warning. Got: {log_output}"
)
return
# Should have 2 valid components (0, 1)
components = normalized["Auth Module"]["components"]
if len(components) != 2:
results.add_test(
"Out-of-Range IDs",
False,
f"Expected 2 valid components, got {len(components)}"
)
return
print(f"✓ Out-of-range ID rejected: 2 valid, 1 rejected")
results.add_test(
"Out-of-Range IDs",
True,
"Correctly rejected out-of-range ID with warning"
)
except Exception as e:
results.add_test(
"Out-of-Range IDs",
False,
f"Exception: {str(e)}"
)
def test_json_loads_normalization(results: TestResults):
"""Test 7: JSON string parsing with json.loads() approach"""
print("\n[TEST 7] Testing JSON string parsing with json.loads()...")
try:
id_to_fqdn, _ = create_component_id_map(SAMPLE_COMPONENTS)
# Simulate LLM response as JSON string
json_string = "[0, 1, 2]"
parsed_ids = json.loads(json_string)
# Create module tree with parsed IDs
module_tree = {
"Auth Module": {
"components": parsed_ids,
"description": "JSON parsed components"
}
}
# Normalize
@capture_log_warnings
def normalize_test():
return normalize_component_ids_by_lookup(module_tree, id_to_fqdn)
normalized, log_output = normalize_test()
# Should NOT have warnings
if "❌" in log_output or "⚠️" in log_output:
results.add_test(
"JSON Loads Normalization",
False,
f"Unexpected warnings: {log_output}"
)
return
# Should have 3 valid components
components = normalized["Auth Module"]["components"]
if len(components) != 3:
results.add_test(
"JSON Loads Normalization",
False,
f"Expected 3 components, got {len(components)}"
)
return
print(f"✓ JSON string correctly parsed and normalized")
results.add_test(
"JSON Loads Normalization",
True,
f"JSON string successfully parsed: {json_string} → 3 FQDNs"
)
except Exception as e:
results.add_test(
"JSON Loads Normalization",
False,
f"Exception: {str(e)}"
)
def test_empty_list(results: TestResults):
"""Test 8: Empty component list (edge case)"""
print("\n[TEST 8] Testing empty component list...")
try:
id_to_fqdn, _ = create_component_id_map(SAMPLE_COMPONENTS)
# Create module tree with empty components
module_tree = {
"Empty Module": {
"components": [],
"description": "No components"
}
}
# Normalize
@capture_log_warnings
def normalize_test():
return normalize_component_ids_by_lookup(module_tree, id_to_fqdn)
normalized, log_output = normalize_test()
# Should NOT have warnings
if "❌" in log_output:
results.add_test(
"Empty List",
False,
f"Unexpected warnings for empty list: {log_output}"
)
return
# Components should still be empty
components = normalized["Empty Module"]["components"]
if len(components) != 0:
results.add_test(
"Empty List",
False,
f"Expected 0 components, got {len(components)}"
)
return
print(f"✓ Empty list correctly handled")
results.add_test(
"Empty List",
True,
"Empty list correctly handled without warnings"
)
except Exception as e:
results.add_test(
"Empty List",
False,
f"Exception: {str(e)}"
)
def test_duplicate_ids(results: TestResults):
"""Test 9: Duplicate IDs in list (should allow duplicates)"""
print("\n[TEST 9] Testing duplicate IDs...")
try:
id_to_fqdn, _ = create_component_id_map(SAMPLE_COMPONENTS)
# Create module tree with duplicate IDs
module_tree = {
"Duplicate Module": {
"components": [0, 1, 1, 2],
"description": "Duplicate components"
}
}
# Normalize
@capture_log_warnings
def normalize_test():
return normalize_component_ids_by_lookup(module_tree, id_to_fqdn)
normalized, log_output = normalize_test()
# Should NOT have warnings
if "❌" in log_output or "⚠️" in log_output:
results.add_test(
"Duplicate IDs",
False,
f"Unexpected warnings for duplicates: {log_output}"
)
return
# Should have 4 components (duplicates preserved)
components = normalized["Duplicate Module"]["components"]
if len(components) != 4:
results.add_test(
"Duplicate IDs",
False,
f"Expected 4 components (with duplicates), got {len(components)}"
)
return
# Verify duplicate FQDN
fqdn_1 = id_to_fqdn[1]
if components.count(fqdn_1) != 2:
results.add_test(
"Duplicate IDs",
False,
f"Expected 2 instances of FQDN for ID 1, got {components.count(fqdn_1)}"
)
return
print(f"✓ Duplicate IDs correctly preserved")
results.add_test(
"Duplicate IDs",
True,
"Duplicate IDs correctly preserved in normalization"
)
except Exception as e:
results.add_test(
"Duplicate IDs",
False,
f"Exception: {str(e)}"
)
def test_negative_ids(results: TestResults):
"""Test 10: Negative IDs (should log warnings)"""
print("\n[TEST 10] Testing negative IDs...")
try:
id_to_fqdn, _ = create_component_id_map(SAMPLE_COMPONENTS)
# Create module tree with negative IDs
module_tree = {
"Negative Module": {
"components": [-1, 0, 1],
"description": "Negative IDs"
}
}
# Normalize
@capture_log_warnings
def normalize_test():
return normalize_component_ids_by_lookup(module_tree, id_to_fqdn)
normalized, log_output = normalize_test()
# Should have warnings for negative ID
if "❌" not in log_output or "Invalid ID -1" not in log_output:
results.add_test(
"Negative IDs",
False,
"Missing warning for negative ID -1"
)
return
# Should have 2 valid components (0, 1)
components = normalized["Negative Module"]["components"]
if len(components) != 2:
results.add_test(
"Negative IDs",
False,
f"Expected 2 valid components, got {len(components)}"
)
return
print(f"✓ Negative ID rejected: 2 valid, 1 rejected")
results.add_test(
"Negative IDs",
True,
"Correctly rejected negative ID with warning"
)
except Exception as e:
results.add_test(
"Negative IDs",
False,
f"Exception: {str(e)}"
)
def main():
print("="*80)
print("CodeWiki ID-Based Clustering System - Integration Tests")
print("="*80)
print(f"Testing with {len(SAMPLE_COMPONENTS)} sample components")
print(f"Component names: {[SAMPLE_COMPONENTS[k].name for k in sorted(SAMPLE_COMPONENTS.keys())]}")
results = TestResults()
# Run all tests
test_component_id_map_creation(results)
test_valid_integer_ids(results)
test_invalid_quoted_integers(results)
test_invalid_class_names(results)
test_mixed_invalid_ids(results)
test_out_of_range_ids(results)
test_json_loads_normalization(results)
test_empty_list(results)
test_duplicate_ids(results)
test_negative_ids(results)
# Print summary
success = results.print_summary()
return 0 if success else 1
if __name__ == "__main__":
sys.exit(main())