Debian Codegraph @

oslo_db/tests/sqlalchemy/test_asyncio_facade.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
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)