Skip to content

Commit d1534da

Browse files
committed
fixes test
1 parent f5abb5c commit d1534da

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

services/web/server/tests/unit/with_dbs/03/test_users_rest_profiles.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from copy import deepcopy
1212
from http import HTTPStatus
1313
from typing import Any
14-
from unittest.mock import MagicMock, Mock
14+
from unittest.mock import patch
1515

1616
import pytest
1717
from aiohttp.test_utils import TestClient
@@ -544,28 +544,6 @@ async def test_update_existing_user_name(
544544
await assert_status(resp, status.HTTP_409_CONFLICT)
545545

546546

547-
@pytest.fixture
548-
def mock_failing_database_connection(mocker: Mock) -> MagicMock:
549-
"""
550-
async with engine.acquire() as conn:
551-
await conn.execute(query) --> will raise OperationalError
552-
"""
553-
# See http://initd.org/psycopg/docs/module.html
554-
conn_execute = mocker.patch.object(SAConnection, "execute")
555-
conn_execute.side_effect = OperationalError(
556-
"MOCK: server closed the connection unexpectedly"
557-
)
558-
559-
aysncpg_conn_execute = mocker.patch.object(AsyncConnection, "execute")
560-
aysncpg_conn_execute.side_effect = SQLAlchemyOperationalError(
561-
statement="MOCK statement",
562-
params=(),
563-
orig=OperationalError("MOCK: server closed the connection unexpectedly"),
564-
)
565-
566-
return conn_execute
567-
568-
569547
@pytest.mark.parametrize(
570548
"user_role,expected",
571549
[
@@ -575,7 +553,6 @@ def mock_failing_database_connection(mocker: Mock) -> MagicMock:
575553
async def test_get_profile_with_failing_db_connection(
576554
logged_user: UserInfoDict,
577555
client: TestClient,
578-
mock_failing_database_connection: MagicMock,
579556
expected: HTTPStatus,
580557
):
581558
"""
@@ -594,8 +571,22 @@ async def test_get_profile_with_failing_db_connection(
594571
url = client.app.router["get_my_profile"].url_for()
595572
assert str(url) == "/v0/me"
596573

597-
resp = await client.get(url.path)
598-
599-
data, error = await assert_status(resp, expected)
600-
assert not data
601-
assert error["message"] == "Authentication service is temporary unavailable"
574+
with patch.object(SAConnection, "execute") as mock_sa_execute, patch.object(
575+
AsyncConnection, "execute"
576+
) as mock_async_execute:
577+
578+
# Emulates a database connection failure
579+
mock_sa_execute.side_effect = OperationalError(
580+
"MOCK: server closed the connection unexpectedly"
581+
)
582+
mock_async_execute.side_effect = SQLAlchemyOperationalError(
583+
statement="MOCK statement",
584+
params=(),
585+
orig=OperationalError("MOCK: server closed the connection unexpectedly"),
586+
)
587+
588+
resp = await client.get(url.path)
589+
590+
data, error = await assert_status(resp, expected)
591+
assert not data
592+
assert error["message"] == "Authentication service is temporary unavailable"

0 commit comments

Comments
 (0)