Skip to content

Commit

Permalink
fix deprecation warning and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Aug 17, 2021
1 parent 11aed30 commit bce05db
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
6 changes: 4 additions & 2 deletions databases/backends/postgres.py
Expand Up @@ -113,7 +113,8 @@ def keys(self) -> typing.KeysView:

warnings.warn(
"The `Row.keys()` method is deprecated to mimic SQLAlchemy behaviour, "
"use `Row._mapping.keys()` instead."
"use `Row._mapping.keys()` instead.",
DeprecationWarning,
)
return self._mapping.keys()

Expand All @@ -122,7 +123,8 @@ def values(self) -> typing.ValuesView:

warnings.warn(
"The `Row.values()` method is deprecated to mimic SQLAlchemy behaviour, "
"use `Row._mapping.values()` instead."
"use `Row._mapping.values()` instead.",
DeprecationWarning,
)
return self._mapping.values()

Expand Down
47 changes: 47 additions & 0 deletions tests/test_databases.py
Expand Up @@ -3,6 +3,7 @@
import decimal
import functools
import os
import re

import pytest
import sqlalchemy
Expand Down Expand Up @@ -1014,3 +1015,49 @@ async def test_task(db):

tasks = [test_task(database) for i in range(4)]
await asyncio.gather(*tasks)


@pytest.mark.parametrize("database_url", DATABASE_URLS)
@async_adapter
async def test_posgres_interface(database_url):
"""
Since SQLAlchemy 1.4, `Row.values()` is removed and `Row.keys()` is deprecated.
Custom postgres interface mimics more or less this behaviour by deprecating those
two methods
"""
database_url = DatabaseURL(database_url)

if database_url.scheme != "postgresql":
pytest.skip("Test is only for postgresql")

async with Database(database_url) as database:
async with database.transaction(force_rollback=True):
query = notes.insert()
values = {"text": "example1", "completed": True}
await database.execute(query, values)

query = notes.select()
result = await database.fetch_one(query=query)

with pytest.warns(
DeprecationWarning,
match=re.escape(
"The `Row.keys()` method is deprecated to mimic SQLAlchemy behaviour, "
"use `Row._mapping.keys()` instead."
),
):
assert (
list(result.keys())
== [k for k in result]
== ["id", "text", "completed"]
)

with pytest.warns(
DeprecationWarning,
match=re.escape(
"The `Row.values()` method is deprecated to mimic SQLAlchemy behaviour, "
"use `Row._mapping.values()` instead."
),
):
# avoid checking `id` at index 0 since it may change depending on the launched tests
assert list(result.values())[1:] == ["example1", True]

0 comments on commit bce05db

Please sign in to comment.