Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions cl/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

from asgiref.sync import sync_to_async
from django import test
from django.apps import apps
from django.contrib.staticfiles import testing
from django.core.management import call_command
from django.test import SimpleTestCase
from django.urls import reverse
from django.utils.dateformat import format
Expand Down Expand Up @@ -146,18 +146,49 @@ def tearDownClass(cls):

@classmethod
def rebuild_index(cls, model):
"""Create and populate the Elasticsearch index and mapping"""
call_command("search_index", "--rebuild", "-f", "--models", model)
"""Delete, recreate, and populate the Elasticsearch index.

Uses the registry API directly instead of the search_index
management command to avoid its get_alias() call which queries
ALL ES indices and fails if any other test's index was already
deleted.
"""
models = model if isinstance(model, list) else [model]
model_classes = [apps.get_model(m) for m in models]
for index in registry.get_indices(models=model_classes):
Comment on lines +156 to +158
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 3 lines are common in the 3 methods. Can we move them to a helper method like:

  def _get_indices(cls, model):                                                                                                                                        
      models = model if isinstance(model, list) else [model]                                                                                                                                               
      model_classes = [apps.get_model(m) for m in models]
      return registry.get_indices(models=model_classes)

And then just call it like:

for index in cls._get_indices(model):
          index.create(ignore=[400])

To prevent duplicated code.

index.delete(ignore=[404, 400])
index.create()
for doc in registry.get_documents(models=model_classes):
qs = doc().get_indexing_queryset()
doc().update(qs)

@classmethod
def create_index(cls, model):
"""Create the elasticsearch index."""
call_command("search_index", "--create", "-f", "--models", model)
"""Create the elasticsearch index.

Uses the registry API directly instead of the search_index
management command to avoid its get_alias() call which queries
ALL ES indices and fails if any other test's index was already
deleted.
"""
models = model if isinstance(model, list) else [model]
model_classes = [apps.get_model(m) for m in models]
for index in registry.get_indices(models=model_classes):
index.create(ignore=[400])

@classmethod
def delete_index(cls, model):
"""Delete the elasticsearch index."""
call_command("search_index", "--delete", "-f", "--models", model)
"""Delete the elasticsearch index.

Uses the registry API directly instead of the search_index
management command to avoid its get_alias() call which queries
ALL ES indices and fails if any other test's index was already
deleted.
"""
models = model if isinstance(model, list) else [model]
model_classes = [apps.get_model(m) for m in models]
for index in registry.get_indices(models=model_classes):
index.delete(ignore=[404, 400])

@classmethod
def restart_celery_throttle_key(cls):
Expand Down
Loading