|
1 | 1 | from datetime import date
|
| 2 | +from django.conf import settings |
2 | 3 | from django.test import TestCase
|
3 | 4 | from haystack import connections, connection_router
|
| 5 | +from haystack import indexes |
| 6 | +from haystack.query import SearchQuerySet |
| 7 | +from haystack.utils.loading import UnifiedIndex |
4 | 8 | from core.models import MockModel
|
5 | 9 | from core.tests.mocks import MockSearchResult
|
6 | 10 |
|
7 | 11 |
|
| 12 | +class SimpleMockSearchIndex(indexes.SearchIndex): |
| 13 | + text = indexes.CharField(document=True, use_template=True) |
| 14 | + name = indexes.CharField(model_attr='author', faceted=True) |
| 15 | + pub_date = indexes.DateField(model_attr='pub_date') |
| 16 | + |
| 17 | + def get_model(self): |
| 18 | + return MockModel |
| 19 | + |
| 20 | + |
8 | 21 | class SimpleSearchBackendTestCase(TestCase):
|
9 | 22 | fixtures = ['bulk_data.json']
|
10 | 23 |
|
@@ -72,3 +85,37 @@ def test_more_like_this(self):
|
72 | 85 |
|
73 | 86 | # Unsupported by 'simple'. Should see empty results.
|
74 | 87 | self.assertEqual(self.backend.more_like_this(self.sample_objs[0])['hits'], 0)
|
| 88 | + |
| 89 | + |
| 90 | +class LiveSimpleSearchQuerySetTestCase(TestCase): |
| 91 | + fixtures = ['bulk_data.json'] |
| 92 | + |
| 93 | + def setUp(self): |
| 94 | + super(LiveSimpleSearchQuerySetTestCase, self).setUp() |
| 95 | + |
| 96 | + # Stow. |
| 97 | + self.old_debug = settings.DEBUG |
| 98 | + settings.DEBUG = True |
| 99 | + self.old_ui = connections['default'].get_unified_index() |
| 100 | + self.ui = UnifiedIndex() |
| 101 | + self.smmi = SimpleMockSearchIndex() |
| 102 | + self.ui.build(indexes=[self.smmi]) |
| 103 | + connections['default']._index = self.ui |
| 104 | + |
| 105 | + self.sample_objs = MockModel.objects.all() |
| 106 | + self.sqs = SearchQuerySet() |
| 107 | + |
| 108 | + def tearDown(self): |
| 109 | + # Restore. |
| 110 | + connections['default']._index = self.old_ui |
| 111 | + settings.DEBUG = self.old_debug |
| 112 | + super(LiveSimpleSearchQuerySetTestCase, self).tearDown() |
| 113 | + |
| 114 | + def test_general_queries(self): |
| 115 | + # For now, just make sure these don't throw an exception. |
| 116 | + # They won't work until the simple backend is improved. |
| 117 | + self.assertTrue(len(self.sqs.auto_query('daniel')) > 0) |
| 118 | + self.assertTrue(len(self.sqs.filter(text='index')) > 0) |
| 119 | + self.assertTrue(len(self.sqs.exclude(name='daniel')) > 0) |
| 120 | + self.assertTrue(len(self.sqs.order_by('-pub_date')) > 0) |
| 121 | + |
0 commit comments