+
Skip to content

pytest anyio #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ twine
wheel

# Testing
anyio>=3.0.0,<4
autoflake
black
codecov
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest


@pytest.fixture
def anyio_backend():
return ("asyncio", {"debug": True})
19 changes: 2 additions & 17 deletions tests/test_columns.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import asyncio
import datetime
import functools
from enum import Enum

import databases
Expand All @@ -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()

Expand Down Expand Up @@ -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()
Expand Down
23 changes: 2 additions & 21 deletions tests/test_foreignkey.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import asyncio
import functools

import databases
import pytest
import sqlalchemy

import orm
from tests.settings import DATABASE_URL

pytestmark = pytest.mark.anyio

database = databases.Database(DATABASE_URL, force_rollback=True)
metadata = sqlalchemy.MetaData()

Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
28 changes: 2 additions & 26 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import asyncio
import functools

import databases
import pytest
import sqlalchemy

import orm
from tests.settings import DATABASE_URL

pytestmark = pytest.mark.anyio

database = databases.Database(DATABASE_URL, force_rollback=True)
metadata = sqlalchemy.MetaData()

Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand 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):
Expand All @@ -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")
Expand Down Expand Up @@ -174,15 +156,13 @@ 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")
assert await User.objects.filter(name="Tom").exists() is True
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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载