1- import os
21import sys
3- from collections import namedtuple
42import pytest
5- from rethinkdb import r
6- from rethinkdb .errors import ReqlRuntimeError
73
8- Helper = namedtuple ("Helper" , "r connection" )
9-
10- INTEGRATION_TEST_DB = 'integration_test'
4+ from asyncio import coroutine
5+ from tests .helpers import INTEGRATION_TEST_DB , IntegrationTestCaseBase
116
127
138@pytest .mark .asyncio
149@pytest .mark .integration
15- @pytest .mark .skipif (sys .version_info < (3 , 6 ),
16- reason = "requires python3.6 or higher" )
17- async def test_flow ():
18- """
19- Test the flow for 3.6 and up, async generators are
20- not supported in 3.5.
21- """
22-
23- r .set_loop_type ("asyncio" )
24-
25- connection = await r .connect (os .getenv ("REBIRTHDB_HOST" ))
26-
27- try :
28- await r .db_create (INTEGRATION_TEST_DB ).run (connection )
29- except ReqlRuntimeError :
30- pass
31-
32- connection .use (INTEGRATION_TEST_DB )
33-
34- await r .table_create ("marvel" ).run (connection )
35-
36- marvel_heroes = r .table ('marvel' )
37- await marvel_heroes .insert ({
38- 'id' : 1 ,
39- 'name' : 'Iron Man' ,
40- 'first_appearance' : 'Tales of Suspense #39'
41- }).run (connection )
42-
43- cursor = await marvel_heroes .run (connection )
44- async for hero in cursor :
45- assert hero ['name' ] == 'Iron Man'
46-
47- await connection .close ()
10+ @pytest .mark .skipif (
11+ sys .version_info == (3 , 4 ) or sys .version_info == (3 , 5 ),
12+ reason = "requires python3.4 or python3.5"
13+ )
14+ class TestAsyncio (IntegrationTestCaseBase ):
15+ def setup_method (self ):
16+ super (TestAsyncio , self ).setup_method ()
17+ self .table_name = 'test_asyncio'
18+ self .r .set_loop_type ('asyncio' )
19+
20+ def teardown_method (self ):
21+ super (TestAsyncio , self ).teardown_method ()
22+ self .r .set_loop_type (None )
23+
24+ @coroutine
25+ def test_flow_coroutine_paradigm (self ):
26+ connection = yield from self .conn
27+
28+ yield from self .r .table_create (self .table_name ).run (connection )
29+
30+ table = self .r .table (self .table_name )
31+ yield from table .insert ({
32+ 'id' : 1 ,
33+ 'name' : 'Iron Man' ,
34+ 'first_appearance' : 'Tales of Suspense #39'
35+ }).run (connection )
36+
37+ cursor = yield from table .run (connection )
38+
39+ while (yield from cursor .fetch_next ()):
40+ hero = yield from cursor .__anext__ ()
41+ assert hero ['name' ] == 'Iron Man'
42+
43+ yield from connection .close ()
0 commit comments