Skip to content

Commit c138c5e

Browse files
committed
Add index config for test
1 parent 8973a0b commit c138c5e

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ services:
136136
<<: *base_server_setup
137137
command: python manage.py ingest_appeal_docs
138138

139-
ingest_appeal_docs:
139+
user_registration_reminder:
140140
<<: *base_server_setup
141141
command: python manage.py user_registration_reminder
142142

main/settings.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
PYTEST_XDIST_WORKER=(str, None),
6868
# Elastic-Cache
6969
ELASTIC_SEARCH_HOST=(str, None),
70+
ELASTIC_SEARCH_INDEX=(str, 'new_index'),
71+
ELASTIC_SEARCH_TEST_INDEX=(str, 'new_test_index'), # This will be used and cleared by test
7072
# FTP
7173
GO_FTPHOST=(str, None),
7274
GO_FTPUSER=(str, None),
@@ -477,6 +479,8 @@
477479

478480
# Elastic search host
479481
ELASTIC_SEARCH_HOST = env('ELASTIC_SEARCH_HOST')
482+
ELASTIC_SEARCH_INDEX = env('ELASTIC_SEARCH_INDEX')
483+
ELASTIC_SEARCH_TEST_INDEX = env('ELASTIC_SEARCH_TEST_INDEX')
480484

481485
# FTP
482486
GO_FTPHOST = env('GO_FTPHOST')
@@ -522,7 +526,7 @@
522526
'default': {
523527
'ENGINE': 'haystack.backends.elasticsearch7_backend.Elasticsearch7SearchEngine',
524528
'URL': ELASTIC_SEARCH_HOST,
525-
'INDEX_NAME': 'new_index',
529+
'INDEX_NAME': ELASTIC_SEARCH_INDEX,
526530
},
527531
}
528532

main/test_case.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import pytz
3+
import haystack
34
from unittest import mock
45

56
import snapshottest.django as django_snapshottest
@@ -9,6 +10,7 @@
910
from rest_framework import test, status
1011

1112
from django.core import management
13+
from django.conf import settings
1214
from django.contrib.auth.models import Permission
1315
from django.contrib.contenttypes.models import ContentType
1416
from django.db import DEFAULT_DB_ALIAS, connections
@@ -19,16 +21,32 @@
1921

2022
from lang.translation import AmazonTranslate
2123

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+
2233

2334
class GoAPITestMixin():
2435
"""
2536
Base TestCase
2637
"""
2738
client_class = test.APIClient
2839

29-
def setUp(self):
30-
super().setUp()
40+
def set_up_haystack(self):
41+
haystack.connections.reload('default')
3142

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):
3250
self.root_user = UserFactory.create(
3351
username='[email protected]',
3452
first_name='Root',
@@ -127,24 +145,42 @@ def capture_on_commit_callbacks(cls, *, using=DEFAULT_DB_ALIAS, execute=False):
127145
return CaptureOnCommitCallbacksContext(using=using, execute=execute)
128146

129147

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+
)
132153
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()
134162

135163

136-
@override_settings(SUSPEND_SIGNALS=True)
164+
@override_settings(
165+
SUSPEND_SIGNALS=True,
166+
HAYSTACK_CONNECTIONS=TEST_HAYSTACK_CONNECTIONS,
167+
)
137168
class SnapshotTestCase(GoAPITestMixin, django_snapshottest.TestCase):
138169
maxDiff = None
139170

140171
def setUp(self):
172+
self.set_up_haystack()
173+
super().setUp()
141174
management.call_command("flush", "--no-input")
142175
factory.random.reseed_random(42)
176+
self.set_up_seed()
143177
self.patcher = mock.patch('django.utils.timezone.now')
144178
self.patcher.start().return_value = datetime.datetime(2008, 1, 1, 0, 0, 0, 123456, tzinfo=pytz.UTC)
145179

146180
def tearDown(self):
147181
self.patcher.stop()
182+
super().tear_down_haystack()
183+
super().tearDown()
148184

149185

150186
class CaptureOnCommitCallbacksContext:

0 commit comments

Comments
 (0)