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_connection_await(self):
db = await aiosqlite.connect(TEST_DB)
self.assertIsInstance(db, aiosqlite.Connection)
async with db.execute("select 1, 2") as cursor:
rows = await cursor.fetchall()
self.assertEqual(rows, [(1, 2)])
await db.close()
async def test_async_methods(sqlite3_db_path, queries):
async with aiosqlite.connect(sqlite3_db_path) as conn:
conn.row_factory = dict_factory
users, sorted_users = await asyncio.gather(
queries.users.get_all(conn), queries.users.get_all_sorted(conn)
)
assert users == [
{"userid": 1, "username": "bobsmith", "firstname": "Bob", "lastname": "Smith"},
{"userid": 2, "username": "johndoe", "firstname": "John", "lastname": "Doe"},
{"userid": 3, "username": "janedoe", "firstname": "Jane", "lastname": "Doe"},
]
assert sorted_users == [
{"userid": 1, "username": "bobsmith", "firstname": "Bob", "lastname": "Smith"},
{"userid": 3, "username": "janedoe", "firstname": "Jane", "lastname": "Doe"},
{"userid": 2, "username": "johndoe", "firstname": "John", "lastname": "Doe"},
]
self.assertFalse(db.in_transaction)
self.assertEqual(db.total_changes, 1)
self.assertIsNone(db.row_factory)
self.assertEqual(db.text_factory, str)
async with db.cursor() as cursor:
await cursor.execute("select * from test_properties")
row = await cursor.fetchone()
self.assertIsInstance(row, tuple)
self.assertEqual(row, (1, 1, "hi"))
with self.assertRaises(TypeError):
_ = row["k"]
db.row_factory = aiosqlite.Row
db.text_factory = bytes
self.assertEqual(db.row_factory, aiosqlite.Row)
self.assertEqual(db.text_factory, bytes)
async with db.cursor() as cursor:
await cursor.execute("select * from test_properties")
row = await cursor.fetchone()
self.assertIsInstance(row, aiosqlite.Row)
self.assertEqual(row[1], 1)
self.assertEqual(row[2], b"hi")
self.assertEqual(row["k"], 1)
self.assertEqual(row["d"], b"hi")
async def init_db(app: web.Application) -> AsyncIterator[None]:
sqlite_db = app["DB_PATH"]
db = await aiosqlite.connect(sqlite_db)
db.row_factory = aiosqlite.Row
app["DB"] = db
yield
await db.close()
await cursor.execute("select * from test_properties")
row = await cursor.fetchone()
self.assertIsInstance(row, tuple)
self.assertEqual(row, (1, 1, "hi"))
with self.assertRaises(TypeError):
_ = row["k"]
db.row_factory = aiosqlite.Row
db.text_factory = bytes
self.assertEqual(db.row_factory, aiosqlite.Row)
self.assertEqual(db.text_factory, bytes)
async with db.cursor() as cursor:
await cursor.execute("select * from test_properties")
row = await cursor.fetchone()
self.assertIsInstance(row, aiosqlite.Row)
self.assertEqual(row[1], 1)
self.assertEqual(row[2], b"hi")
self.assertEqual(row["k"], 1)
self.assertEqual(row["d"], b"hi")
async def test_connection_context(self):
async with aiosqlite.connect(TEST_DB) as db:
self.assertIsInstance(db, aiosqlite.Connection)
async with db.execute("select 1, 2") as cursor:
rows = await cursor.fetchall()
self.assertEqual(rows, [(1, 2)])
async def execute(query: str, *args: str):
"""
Executes the given query + args in the database
"""
async with aiosqlite.connect(DB_FILE) as db:
await db.execute(query, args)
await db.commit()
async def init_db(app: web.Application) -> AsyncIterator[None]:
sqlite_db = get_db_path()
db = await aiosqlite.connect(sqlite_db)
db.row_factory = aiosqlite.Row
app["DB"] = db
yield
await db.close()
async def create_connection(self, with_db: bool) -> None:
if not self._connection: # pragma: no branch
self._connection = aiosqlite.connect(self.filename, isolation_level=None)
self._connection.start()
await self._connection._connect()
self._connection._conn.row_factory = sqlite3.Row
for pragma, val in self.pragmas.items():
cursor = await self._connection.execute("PRAGMA {}={}".format(pragma, val))
await cursor.close()
self.log.debug(
"Created connection %s with params: filename=%s %s",
self._connection,
self.filename,
" ".join(["{}={}".format(k, v) for k, v in self.pragmas.items()]),
)
async def setup(self):
try:
self._db = await aiosqlite.connect(self._path)
self._db.row_factory = aiosqlite.Row
except:
return False
def adapt_datetime(ts):
return time.mktime(ts.timetuple())
sqlite3.register_adapter(datetime, adapt_datetime)
try:
await self.db.executescript(schemaScript)
except Exception:
log.debug('Error while executing schema script')
return False