-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbase.py
92 lines (77 loc) · 2.84 KB
/
base.py
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
"""
Base test classes for search
"""
from unittest.mock import patch
from django.test import (
TestCase,
override_settings
)
from dashboard.models import ProgramEnrollment
from search import tasks
from search.indexing_api import delete_indices, create_backing_indices
from search.models import PercolateQuery
@override_settings(DEBUG=True)
class ESTestCase(TestCase):
"""
Test class for test cases that need a live ES index
"""
@classmethod
def setUpClass(cls):
# Make sure index exists when signals are run.
reindex_test_es_data()
super().setUpClass()
def setUp(self):
# Make sure index exists when signals are run.
# We want to run recreate_index instead of clear_index
# because the test data is contained in a transaction
# which is reverted after each test runs, so signals don't get run
# that keep ES up to date.
reindex_test_es_data()
super().setUp()
@classmethod
def tearDownClass(cls):
delete_indices()
super().tearDownClass()
@override_settings(DEBUG=True)
class MockedESTestCase(TestCase):
"""
Test class that mocks the MicroMasters indexing API to avoid unnecessary ES index operations
"""
@classmethod
def setUpClass(cls):
cls.patchers = []
cls.patcher_mocks = []
for name, val in tasks.__dict__.items():
# This looks for functions starting with _ because those are the functions which are imported
# from indexing_api. The _ lets it prevent name collisions.
if callable(val) and name.startswith("_"):
cls.patchers.append(patch('search.tasks.{0}'.format(name), autospec=True))
for patcher in cls.patchers:
mock = patcher.start()
mock.name = patcher.attribute
cls.patcher_mocks.append(mock)
try:
super().setUpClass()
except:
for patcher in cls.patchers:
patcher.stop()
raise
def setUp(self):
super().setUp()
for mock in self.patcher_mocks:
mock.reset_mock()
@classmethod
def tearDownClass(cls):
for patcher in cls.patchers:
patcher.stop()
super().tearDownClass()
def reindex_test_es_data():
"""
Recreates the OpenSearch indices for the live data used in tests
"""
backing_indices = create_backing_indices()
tasks.bulk_index_program_enrollments(ProgramEnrollment.objects.order_by("id").values_list("id", flat=True),
backing_indices[0][0], backing_indices[1][0])
tasks.bulk_index_percolate_queries(PercolateQuery.objects.order_by("id").values_list("id", flat=True),
backing_indices[2][0])
tasks.finish_recreate_index(results=[], backing_indices=backing_indices)