|
12 | 12 | from core.models import Pattern
|
13 | 13 | from core.models import PatternInstance
|
14 | 14 | from core.models import Task
|
15 |
| -from core.tasks import run_pattern_task |
| 15 | +from core.services import pattern_task |
16 | 16 |
|
17 | 17 |
|
18 | 18 | class SharedDataMixin:
|
@@ -70,28 +70,62 @@ def tearDown(self):
|
70 | 70 | self.temp_dirs.clear()
|
71 | 71 |
|
72 | 72 |
|
73 |
| -class TaskTests(SharedDataMixin, TestCase): |
| 73 | +class PatternTaskTest(SharedDataMixin, TestCase): |
| 74 | + @patch("core.services.update_task_status", wraps=pattern_task.__globals__["update_task_status"]) |
| 75 | + @patch("core.services.open", new_callable=mock_open, read_data='{"name": "test"}') |
| 76 | + @patch("core.services.download_collection") |
| 77 | + def test_run_pattern_task_success(self, mock_download, mock_open_fn, mock_update_status): |
| 78 | + pattern = Pattern.objects.create( |
| 79 | + collection_name="demo.collection", |
| 80 | + collection_version="1.0.0", |
| 81 | + pattern_name="test_pattern", |
| 82 | + ) |
| 83 | + task = Task.objects.create(status="Initiated", details={}) |
| 84 | + temp_dir = tempfile.mkdtemp() |
| 85 | + mock_download.return_value.__enter__.return_value = temp_dir |
| 86 | + |
| 87 | + os.makedirs(os.path.join(temp_dir, "extensions", "patterns", "test_pattern", "meta")) |
| 88 | + with open(os.path.join(temp_dir, "extensions", "patterns", "test_pattern", "meta", "pattern.json"), "w") as f: |
| 89 | + f.write(json.dumps({"name": "test"})) |
| 90 | + |
| 91 | + pattern_task(pattern.id, task.id) |
| 92 | + |
| 93 | + mock_update_status.assert_any_call(task, "Running", {"info": "Processing pattern"}) |
| 94 | + mock_update_status.assert_any_call(task, "Completed", {"info": "Pattern processed successfully"}) |
| 95 | + |
| 96 | + @patch("core.services.update_task_status", wraps=pattern_task.__globals__["update_task_status"]) |
| 97 | + @patch("core.services.download_collection", side_effect=FileNotFoundError) |
| 98 | + def test_run_pattern_task_file_not_found(self, mock_download, mock_update_status): |
| 99 | + pattern = Pattern.objects.create( |
| 100 | + collection_name="demo.collection", |
| 101 | + collection_version="1.0.0", |
| 102 | + pattern_name="missing_pattern", |
| 103 | + ) |
| 104 | + task = Task.objects.create(status="Initiated", details={}) |
| 105 | + |
| 106 | + pattern_task(pattern.id, task.id) |
| 107 | + |
| 108 | + mock_update_status.assert_called_with(task, "Failed", {"error": "Pattern definition not found."}) |
74 | 109 |
|
75 |
| - @patch("core.tasks.download_collection", side_effect=Exception("Download failed")) |
| 110 | + @patch("core.services.download_collection", side_effect=Exception("Download failed")) |
76 | 111 | def test_run_pattern_task_handles_download_failure(self, mock_download):
|
77 |
| - run_pattern_task(self.pattern.id, self.task.id) |
| 112 | + pattern_task(self.pattern.id, self.task.id) |
78 | 113 | self.task.refresh_from_db()
|
79 | 114 | self.assertEqual(self.task.status, "Failed")
|
80 | 115 | self.assertIn("Download failed", self.task.details.get("error", ""))
|
81 | 116 |
|
82 |
| - @patch("core.tasks.update_task_status", wraps=run_pattern_task.__globals__["update_task_status"]) |
83 |
| - @patch("core.tasks.download_collection") |
| 117 | + @patch("core.services.update_task_status", wraps=pattern_task.__globals__["update_task_status"]) |
| 118 | + @patch("core.services.download_collection") |
84 | 119 | def test_full_status_update_flow(self, mock_download, mock_update_status):
|
85 | 120 | temp_dir_path = self.create_temp_collection_dir()
|
86 | 121 | mock_download.return_value.__enter__.return_value = temp_dir_path
|
87 | 122 |
|
88 | 123 | # Run the task
|
89 |
| - run_pattern_task(self.pattern.id, self.task.id) |
| 124 | + pattern_task(self.pattern.id, self.task.id) |
90 | 125 |
|
91 | 126 | # Verify calls to update_task_status
|
92 | 127 | expected_calls = [
|
93 | 128 | (self.task, "Running", {"info": "Processing pattern"}),
|
94 |
| - (self.task, "Running", {"info": "Downloading collection tarball"}), |
95 | 129 | (self.task, "Completed", {"info": "Pattern processed successfully"}),
|
96 | 130 | ]
|
97 | 131 | actual_calls = [tuple(call.args) for call in mock_update_status.call_args_list]
|
|
0 commit comments