Skip to content

Commit

Permalink
chore: fix query test asyncio
Browse files Browse the repository at this point in the history
  • Loading branch information
tiptenbrink committed Dec 21, 2023
1 parent 54b8804 commit df32be0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
14 changes: 6 additions & 8 deletions backend/tests/query_test/data_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import asyncio
from datetime import date, datetime
import os
from uuid import uuid4

import uvloop
import pytest
import pytest_asyncio
from sqlalchemy import Engine, create_engine, text
Expand All @@ -18,6 +18,7 @@
CLASS_TYPE,
CLASSIFICATION_TABLE,
)

from tests.test_util import Fixture
from store.conn import get_conn
from store.store import Store
Expand All @@ -29,12 +30,10 @@
)


@pytest.fixture(scope="session", autouse=True)
def event_loop():
"""Necessary for async tests with module-scoped fixtures"""
loop = asyncio.get_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="module")
def event_loop_policy():
return uvloop.EventLoopPolicy()



@pytest.fixture(scope="module")
Expand Down Expand Up @@ -85,7 +84,6 @@ async def new_db_store(api_config: Config, admin_engine: Engine):
drop_db = text(f"DROP DATABASE {db_name};")
conn.execute(drop_db)


@pytest.mark.asyncio
async def test_create_class(new_db_store: Store):
async with get_conn(new_db_store) as conn:
Expand Down
17 changes: 6 additions & 11 deletions backend/tests/query_test/store_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


from apiserver.env import Config, load_config
import uvloop
from tests.test_util import Fixture, AsyncFixture
from store.conn import get_conn
from store.store import Store
Expand All @@ -22,21 +23,15 @@
"Skipping store_test as QUERY_TEST is not set.", allow_module_level=True
)


@pytest.fixture(scope="session", autouse=True)
def event_loop():
"""Necessary for async tests with module-scoped fixtures"""
loop = asyncio.get_event_loop()
yield loop
loop.close()

@pytest.fixture(scope="module")
def event_loop_policy():
return uvloop.EventLoopPolicy()

@pytest.fixture(scope="module")
def api_config() -> Fixture[Config]:
test_config_path = res_path.joinpath("querytestenv.toml")
yield load_config(test_config_path)


@pytest_asyncio.fixture(scope="module")
async def local_store(api_config: Config):
# each module makes its own database
Expand All @@ -48,7 +43,7 @@ async def local_store(api_config: Config):
yield store


@pytest_asyncio.fixture
@pytest_asyncio.fixture(scope="module")
async def setup_table(local_store: Store) -> AsyncFixture[str]:
table_name = f"table_{randint(0, 100000)}"
async with get_conn(local_store) as conn:
Expand All @@ -72,7 +67,7 @@ async def setup_table(local_store: Store) -> AsyncFixture[str]:
await conn.execute(query)


@pytest.mark.asyncio
@pytest.mark.asyncio(scope="module")
async def test_insert(local_store: Store, setup_table: LiteralString):
row: LiteralDict = {"first": 3, "second": "some", "third": "other"}
async with get_conn(local_store) as conn:
Expand Down

0 comments on commit df32be0

Please sign in to comment.