diff --git a/backend/pyproject.toml b/backend/pyproject.toml index 246f398..be37e41 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -103,3 +103,10 @@ env = "GL_TOKEN" [tool.semantic_release.publish] dist_glob_patterns = ["dist/*"] upload_to_vcs_release = true + +[tool.pytest.ini_options] +asyncio_mode = "auto" +asyncio_default_fixture_loop_scope = "function" + +[tool.coverage.run] +concurrency = ["greenlet", "thread"] diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 44a4d5a..45b11d6 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -1,3 +1,4 @@ +import asyncio import json import os from unittest import mock @@ -14,6 +15,11 @@ from bec_atlas.main import AtlasApp from bec_atlas.router.redis_router import BECAsyncRedisManager +class TestRedis(fakeredis.FakeAsyncRedis): + async def execute_command(self, *args, **options): + return await asyncio.shield(super().execute_command(*args, **options)) + + def import_mongodb_data(mongo_client: pymongo.MongoClient): """ Import the test data into the mongodb container. The data is stored in the @@ -86,9 +92,7 @@ def backend(redis_server): return fakeredis.FakeStrictRedis(server=redis_server) mongo_client = mongomock.MongoClient("localhost", 27027) - fake_async_redis = fakeredis.FakeAsyncRedis( - server=redis_server, username="ingestor", password="ingestor" - ) + fake_async_redis = TestRedis(server=redis_server, username="ingestor", password="ingestor") fake_async_redis.connection_pool.connection_kwargs["username"] = "ingestor" fake_async_redis.connection_pool.connection_kwargs["password"] = "ingestor" diff --git a/backend/tests/test_redis_router.py b/backend/tests/test_redis_router.py index ed67d9e..d882b77 100644 --- a/backend/tests/test_redis_router.py +++ b/backend/tests/test_redis_router.py @@ -1,4 +1,3 @@ -import asyncio from unittest import mock import pytest @@ -332,8 +331,7 @@ def test_bec_access_profile_allows_op(backend, bec_access, key, redis_op, raise_ app.redis_router.bec_access_profile_allows_op(bec_access, key, redis_op) -# @pytest.mark.asyncio -def test_redis_get(logged_in_client, deployment, backend): +async def test_redis_get(logged_in_client, deployment, backend): client = logged_in_client _, app = backend response = client.patch( @@ -364,7 +362,5 @@ def test_redis_get(logged_in_client, deployment, backend): "/api/v1/redis", params={"deployment": deployment["_id"], "key": "test_key"} ) assert response.status_code == 200 - assert response.json() == { - "data": {"data": {"test_key": "test"}}, - "metadata": {"message": "test"}, - } + test_response = {"data": {"data": {"test_key": "test"}}, "metadata": {"message": "test"}} + assert response.json() == test_response diff --git a/backend/tests/test_redis_websocket.py b/backend/tests/test_redis_websocket.py index 7bfef4c..4a04fe3 100644 --- a/backend/tests/test_redis_websocket.py +++ b/backend/tests/test_redis_websocket.py @@ -5,6 +5,7 @@ import pytest from bec_lib.endpoints import MessageEndpoints from bec_atlas.router.redis_router import RedisAtlasEndpoints, RemoteAccess +import pytest_asyncio @pytest.fixture @@ -23,8 +24,7 @@ def backend_client(backend): return client, app -@pytest.fixture -@pytest.mark.asyncio(loop_scope="session") +@pytest_asyncio.fixture async def connected_ws(backend_client): client, app = backend_client deployment = client.get("/api/v1/deployments/realm", params={"realm": "demo_beamline_1"}).json() @@ -39,22 +39,19 @@ async def connected_ws(backend_client): yield backend_client -@pytest.mark.asyncio(loop_scope="session") async def test_redis_websocket_connect(connected_ws): - _, app = await anext(connected_ws) + _, app = connected_ws assert "sid" in app.redis_websocket.users -@pytest.mark.asyncio(loop_scope="session") async def test_redis_websocket_disconnect(connected_ws): - _, app = await anext(connected_ws) + _, app = connected_ws await app.redis_websocket.socket.handlers["/"]["disconnect"]("sid") assert "sid" not in app.redis_websocket.users -@pytest.mark.asyncio(loop_scope="session") async def test_redis_websocket_multiple_connect(connected_ws): - client, app = await anext(connected_ws) + client, app = connected_ws await app.redis_websocket.socket.handlers["/"]["connect"]( "sid2", @@ -70,9 +67,8 @@ async def test_redis_websocket_multiple_connect(connected_ws): assert "sid2" in app.redis_websocket.users -@pytest.mark.asyncio(loop_scope="session") async def test_redis_websocket_multiple_connect_same_sid(connected_ws): - client, app = await anext(connected_ws) + client, app = connected_ws await app.redis_websocket.socket.handlers["/"]["connect"]( "sid", @@ -88,18 +84,16 @@ async def test_redis_websocket_multiple_connect_same_sid(connected_ws): assert len(app.redis_websocket.users) == 1 -@pytest.mark.asyncio(loop_scope="session") async def test_redis_websocket_multiple_disconnect_same_sid(connected_ws): - client, app = await anext(connected_ws) + _, app = connected_ws await app.redis_websocket.socket.handlers["/"]["disconnect"]("sid") await app.redis_websocket.socket.handlers["/"]["disconnect"]("sid") assert "sid" not in app.redis_websocket.users assert len(app.redis_websocket.users) == 0 -@pytest.mark.asyncio(loop_scope="session") async def test_redis_websocket_register_wrong_endpoint_raises(backend_client): - client, app = backend_client + _, app = backend_client with mock.patch.object(app.redis_websocket.socket, "emit") as emit: await app.redis_websocket.socket.handlers["/"]["connect"]("sid") await app.redis_websocket.socket.handlers["/"]["register"]( @@ -108,9 +102,8 @@ async def test_redis_websocket_register_wrong_endpoint_raises(backend_client): assert mock.call("error", mock.ANY, room="sid") in emit.mock_calls -@pytest.mark.asyncio(loop_scope="session") async def test_redis_websocket_register(connected_ws): - client, app = await anext(connected_ws) + _, app = connected_ws with mock.patch.object(app.redis_websocket.socket, "emit") as emit: with mock.patch.object(app.redis_websocket.socket, "enter_room") as enter_room: await app.redis_websocket.socket.handlers["/"]["register"](