1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
from datetime import date
from django.test import TestCase
from django.test.utils import override_settings
from haystack import connections
from haystack.query import SearchQuerySet
from haystack.utils.loading import UnifiedIndex
from ..core.models import MockModel, OneToManyRightSideModel, ScoreMockModel
from ..mocks import MockSearchResult
from .search_indexes import SimpleMockScoreIndex, SimpleMockSearchIndex
class SimpleSearchBackendTestCase(TestCase):
fixtures = ["base_data.json", "bulk_data.json"]
def setUp(self):
super().setUp()
self.backend = connections["simple"].get_backend()
ui = connections["simple"].get_unified_index()
self.index = SimpleMockSearchIndex()
ui.build(indexes=[self.index, SimpleMockScoreIndex()])
self.sample_objs = MockModel.objects.all()
def test_update(self):
self.backend.update(self.index, self.sample_objs)
def test_remove(self):
self.backend.remove(self.sample_objs[0])
def test_clear(self):
self.backend.clear()
def test_search(self):
# No query string should always yield zero results.
self.assertEqual(self.backend.search(""), {"hits": 0, "results": []})
self.assertEqual(self.backend.search("*")["hits"], 24)
self.assertEqual(
sorted([result.pk for result in self.backend.search("*")["results"]]),
[
1,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
],
)
self.assertEqual(self.backend.search("daniel")["hits"], 23)
self.assertEqual(
[result.pk for result in self.backend.search("daniel")["results"]],
[
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
],
)
self.assertEqual(self.backend.search("should be a string")["hits"], 1)
self.assertEqual(
[
result.pk
for result in self.backend.search("should be a string")["results"]
],
[8],
)
# Ensure the results are ``SearchResult`` instances...
self.assertEqual(
self.backend.search("should be a string")["results"][0].score, 0
)
self.assertEqual(self.backend.search("index document")["hits"], 6)
self.assertEqual(
[result.pk for result in self.backend.search("index document")["results"]],
[2, 3, 15, 16, 17, 18],
)
# Regression-ville
self.assertEqual(
[
result.object.id
for result in self.backend.search("index document")["results"]
],
[2, 3, 15, 16, 17, 18],
)
self.assertEqual(
self.backend.search("index document")["results"][0].model, MockModel
)
# No support for spelling suggestions
self.assertEqual(self.backend.search("Indx")["hits"], 0)
self.assertFalse(self.backend.search("Indx").get("spelling_suggestion"))
# No support for facets
self.assertEqual(
self.backend.search("", facets=["name"]), {"hits": 0, "results": []}
)
self.assertEqual(self.backend.search("daniel", facets=["name"])["hits"], 23)
self.assertEqual(
self.backend.search(
"",
date_facets={
"pub_date": {
"start_date": date(2008, 2, 26),
"end_date": date(2008, 2, 26),
"gap": "/MONTH",
}
},
),
{"hits": 0, "results": []},
)
self.assertEqual(
self.backend.search(
"daniel",
date_facets={
"pub_date": {
"start_date": date(2008, 2, 26),
"end_date": date(2008, 2, 26),
"gap": "/MONTH",
}
},
)["hits"],
23,
)
self.assertEqual(
self.backend.search("", query_facets={"name": "[* TO e]"}),
{"hits": 0, "results": []},
)
self.assertEqual(
self.backend.search("daniel", query_facets={"name": "[* TO e]"})["hits"], 23
)
self.assertFalse(self.backend.search("").get("facets"))
self.assertFalse(self.backend.search("daniel").get("facets"))
# Note that only textual-fields are supported.
self.assertEqual(self.backend.search("2009-06-18")["hits"], 0)
# Ensure that swapping the ``result_class`` works.
self.assertTrue(
isinstance(
self.backend.search("index document", result_class=MockSearchResult)[
"results"
][0],
MockSearchResult,
)
)
# Ensure empty queries does not raise.
self.assertEqual(
self.backend.search("foo", models=[OneToManyRightSideModel]),
{"hits": 0, "results": []},
)
def test_filter_models(self):
self.backend.update(self.index, self.sample_objs)
self.assertEqual(self.backend.search("*", models=set([]))["hits"], 24)
self.assertEqual(self.backend.search("*", models=set([MockModel]))["hits"], 23)
def test_more_like_this(self):
self.backend.update(self.index, self.sample_objs)
self.assertEqual(self.backend.search("*")["hits"], 24)
# Unsupported by 'simple'. Should see empty results.
self.assertEqual(self.backend.more_like_this(self.sample_objs[0])["hits"], 0)
def test_score_field_collision(self):
index = connections["simple"].get_unified_index().get_index(ScoreMockModel)
sample_objs = ScoreMockModel.objects.all()
self.backend.update(index, self.sample_objs)
# 42 is the in the match, which will be removed from the result
self.assertEqual(self.backend.search("42")["results"][0].score, 0)
@override_settings(DEBUG=True)
class LiveSimpleSearchQuerySetTestCase(TestCase):
fixtures = ["base_data.json", "bulk_data.json"]
def setUp(self):
super().setUp()
# Stow.
self.old_ui = connections["simple"].get_unified_index()
self.ui = UnifiedIndex()
self.smmi = SimpleMockSearchIndex()
self.ui.build(indexes=[self.smmi])
connections["simple"]._index = self.ui
self.sample_objs = MockModel.objects.all()
self.sqs = SearchQuerySet(using="simple")
def tearDown(self):
# Restore.
connections["simple"]._index = self.old_ui
super().tearDown()
def test_general_queries(self):
# For now, just make sure these don't throw an exception.
# They won't work until the simple backend is improved.
self.assertTrue(len(self.sqs.auto_query("daniel")) > 0)
self.assertTrue(len(self.sqs.filter(text="index")) > 0)
self.assertTrue(len(self.sqs.exclude(name="daniel")) > 0)
self.assertTrue(len(self.sqs.order_by("-pub_date")) > 0)
def test_general_queries_unicode(self):
self.assertEqual(len(self.sqs.auto_query("Привет")), 0)
def test_more_like_this(self):
# MLT shouldn't be horribly broken. This used to throw an exception.
mm1 = MockModel.objects.get(pk=1)
self.assertEqual(len(self.sqs.filter(text=1).more_like_this(mm1)), 0)
def test_values_queries(self):
sqs = self.sqs.auto_query("daniel")
self.assertTrue(len(sqs) > 0)
flat_scores = sqs.values_list("score", flat=True)
self.assertEqual(flat_scores[0], 0)
scores = sqs.values_list("id", "score")
self.assertEqual(scores[0], [1, 0])
scores_dict = sqs.values("id", "score")
self.assertEqual(scores_dict[0], {"id": 1, "score": 0})
|