1- import asyncio
2- import functools
3-
41import databases
52import pytest
63import sqlalchemy
74
85import orm
96from tests .settings import DATABASE_URL
107
8+ pytestmark = pytest .mark .anyio
9+
1110database = databases .Database (DATABASE_URL , force_rollback = True )
1211metadata = 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-
5742def 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
7357async 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
9982async 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
118100async 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
178159async 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
186166async 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
197176async 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
207185async 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
217194async 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
227203async def test_model_first ():
228204 async with database :
229205 tom = await User .objects .create (name = "Tom" )
0 commit comments