Skip to content

Commit a5cbd23

Browse files
committed
Add SearchQueryString tests
1 parent 5948d22 commit a5cbd23

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

tests/atlas_search_/test_search.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
SearchIn,
2121
SearchMoreLikeThis,
2222
SearchPhrase,
23+
SearchQueryString,
2324
SearchRange,
2425
SearchRegex,
2526
SearchScoreOption,
@@ -359,6 +360,68 @@ def test_str_returns_expected_format(self):
359360
)
360361

361362

363+
@skipUnlessDBFeature("supports_atlas_search")
364+
class SearchQueryStringTests(SearchUtilsMixin):
365+
@classmethod
366+
def setUpClass(cls):
367+
cls.create_search_index(
368+
Article,
369+
"query_string_index",
370+
{
371+
"mappings": {
372+
"dynamic": False,
373+
"fields": {
374+
"headline": {"type": "string"},
375+
"body": {"type": "string"},
376+
},
377+
}
378+
},
379+
)
380+
381+
def setUp(self):
382+
self.mars_mission = Article.objects.create(
383+
headline="space exploration",
384+
body="NASA launches a new mission to Mars",
385+
number=1,
386+
)
387+
self.exoplanet = Article.objects.create(
388+
headline="space exploration",
389+
body="Astronomers discover exoplanets orbiting distant stars",
390+
number=2,
391+
)
392+
Article.objects.create(
393+
headline="other news",
394+
body="Local team wins championship",
395+
number=3,
396+
)
397+
398+
def test_search_query_string_basic(self):
399+
qs = Article.objects.annotate(
400+
score=SearchQueryString(path="headline", query="space AND body:Mars")
401+
)
402+
self.assertCountEqual(qs, [self.mars_mission])
403+
404+
def test_search_query_string_with_score(self):
405+
constant_score = SearchScoreOption({"constant": {"value": 10}})
406+
qs = Article.objects.annotate(
407+
score=SearchQueryString(
408+
path="headline",
409+
query="space AND body:exoplanets",
410+
score=constant_score,
411+
)
412+
)
413+
self.assertCountEqual(qs, [self.exoplanet])
414+
self.assertAlmostEqual(qs.first().score, 10.0, places=2)
415+
416+
def test_str_returns_expected_format(self):
417+
score = SearchScoreOption({"constant": {"value": 10}})
418+
se = SearchQueryString(path="body", query="space AND body:exoplanets", score=score)
419+
self.assertEqual(
420+
str(se),
421+
f"SearchQueryString(path='body', query='space AND body:exoplanets, score={score})>",
422+
)
423+
424+
362425
class SearchRangeTests(SearchUtilsMixin):
363426
@classmethod
364427
def setUpClass(cls):

0 commit comments

Comments
 (0)