Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@tb.with_connection_options(server_settings={
'client_min_messages': 'notice'
})
async def test_log_listener_01(self):
q1 = asyncio.Queue()
def notice_callb(con, message):
# Message fields depend on PG version, hide some values.
dct = message.as_dict()
del dct['server_source_line']
q1.put_nowait((con, type(message), dct))
async def raise_notice():
await self.con.execute(
"""DO $$
BEGIN RAISE NOTICE 'catch me!'; END;
$$ LANGUAGE plpgsql"""
@tb.with_connection_options(server_settings={
'client_min_messages': 'notice'
})
async def test_log_listener_02(self):
q1 = asyncio.Queue()
cur_id = None
def notice_callb(con, message):
q1.put_nowait((con, cur_id, message.message))
con = self.con
await con.execute(
"CREATE FUNCTION _test(i INT) RETURNS int LANGUAGE plpgsql AS $$"
" BEGIN"
" RAISE NOTICE '1_%', i;"
" PERFORM pg_sleep(0.1);"
@tb.with_connection_options(command_timeout=0.2)
async def test_command_timeout_01(self):
for methname in {'fetch', 'fetchrow', 'fetchval', 'execute'}:
with self.assertRaises(asyncio.TimeoutError), \
self.assertRunUnder(MAX_RUNTIME):
meth = getattr(self.con, methname)
await meth('select pg_sleep(10)')
self.assertEqual(await self.con.fetch('select 1'), [(1,)])
@tb.with_connection_options(statement_cache_size=0)
async def test_prepare_23_no_stmt_cache_seq(self):
self.assertEqual(self.con._stmt_cache.get_max_size(), 0)
async def check_simple():
# Run a simple query a few times.
self.assertEqual(await self.con.fetchval('SELECT 1'), 1)
self.assertEqual(await self.con.fetchval('SELECT 2'), 2)
self.assertEqual(await self.con.fetchval('SELECT 1'), 1)
await check_simple()
# Run a query that timeouts.
with self.assertRaises(asyncio.TimeoutError):
await self.con.fetchrow('select pg_sleep(10)', timeout=0.02)
# Check that we can run new queries after a timeout.
@tb.with_connection_options(database='asyncpg_intro_test')
async def test_introspection_on_large_db(self):
await self.con.execute(
'CREATE TABLE base ({})'.format(
','.join('c{:02} varchar'.format(n) for n in range(50))
)
)
for n in range(1000):
await self.con.execute(
'CREATE TABLE child_{:04} () inherits (base)'.format(n)
)
with self.assertRunUnder(MAX_RUNTIME):
await self.con.fetchval('SELECT $1::int[]', [1, 2])
@tb.with_connection_options(connection_class=SlowPrepareConnection,
command_timeout=0.3)
async def test_timeout_covers_prepare_01(self):
for methname in {'fetch', 'fetchrow', 'fetchval', 'execute'}:
with self.assertRaises(asyncio.TimeoutError):
meth = getattr(self.con, methname)
await meth('select pg_sleep($1)', 0.2)
@tb.with_connection_options(max_cached_statement_lifetime=142)
async def test_prepare_24_max_lifetime(self):
cache = self.con._stmt_cache
self.assertEqual(cache.get_max_lifetime(), 142)
cache.set_max_lifetime(1)
s = await self.con._prepare('SELECT 1', use_cache=True)
state = s._state
s = await self.con._prepare('SELECT 1', use_cache=True)
self.assertIs(s._state, state)
s = await self.con._prepare('SELECT 1', use_cache=True)
self.assertIs(s._state, state)
await asyncio.sleep(1)
@tb.with_connection_options(max_cacheable_statement_size=1)
async def test_introspection_no_stmt_cache_02(self):
# max_cacheable_statement_size will disable caching both for
# the user query and for the introspection query.
old_uid = apg_con._uid
await self.con.fetchval('SELECT $1::int[]', [1, 2])
await self.con.execute('''
CREATE EXTENSION IF NOT EXISTS hstore
''')
try:
await self.con.set_builtin_type_codec(
'hstore', codec_name='pg_contrib.hstore')
finally:
await self.con.execute('''