diff options
| author | Hannes Ljungberg <hannes@5monkeys.se> | 2020-01-24 22:23:30 +0000 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-01-27 15:09:47 +0100 |
| commit | 7edd06a9cf0e7207d21d4afc819fac80e1288ff0 (patch) | |
| tree | 64dbe5ca0a90685aa91aef978abac1bd53a7f29a | |
| parent | 50cf183d219face91822c75fa0a15fe2fe3cb32d (diff) | |
Improved SearchVectorCombinable and SearchQueryCombinable error messages.
| -rw-r--r-- | django/contrib/postgres/search.py | 9 | ||||
| -rw-r--r-- | tests/postgres_tests/test_search.py | 13 |
2 files changed, 18 insertions, 4 deletions
diff --git a/django/contrib/postgres/search.py b/django/contrib/postgres/search.py index c3b940d436..ff0e9cf3b5 100644 --- a/django/contrib/postgres/search.py +++ b/django/contrib/postgres/search.py @@ -38,7 +38,10 @@ class SearchVectorCombinable: def _combine(self, other, connector, reversed): if not isinstance(other, SearchVectorCombinable) or not self.config == other.config: - raise TypeError('SearchVector can only be combined with other SearchVectors') + raise TypeError( + 'SearchVector can only be combined with other SearchVector ' + 'instances, got %s.' % type(other).__name__ + ) if reversed: return CombinedSearchVector(other, connector, self, self.config) return CombinedSearchVector(self, connector, other, self.config) @@ -105,8 +108,8 @@ class SearchQueryCombinable: def _combine(self, other, connector, reversed): if not isinstance(other, SearchQueryCombinable): raise TypeError( - 'SearchQuery can only be combined with other SearchQuerys, ' - 'got {}.'.format(type(other)) + 'SearchQuery can only be combined with other SearchQuery ' + 'instances, got %s.' % type(other).__name__ ) if reversed: return CombinedSearchQuery(other, connector, self, self.config) diff --git a/tests/postgres_tests/test_search.py b/tests/postgres_tests/test_search.py index 07b1f9e9b4..068a4afe93 100644 --- a/tests/postgres_tests/test_search.py +++ b/tests/postgres_tests/test_search.py @@ -289,6 +289,14 @@ class TestCombinations(GrailTestData, PostgreSQLTestCase): ).filter(search='bedemir') self.assertCountEqual(searched, [self.bedemir0, self.bedemir1, self.crowd, self.witch, self.duck]) + def test_vector_combined_mismatch(self): + msg = ( + 'SearchVector can only be combined with other SearchVector ' + 'instances, got NoneType.' + ) + with self.assertRaisesMessage(TypeError, msg): + Line.objects.filter(dialogue__search=None + SearchVector('character__name')) + def test_query_and(self): searched = Line.objects.annotate( search=SearchVector('scene__setting', 'dialogue'), @@ -340,7 +348,10 @@ class TestCombinations(GrailTestData, PostgreSQLTestCase): self.assertCountEqual(searched, [self.verse0, self.verse1, self.verse2]) def test_query_combined_mismatch(self): - msg = "SearchQuery can only be combined with other SearchQuerys, got" + msg = ( + 'SearchQuery can only be combined with other SearchQuery ' + 'instances, got NoneType.' + ) with self.assertRaisesMessage(TypeError, msg): Line.objects.filter(dialogue__search=None | SearchQuery('kneecaps')) |
