Skip to content

Commit 20a0fe2

Browse files
tests: fix
1 parent 525d1f0 commit 20a0fe2

File tree

1 file changed

+48
-54
lines changed

1 file changed

+48
-54
lines changed

services/web/server/tests/unit/with_dbs/02/test_projects_conversations_handlers.py

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
# pylint: disable=too-many-statements
77

88

9-
from collections.abc import Callable
9+
from collections.abc import Callable, Iterable
1010
from http import HTTPStatus
11-
from unittest.mock import MagicMock
11+
from types import SimpleNamespace
1212

1313
import pytest
14-
import simcore_service_webserver.conversations._conversation_message_service
15-
import simcore_service_webserver.conversations._conversation_service
14+
import simcore_service_webserver.conversations._conversation_message_service as conversation_message_service
15+
import simcore_service_webserver.conversations._conversation_service as conversation_service
1616
import sqlalchemy as sa
1717
from aiohttp.test_utils import TestClient
1818
from models_library.api_schemas_webserver.projects_conversations import (
@@ -34,14 +34,18 @@
3434

3535

3636
@pytest.fixture
37-
def mock_notify_function(mocker: MockerFixture) -> Callable[[object, str], MagicMock]:
38-
def _mock(target: object, function_name: str) -> MagicMock:
39-
return mocker.patch.object(
40-
target,
41-
function_name,
37+
def mock_functions_factory(
38+
mocker: MockerFixture,
39+
) -> Callable[[Iterable[tuple[object, str]]], SimpleNamespace]:
40+
def _patch(targets_and_names: Iterable[tuple[object, str]]) -> SimpleNamespace:
41+
return SimpleNamespace(
42+
**{
43+
name: mocker.patch.object(target, name)
44+
for target, name in targets_and_names
45+
}
4246
)
4347

44-
return _mock
48+
return _patch
4549

4650

4751
@pytest.mark.parametrize(
@@ -82,19 +86,14 @@ async def test_project_conversations_full_workflow(
8286
logged_user: UserInfoDict,
8387
user_project: ProjectDict,
8488
expected: HTTPStatus,
85-
mock_notify_function: Callable[[object, str], MagicMock],
89+
mock_functions_factory: Callable[[Iterable[tuple[object, str]]], SimpleNamespace],
8690
):
87-
mocked_notify_conversation_created = mock_notify_function(
88-
simcore_service_webserver.conversations._conversation_service,
89-
"notify_conversation_created",
90-
)
91-
mocked_notify_conversation_updated = mock_notify_function(
92-
simcore_service_webserver.conversations._conversation_service,
93-
"notify_conversation_updated",
94-
)
95-
mocked_notify_conversation_deleted = mock_notify_function(
96-
simcore_service_webserver.conversations._conversation_service,
97-
"notify_conversation_deleted",
91+
mocks = mock_functions_factory(
92+
[
93+
(conversation_service, "notify_conversation_created"),
94+
(conversation_service, "notify_conversation_updated"),
95+
(conversation_service, "notify_conversation_deleted"),
96+
]
9897
)
9998

10099
base_url = client.app.router["list_project_conversations"].url_for(
@@ -121,11 +120,11 @@ async def test_project_conversations_full_workflow(
121120
assert ConversationRestGet.model_validate(data)
122121
_first_conversation_id = data["conversationId"]
123122

124-
assert mocked_notify_conversation_created.call_count == 1
125-
kwargs = mocked_notify_conversation_created.call_args.kwargs
123+
assert mocks.notify_conversation_created.call_count == 1
124+
kwargs = mocks.notify_conversation_created.call_args.kwargs
126125

127126
assert f"{kwargs['project_id']}" == user_project["uuid"]
128-
assert kwargs["conversation"].name == "My first conversation"
127+
assert kwargs["conversation"].name == "My conversation"
129128

130129
# Now we will create second conversation
131130
body = {"name": "My conversation", "type": "PROJECT_ANNOTATION"}
@@ -136,8 +135,8 @@ async def test_project_conversations_full_workflow(
136135
)
137136
assert ConversationRestGet.model_validate(data)
138137

139-
assert mocked_notify_conversation_created.call_count == 2
140-
kwargs = mocked_notify_conversation_created.call_args.kwargs
138+
assert mocks.notify_conversation_created.call_count == 2
139+
kwargs = mocks.notify_conversation_created.call_args.kwargs
141140

142141
assert f"{kwargs['project_id']}" == user_project["uuid"]
143142
assert kwargs["conversation"].name == "My conversation"
@@ -172,8 +171,8 @@ async def test_project_conversations_full_workflow(
172171
)
173172
assert data["name"] == updated_name
174173

175-
assert mocked_notify_conversation_updated.call_count == 1
176-
kwargs = mocked_notify_conversation_updated.call_args.kwargs
174+
assert mocks.notify_conversation_updated.call_count == 1
175+
kwargs = mocks.notify_conversation_updated.call_args.kwargs
177176

178177
assert f"{kwargs['project_id']}" == user_project["uuid"]
179178
assert kwargs["conversation"].name == updated_name
@@ -185,10 +184,10 @@ async def test_project_conversations_full_workflow(
185184
status.HTTP_204_NO_CONTENT,
186185
)
187186

188-
assert mocked_notify_conversation_deleted.call_count == 1
189-
kwargs = mocked_notify_conversation_deleted.call_args.kwargs
187+
assert mocks.notify_conversation_deleted.call_count == 1
188+
kwargs = mocks.notify_conversation_deleted.call_args.kwargs
190189

191-
assert kwargs["conversation"].conversation_id == _first_conversation_id
190+
assert f"{kwargs['conversation_id']}" == _first_conversation_id
192191

193192
# Now we will list all conversations for the project
194193
resp = await client.get(f"{base_url}")
@@ -216,19 +215,14 @@ async def test_project_conversation_messages_full_workflow(
216215
user_project: ProjectDict,
217216
expected: HTTPStatus,
218217
postgres_db: sa.engine.Engine,
219-
mock_notify_function: Callable[[object, str], MagicMock],
218+
mock_functions_factory: Callable[[Iterable[tuple[object, str]]], SimpleNamespace],
220219
):
221-
mocked_notify_conversation_message_created = mock_notify_function(
222-
simcore_service_webserver.conversations._conversation_message_service,
223-
"notify_conversation_message_created",
224-
)
225-
mocked_notify_conversation_message_updated = mock_notify_function(
226-
simcore_service_webserver.conversations._conversation_message_service,
227-
"notify_conversation_message_updated",
228-
)
229-
mocked_notify_conversation_message_deleted = mock_notify_function(
230-
simcore_service_webserver.conversations._conversation_message_service,
231-
"notify_conversation_message_deleted",
220+
mocks = mock_functions_factory(
221+
[
222+
(conversation_message_service, "notify_conversation_message_created"),
223+
(conversation_message_service, "notify_conversation_message_updated"),
224+
(conversation_message_service, "notify_conversation_message_deleted"),
225+
]
232226
)
233227

234228
base_project_url = client.app.router["list_project_conversations"].url_for(
@@ -258,8 +252,8 @@ async def test_project_conversation_messages_full_workflow(
258252
assert ConversationMessageRestGet.model_validate(data)
259253
_first_message_id = data["messageId"]
260254

261-
assert mocked_notify_conversation_message_created.call_count == 1
262-
kwargs = mocked_notify_conversation_message_created.call_args.kwargs
255+
assert mocks.notify_conversation_message_created.call_count == 1
256+
kwargs = mocks.notify_conversation_message_created.call_args.kwargs
263257

264258
assert f"{kwargs['project_id']}" == user_project["uuid"]
265259
assert kwargs["conversation_message"].content == "My first message"
@@ -274,8 +268,8 @@ async def test_project_conversation_messages_full_workflow(
274268
assert ConversationMessageRestGet.model_validate(data)
275269
_second_message_id = data["messageId"]
276270

277-
assert mocked_notify_conversation_message_created.call_count == 2
278-
kwargs = mocked_notify_conversation_message_created.call_args.kwargs
271+
assert mocks.notify_conversation_message_created.call_count == 2
272+
kwargs = mocks.notify_conversation_message_created.call_args.kwargs
279273

280274
assert user_project["uuid"] == f"{kwargs['project_id']}"
281275
assert kwargs["conversation_message"].content == "My second message"
@@ -306,8 +300,8 @@ async def test_project_conversation_messages_full_workflow(
306300
expected,
307301
)
308302

309-
assert mocked_notify_conversation_message_updated.call_count == 1
310-
kwargs = mocked_notify_conversation_message_updated.call_args.kwargs
303+
assert mocks.notify_conversation_message_updated.call_count == 1
304+
kwargs = mocks.notify_conversation_message_updated.call_args.kwargs
311305

312306
assert user_project["uuid"] == f"{kwargs['project_id']}"
313307
assert kwargs["conversation_message"].content == updated_content
@@ -342,8 +336,8 @@ async def test_project_conversation_messages_full_workflow(
342336
status.HTTP_204_NO_CONTENT,
343337
)
344338

345-
assert mocked_notify_conversation_message_deleted.call_count == 1
346-
kwargs = mocked_notify_conversation_message_deleted.call_args.kwargs
339+
assert mocks.notify_conversation_message_deleted.call_count == 1
340+
kwargs = mocks.notify_conversation_message_deleted.call_args.kwargs
347341

348342
assert f"{kwargs['project_id']}" == user_project["uuid"]
349343
assert f"{kwargs['conversation_id']}" == _conversation_id
@@ -440,8 +434,8 @@ async def test_project_conversation_messages_full_workflow(
440434
status.HTTP_204_NO_CONTENT,
441435
)
442436

443-
assert mocked_notify_conversation_message_deleted.call_count == 2
444-
kwargs = mocked_notify_conversation_message_deleted.call_args.kwargs
437+
assert mocks.notify_conversation_message_deleted.call_count == 2
438+
kwargs = mocks.notify_conversation_message_deleted.call_args.kwargs
445439

446440
assert f"{kwargs['project_id']}" == user_project["uuid"]
447441
assert f"{kwargs['conversation_id']}" == _conversation_id

0 commit comments

Comments
 (0)