diff --git a/requirements.txt b/requirements.txt index 73d0670..2b39c79 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,6 +6,7 @@ twine wheel # Testing +anyio>=3.0.0,<4 autoflake black codecov diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..2fa5cff --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,6 @@ +import pytest + + +@pytest.fixture +def anyio_backend(): + return ("asyncio", {"debug": True}) diff --git a/tests/test_columns.py b/tests/test_columns.py index 1dcb008..e0aaea3 100644 --- a/tests/test_columns.py +++ b/tests/test_columns.py @@ -1,6 +1,4 @@ -import asyncio import datetime -import functools from enum import Enum import databases @@ -10,6 +8,8 @@ import orm from tests.settings import DATABASE_URL +pytestmark = pytest.mark.anyio + database = databases.Database(DATABASE_URL, force_rollback=True) metadata = sqlalchemy.MetaData() @@ -47,21 +47,6 @@ def create_test_database(): metadata.drop_all(engine) -def async_adapter(wrapped_func): - """ - Decorator used to run async test cases. - """ - - @functools.wraps(wrapped_func) - def run_sync(*args, **kwargs): - loop = asyncio.new_event_loop() - task = wrapped_func(*args, **kwargs) - return loop.run_until_complete(task) - - return run_sync - - -@async_adapter async def test_model_crud(): async with database: await Example.objects.create() diff --git a/tests/test_foreignkey.py b/tests/test_foreignkey.py index 4e0e957..99fd4c8 100644 --- a/tests/test_foreignkey.py +++ b/tests/test_foreignkey.py @@ -1,6 +1,3 @@ -import asyncio -import functools - import databases import pytest import sqlalchemy @@ -8,6 +5,8 @@ import orm from tests.settings import DATABASE_URL +pytestmark = pytest.mark.anyio + database = databases.Database(DATABASE_URL, force_rollback=True) metadata = sqlalchemy.MetaData() @@ -69,21 +68,6 @@ def create_test_database(): metadata.drop_all(engine) -def async_adapter(wrapped_func): - """ - Decorator used to run async test cases. - """ - - @functools.wraps(wrapped_func) - def run_sync(*args, **kwargs): - loop = asyncio.new_event_loop() - task = wrapped_func(*args, **kwargs) - return loop.run_until_complete(task) - - return run_sync - - -@async_adapter async def test_model_crud(): async with database: album = await Album.objects.create(name="Malibu") @@ -100,7 +84,6 @@ async def test_model_crud(): assert track.album.name == "Malibu" -@async_adapter async def test_select_related(): async with database: album = await Album.objects.create(name="Malibu") @@ -122,7 +105,6 @@ async def test_select_related(): assert len(tracks) == 6 -@async_adapter async def test_fk_filter(): async with database: malibu = await Album.objects.create(name="Malibu") @@ -166,7 +148,6 @@ async def test_fk_filter(): assert track.album.name == "Malibu" -@async_adapter async def test_multiple_fk(): async with database: acme = await Organisation.objects.create(ident="ACME Ltd") diff --git a/tests/test_models.py b/tests/test_models.py index a5c2480..0bc6940 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,6 +1,3 @@ -import asyncio -import functools - import databases import pytest import sqlalchemy @@ -8,6 +5,8 @@ import orm from tests.settings import DATABASE_URL +pytestmark = pytest.mark.anyio + database = databases.Database(DATABASE_URL, force_rollback=True) metadata = sqlalchemy.MetaData() @@ -40,20 +39,6 @@ def create_test_database(): metadata.drop_all(engine) -def async_adapter(wrapped_func): - """ - Decorator used to run async test cases. - """ - - @functools.wraps(wrapped_func) - def run_sync(*args, **kwargs): - loop = asyncio.new_event_loop() - task = wrapped_func(*args, **kwargs) - return loop.run_until_complete(task) - - return run_sync - - def test_model_class(): assert list(User.fields.keys()) == ["id", "name"] assert isinstance(User.fields["id"], orm.Integer) @@ -69,7 +54,6 @@ def test_model_pk(): assert user.id == 1 -@async_adapter async def test_model_crud(): async with database: users = await User.objects.all() @@ -95,7 +79,6 @@ async def test_model_crud(): assert users == [] -@async_adapter async def test_model_get(): async with database: with pytest.raises(orm.NoMatch): @@ -114,7 +97,6 @@ async def test_model_get(): assert same_user.pk == user.pk -@async_adapter async def test_model_filter(): async with database: await User.objects.create(name="Tom") @@ -174,7 +156,6 @@ async def test_model_filter(): assert await products.count() == 3 -@async_adapter async def test_model_exists(): async with database: await User.objects.create(name="Tom") @@ -182,7 +163,6 @@ async def test_model_exists(): assert await User.objects.filter(name="Jane").exists() is False -@async_adapter async def test_model_count(): async with database: await User.objects.create(name="Tom") @@ -193,7 +173,6 @@ async def test_model_count(): assert await User.objects.filter(name__icontains="T").count() == 1 -@async_adapter async def test_model_limit(): async with database: await User.objects.create(name="Tom") @@ -203,7 +182,6 @@ async def test_model_limit(): assert len(await User.objects.limit(2).all()) == 2 -@async_adapter async def test_model_limit_with_filter(): async with database: await User.objects.create(name="Tom") @@ -213,7 +191,6 @@ async def test_model_limit_with_filter(): assert len(await User.objects.limit(2).filter(name__iexact="Tom").all()) == 2 -@async_adapter async def test_offset(): async with database: await User.objects.create(name="Tom") @@ -223,7 +200,6 @@ async def test_offset(): assert users[0].name == "Jane" -@async_adapter async def test_model_first(): async with database: tom = await User.objects.create(name="Tom")