Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_prepare_28_max_args(self):
N = 32768
args = ','.join('${}'.format(i) for i in range(1, N + 1))
query = 'SELECT ARRAY[{}]'.format(args)
with self.assertRaisesRegex(
exceptions.InterfaceError,
'the number of query arguments cannot exceed 32767'):
await self.con.fetchval(query, *range(1, N + 1))
async def test_prepare_30_invalid_arg_count(self):
with self.assertRaisesRegex(
exceptions.InterfaceError,
'the server expects 1 argument for this query, 0 were passed'):
await self.con.fetchval('SELECT $1::int')
with self.assertRaisesRegex(
exceptions.InterfaceError,
'the server expects 0 arguments for this query, 1 was passed'):
await self.con.fetchval('SELECT 1', 1)
async def get_list(self, query) -> list:
if self.limit != -1:
query = query.limit(self.limit)
if self.sort:
query = query.order_by(text(f"{self.sort} {self.desc_s}"))
try:
return await query.offset(self.offset).gino.all()
except asyncpg.exceptions.UndefinedColumnError:
return []
def _check_listeners(self, listeners, listener_type):
if listeners:
count = len(listeners)
w = exceptions.InterfaceWarning(
'{conn!r} is being released to the pool but has {c} active '
'{type} listener{s}'.format(
conn=self, c=count, type=listener_type,
s='s' if count > 1 else ''))
warnings.warn(w)
model: schemes.EditToken,
user: models.User = Security(utils.AuthDependency(), scopes=["token_management"]),
):
item = (
await models.Token.query.where(models.Token.user_id == user.id)
.where(models.Token.id == model_id)
.gino.first()
)
if not item:
raise HTTPException(
status_code=404, detail=f"Token with id {model_id} does not exist!"
)
try:
await item.update(**model.dict(exclude_unset=True)).apply()
except (
asyncpg.exceptions.UniqueViolationError,
asyncpg.exceptions.NotNullViolationError,
asyncpg.exceptions.ForeignKeyViolationError,
) as e:
raise HTTPException(422, e.message)
return item
def __check_state_base(self, opname):
if self._state is TransactionState.COMMITTED:
raise apg_errors.InterfaceError(
'cannot {}; the transaction is already committed'.format(
opname))
if self._state is TransactionState.ROLLEDBACK:
raise apg_errors.InterfaceError(
'cannot {}; the transaction is already rolled back'.format(
opname))
if self._state is TransactionState.FAILED:
raise apg_errors.InterfaceError(
'cannot {}; the transaction is in error state'.format(
opname))
def _check_conn_validity(self, meth_name):
con_release_ctr = self._connection._pool_release_ctr
if con_release_ctr != self._con_release_ctr:
raise exceptions.InterfaceError(
'cannot call {}.{}(): '
'the underlying connection has been released back '
'to the pool'.format(self.__class__.__name__, meth_name))
if self._connection.is_closed():
raise exceptions.InterfaceError(
'cannot call {}.{}(): '
'the underlying connection is closed'.format(
self.__class__.__name__, meth_name))
pass
else:
raise
except asyncpg.exceptions.InternalClientError:
# edge-case where connection is already released
if txn._db_conn is not None:
raise
except Exception:
# failsafe terminate to make sure connection is cleaned
if txn._db_conn is None:
raise
if txn._db_conn._con is None:
raise
try:
await self._storage.terminate(txn._db_conn)
except asyncpg.exceptions.InterfaceError as ex:
if "released back to the pool" in str(ex):
pass
else:
raise
txn._db_conn = None
async def __bind_execute(self, args, limit, timeout):
protocol = self._connection._protocol
try:
data, status, _ = await protocol.bind_execute(
self._state, args, '', limit, True, timeout)
except exceptions.OutdatedSchemaCacheError:
await self._connection.reload_schema_state()
# We can not find all manually created prepared statements, so just
# drop known cached ones in the `self._connection`.
# Other manually created prepared statements will fail and
# invalidate themselves (unfortunately, clearing caches again).
self._state.mark_closed()
raise
self._last_status = status
return data