12
12
from core .models import Pattern
13
13
from core .models import PatternInstance
14
14
from core .models import Task
15
- from core .services import pattern_task
15
+ from core .tasks import run_pattern_task
16
16
17
17
18
18
class SharedDataMixin :
@@ -78,12 +78,9 @@ def tearDown(self):
78
78
79
79
80
80
class PatternTaskTest (SharedDataMixin , TestCase ):
81
- @patch (
82
- "core.services.update_task_status" ,
83
- wraps = pattern_task .__globals__ ["update_task_status" ],
84
- )
85
- @patch ("core.services.open" , new_callable = mock_open , read_data = '{"name": "test"}' )
86
- @patch ("core.services.download_collection" )
81
+ @patch ("core.models.Task.update_task_status" , autospec = True )
82
+ @patch ("core.tasks.open" , new_callable = mock_open , read_data = '{"name": "test"}' )
83
+ @patch ("core.tasks.download_collection" )
87
84
def test_run_pattern_task_success (
88
85
self , mock_download , mock_open_fn , mock_update_status
89
86
):
@@ -112,7 +109,7 @@ def test_run_pattern_task_success(
112
109
) as f :
113
110
f .write (json .dumps ({"name" : "test" }))
114
111
115
- pattern_task (pattern .id , task .id )
112
+ run_pattern_task (pattern .id , task .id )
116
113
117
114
mock_update_status .assert_any_call (
118
115
task , "Running" , {"info" : "Processing pattern" }
@@ -121,11 +118,8 @@ def test_run_pattern_task_success(
121
118
task , "Completed" , {"info" : "Pattern processed successfully" }
122
119
)
123
120
124
- @patch (
125
- "core.services.update_task_status" ,
126
- wraps = pattern_task .__globals__ ["update_task_status" ],
127
- )
128
- @patch ("core.services.download_collection" , side_effect = FileNotFoundError )
121
+ @patch ("core.models.Task.update_task_status" , autospec = True )
122
+ @patch ("core.tasks.download_collection" , side_effect = FileNotFoundError )
129
123
def test_run_pattern_task_file_not_found (self , mock_download , mock_update_status ):
130
124
pattern = Pattern .objects .create (
131
125
collection_name = "demo.collection" ,
@@ -134,49 +128,55 @@ def test_run_pattern_task_file_not_found(self, mock_download, mock_update_status
134
128
)
135
129
task = Task .objects .create (status = "Initiated" , details = {})
136
130
137
- pattern_task (pattern .id , task .id )
131
+ run_pattern_task (pattern .id , task .id )
138
132
139
133
mock_update_status .assert_called_with (
140
134
task , "Failed" , {"error" : "Pattern definition not found." }
141
135
)
142
136
143
- @patch (
144
- "core.services.download_collection" , side_effect = Exception ("Download failed" )
145
- )
137
+ @patch ("core.tasks.download_collection" , side_effect = Exception ("Download failed" ))
146
138
def test_run_pattern_task_handles_download_failure (self , mock_download ):
147
- pattern_task (self .pattern .id , self .task .id )
139
+ run_pattern_task (self .pattern .id , self .task .id )
148
140
self .task .refresh_from_db ()
149
141
self .assertEqual (self .task .status , "Failed" )
150
142
self .assertIn ("Download failed" , self .task .details .get ("error" , "" ))
151
143
152
- @patch (
153
- "core.services.update_task_status" ,
154
- wraps = pattern_task .__globals__ ["update_task_status" ],
155
- )
156
- @patch ("core.services.download_collection" )
144
+ @patch ("core.models.Task.update_task_status" , autospec = True )
145
+ @patch ("core.tasks.download_collection" )
157
146
def test_full_status_update_flow (self , mock_download , mock_update_status ):
158
147
temp_dir_path = self .create_temp_collection_dir ()
159
148
mock_download .return_value .__enter__ .return_value = temp_dir_path
160
149
161
- # Run the task
162
- pattern_task (self .pattern .id , self .task .id )
150
+ def _side_effect (self_task , status_ , details ):
151
+ self_task .status = status_
152
+ self_task .details = details
153
+ self_task .save ()
154
+
155
+ mock_update_status .side_effect = _side_effect
156
+
157
+ run_pattern_task (self .pattern .id , self .task .id )
163
158
164
- # Verify calls to update_task_status
165
159
expected_calls = [
166
160
(self .task , "Running" , {"info" : "Processing pattern" }),
167
161
(self .task , "Completed" , {"info" : "Pattern processed successfully" }),
168
162
]
169
- actual_calls = [tuple (call .args ) for call in mock_update_status .call_args_list ]
163
+
164
+ actual_calls = [
165
+ (call_args [0 ][0 ], call_args [0 ][1 ], call_args [0 ][2 ])
166
+ for call_args in mock_update_status .call_args_list
167
+ ]
168
+
169
+ # Assert update_task_status calls
170
170
for expected in expected_calls :
171
171
self .assertIn (expected , actual_calls )
172
172
173
- # Verify final DB state
173
+ # Assert final DB state
174
174
self .task .refresh_from_db ()
175
175
self .assertEqual (self .task .status , "Completed" )
176
176
self .assertEqual (
177
177
self .task .details .get ("info" ), "Pattern processed successfully"
178
178
)
179
179
180
- # Verify pattern_definition was updated and saved
180
+ # Assert pattern definition was updated
181
181
self .pattern .refresh_from_db ()
182
182
self .assertEqual (self .pattern .pattern_definition , {"mock_key" : "mock_value" })
0 commit comments