Skip to content

Commit

Permalink
[4.2.x] Moved SearchVectorIndexTests.test_search_vector_index to post…
Browse files Browse the repository at this point in the history
…gres_tests.test_indexes.

Backport of 02a04ab from main
  • Loading branch information
felixxm committed Apr 6, 2023
1 parent e68da20 commit dece89d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
15 changes: 15 additions & 0 deletions tests/postgres_tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,21 @@ def test_tsvector_op_class_gist_index(self):
editor.remove_index(Scene, index)
self.assertNotIn(index_name, self.get_constraints(table))

def test_search_vector(self):
"""SearchVector generates IMMUTABLE SQL in order to be indexable."""
index_name = "test_search_vector"
index = Index(SearchVector("id", "scene", config="english"), name=index_name)
# Indexed function must be IMMUTABLE.
with connection.schema_editor() as editor:
editor.add_index(Scene, index)
constraints = self.get_constraints(Scene._meta.db_table)
self.assertIn(index_name, constraints)
self.assertIs(constraints[index_name]["index"], True)

with connection.schema_editor() as editor:
editor.remove_index(Scene, index)
self.assertNotIn(index_name, self.get_constraints(Scene._meta.db_table))

def test_hash_index(self):
# Ensure the table is there and doesn't have an index.
self.assertNotIn("field", self.get_constraints(CharFieldModel._meta.db_table))
Expand Down
21 changes: 0 additions & 21 deletions tests/postgres_tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
All text copyright Python (Monty) Pictures. Thanks to sacred-texts.com for the
transcript.
"""
from django.db import connection
from django.db.models import F, Value

from . import PostgreSQLSimpleTestCase, PostgreSQLTestCase
Expand Down Expand Up @@ -609,26 +608,6 @@ def test_ranking_with_masked_normalization(self):
)


class SearchVectorIndexTests(PostgreSQLTestCase):
def test_search_vector_index(self):
"""SearchVector generates IMMUTABLE SQL in order to be indexable."""
# This test should be moved to test_indexes and use a functional
# index instead once support lands (see #26167).
query = Line.objects.all().query
resolved = SearchVector("id", "dialogue", config="english").resolve_expression(
query
)
compiler = query.get_compiler(connection.alias)
sql, params = resolved.as_sql(compiler, connection)
# Indexed function must be IMMUTABLE.
with connection.cursor() as cursor:
cursor.execute(
"CREATE INDEX search_vector_index ON %s USING GIN (%s)"
% (Line._meta.db_table, sql),
params,
)


class SearchQueryTests(PostgreSQLSimpleTestCase):
def test_str(self):
tests = (
Expand Down

0 comments on commit dece89d

Please sign in to comment.