Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit ec54759

Browse files
aminalaeegraingert
andauthored
pytest anyio (#101)
Co-authored-by: Thomas Grainger <[email protected]>
1 parent 289eb92 commit ec54759

File tree

5 files changed

+13
-64
lines changed

5 files changed

+13
-64
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ twine
66
wheel
77

88
# Testing
9+
anyio>=3.0.0,<4
910
autoflake
1011
black
1112
codecov

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def anyio_backend():
6+
return ("asyncio", {"debug": True})

tests/test_columns.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import asyncio
21
import datetime
3-
import functools
42
from enum import Enum
53

64
import databases
@@ -10,6 +8,8 @@
108
import orm
119
from tests.settings import DATABASE_URL
1210

11+
pytestmark = pytest.mark.anyio
12+
1313
database = databases.Database(DATABASE_URL, force_rollback=True)
1414
metadata = sqlalchemy.MetaData()
1515

@@ -47,21 +47,6 @@ def create_test_database():
4747
metadata.drop_all(engine)
4848

4949

50-
def async_adapter(wrapped_func):
51-
"""
52-
Decorator used to run async test cases.
53-
"""
54-
55-
@functools.wraps(wrapped_func)
56-
def run_sync(*args, **kwargs):
57-
loop = asyncio.new_event_loop()
58-
task = wrapped_func(*args, **kwargs)
59-
return loop.run_until_complete(task)
60-
61-
return run_sync
62-
63-
64-
@async_adapter
6550
async def test_model_crud():
6651
async with database:
6752
await Example.objects.create()

tests/test_foreignkey.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import asyncio
2-
import functools
3-
41
import databases
52
import pytest
63
import sqlalchemy
74

85
import orm
96
from tests.settings import DATABASE_URL
107

8+
pytestmark = pytest.mark.anyio
9+
1110
database = databases.Database(DATABASE_URL, force_rollback=True)
1211
metadata = sqlalchemy.MetaData()
1312

@@ -69,21 +68,6 @@ def create_test_database():
6968
metadata.drop_all(engine)
7069

7170

72-
def async_adapter(wrapped_func):
73-
"""
74-
Decorator used to run async test cases.
75-
"""
76-
77-
@functools.wraps(wrapped_func)
78-
def run_sync(*args, **kwargs):
79-
loop = asyncio.new_event_loop()
80-
task = wrapped_func(*args, **kwargs)
81-
return loop.run_until_complete(task)
82-
83-
return run_sync
84-
85-
86-
@async_adapter
8771
async def test_model_crud():
8872
async with database:
8973
album = await Album.objects.create(name="Malibu")
@@ -100,7 +84,6 @@ async def test_model_crud():
10084
assert track.album.name == "Malibu"
10185

10286

103-
@async_adapter
10487
async def test_select_related():
10588
async with database:
10689
album = await Album.objects.create(name="Malibu")
@@ -122,7 +105,6 @@ async def test_select_related():
122105
assert len(tracks) == 6
123106

124107

125-
@async_adapter
126108
async def test_fk_filter():
127109
async with database:
128110
malibu = await Album.objects.create(name="Malibu")
@@ -166,7 +148,6 @@ async def test_fk_filter():
166148
assert track.album.name == "Malibu"
167149

168150

169-
@async_adapter
170151
async def test_multiple_fk():
171152
async with database:
172153
acme = await Organisation.objects.create(ident="ACME Ltd")

tests/test_models.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import asyncio
2-
import functools
3-
41
import databases
52
import pytest
63
import sqlalchemy
74

85
import orm
96
from tests.settings import DATABASE_URL
107

8+
pytestmark = pytest.mark.anyio
9+
1110
database = databases.Database(DATABASE_URL, force_rollback=True)
1211
metadata = sqlalchemy.MetaData()
1312

@@ -40,20 +39,6 @@ def create_test_database():
4039
metadata.drop_all(engine)
4140

4241

43-
def async_adapter(wrapped_func):
44-
"""
45-
Decorator used to run async test cases.
46-
"""
47-
48-
@functools.wraps(wrapped_func)
49-
def run_sync(*args, **kwargs):
50-
loop = asyncio.new_event_loop()
51-
task = wrapped_func(*args, **kwargs)
52-
return loop.run_until_complete(task)
53-
54-
return run_sync
55-
56-
5742
def test_model_class():
5843
assert list(User.fields.keys()) == ["id", "name"]
5944
assert isinstance(User.fields["id"], orm.Integer)
@@ -69,7 +54,6 @@ def test_model_pk():
6954
assert user.id == 1
7055

7156

72-
@async_adapter
7357
async def test_model_crud():
7458
async with database:
7559
users = await User.objects.all()
@@ -95,7 +79,6 @@ async def test_model_crud():
9579
assert users == []
9680

9781

98-
@async_adapter
9982
async def test_model_get():
10083
async with database:
10184
with pytest.raises(orm.NoMatch):
@@ -114,7 +97,6 @@ async def test_model_get():
11497
assert same_user.pk == user.pk
11598

11699

117-
@async_adapter
118100
async def test_model_filter():
119101
async with database:
120102
await User.objects.create(name="Tom")
@@ -174,15 +156,13 @@ async def test_model_filter():
174156
assert await products.count() == 3
175157

176158

177-
@async_adapter
178159
async def test_model_exists():
179160
async with database:
180161
await User.objects.create(name="Tom")
181162
assert await User.objects.filter(name="Tom").exists() is True
182163
assert await User.objects.filter(name="Jane").exists() is False
183164

184165

185-
@async_adapter
186166
async def test_model_count():
187167
async with database:
188168
await User.objects.create(name="Tom")
@@ -193,7 +173,6 @@ async def test_model_count():
193173
assert await User.objects.filter(name__icontains="T").count() == 1
194174

195175

196-
@async_adapter
197176
async def test_model_limit():
198177
async with database:
199178
await User.objects.create(name="Tom")
@@ -203,7 +182,6 @@ async def test_model_limit():
203182
assert len(await User.objects.limit(2).all()) == 2
204183

205184

206-
@async_adapter
207185
async def test_model_limit_with_filter():
208186
async with database:
209187
await User.objects.create(name="Tom")
@@ -213,7 +191,6 @@ async def test_model_limit_with_filter():
213191
assert len(await User.objects.limit(2).filter(name__iexact="Tom").all()) == 2
214192

215193

216-
@async_adapter
217194
async def test_offset():
218195
async with database:
219196
await User.objects.create(name="Tom")
@@ -223,7 +200,6 @@ async def test_offset():
223200
assert users[0].name == "Jane"
224201

225202

226-
@async_adapter
227203
async def test_model_first():
228204
async with database:
229205
tom = await User.objects.create(name="Tom")

0 commit comments

Comments
 (0)