|
| 1 | +from collections import namedtuple |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +from async_generator import async_generator, yield_ |
| 6 | +import pytest |
| 7 | +from rethinkdb import RethinkDB |
| 8 | +from rethinkdb.errors import ReqlRuntimeError |
| 9 | +import trio |
| 10 | + |
| 11 | + |
| 12 | +INTEGRATION_TEST_DB = 'integration_test' |
| 13 | +r = RethinkDB() |
| 14 | +r.set_loop_type('trio') |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +@async_generator |
| 19 | +async def integration_db(nursery): |
| 20 | + async with r.open(db='test', nursery=nursery) as conn: |
| 21 | + try: |
| 22 | + await r.db_create(INTEGRATION_TEST_DB).run(conn) |
| 23 | + except ReqlRuntimeError: |
| 24 | + pass |
| 25 | + await yield_(r.db(INTEGRATION_TEST_DB)) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +@async_generator |
| 30 | +async def marvel_table(integration_db, nursery): |
| 31 | + async with r.open(db='test', nursery=nursery) as conn: |
| 32 | + await r.table_create('marvel').run(conn) |
| 33 | + await yield_(r.table('marvel')) |
| 34 | + await r.table_drop('marvel').run(conn) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.trio |
| 38 | +@pytest.mark.integration |
| 39 | +async def test_trio(marvel_table, nursery): |
| 40 | + """ |
| 41 | + Test the flow for 3.6 and up, async generators are |
| 42 | + not supported in 3.5. |
| 43 | + """ |
| 44 | + async with r.open(db='test', nursery=nursery) as conn: |
| 45 | + await marvel_table.insert({ |
| 46 | + 'id': 1, |
| 47 | + 'name': 'Iron Man', |
| 48 | + 'first_appearance': 'Tales of Suspense #39' |
| 49 | + }).run(conn) |
| 50 | + |
| 51 | + cursor = await marvel_table.run(conn) |
| 52 | + async for hero in cursor: |
| 53 | + hero['name'] == 'Iron Man' |
0 commit comments