Skip to content

Commit

Permalink
test: wrap plain string sql statements with text() construct
Browse files Browse the repository at this point in the history
Use `text()` construct to wrap plain string SQL statements to avoid the
following `RemovedIn20Warning`:

    Using plain strings to indicate SQL statements without using the
    text() construct is  deprecated and will be removed in version 2.0.
    Ensure plain SQL statements are passed using the text() construct.

Refs #111
  • Loading branch information
jpvanhal committed Aug 7, 2023
1 parent 58eadb0 commit 44104f0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ filterwarnings = [
"ignore:The ``declarative_base\\(\\)`` function is now available as sqlalchemy.orm.declarative_base\\(\\).*:sqlalchemy.exc.RemovedIn20Warning",
"ignore:The autoload parameter is deprecated.*:sqlalchemy.exc.RemovedIn20Warning",
"ignore:The connection.execute\\(\\) method in SQLAlchemy 2.0 will accept parameters as a single dictionary or a single sequence of dictionaries only.*:sqlalchemy.exc.RemovedIn20Warning",
"ignore:Using plain strings to indicate SQL statements without using the text\\(\\) construct.*:sqlalchemy.exc.RemovedIn20Warning",
]

[tool.ruff]
Expand Down
38 changes: 21 additions & 17 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

import sqlalchemy as sa
from sqlalchemy import create_engine
from sqlalchemy import create_engine, text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.query import Query
Expand Down Expand Up @@ -117,27 +117,31 @@ class SchemaTestCase(TestCase):

def test_creates_search_index(self):
rows = self.session.execute(
"""SELECT relname
FROM pg_class
WHERE oid IN (
SELECT indexrelid
FROM pg_index, pg_class
WHERE pg_class.relname = 'textitem'
AND pg_class.oid = pg_index.indrelid
AND indisunique != 't'
AND indisprimary != 't'
) ORDER BY relname"""
text(
"""SELECT relname
FROM pg_class
WHERE oid IN (
SELECT indexrelid
FROM pg_index, pg_class
WHERE pg_class.relname = 'textitem'
AND pg_class.oid = pg_index.indrelid
AND indisunique != 't'
AND indisprimary != 't'
) ORDER BY relname"""
)
).fetchall()
assert self.should_create_indexes == [row[0] for row in rows]

def test_creates_search_trigger(self):
rows = self.session.execute(
"""SELECT DISTINCT trigger_name
FROM information_schema.triggers
WHERE event_object_table = 'textitem'
AND trigger_schema NOT IN
('pg_catalog', 'information_schema')
ORDER BY trigger_name"""
text(
"""SELECT DISTINCT trigger_name
FROM information_schema.triggers
WHERE event_object_table = 'textitem'
AND trigger_schema NOT IN
('pg_catalog', 'information_schema')
ORDER BY trigger_name"""
)
).fetchall()
assert self.should_create_triggers == [row[0] for row in rows]

Expand Down
6 changes: 4 additions & 2 deletions tests/test_sql_functions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from sqlalchemy import text

from tests import TestCase

Expand Down Expand Up @@ -87,9 +88,10 @@ class TestParse(TestCase):
def test_parse(self, input, output):
assert (
self.session.execute(
"SELECT parse_websearch('pg_catalog.simple', :input)", {"input": input}
text("SELECT parse_websearch('pg_catalog.simple', :input)"),
{"input": input},
).scalar()
== self.session.execute(
"SELECT CAST(:output AS tsquery)", {"output": output}
text("SELECT CAST(:output AS tsquery)"), {"output": output}
).scalar()
)
6 changes: 3 additions & 3 deletions tests/test_weighted_search_vector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re

import sqlalchemy as sa
from sqlalchemy import text
from sqlalchemy_utils import TSVectorType

from sqlalchemy_searchable import search
Expand Down Expand Up @@ -29,9 +30,8 @@ class TestCreateWeightedSearchVector(WeightedBase, SchemaTestCase):

def test_search_function_weights(self):
func_name = "textitem_search_vector_update"
sql = """SELECT proname,prosrc FROM pg_proc
WHERE proname='{name}';"""
name, src = self.session.execute(sql.format(name=func_name)).fetchone()
sql = text("SELECT proname,prosrc FROM pg_proc WHERE proname=:name")
name, src = self.session.execute(sql, {"name": func_name}).fetchone()
pattern = (
r"setweight\(to_tsvector\(.+?"
r"coalesce\(NEW.(\w+).+?"
Expand Down

0 comments on commit 44104f0

Please sign in to comment.