Fix TestIndexCommand under django3.2 (#17438)

This commit is contained in:
Andrew Williamson 2021-07-07 12:17:28 +01:00 коммит произвёл GitHub
Родитель c6b3e4618e
Коммит 5740aee15c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 23 добавлений и 21 удалений

Просмотреть файл

@ -697,7 +697,7 @@ workflows:
parameters:
djangoversion:
- django22
# - django32 # django3.2 tests still failing
- django32
- release-master:
filters:
branches:

Просмотреть файл

@ -942,7 +942,7 @@ def version_factory(file_kw=None, **kw):
@pytest.mark.es_tests
class ESTestCase(TestCase):
class ESTestCaseMixin:
@classmethod
def get_index_name(cls, key):
return get_es_index_name(key)
@ -953,18 +953,18 @@ class ESTestCase(TestCase):
# right before each test.
stop_es_mocks()
cls.es = amo_search.get_es(timeout=settings.ES_TIMEOUT)
super(ESTestCase, cls).setUpClass()
super().setUpClass()
def setUp(self):
# Stop the mocks again, we stopped them in `setUpClass` but our
# generic pytest fixture started the mocks in the meantime
stop_es_mocks()
super(ESTestCase, self).setUp()
super().setUp()
@classmethod
def setUpTestData(cls):
setup_es_test_data(cls.es)
super(ESTestCase, cls).setUpTestData()
super().setUpTestData()
@classmethod
def refresh(cls, index='default'):
@ -988,6 +988,10 @@ class ESTestCase(TestCase):
)
class ESTestCase(ESTestCaseMixin, TestCase):
pass
class ESTestCaseWithAddons(ESTestCase):
@classmethod
def setUpTestData(cls):

Просмотреть файл

@ -12,7 +12,8 @@ from django.test.testcases import TransactionTestCase
from celery import group, task
from celery.canvas import _chain
from olympia.amo.tests import addon_factory, ESTestCase, reverse_ns
from olympia.addons.models import Addon
from olympia.amo.tests import addon_factory, ESTestCaseMixin, PatchMixin, reverse_ns
from olympia.amo.utils import urlparams
from olympia.lib.es.management.commands import reindex
from olympia.lib.es.utils import is_reindexing_amo, unflag_reindexing_amo
@ -23,7 +24,7 @@ def dummy_task():
return None
class TestIndexCommand(ESTestCase):
class TestIndexCommand(ESTestCaseMixin, PatchMixin, TransactionTestCase):
def setUp(self):
super(TestIndexCommand, self).setUp()
if is_reindexing_amo():
@ -43,21 +44,21 @@ class TestIndexCommand(ESTestCase):
# eager-mode) fixed.
self.patch('celery.app.task.denied_join_result')
# Since this test plays with transactions, but we don't have (and don't
# really want to have) a ESTransactionTestCase class, use the fixture setup
# and teardown methods from TransactionTestCase.
def _fixture_setup(self):
return TransactionTestCase._fixture_setup(self)
def _fixture_teardown(self):
return TransactionTestCase._fixture_teardown(self)
def tearDown(self):
current_indices = self.es.indices.stats()['indices'].keys()
for index in current_indices:
if index not in self.indices:
self.es.indices.delete(index, ignore=404)
super(TestIndexCommand, self).tearDown()
super().tearDown()
@classmethod
def tearDownClass(cls):
super().tearDownClass()
try:
assert not Addon.objects.exists(), Addon.objects.values('id', 'slug')
except AssertionError as ae:
Addon.objects.all().delete()
raise ae
def check_settings(self, new_indices):
"""Make sure the indices settings are properly set."""
@ -123,7 +124,6 @@ class TestIndexCommand(ESTestCase):
# commit.
while t.is_alive() and not is_reindexing_amo():
connection._commit()
connection.clean_savepoints()
if not wipe:
# We should still be able to search in the foreground while the
@ -133,7 +133,6 @@ class TestIndexCommand(ESTestCase):
while t.is_alive() and len(self.expected) < old_addons_count + 3:
self.expected.append(addon_factory())
connection._commit()
connection.clean_savepoints()
# We don't know where the search will happen, the reindexing
# could be over by now. So force a refresh on *all* indices.
self.refresh(None)
@ -142,7 +141,7 @@ class TestIndexCommand(ESTestCase):
if len(self.expected) == old_addons_count:
raise AssertionError(
'Could not index objects in foreground while reindexing '
'in the background.'
'in the background. (expected: %d)' % len(self.expected)
)
t.join() # Wait for the thread to finish.
@ -152,7 +151,6 @@ class TestIndexCommand(ESTestCase):
# The reindexation is done, let's double check we have all our docs.
connection._commit()
connection.clean_savepoints()
self.refresh()
self.check_results(self.expected)