Skip to content

Commit

Permalink
avoid clobbering all indexes by running tests
Browse files Browse the repository at this point in the history
- rename the `es` pytest marker to `es_metrics` (for clarity)
- update the effect of that marker:
    - patch a prefix to each metric class's index and template names
    - instead of deleting ALL indexes and index templates, delete only
      those with the patched prefix
  • Loading branch information
aaxelb committed Sep 10, 2024
1 parent 4a2e997 commit 6d33ade
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from osf.metrics import UserInstitutionProjectCounts


@pytest.mark.es
@pytest.mark.es_metrics
@pytest.mark.django_db
class TestInstitutionDepartmentList:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from osf.metrics import InstitutionProjectCounts


@pytest.mark.es
@pytest.mark.es_metrics
@pytest.mark.django_db
class TestInstitutionSummaryMetrics:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from osf.metrics import UserInstitutionProjectCounts
from api.base import settings

@pytest.mark.es
@pytest.mark.es_metrics
@pytest.mark.django_db
class TestInstitutionUserMetricList:

Expand Down
2 changes: 1 addition & 1 deletion api_tests/metrics/test_composite_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def base_url():
return f'/{API_BASE}metrics/preprints/'


@pytest.mark.es
@pytest.mark.es_metrics
@pytest.mark.django_db
class TestElasticSearch:

Expand Down
2 changes: 1 addition & 1 deletion api_tests/metrics/test_preprint_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_custom_metric_malformed_query(self, mock_execute, app, user, base_url):
assert res.status_code == 400
assert res.json['errors'][0]['detail'] == 'Malformed elasticsearch query.'

@pytest.mark.es
@pytest.mark.es_metrics
def test_agg_query(self, app, user, base_url):

post_url = f'{base_url}downloads/'
Expand Down
2 changes: 1 addition & 1 deletion api_tests/metrics/test_raw_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
pytestmark = pytest.mark.django_db


@pytest.mark.es
@pytest.mark.es_metrics
class TestRawMetrics:

@pytest.fixture(autouse=True)
Expand Down
4 changes: 2 additions & 2 deletions api_tests/metrics/test_registries_moderation_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def enable_elasticsearch_metrics(self):
with override_switch(features.ELASTICSEARCH_METRICS, active=True):
yield

@pytest.mark.es
@pytest.mark.es_metrics
def test_record_transitions(self, registration):
registration._write_registration_action(
RegistrationModerationStates.INITIAL,
Expand Down Expand Up @@ -70,7 +70,7 @@ def other_user(self):
def base_url(self):
return '/_/metrics/registries_moderation/transitions/'

@pytest.mark.es
@pytest.mark.es_metrics
def test_registries_moderation_view(self, app, user, base_url, registration):
registration._write_registration_action(
RegistrationModerationStates.INITIAL,
Expand Down
46 changes: 34 additions & 12 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import contextlib
from unittest import mock
import logging
import os
import re

from django.core.management import call_command
from django.db import transaction
from elasticsearch import exceptions as es_exceptions
from elasticsearch_dsl.connections import connections
from elasticsearch_metrics.registry import registry as es_metrics_registry
from faker import Factory
import pytest
import responses
Expand Down Expand Up @@ -133,22 +136,41 @@ def es6_client(setup_connections):


@pytest.fixture(scope='function', autouse=True)
def _es_marker(request):
def _es_metrics_marker(request):
"""Clear out all indices and index templates before and after
tests marked with ``es``.
tests marked with `es_metrics`.
"""
marker = request.node.get_closest_marker('es')
marker = request.node.get_closest_marker('es_metrics')
if marker:
es6_client = request.getfixturevalue('es6_client')

def teardown_es():
es6_client.indices.delete(index='*')
es6_client.indices.delete_template('*')

teardown_es()
call_command('sync_metrics')
yield
teardown_es()
_temp_prefix = 'temp_metrics_'
_temp_wildcard = f'{_temp_prefix}*'

def _teardown_es_temps():
es6_client.indices.delete(index=_temp_wildcard)
try:
es6_client.indices.delete_template(_temp_wildcard)
except es_exceptions.NotFoundError:
pass

@contextlib.contextmanager
def _mock_metric_names():
with contextlib.ExitStack() as _exit:
for _metric_class in es_metrics_registry.get_metrics():
_exit.enter_context(mock.patch.multiple(
_metric_class,
# `_template_name` also used to construct index names
_template_name=f'{_temp_prefix}{_metric_class._template_name}',
# `_template` is a wildcard string for indexes and templates
_template=f'{_temp_prefix}{_metric_class._template}',
))
yield

_teardown_es_temps()
with _mock_metric_names():
call_command('sync_metrics')
yield
_teardown_es_temps()
else:
yield

Expand Down
2 changes: 1 addition & 1 deletion osf_tests/management_commands/test_reindex_es6.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def admin(self):
def url(self):
return f'{settings.API_DOMAIN}_/metrics/preprints/downloads/'

@pytest.mark.es
@pytest.mark.es_metrics
@pytest.mark.skipif(django_settings.TRAVIS_ENV, reason='Non-deterministic fails on travis')
def test_reindexing(self, app, url, preprint, user, admin, es6_client):
preprint_download = PreprintDownload.record_for_preprint(
Expand Down
2 changes: 1 addition & 1 deletion osf_tests/test_management_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def test_data_storage_usage_command(self):
assert (key, expected_summary_data[key]) == (key, actual_summary_data[key])


@pytest.mark.es
@pytest.mark.es_metrics
@pytest.mark.django_db
class TestInstitutionMetricsUpdate:

Expand Down

0 comments on commit 6d33ade

Please sign in to comment.