-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_task_processor.py
More file actions
175 lines (147 loc) · 7.66 KB
/
Copy pathfix_task_processor.py
File metadata and controls
175 lines (147 loc) · 7.66 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
import re
with open('tests/test_tasks_processor.py', 'r') as f:
c = f.read()
# Fix 1: Change get_file to get_file_path in all mocks
c = c.replace('mock_storage.get_file.return_value', 'mock_storage.get_file_path.return_value')
c = c.replace('mock_storage.get_file.side_effect', 'mock_storage.get_file_path.side_effect')
# Fix 2: Fix LLM mock to return object with attributes instead of dict
c = re.sub(
r'mock_llm\.analyze\.return_value = \{[^}]+\}',
'''mock_llm.analyze.return_value = MagicMock(
summary="Test document summary",
key_points=["Point 1", "Point 2"],
entities=["Entity 1", "Entity 2"],
sentiment="positive",
topics=["Topic 1", "Topic 2"],
tokens_used=150,
raw="Raw LLM response",
)''',
c
)
c = re.sub(
r'mock_llm\.analyze\.return_value = \{[^}]+\}',
'''mock_llm.analyze.return_value = MagicMock(
summary="Test",
key_points=[],
entities=[],
sentiment="neutral",
topics=[],
tokens_used=50,
raw="Raw",
)''',
c
)
# Fix 3: Add patch for init_db and close_db at the top of each test
# We need to wrap each test's patch blocks with init_db/close_db mocks
# This is complex, let's add the mocks to the existing patch blocks
# Fix 4: Fix the storage mock to use get_file_path
c = c.replace(
'with patch("app.tasks.processor.get_storage_service") as mock_storage_factory:',
'''with patch("app.tasks.processor.get_storage_service") as mock_storage_factory:
with patch("app.tasks.processor.init_db") as mock_init_db:
with patch("app.tasks.processor.close_db") as mock_close_db:'''
)
# Fix the closing braces
c = c.replace(
'''mock_storage_factory.return_value = mock_storage
# Run the background task
await process_document_task(str(test_document.id))''',
'''mock_storage_factory.return_value = mock_storage
# Run the background task
await process_document_task(str(test_document.id))'''
)
# Fix the second test
c = c.replace(
'''with patch("app.tasks.processor.get_storage_service") as mock_storage_factory:
mock_storage = AsyncMock()
mock_storage.get_file.return_value = b"%PDF-1.4\n%Test"
mock_storage_factory.return_value = mock_storage
await process_document_task(str(test_document.id))''',
'''with patch("app.tasks.processor.get_storage_service") as mock_storage_factory:
with patch("app.tasks.processor.init_db") as mock_init_db:
with patch("app.tasks.processor.close_db") as mock_close_db:
mock_storage = AsyncMock()
mock_storage.get_file_path.return_value = b"%PDF-1.4\n%Test"
mock_storage_factory.return_value = mock_storage
await process_document_task(str(test_document.id))'''
)
# Fix the third test (llm failure)
c = c.replace(
'''with patch("app.tasks.processor.get_llm_service") as mock_llm_factory:
mock_llm = AsyncMock()
mock_llm.analyze.side_effect = Exception("LLM API error")
mock_llm_factory.return_value = mock_llm
with patch("app.tasks.processor.get_storage_service") as mock_storage_factory:
mock_storage = AsyncMock()
mock_storage.get_file.return_value = b"%PDF-1.4\n%Test"
mock_storage_factory.return_value = mock_storage
await process_document_task(str(test_document.id))''',
'''with patch("app.tasks.processor.get_llm_service") as mock_llm_factory:
with patch("app.tasks.processor.init_db") as mock_init_db:
with patch("app.tasks.processor.close_db") as mock_close_db:
mock_llm = AsyncMock()
mock_llm.analyze.side_effect = Exception("LLM API error")
mock_llm_factory.return_value = mock_llm
with patch("app.tasks.processor.get_storage_service") as mock_storage_factory:
mock_storage = AsyncMock()
mock_storage.get_file_path.return_value = b"%PDF-1.4\n%Test"
mock_storage_factory.return_value = mock_storage
await process_document_task(str(test_document.id))'''
)
# Fix the fourth test (storage failure)
c = c.replace(
'''with patch("app.tasks.processor.get_storage_service") as mock_storage_factory:
mock_storage = AsyncMock()
mock_storage.get_file.side_effect = Exception("Storage unavailable")
mock_storage_factory.return_value = mock_storage
await process_document_task(str(test_document.id))''',
'''with patch("app.tasks.processor.get_storage_service") as mock_storage_factory:
with patch("app.tasks.processor.init_db") as mock_init_db:
with patch("app.tasks.processor.close_db") as mock_close_db:
mock_storage = AsyncMock()
mock_storage.get_file_path.side_effect = Exception("Storage unavailable")
mock_storage_factory.return_value = mock_storage
await process_document_task(str(test_document.id))'''
)
# Fix the fifth test (database error)
c = c.replace(
'''with patch("app.tasks.processor.get_storage_service") as mock_storage_factory:
mock_storage = AsyncMock()
mock_storage.get_file.return_value = b"%PDF-1.4\n%Test"
mock_storage_factory.return_value = mock_storage
# Make db commit fail
with patch.object(db_session, "commit", side_effect=Exception("DB error")):
await process_document_task(str(test_document.id))''',
'''with patch("app.tasks.processor.get_storage_service") as mock_storage_factory:
with patch("app.tasks.processor.init_db") as mock_init_db:
with patch("app.tasks.processor.close_db") as mock_close_db:
mock_storage = AsyncMock()
mock_storage.get_file_path.return_value = b"%PDF-1.4\n%Test"
mock_storage_factory.return_value = mock_storage
# Make db commit fail
with patch.object(db_session, "commit", side_effect=Exception("DB error")):
await process_document_task(str(test_document.id))'''
)
# Fix the sixth test (processing status)
c = c.replace(
'''with patch("app.tasks.processor.get_storage_service") as mock_storage_factory:
mock_storage = AsyncMock()
mock_storage.get_file.return_value = b"%PDF-1.4\n%Test"
mock_storage_factory.return_value = mock_storage
await process_document_task(str(test_document.id))''',
'''with patch("app.tasks.processor.get_storage_service") as mock_storage_factory:
with patch("app.tasks.processor.init_db") as mock_init_db:
with patch("app.tasks.processor.close_db") as mock_close_db:
mock_storage = AsyncMock()
mock_storage.get_file_path.return_value = b"%PDF-1.4\n%Test"
mock_storage_factory.return_value = mock_storage
await process_document_task(str(test_document.id))'''
)
# Fix imports - add MagicMock
c = c.replace(
'from unittest.mock import AsyncMock, MagicMock, patch',
'from unittest.mock import AsyncMock, MagicMock, patch, MagicMock'
)
with open('tests/test_tasks_processor.py', 'w') as f:
f.write(c)
print('Done')