|
1 | 1 | import datetime |
2 | 2 | import pytz |
| 3 | +import haystack |
3 | 4 | from unittest import mock |
4 | 5 |
|
5 | 6 | import snapshottest.django as django_snapshottest |
|
9 | 10 | from rest_framework import test, status |
10 | 11 |
|
11 | 12 | from django.core import management |
| 13 | +from django.conf import settings |
12 | 14 | from django.contrib.auth.models import Permission |
13 | 15 | from django.contrib.contenttypes.models import ContentType |
14 | 16 | from django.db import DEFAULT_DB_ALIAS, connections |
|
19 | 21 |
|
20 | 22 | from lang.translation import AmazonTranslate |
21 | 23 |
|
| 24 | +# XXX: Will not support if test are run in parallel |
| 25 | +TEST_HAYSTACK_CONNECTIONS = { |
| 26 | + 'default': { |
| 27 | + 'ENGINE': 'haystack.backends.elasticsearch7_backend.Elasticsearch7SearchEngine', |
| 28 | + 'URL': settings.ELASTIC_SEARCH_HOST, |
| 29 | + 'INDEX_NAME': settings.ELASTIC_SEARCH_TEST_INDEX, |
| 30 | + }, |
| 31 | +} |
| 32 | + |
22 | 33 |
|
23 | 34 | class GoAPITestMixin(): |
24 | 35 | """ |
25 | 36 | Base TestCase |
26 | 37 | """ |
27 | 38 | client_class = test.APIClient |
28 | 39 |
|
29 | | - def setUp(self): |
30 | | - super().setUp() |
| 40 | + def set_up_haystack(self): |
| 41 | + haystack.connections.reload('default') |
31 | 42 |
|
| 43 | + def tear_down_haystack(self): |
| 44 | + haystack.connections.reload('default') |
| 45 | + backend = haystack.connections['default'].get_backend() |
| 46 | + assert backend.index_name == settings.ELASTIC_SEARCH_TEST_INDEX |
| 47 | + backend.clear() |
| 48 | + |
| 49 | + def set_up_seed(self): |
32 | 50 | self.root_user = UserFactory.create( |
33 | 51 | |
34 | 52 | first_name='Root', |
@@ -127,24 +145,42 @@ def capture_on_commit_callbacks(cls, *, using=DEFAULT_DB_ALIAS, execute=False): |
127 | 145 | return CaptureOnCommitCallbacksContext(using=using, execute=execute) |
128 | 146 |
|
129 | 147 |
|
130 | | -@override_settings(CELERY_TASK_ALWAYS_EAGER=True) |
131 | | -@override_settings(SUSPEND_SIGNALS=True) |
| 148 | +@override_settings( |
| 149 | + CELERY_TASK_ALWAYS_EAGER=True, |
| 150 | + SUSPEND_SIGNALS=True, |
| 151 | + HAYSTACK_CONNECTIONS=TEST_HAYSTACK_CONNECTIONS, |
| 152 | +) |
132 | 153 | class APITestCase(GoAPITestMixin, test.APITestCase): |
133 | | - pass |
| 154 | + def setUp(self): |
| 155 | + self.set_up_haystack() |
| 156 | + super().setUp() |
| 157 | + self.set_up_seed() |
| 158 | + |
| 159 | + def tearDown(self): |
| 160 | + super().tear_down_haystack() |
| 161 | + super().tearDown() |
134 | 162 |
|
135 | 163 |
|
136 | | -@override_settings(SUSPEND_SIGNALS=True) |
| 164 | +@override_settings( |
| 165 | + SUSPEND_SIGNALS=True, |
| 166 | + HAYSTACK_CONNECTIONS=TEST_HAYSTACK_CONNECTIONS, |
| 167 | +) |
137 | 168 | class SnapshotTestCase(GoAPITestMixin, django_snapshottest.TestCase): |
138 | 169 | maxDiff = None |
139 | 170 |
|
140 | 171 | def setUp(self): |
| 172 | + self.set_up_haystack() |
| 173 | + super().setUp() |
141 | 174 | management.call_command("flush", "--no-input") |
142 | 175 | factory.random.reseed_random(42) |
| 176 | + self.set_up_seed() |
143 | 177 | self.patcher = mock.patch('django.utils.timezone.now') |
144 | 178 | self.patcher.start().return_value = datetime.datetime(2008, 1, 1, 0, 0, 0, 123456, tzinfo=pytz.UTC) |
145 | 179 |
|
146 | 180 | def tearDown(self): |
147 | 181 | self.patcher.stop() |
| 182 | + super().tear_down_haystack() |
| 183 | + super().tearDown() |
148 | 184 |
|
149 | 185 |
|
150 | 186 | class CaptureOnCommitCallbacksContext: |
|
0 commit comments