Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit c708bb7

Browse files
committed
Handling raw response from sql drivers (as part of #9)
1 parent c4cbf2a commit c708bb7

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

databases/backends/postgres.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,21 @@ def __init__(self, row: tuple, result_columns: tuple, dialect: Dialect) -> None:
7474
for idx, (column_name, _, _, datatype) in enumerate(self._result_columns)
7575
}
7676

77-
def __getitem__(self, key: str) -> typing.Any:
78-
idx, datatype = self._column_map[key]
79-
raw = self._row[idx]
77+
def __getitem__(self, key: typing.Any) -> typing.Any:
8078
try:
81-
processor = _result_processors[datatype]
79+
idx, datatype = self._column_map[key]
80+
raw = self._row[idx]
81+
try:
82+
processor = _result_processors[datatype]
83+
except KeyError:
84+
processor = datatype.result_processor(self._dialect, None)
85+
_result_processors[datatype] = processor
86+
87+
if processor is not None:
88+
return processor(raw)
89+
return raw
8290
except KeyError:
83-
processor = datatype.result_processor(self._dialect, None)
84-
_result_processors[datatype] = processor
85-
86-
if processor is not None:
87-
return processor(raw)
88-
return raw
91+
return self._row[key]
8992

9093
def __iter__(self) -> typing.Iterator:
9194
return iter(self._column_map)

tests/test_databases.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,5 +587,37 @@ async def test_queries_with_expose_backend_connection(database_url):
587587
assert result[1] == "example1"
588588
assert result[2] == True
589589

590-
# TODO unittests at the connection level
591-
# TODO (?) Double-check connections are released back to the pool
590+
591+
@pytest.mark.parametrize("database_url", DATABASE_URLS)
592+
@async_adapter
593+
async def test_queries_with_sqlalchemy_test(database_url):
594+
"""
595+
Test for inserting and retriving the data using the `sqlalchemy.text` query object.
596+
"""
597+
async with Database(database_url) as database:
598+
async with database.transaction(force_rollback=True):
599+
# Insert query
600+
insert_query = "INSERT INTO notes (text, completed) VALUES (:text, :completed)"
601+
602+
# execute_many()
603+
query = sqlalchemy.text(insert_query)
604+
values = [("example1", True), ("example2", False), ("example3", True)]
605+
for text, completed in values:
606+
current_query = query.bindparams(text=text, completed=completed)
607+
await database.execute(current_query)
608+
609+
# Select query
610+
select_query = "SELECT notes.id, notes.text, notes.completed FROM notes"
611+
612+
# fetch_all()
613+
query = sqlalchemy.text(select_query)
614+
results = await database.fetch_all(query)
615+
616+
assert len(results) == 3
617+
# Raw output for the raw request
618+
assert results[0][1] == "example1"
619+
assert results[0][2] == True
620+
assert results[1][1] == "example2"
621+
assert results[1][2] == False
622+
assert results[2][1] == "example3"
623+
assert results[2][2] == True

0 commit comments

Comments
 (0)