Improved SearchVectorCombinable and SearchQueryCombinable error messages.

This commit is contained in:
Hannes Ljungberg 2020-01-24 22:23:30 +00:00 committed by Mariusz Felisiak
parent 50cf183d21
commit 7edd06a9cf
2 changed files with 18 additions and 4 deletions

View File

@ -38,7 +38,10 @@ class SearchVectorCombinable:
def _combine(self, other, connector, reversed): def _combine(self, other, connector, reversed):
if not isinstance(other, SearchVectorCombinable) or not self.config == other.config: 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: if reversed:
return CombinedSearchVector(other, connector, self, self.config) return CombinedSearchVector(other, connector, self, self.config)
return CombinedSearchVector(self, connector, other, self.config) return CombinedSearchVector(self, connector, other, self.config)
@ -105,8 +108,8 @@ class SearchQueryCombinable:
def _combine(self, other, connector, reversed): def _combine(self, other, connector, reversed):
if not isinstance(other, SearchQueryCombinable): if not isinstance(other, SearchQueryCombinable):
raise TypeError( raise TypeError(
'SearchQuery can only be combined with other SearchQuerys, ' 'SearchQuery can only be combined with other SearchQuery '
'got {}.'.format(type(other)) 'instances, got %s.' % type(other).__name__
) )
if reversed: if reversed:
return CombinedSearchQuery(other, connector, self, self.config) return CombinedSearchQuery(other, connector, self, self.config)

View File

@ -289,6 +289,14 @@ class TestCombinations(GrailTestData, PostgreSQLTestCase):
).filter(search='bedemir') ).filter(search='bedemir')
self.assertCountEqual(searched, [self.bedemir0, self.bedemir1, self.crowd, self.witch, self.duck]) 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): def test_query_and(self):
searched = Line.objects.annotate( searched = Line.objects.annotate(
search=SearchVector('scene__setting', 'dialogue'), search=SearchVector('scene__setting', 'dialogue'),
@ -340,7 +348,10 @@ class TestCombinations(GrailTestData, PostgreSQLTestCase):
self.assertCountEqual(searched, [self.verse0, self.verse1, self.verse2]) self.assertCountEqual(searched, [self.verse0, self.verse1, self.verse2])
def test_query_combined_mismatch(self): 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): with self.assertRaisesMessage(TypeError, msg):
Line.objects.filter(dialogue__search=None | SearchQuery('kneecaps')) Line.objects.filter(dialogue__search=None | SearchQuery('kneecaps'))