From 9b221bd0f8d634028d389282bfb65bbfe225b01f Mon Sep 17 00:00:00 2001 From: zermelo-wisen Date: Wed, 15 May 2024 09:58:27 +0300 Subject: [PATCH] test: fix sqlalchemy deprecated execute param --- _appmap/test/test_sqlalchemy.py | 32 +++++++++++++++++++------------- appmap/sqlalchemy.py | 2 +- pyproject.toml | 1 - requirements-dev.txt | 3 ++- requirements-test.txt | 1 + tox.ini | 6 ++++-- 6 files changed, 27 insertions(+), 18 deletions(-) diff --git a/_appmap/test/test_sqlalchemy.py b/_appmap/test/test_sqlalchemy.py index f0d97a7a..5da284d9 100644 --- a/_appmap/test/test_sqlalchemy.py +++ b/_appmap/test/test_sqlalchemy.py @@ -10,6 +10,7 @@ MetaData, String, Table, + text, create_engine, ) @@ -23,7 +24,10 @@ class TestSQLAlchemy(AppMapTestBase): @staticmethod def test_sql_capture(connection, events): - connection.execute("SELECT 1") + # Passing a string to execute is deprecated in 1.4 + # and removed in 2.0. We wrap it with text(). + # https://docs.sqlalchemy.org/en/14/core/connections.html#sqlalchemy.engine.Connection.execute + connection.execute(text("SELECT 1")) assert events[0].sql_query == DictIncluding( {"sql": "SELECT 1", "database_type": "sqlite"} ) @@ -38,25 +42,27 @@ def test_capture_ddl(events, schema): assert "CREATE TABLE addresses" in events[-2].sql_query["sql"] # pylint: disable=unused-argument - def test_capture_insert(self, connection, schema, events): + def test_capture_insert(self, engine, schema, events): ins = self.users.insert().values(name="jack", fullname="Jack Jones") - connection.execute(ins) + with engine.begin() as conn: + conn.execute(ins) assert ( events[-2].sql_query["sql"] == "INSERT INTO users (name, fullname) VALUES (?, ?)" ) # pylint: disable=unused-argument - def test_capture_insert_many(self, connection, schema, events): - connection.execute( - self.addresses.insert(), - [ - {"user_id": 1, "email_address": "jack@yahoo.com"}, - {"user_id": 1, "email_address": "jack@msn.com"}, - {"user_id": 2, "email_address": "www@www.org"}, - {"user_id": 2, "email_address": "wendy@aol.com"}, - ], - ) + def test_capture_insert_many(self, engine, schema, events): + with engine.begin() as conn: + conn.execute( + self.addresses.insert(), + [ + {"user_id": 1, "email_address": "jack@yahoo.com"}, + {"user_id": 1, "email_address": "jack@msn.com"}, + {"user_id": 2, "email_address": "www@www.org"}, + {"user_id": 2, "email_address": "wendy@aol.com"}, + ], + ) assert ( events[-2].sql_query["sql"] == "-- 4 times\nINSERT INTO addresses (user_id, email_address) VALUES (?, ?)" diff --git a/appmap/sqlalchemy.py b/appmap/sqlalchemy.py index 3358b7c4..2e4c382f 100644 --- a/appmap/sqlalchemy.py +++ b/appmap/sqlalchemy.py @@ -15,7 +15,7 @@ @event.listens_for(Engine, "before_cursor_execute") # pylint: disable=too-many-arguments,unused-argument def capture_sql_call(conn, cursor, statement, parameters, context, executemany): - """Capture SQL query callinto appmap.""" + """Capture SQL query call into appmap.""" if is_instrumentation_disabled(): # We must be in the middle of fetching object representation. # Don't record this query in the appmap. diff --git a/pyproject.toml b/pyproject.toml index e1aa1918..33df765a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,7 +51,6 @@ packaging = ">=19.0" # install it and the rest of the dev dependencies. [tool.poetry.group.dev.dependencies] -SQLAlchemy = "^1.4.11" Twisted = "^22.4.0" asgiref = "^3.7.2" black = "^24.2.0" diff --git a/requirements-dev.txt b/requirements-dev.txt index c2d5913e..bc06c81f 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -4,4 +4,5 @@ django flask >=2, <= 3 pytest-django<4.8 fastapi -httpx \ No newline at end of file +httpx +sqlalchemy \ No newline at end of file diff --git a/requirements-test.txt b/requirements-test.txt index 04392b28..16b853d6 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,2 +1,3 @@ django ~= 3.2 pytest-django < 4.8 +sqlalchemy < 2.0 diff --git a/tox.ini b/tox.ini index a25a18d4..ed1c73b7 100644 --- a/tox.ini +++ b/tox.ini @@ -2,7 +2,7 @@ isolated_build = true # The *-web environments test the latest versions of Django and Flask with the full test suite. For # older version of the web frameworks, just run the tests that are specific to them. -envlist = py3{8,9,10,11,12}-{web,django3,flask2} +envlist = py3{8,9,10,11,12}-{web,django3,flask2,sqlalchemy1} [testenv] allowlist_externals = @@ -13,9 +13,10 @@ deps= poetry web: Django >=4.0, <5.0 web: Flask >=3.0 + web: sqlalchemy >=2.0, <3.0 flask2: Flask >= 2.0, <3.0 django3: Django >=3.2, <4.0 - + sqlalchemy1: sqlalchemy >=1.4.11, <2.0 commands = poetry install -v @@ -23,6 +24,7 @@ commands = web: poetry run appmap-python {posargs:pytest} django3: poetry run appmap-python pytest _appmap/test/test_django.py flask2: poetry run appmap-python pytest _appmap/test/test_flask.py + sqlalchemy1: poetry run appmap-python pytest _appmap/test/test_sqlalchemy.py [testenv:vendoring] skip_install = True