1111from copy import deepcopy
1212from http import HTTPStatus
1313from typing import Any
14- from unittest .mock import MagicMock , Mock
14+ from unittest .mock import patch
1515
1616import pytest
1717from 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:
575553async 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