summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2020-02-24 23:48:07 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-02-26 08:03:46 +0100
commitd0f1c03331718f1b146cf0fd6ffefa19538eebe4 (patch)
tree4025d1dcf1a1bde07d58bccaf8c378582f564b74
parent3d62ddb02695cfafc847951b45e6e1ec5dc0b149 (diff)
Refs #31211 -- Prevented SearchConfig nesting in SearchVector and SearchQuery init.
Passing a SearchConfig instance directly to SearchVector and SearchQuery would result in nested SearchConfig instance.
-rw-r--r--django/contrib/postgres/search.py10
-rw-r--r--tests/postgres_tests/test_search.py11
2 files changed, 17 insertions, 4 deletions
diff --git a/django/contrib/postgres/search.py b/django/contrib/postgres/search.py
index dceda6c8da..19a0ca777c 100644
--- a/django/contrib/postgres/search.py
+++ b/django/contrib/postgres/search.py
@@ -41,6 +41,12 @@ class SearchConfig(Expression):
config = Value(config)
self.config = config
+ @classmethod
+ def from_parameter(cls, config):
+ if config is None or isinstance(config, cls):
+ return config
+ return cls(config)
+
def get_source_expressions(self):
return [self.config]
@@ -75,7 +81,7 @@ class SearchVector(SearchVectorCombinable, Func):
def __init__(self, *expressions, **extra):
super().__init__(*expressions, **extra)
config = self.extra.get('config', self.config)
- self.config = SearchConfig(config) if config else None
+ self.config = SearchConfig.from_parameter(config)
weight = self.extra.get('weight')
if weight is not None and not hasattr(weight, 'resolve_expression'):
weight = Value(weight)
@@ -162,7 +168,7 @@ class SearchQuery(SearchQueryCombinable, Value):
}
def __init__(self, value, output_field=None, *, config=None, invert=False, search_type='plain'):
- self.config = SearchConfig(config) if config else None
+ self.config = SearchConfig.from_parameter(config)
self.invert = invert
if search_type not in self.SEARCH_TYPES:
raise ValueError("Unknown search_type argument '%s'." % search_type)
diff --git a/tests/postgres_tests/test_search.py b/tests/postgres_tests/test_search.py
index 89aa8c6662..765b846fb3 100644
--- a/tests/postgres_tests/test_search.py
+++ b/tests/postgres_tests/test_search.py
@@ -6,13 +6,13 @@ All text copyright Python (Monty) Pictures. Thanks to sacred-texts.com for the
transcript.
"""
from django.contrib.postgres.search import (
- SearchQuery, SearchRank, SearchVector,
+ SearchConfig, SearchQuery, SearchRank, SearchVector,
)
from django.db import connection
from django.db.models import F
from django.test import SimpleTestCase, modify_settings, skipUnlessDBFeature
-from . import PostgreSQLTestCase
+from . import PostgreSQLSimpleTestCase, PostgreSQLTestCase
from .models import Character, Line, Scene
@@ -118,6 +118,13 @@ class SearchVectorFieldTest(GrailTestData, PostgreSQLTestCase):
self.assertNotIn('COALESCE(COALESCE', str(searched.query))
+class SearchConfigTests(PostgreSQLSimpleTestCase):
+ def test_from_parameter(self):
+ self.assertIsNone(SearchConfig.from_parameter(None))
+ self.assertEqual(SearchConfig.from_parameter('foo'), SearchConfig('foo'))
+ self.assertEqual(SearchConfig.from_parameter(SearchConfig('bar')), SearchConfig('bar'))
+
+
class MultipleFieldsTest(GrailTestData, PostgreSQLTestCase):
def test_simple_on_dialogue(self):