diff --git a/databases/backends/postgres.py b/databases/backends/postgres.py index 7d0f8e92..73f998a2 100644 --- a/databases/backends/postgres.py +++ b/databases/backends/postgres.py @@ -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() @@ -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() diff --git a/tests/test_databases.py b/tests/test_databases.py index 2477960e..61568a7c 100644 --- a/tests/test_databases.py +++ b/tests/test_databases.py @@ -3,6 +3,7 @@ import decimal import functools import os +import re import pytest import sqlalchemy @@ -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]