1
+ from unittest .mock import AsyncMock
2
+
1
3
import pytest
2
4
from httpx import AsyncClient
3
5
from sqlalchemy import desc , select
@@ -19,7 +21,8 @@ async def test_developer_plus_can_create_run_of_transfer_his_group(
19
21
) -> None :
20
22
# Arrange
21
23
user = group_transfer .owner_group .get_member_of_role (role_developer_plus )
22
- mocker .patch ("syncmaster.worker.config.celery.send_task" )
24
+ mock_send_task = mocker .patch ("syncmaster.worker.config.celery.send_task" )
25
+ mock_to_thread = mocker .patch ("asyncio.to_thread" , new_callable = AsyncMock )
23
26
24
27
run = (
25
28
await session .scalars (
@@ -55,6 +58,13 @@ async def test_developer_plus_can_create_run_of_transfer_his_group(
55
58
}
56
59
assert result .status_code == 200
57
60
61
+ mock_to_thread .assert_awaited_once_with (
62
+ mock_send_task ,
63
+ "run_transfer_task" ,
64
+ kwargs = {"run_id" : run .id },
65
+ queue = group_transfer .queue .name ,
66
+ )
67
+
58
68
59
69
async def test_groupless_user_cannot_create_run (
60
70
client : AsyncClient ,
@@ -65,6 +75,7 @@ async def test_groupless_user_cannot_create_run(
65
75
) -> None :
66
76
# Arrange
67
77
mocker .patch ("syncmaster.worker.config.celery.send_task" )
78
+ mocker .patch ("asyncio.to_thread" , new_callable = AsyncMock )
68
79
69
80
# Act
70
81
result = await client .post (
@@ -94,6 +105,7 @@ async def test_group_member_cannot_create_run_of_other_group_transfer(
94
105
):
95
106
# Arrange
96
107
mocker .patch ("syncmaster.worker.config.celery.send_task" )
108
+ mocker .patch ("asyncio.to_thread" , new_callable = AsyncMock )
97
109
user = group .get_member_of_role (role_guest_plus )
98
110
99
111
# Act
@@ -132,7 +144,8 @@ async def test_superuser_can_create_run(
132
144
settings .worker .LOG_URL_TEMPLATE = (
133
145
"https://grafana.example.com?correlation_id={{ correlation_id }}&run_id={{ run.id }}"
134
146
)
135
- mocker .patch ("syncmaster.worker.config.celery.send_task" )
147
+ mock_send_task = mocker .patch ("syncmaster.worker.config.celery.send_task" )
148
+ mock_to_thread = mocker .patch ("asyncio.to_thread" , new_callable = AsyncMock )
136
149
137
150
# Act
138
151
result = await client .post (
@@ -161,6 +174,12 @@ async def test_superuser_can_create_run(
161
174
assert result .status_code == 200
162
175
assert "correlation_id" in response .get ("log_url" )
163
176
assert "run_id" in response .get ("log_url" )
177
+ mock_to_thread .assert_awaited_once_with (
178
+ mock_send_task ,
179
+ "run_transfer_task" ,
180
+ kwargs = {"run_id" : run .id },
181
+ queue = group_transfer .queue .name ,
182
+ )
164
183
165
184
166
185
async def test_unauthorized_user_cannot_create_run (
@@ -170,6 +189,7 @@ async def test_unauthorized_user_cannot_create_run(
170
189
) -> None :
171
190
# Arrange
172
191
mocker .patch ("syncmaster.worker.config.celery.send_task" )
192
+ mocker .patch ("asyncio.to_thread" , new_callable = AsyncMock )
173
193
174
194
# Act
175
195
result = await client .post (
@@ -198,6 +218,7 @@ async def test_group_member_cannot_create_run_of_unknown_transfer_error(
198
218
# Arrange
199
219
user = group_transfer .owner_group .get_member_of_role (role_guest_plus )
200
220
mocker .patch ("syncmaster.worker.config.celery.send_task" )
221
+ mocker .patch ("asyncio.to_thread" , new_callable = AsyncMock )
201
222
202
223
# Act
203
224
result = await client .post (
@@ -225,6 +246,7 @@ async def test_superuser_cannot_create_run_of_unknown_transfer_error(
225
246
) -> None :
226
247
# Arrange
227
248
mocker .patch ("syncmaster.worker.config.celery.send_task" )
249
+ mocker .patch ("asyncio.to_thread" , new_callable = AsyncMock )
228
250
229
251
# Act
230
252
result = await client .post (
0 commit comments