1
- from unittest .mock import patch
2
-
3
1
from django .urls import reverse
4
2
from rest_framework import status
5
3
from rest_framework .test import APITestCase
9
7
from core .models import Pattern
10
8
from core .models import PatternInstance
11
9
from core .models import Task
12
- from core .tasks import run_pattern_task
13
10
14
11
15
12
class SharedDataMixin :
@@ -59,12 +56,7 @@ def test_pattern_detail_view(self):
59
56
self .assertEqual (response .status_code , status .HTTP_200_OK )
60
57
self .assertEqual (response .data ["collection_name" ], "mynamespace.mycollection" )
61
58
62
- @patch ("core.views.async_to_sync" )
63
- def test_pattern_create_view (self , mock_async_to_sync ):
64
- """
65
- Test POST /patterns/ creates Pattern and Task,
66
- calls run_pattern_task and returns 202 with task_id.
67
- """
59
+ def test_pattern_create_view (self ):
68
60
url = reverse ("pattern-list" )
69
61
data = {
70
62
"collection_name" : "new.namespace.collection" ,
@@ -80,17 +72,15 @@ def test_pattern_create_view(self, mock_async_to_sync):
80
72
pattern = Pattern .objects .get (pattern_name = "new_pattern" )
81
73
self .assertIsNotNone (pattern )
82
74
83
- # Task created pointing to pattern id
84
- task_id = response .data ["task_id" ]
75
+ # Task id returned directly
76
+ task_id = response .data .get ("task_id" )
77
+ self .assertIsInstance (task_id , int )
78
+
79
+ # Task exists
85
80
task = Task .objects .get (id = task_id )
86
81
self .assertEqual (task .status , "Initiated" )
87
-
88
- mock_async_to_sync .assert_called_once ()
89
- args , kwargs = mock_async_to_sync .call_args
90
- # The first argument should be run_pattern_task
91
- self .assertEqual (args [0 ], run_pattern_task )
92
- self .assertIn ("task_id" , response .data )
93
- self .assertIn ("message" , response .data )
82
+ self .assertEqual (task .details .get ("model" ), "Pattern" )
83
+ self .assertEqual (task .details .get ("id" ), pattern .id )
94
84
95
85
96
86
class PatternInstanceViewSetTest (SharedDataMixin , APITestCase ):
@@ -106,8 +96,7 @@ def test_pattern_instance_detail_view(self):
106
96
self .assertEqual (response .status_code , status .HTTP_200_OK )
107
97
self .assertEqual (response .data ["organization_id" ], 1 )
108
98
109
- @patch ("core.views.async_to_sync" )
110
- def test_pattern_instance_create_view (self , mock_async_to_sync ):
99
+ def test_pattern_instance_create_view (self ):
111
100
url = reverse ("patterninstance-list" )
112
101
data = {
113
102
"organization_id" : 2 ,
@@ -124,13 +113,13 @@ def test_pattern_instance_create_view(self, mock_async_to_sync):
124
113
instance = PatternInstance .objects .get (organization_id = 2 )
125
114
self .assertIsNotNone (instance )
126
115
127
- task_id = response .data ["task_id" ]
116
+ task_id = response .data .get ("task_id" )
117
+ self .assertIsInstance (task_id , int )
118
+
128
119
task = Task .objects .get (id = task_id )
129
120
self .assertEqual (task .status , "Initiated" )
130
-
131
- mock_async_to_sync .assert_called_once ()
132
- self .assertIn ("task_id" , response .data )
133
- self .assertIn ("message" , response .data )
121
+ self .assertEqual (task .details .get ("model" ), "PatternInstance" )
122
+ self .assertEqual (task .details .get ("id" ), instance .id )
134
123
135
124
136
125
class AutomationViewSetTest (SharedDataMixin , APITestCase ):
0 commit comments