3
3
import shutil
4
4
import tempfile
5
5
from typing import List
6
- from unittest .mock import mock_open
7
6
from unittest .mock import patch
8
7
9
8
from django .test import TestCase
12
11
from core .models import Pattern
13
12
from core .models import PatternInstance
14
13
from core .models import Task
15
- from core .tasks import run_pattern_task
14
+ from core .task_runner import run_pattern_task
16
15
17
16
18
17
class SharedDataMixin :
@@ -78,82 +77,12 @@ def tearDown(self):
78
77
79
78
80
79
class PatternTaskTest (SharedDataMixin , TestCase ):
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" )
84
- def test_run_pattern_task_success (
85
- self , mock_download , mock_open_fn , mock_update_status
86
- ):
87
- pattern = Pattern .objects .create (
88
- collection_name = "demo.collection" ,
89
- collection_version = "1.0.0" ,
90
- pattern_name = "test_pattern" ,
91
- )
92
- task = Task .objects .create (status = "Initiated" , details = {})
93
- temp_dir = tempfile .mkdtemp ()
94
- mock_download .return_value .__enter__ .return_value = temp_dir
95
-
96
- os .makedirs (
97
- os .path .join (temp_dir , "extensions" , "patterns" , "test_pattern" , "meta" )
98
- )
99
- with open (
100
- os .path .join (
101
- temp_dir ,
102
- "extensions" ,
103
- "patterns" ,
104
- "test_pattern" ,
105
- "meta" ,
106
- "pattern.json" ,
107
- ),
108
- "w" ,
109
- ) as f :
110
- f .write (json .dumps ({"name" : "test" }))
111
-
112
- run_pattern_task (pattern .id , task .id )
113
-
114
- mock_update_status .assert_any_call (
115
- task , "Running" , {"info" : "Processing pattern" }
116
- )
117
- mock_update_status .assert_any_call (
118
- task , "Completed" , {"info" : "Pattern processed successfully" }
119
- )
120
-
121
- @patch ("core.models.Task.update_task_status" , autospec = True )
122
- @patch ("core.tasks.download_collection" , side_effect = FileNotFoundError )
123
- def test_run_pattern_task_file_not_found (self , mock_download , mock_update_status ):
124
- pattern = Pattern .objects .create (
125
- collection_name = "demo.collection" ,
126
- collection_version = "1.0.0" ,
127
- pattern_name = "missing_pattern" ,
128
- )
129
- task = Task .objects .create (status = "Initiated" , details = {})
130
-
131
- run_pattern_task (pattern .id , task .id )
132
-
133
- mock_update_status .assert_called_with (
134
- task , "Failed" , {"error" : "Pattern definition not found." }
135
- )
136
-
137
- @patch ("core.tasks.download_collection" , side_effect = Exception ("Download failed" ))
138
- def test_run_pattern_task_handles_download_failure (self , mock_download ):
139
- run_pattern_task (self .pattern .id , self .task .id )
140
- self .task .refresh_from_db ()
141
- self .assertEqual (self .task .status , "Failed" )
142
- self .assertIn ("Download failed" , self .task .details .get ("error" , "" ))
143
-
144
- @patch ("core.models.Task.update_task_status" , autospec = True )
145
- @patch ("core.tasks.download_collection" )
146
- def test_full_status_update_flow (self , mock_download , mock_update_status ):
80
+ @patch ("core.models.Task.set_status" , autospec = True , wraps = Task .set_status )
81
+ @patch ("core.task_runner.download_collection" )
82
+ def test_run_pattern_task_success (self , mock_download , mock_update_status ):
147
83
temp_dir_path = self .create_temp_collection_dir ()
148
84
mock_download .return_value .__enter__ .return_value = temp_dir_path
149
85
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
86
run_pattern_task (self .pattern .id , self .task .id )
158
87
159
88
expected_calls = [
@@ -180,3 +109,28 @@ def _side_effect(self_task, status_, details):
180
109
# Assert pattern definition was updated
181
110
self .pattern .refresh_from_db ()
182
111
self .assertEqual (self .pattern .pattern_definition , {"mock_key" : "mock_value" })
112
+
113
+ @patch ("core.models.Task.set_status" , autospec = True )
114
+ @patch ("core.task_runner.download_collection" , side_effect = FileNotFoundError )
115
+ def test_run_pattern_task_file_not_found (self , mock_download , mock_update_status ):
116
+ pattern = Pattern .objects .create (
117
+ collection_name = "demo.collection" ,
118
+ collection_version = "1.0.0" ,
119
+ pattern_name = "missing_pattern" ,
120
+ )
121
+ task = Task .objects .create (status = "Initiated" , details = {})
122
+
123
+ run_pattern_task (pattern .id , task .id )
124
+
125
+ mock_update_status .assert_called_with (
126
+ task , "Failed" , {"error" : "Pattern definition not found." }
127
+ )
128
+
129
+ @patch (
130
+ "core.task_runner.download_collection" , side_effect = Exception ("Download failed" )
131
+ )
132
+ def test_run_pattern_task_handles_download_failure (self , mock_download ):
133
+ run_pattern_task (self .pattern .id , self .task .id )
134
+ self .task .refresh_from_db ()
135
+ self .assertEqual (self .task .status , "Failed" )
136
+ self .assertIn ("Download failed" , self .task .details .get ("error" , "" ))
0 commit comments