Skip to content

Commit bf444d5

Browse files
authored
Merge pull request #121 from coobas/fix/record-iteration
Fix record iteration
2 parents fedddc7 + e2afb0a commit bf444d5

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

databases/backends/postgres.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ def __getitem__(self, key: typing.Any) -> typing.Any:
115115
return raw
116116

117117
def __iter__(self) -> typing.Iterator:
118-
return iter(self._column_map)
118+
return iter(self._row.keys())
119119

120120
def __len__(self) -> int:
121-
return len(self._column_map)
121+
return len(self._row)
122122

123123

124124
class PostgresConnection(ConnectionBackend):

tests/test_databases.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,3 +802,25 @@ async def test_iterate_outside_transaction_with_temp_table(database_url):
802802
iterate_results.append(result)
803803

804804
assert len(iterate_results) == 5
805+
806+
807+
@pytest.mark.parametrize("database_url", DATABASE_URLS)
808+
@pytest.mark.parametrize("select_query", [notes.select(), "SELECT * FROM notes"])
809+
@async_adapter
810+
async def test_column_names(database_url, select_query):
811+
"""
812+
Test that column names are exposed correctly through `.keys()` on each row.
813+
"""
814+
async with Database(database_url) as database:
815+
async with database.transaction(force_rollback=True):
816+
# insert values
817+
query = notes.insert()
818+
values = {"text": "example1", "completed": True}
819+
await database.execute(query, values)
820+
# fetch results
821+
results = await database.fetch_all(query=select_query)
822+
assert len(results) == 1
823+
824+
assert sorted(results[0].keys()) == ["completed", "id", "text"]
825+
assert results[0]["text"] == "example1"
826+
assert results[0]["completed"] == True

tests/test_importer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ def test_invalid_format():
77
with pytest.raises(ImportFromStringError) as exc_info:
88
import_from_string("example:")
99
expected = 'Import string "example:" must be in format "<module>:<attribute>".'
10-
assert expected in str(exc_info.value)
10+
assert exc_info.match(expected)
1111

1212

1313
def test_invalid_module():
1414
with pytest.raises(ImportFromStringError) as exc_info:
1515
import_from_string("module_does_not_exist:myattr")
1616
expected = 'Could not import module "module_does_not_exist".'
17-
assert expected in str(exc_info.value)
17+
assert exc_info.match(expected)
1818

1919

2020
def test_invalid_attr():
2121
with pytest.raises(ImportFromStringError) as exc_info:
2222
import_from_string("tempfile:attr_does_not_exist")
2323
expected = 'Attribute "attr_does_not_exist" not found in module "tempfile".'
24-
assert expected in str(exc_info.value)
24+
assert exc_info.match(expected)
2525

2626

2727
def test_internal_import_error():

0 commit comments

Comments
 (0)