-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_manual_persistence.py
More file actions
156 lines (121 loc) · 5.81 KB
/
test_manual_persistence.py
File metadata and controls
156 lines (121 loc) · 5.81 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
#!/usr/bin/env python3
"""
Test manual assignment persistence in cache file.
"""
import sys
import os
import json
# Add the project root to the Python path
sys.path.append(r'C:\Users\Federico\Desktop\Desktop App TF4M')
from core.file_analyzer import FileAnalyzer
from core.project_manager import ProjectManager
from core.models import ProjectData, PatientData, FileData, DataType, MatchStatus
def test_manual_assignment_persistence():
"""Test that manual assignments are immediately saved to cache."""
print("Testing Manual Assignment Cache Persistence")
print("="*50)
# Test with a real patient folder
patient_folder = r"F:\Dati Ferrara - Test Bulk Upload\ESEMPI\1"
if not os.path.exists(patient_folder):
print(f"Test folder not found: {patient_folder}")
return
print(f"Testing with: {patient_folder}")
# Initialize components
analyzer = FileAnalyzer()
project_manager = ProjectManager()
# Get cache file path
cache_file = analyzer.match_cache.get_patient_cache_file(patient_folder)
print(f"Cache file: {cache_file}")
# Clear cache and analyze
analyzer.invalidate_cache(patient_folder)
# Set up project data for project manager
project_manager.project_data = ProjectData(root_path=os.path.dirname(patient_folder))
# Analyze patient folder
patient_data = analyzer.analyze_patient_folder(patient_folder, use_cache=True)
# Add patient to project manager
project_manager.project_data.patients = [patient_data]
print(f"\nInitial state:")
print(f" Patient: {patient_data.patient_id}")
print(f" Total files: {len(patient_data.get_all_files())}")
print(f" Unmatched files: {len(patient_data.unmatched_files)}")
if not patient_data.unmatched_files:
print("No unmatched files to test manual assignment")
return
# Pick first unmatched file for manual assignment
test_file = patient_data.unmatched_files[0]
original_path = test_file.path
print(f" Test file: {test_file.filename}")
# Read cache before manual assignment
cache_before = None
if os.path.exists(cache_file):
with open(cache_file, 'r', encoding='utf-8') as f:
cache_before = json.load(f)
# Perform manual assignment
print(f"\nPerforming manual assignment...")
success = project_manager.update_patient_file_assignment(
patient_data.patient_id,
original_path,
DataType.INTRAORAL_PHOTO
)
if success:
print(f"Manual assignment successful")
# Update cache (this should happen automatically in GUI)
analyzer.update_cache(patient_data)
print(f"Cache updated")
# Read cache after manual assignment
cache_after = None
if os.path.exists(cache_file):
with open(cache_file, 'r', encoding='utf-8') as f:
cache_after = json.load(f)
print(f"\nCache file analysis:")
print(f" Cache file exists: True")
print(f" Cache file size: {os.path.getsize(cache_file):,} bytes")
# Check for manual assignments in cache
cache_key = list(cache_after.keys())[0]
matched_files = cache_after[cache_key].get('matched_files', {})
manual_assignments = cache_after[cache_key].get('manual_assignments', {})
print(f" Total matched files in cache: {len(matched_files)}")
print(f" Manual assignments in cache: {len(manual_assignments)}")
# Check if our manual assignment is recorded
manual_found = False
for file_path, file_info in matched_files.items():
if file_path == original_path:
status = file_info.get('status', 'unknown')
data_type = file_info.get('data_type', 'unknown')
print(f" Found test file in cache:")
print(f" Status: {status}")
print(f" Type: {data_type}")
if status == 'manual':
print(f" SUCCESS: Manual status recorded in cache!")
manual_found = True
break
if not manual_found:
print(f" ISSUE: Manual assignment not found in cache")
# Check manual_assignments section
if original_path in manual_assignments:
assigned_type = manual_assignments[original_path]
print(f" Manual assignment recorded: {assigned_type}")
else:
print(f" No entry in manual_assignments section")
else:
print(f"FAILURE: Cache file not found after update")
# Test persistence by reloading
print(f"\nTesting persistence by reloading...")
patient_data_2 = analyzer.analyze_patient_folder(patient_folder, use_cache=True)
# Check if manual assignment persisted
manual_persisted = False
for file_data in patient_data_2.get_all_files():
if file_data.path == original_path:
print(f" Reloaded file: {file_data.filename}")
print(f" Status: {file_data.status.value}")
print(f" Type: {file_data.data_type.value if file_data.data_type else 'None'}")
if file_data.status == MatchStatus.MANUAL:
print(f" SUCCESS: Manual assignment persisted!")
manual_persisted = True
break
if not manual_persisted:
print(f" FAILURE: Manual assignment not persisted")
else:
print(f"Manual assignment failed")
if __name__ == "__main__":
test_manual_assignment_persistence()