From 40c41c2b7b3fedae484ad94d81b27ce88a09c5ed Mon Sep 17 00:00:00 2001 From: PrettyWood Date: Thu, 13 May 2021 16:27:33 +0200 Subject: [PATCH] fix deprecation warning and add tests --- databases/backends/postgres.py | 6 +++-- tests/test_databases.py | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) 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 67016502..bad9aba2 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 @@ -996,3 +997,49 @@ async def test_column_names(database_url, select_query): assert sorted(results[0]._mapping.keys()) == ["completed", "id", "text"] assert results[0]["text"] == "example1" assert results[0]["completed"] == True + + +@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]