| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from oslo_context import context as oslo_context |
| 14 |
from oslo_db.sqlalchemy import asyncio_facade |
| 15 |
from oslo_db.tests import base as test_base |
| 16 |
from sqlalchemy import text |
| 17 |
|
| 18 |
asyncio_facade.async_transaction_context_provider(oslo_context.RequestContext) |
| 19 |
|
| 20 |
|
| 21 |
class AsyncioFacadeTest(test_base.BaseAsyncioCase): |
| 22 |
|
| 23 |
def setUp(self): |
| 24 |
super().setUp() |
| 25 |
asyncio_facade.configure( |
| 26 |
connection="sqlite+aiosqlite://", |
| 27 |
) |
| 28 |
|
| 29 |
def tearDown(self): |
| 30 |
asyncio_facade._async_context_manager._root_factory = ( |
| 31 |
asyncio_facade._async_context_manager._create_root_factory() |
| 32 |
) |
| 33 |
super().tearDown() |
| 34 |
|
| 35 |
async def test_contextmanager_session(self): |
| 36 |
context = oslo_context.RequestContext() |
| 37 |
async with asyncio_facade.reader.using(context) as session: |
| 38 |
result = await session.execute(text("select 1")) |
| 39 |
self.assertEqual(result.all(), [(1,)]) |
| 40 |
|
| 41 |
async def test_contextmanager_connection(self): |
| 42 |
context = oslo_context.RequestContext() |
| 43 |
async with asyncio_facade.reader.connection.using( |
| 44 |
context |
| 45 |
) as connection: |
| 46 |
result = await connection.execute(text("select 1")) |
| 47 |
self.assertEqual(result.all(), [(1,)]) |
| 48 |
|
| 49 |
async def test_callable_session(self): |
| 50 |
context = oslo_context.RequestContext() |
| 51 |
|
| 52 |
@asyncio_facade.reader |
| 53 |
async def select_one(context): |
| 54 |
result = await context.async_session.execute(text("select 1")) |
| 55 |
self.assertEqual(result.all(), [(1,)]) |
| 56 |
|
| 57 |
await select_one(context) |
| 58 |
|
| 59 |
async def test_callable_connection(self): |
| 60 |
context = oslo_context.RequestContext() |
| 61 |
|
| 62 |
@asyncio_facade.reader.connection |
| 63 |
async def select_one(context): |
| 64 |
result = await context.async_connection.execute(text("select 1")) |
| 65 |
self.assertEqual(result.all(), [(1,)]) |
| 66 |
|
| 67 |
await select_one(context) |