Skip to content

Commit

Permalink
Fix linting and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Feb 21, 2024
1 parent a2835c9 commit c5a5b19
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
15 changes: 11 additions & 4 deletions databases/backends/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def __init__(
self._dialect.supports_native_decimal = False
self._pool = SQLitePool(self._database_url, **self._options)

async def connect(self) -> None: ...
async def connect(self) -> None:
...

async def disconnect(self) -> None:
# if it extsis, remove reference to connection to cached in-memory database on disconnect
Expand Down Expand Up @@ -141,7 +142,9 @@ async def execute_many(self, queries: typing.List[ClauseElement]) -> None:
for single_query in queries:
await self.execute(single_query)

async def iterate(self, query: ClauseElement) -> typing.AsyncGenerator[typing.Any, None]:
async def iterate(
self, query: ClauseElement
) -> typing.AsyncGenerator[typing.Any, None]:
assert self._connection is not None, "Connection is not acquired"
query_str, args, result_columns, context = self._compile(query)
column_maps = create_column_maps(result_columns)
Expand Down Expand Up @@ -191,15 +194,19 @@ def _compile(self, query: ClauseElement) -> typing.Tuple[str, list, tuple]:
compiled._loose_column_name_matching,
)

mapping = {key: "$" + str(i) for i, (key, _) in enumerate(compiled_params, start=1)}
mapping = {
key: "$" + str(i) for i, (key, _) in enumerate(compiled_params, start=1)
}
compiled_query = compiled.string % mapping
result_map = compiled._result_columns

else:
compiled_query = compiled.string

query_message = compiled_query.replace(" \n", " ").replace("\n", " ")
logger.debug("Query: %s Args: %s", query_message, repr(tuple(args)), extra=LOG_EXTRA)
logger.debug(
"Query: %s Args: %s", query_message, repr(tuple(args)), extra=LOG_EXTRA
)
return compiled.string, args, result_map, CompilationContext(execution_context)

@property
Expand Down
29 changes: 21 additions & 8 deletions tests/test_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import gc
import itertools
import os
import re
import sqlite3
from typing import MutableMapping
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -179,7 +178,9 @@ async def test_queries(database_url):
assert result == "example1"

# fetch_val() with no rows
query = sqlalchemy.sql.select(*[notes.c.text]).where(notes.c.text == "impossible")
query = sqlalchemy.sql.select(*[notes.c.text]).where(
notes.c.text == "impossible"
)
result = await database.fetch_val(query=query)
assert result is None

Expand Down Expand Up @@ -496,7 +497,9 @@ async def check_transaction(transaction, active_transaction):
assert transaction._transaction is active_transaction

async with database.transaction() as transaction:
await asyncio.create_task(check_transaction(transaction, transaction._transaction))
await asyncio.create_task(
check_transaction(transaction, transaction._transaction)
)


@pytest.mark.parametrize("database_url", DATABASE_URLS)
Expand All @@ -509,18 +512,24 @@ async def test_transaction_context_child_task_inheritance_example(database_url):
async with Database(database_url) as database:
async with database.transaction():
# Create a note
await database.execute(notes.insert().values(id=1, text="setup", completed=True))
await database.execute(
notes.insert().values(id=1, text="setup", completed=True)
)

# Change the note from the same task
await database.execute(notes.update().where(notes.c.id == 1).values(text="prior"))
await database.execute(
notes.update().where(notes.c.id == 1).values(text="prior")
)

# Confirm the change
result = await database.fetch_one(notes.select().where(notes.c.id == 1))
assert result.text == "prior"

async def run_update_from_child_task(connection):
# Change the note from a child task
await connection.execute(notes.update().where(notes.c.id == 1).values(text="test"))
await connection.execute(
notes.update().where(notes.c.id == 1).values(text="test")
)

await asyncio.create_task(run_update_from_child_task(database.connection()))

Expand Down Expand Up @@ -573,7 +582,9 @@ async def test_transaction_context_sibling_task_isolation_example(database_url):

async def tx1(connection):
async with connection.transaction():
await db.execute(notes.insert(), values={"id": 1, "text": "tx1", "completed": False})
await db.execute(
notes.insert(), values={"id": 1, "text": "tx1", "completed": False}
)
setup.set()
await done.wait()

Expand Down Expand Up @@ -875,7 +886,9 @@ async def test_transaction_decorator_concurrent(database_url):

@database.transaction()
async def insert_data():
await database.execute(query=notes.insert().values(text="example", completed=True))
await database.execute(
query=notes.insert().values(text="example", completed=True)
)

async with database:
await asyncio.gather(
Expand Down

0 comments on commit c5a5b19

Please sign in to comment.