File: tests.py

package info (click to toggle)
django-haystack 3.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: python: 23,475; xml: 1,708; sh: 74; makefile: 71
file content (343 lines) | stat: -rw-r--r-- 12,773 bytes parent folder | download
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from django.apps import apps
from django.db import models

from haystack import connection_router, connections
from haystack.exceptions import NotHandled
from haystack.query import SearchQuerySet
from haystack.signals import BaseSignalProcessor, RealtimeSignalProcessor

from ..whoosh_tests.testcases import WhooshTestCase
from .models import Bar, Foo
from .search_indexes import BarIndex, FooIndex


class MultipleIndexTestCase(WhooshTestCase):
    def setUp(self):
        super().setUp()

        self.ui = connections["solr"].get_unified_index()
        self.fi = self.ui.get_index(Foo)
        self.bi = self.ui.get_index(Bar)
        self.solr_backend = connections["solr"].get_backend()
        self.whoosh_backend = connections["whoosh"].get_backend()
        self.filtered_whoosh_backend = connections["filtered_whoosh"].get_backend()

        Foo.objects.bulk_create(
            [
                Foo(title="Haystack test", body="foo 1"),
                Foo(title="Another Haystack test", body="foo 2"),
            ]
        )

        Bar.objects.bulk_create(
            [
                Bar(author="Haystack test", content="bar 1"),
                Bar(author="Another Haystack test", content="bar 2"),
                Bar(author="Yet another Haystack test", content="bar 3"),
            ]
        )

        self.fi.reindex(using="solr")
        self.fi.reindex(using="whoosh")
        self.bi.reindex(using="solr")

    def tearDown(self):
        self.fi.clear(using="solr")
        self.bi.clear(using="solr")
        super().tearDown()

    def test_index_update_object_using(self):
        results = self.solr_backend.search("foo")
        self.assertEqual(results["hits"], 2)
        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 2)

        foo_3 = Foo.objects.create(title="Whee another Haystack test", body="foo 3")

        self.fi.update_object(foo_3, using="solr")
        results = self.solr_backend.search("foo")
        self.assertEqual(results["hits"], 3)
        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 2)

        self.fi.update_object(foo_3, using="whoosh")
        results = self.solr_backend.search("foo")
        self.assertEqual(results["hits"], 3)
        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 3)

    def test_index_remove_object_using(self):
        results = self.solr_backend.search("foo")
        self.assertEqual(results["hits"], 2)
        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 2)

        foo_1 = Foo.objects.get(pk=1)

        self.fi.remove_object(foo_1, using="solr")
        results = self.solr_backend.search("foo")
        self.assertEqual(results["hits"], 1)
        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 2)

        self.fi.remove_object(foo_1, using="whoosh")
        results = self.solr_backend.search("foo")
        self.assertEqual(results["hits"], 1)
        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 1)

    def test_index_clear_using(self):
        results = self.solr_backend.search("foo")
        self.assertEqual(results["hits"], 2)
        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 2)

        self.fi.clear(using="solr")
        results = self.solr_backend.search("foo")
        self.assertEqual(results["hits"], 0)
        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 2)

        self.fi.clear(using="whoosh")
        results = self.solr_backend.search("foo")
        self.assertEqual(results["hits"], 0)
        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 0)

    def test_index_update_using(self):
        self.fi.clear(using="solr")
        self.fi.clear(using="whoosh")
        self.bi.clear(using="solr")
        self.bi.clear(using="whoosh")

        results = self.solr_backend.search("foo")
        self.assertEqual(results["hits"], 0)
        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 0)

        self.fi.update(using="solr")
        results = self.solr_backend.search("foo")
        self.assertEqual(results["hits"], 2)
        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 0)

        self.fi.update(using="whoosh")
        results = self.solr_backend.search("foo")
        self.assertEqual(results["hits"], 2)
        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 2)

    def test_searchqueryset_using(self):
        # Using the default.
        sqs = SearchQuerySet("solr")
        self.assertEqual(sqs.count(), 5)
        self.assertEqual(sqs.models(Foo).count(), 2)
        self.assertEqual(sqs.models(Bar).count(), 3)

        self.assertEqual(sqs.using("solr").count(), 5)
        self.assertEqual(sqs.using("solr").models(Foo).count(), 2)
        self.assertEqual(sqs.using("solr").models(Bar).count(), 3)

        self.assertEqual(sqs.using("whoosh").count(), 2)
        self.assertEqual(sqs.using("whoosh").models(Foo).count(), 2)
        self.assertEqual(sqs.using("whoosh").models(Bar).count(), 0)

    def test_searchquery_using(self):
        sq = connections["solr"].get_query()

        # Using the default.
        self.assertEqual(sq.get_count(), 5)

        # "Swap" to the default.
        sq = sq.using("solr")
        self.assertEqual(sq.get_count(), 5)

        # Swap the ``SearchQuery`` used.
        sq = sq.using("whoosh")
        self.assertEqual(sq.get_count(), 2)

    def test_excluded_indexes(self):
        wui = connections["filtered_whoosh"].get_unified_index()
        self.assertTrue(any(isinstance(i, FooIndex) for i in wui.collect_indexes()))
        self.assertFalse(any(isinstance(i, BarIndex) for i in wui.collect_indexes()))

        # Shouldn't error.
        wui.get_index(Foo)

        # Should error, since it's not present.
        self.assertRaises(NotHandled, wui.get_index, Bar)

    def test_filtered_index_update(self):
        for i in ("whoosh", "filtered_whoosh"):
            self.fi.clear(using=i)
            self.fi.update(using=i)

        results = self.whoosh_backend.search("foo")
        self.assertEqual(results["hits"], 2)

        results = self.filtered_whoosh_backend.search("foo")
        self.assertEqual(
            results["hits"], 1, "Filtered backend should only contain one record"
        )


class TestSignalProcessor(BaseSignalProcessor):
    def setup(self):
        self.setup_ran = True
        super().setup()

    def teardown(self):
        self.teardown_ran = True
        super().teardown()


class SignalProcessorTestCase(WhooshTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        config = apps.get_app_config("haystack")
        cls._old_sp = config.signal_processor
        config.signal_processor = RealtimeSignalProcessor(
            connections, connection_router
        )

    @classmethod
    def tearDown(cls):
        config = apps.get_app_config("haystack")
        config.signal_processor.teardown()
        config.signal_processor = cls._old_sp
        super().tearDown()

    def setUp(self):
        super().setUp()

        # Blatantly wrong data, just for assertion purposes.
        self.fake_connections = {}
        self.fake_router = []

        self.ui = connections["solr"].get_unified_index()
        self.fi = self.ui.get_index(Foo)
        self.bi = self.ui.get_index(Bar)
        self.solr_backend = connections["solr"].get_backend()
        self.whoosh_backend = connections["whoosh"].get_backend()

        self.foo_1 = Foo.objects.create(title="Haystack test", body="foo 1")
        self.foo_2 = Foo.objects.create(title="Another Haystack test", body="foo 2")
        self.bar_1 = Bar.objects.create(author="Haystack test", content="bar 1")
        self.bar_2 = Bar.objects.create(author="Another Haystack test", content="bar 2")
        self.bar_3 = Bar.objects.create(
            author="Yet another Haystack test", content="bar 3"
        )

        self.fi.reindex(using="solr")
        self.fi.reindex(using="whoosh")
        self.bi.reindex(using="solr")

    def tearDown(self):
        self.fi.clear(using="solr")
        self.bi.clear(using="solr")
        super().tearDown()

    def test_init(self):
        tsp = TestSignalProcessor(self.fake_connections, self.fake_router)
        self.assertEqual(tsp.connections, self.fake_connections)
        self.assertEqual(tsp.connection_router, self.fake_router)
        # We fake some side-effects to make sure it ran.
        self.assertTrue(tsp.setup_ran)

        bsp = BaseSignalProcessor(self.fake_connections, self.fake_router)
        self.assertFalse(getattr(bsp, "setup_ran", False))

    def test_setup(self):
        tsp = TestSignalProcessor(self.fake_connections, self.fake_router)
        tsp.setup()
        self.assertTrue(tsp.setup_ran)

    def test_teardown(self):
        tsp = TestSignalProcessor(self.fake_connections, self.fake_router)
        tsp.teardown()
        self.assertTrue(tsp.teardown_ran)

    def test_handle_save(self):
        # Because the code here is pretty leaky (abstraction-wise), we'll test
        # the actual setup.
        # First, ensure the signal is setup.
        self.assertEqual(len(models.signals.post_save.receivers), 1)

        # Second, check the existing search data.
        sqs = SearchQuerySet("solr")
        self.assertEqual(sqs.using("solr").count(), 5)
        self.assertEqual(sqs.using("solr").models(Foo).count(), 2)
        self.assertEqual(sqs.using("solr").models(Bar).count(), 3)
        self.assertEqual(sqs.using("whoosh").count(), 2)
        self.assertEqual(sqs.using("whoosh").models(Foo).count(), 2)

        self.assertEqual(
            sqs.using("solr").models(Foo).order_by("django_id")[0].text, "foo 1"
        )
        self.assertEqual(
            sqs.using("whoosh").models(Foo).order_by("django_id")[0].text, "foo 1"
        )

        # Third, save the model, which should fire the signal & index the
        # new data.
        self.foo_1.body = "A different body"
        self.foo_1.save()

        # Fourth, check the search data for the updated data, making sure counts
        # haven't changed.
        sqs = SearchQuerySet("solr")
        self.assertEqual(sqs.using("solr").count(), 5)
        self.assertEqual(sqs.using("solr").models(Foo).count(), 2)
        self.assertEqual(sqs.using("solr").models(Bar).count(), 3)
        self.assertEqual(sqs.using("whoosh").count(), 2)
        self.assertEqual(sqs.using("whoosh").models(Foo).count(), 2)

        self.assertEqual(
            sqs.using("solr").models(Foo).order_by("django_id")[0].text,
            "A different body",
        )
        self.assertEqual(
            sqs.using("whoosh").models(Foo).order_by("django_id")[0].text, "foo 1"
        )

    def test_handle_delete(self):
        # Because the code here is pretty leaky (abstraction-wise), we'll test
        # the actual setup.
        # First, ensure the signal is setup.
        self.assertEqual(len(models.signals.post_delete.receivers), 1)

        # Second, check the existing search data.
        sqs = SearchQuerySet("solr")
        self.assertEqual(sqs.using("solr").count(), 5)
        self.assertEqual(sqs.using("solr").models(Foo).count(), 2)
        self.assertEqual(sqs.using("solr").models(Bar).count(), 3)
        self.assertEqual(sqs.using("whoosh").count(), 2)
        self.assertEqual(sqs.using("whoosh").models(Foo).count(), 2)

        self.assertEqual(
            sqs.using("solr").models(Foo).order_by("django_id")[0].text, "foo 1"
        )
        self.assertEqual(
            sqs.using("whoosh").models(Foo).order_by("django_id")[0].text, "foo 1"
        )

        # Third, delete the model, which should fire the signal & remove the
        # record from the index.
        self.foo_1.delete()

        # Fourth, check the search data for the now-removed data, making sure counts
        # have changed correctly.
        sqs = SearchQuerySet("solr")
        self.assertEqual(sqs.using("solr").count(), 4)
        self.assertEqual(sqs.using("solr").models(Foo).count(), 1)
        self.assertEqual(sqs.using("solr").models(Bar).count(), 3)
        self.assertEqual(sqs.using("whoosh").count(), 2)
        self.assertEqual(sqs.using("whoosh").models(Foo).count(), 2)

        self.assertEqual(
            sqs.using("solr").models(Foo).order_by("django_id")[0].text, "foo 2"
        )
        self.assertEqual(
            sqs.using("whoosh").models(Foo).order_by("django_id")[0].text, "foo 1"
        )