-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,38 @@ | ||
import pytest | ||
|
||
from share.search.index_strategy import IndexStrategy | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def elastic_test_index_name(): | ||
return 'test_share' | ||
|
||
from unittest import mock | ||
|
||
@pytest.fixture(params=['es5', 'es8']) | ||
def elastic_test_cluster_url(request, settings): | ||
if request.param == 'es5': | ||
return settings.ELASTICSEARCH5_URL | ||
if request.param == 'es8': | ||
return settings.ELASTICSEARCH8_URL | ||
raise ValueError(request.param) | ||
import pytest | ||
|
||
|
||
@pytest.fixture() | ||
def actual_elasticsearch(elastic_test_index_name, elastic_test_cluster_url, settings): | ||
old_elasticsearch_settings = settings.ELASTICSEARCH | ||
@pytest.fixture | ||
def fake_elastic_strategies(settings): | ||
settings.ELASTICSEARCH = { | ||
**old_elasticsearch_settings, | ||
'TIMEOUT': 5, | ||
'PRIMARY_INDEX': elastic_test_index_name, | ||
'LEGACY_INDEX': elastic_test_index_name, | ||
'BACKCOMPAT_INDEX': elastic_test_index_name, | ||
'ACTIVE_INDEXES': [elastic_test_index_name], | ||
**settings.ELASTICSEARCH, | ||
'INDEX_STRATEGIES': { | ||
elastic_test_index_name: { | ||
'my_es5_strategy': { | ||
'CLUSTER_SETTINGS': {'URL': 'blah'}, | ||
'INDEX_STRATEGY_CLASS': 'share.search.index_strategy.sharev2_elastic5.Sharev2Elastic5IndexStrategy', | ||
'CLUSTER_SETTINGS': { | ||
'URL': settings.ELASTICSEARCH5_URL, | ||
}, | ||
}, | ||
'my_es8_strategy': { | ||
'CLUSTER_SETTINGS': {'URL': 'bleh'}, | ||
'INDEX_STRATEGY_CLASS': 'share.search.index_strategy.sharev2_elastic8.Sharev2Elastic8IndexStrategy', | ||
}, | ||
'another_es8_strategy': { | ||
'CLUSTER_SETTINGS': {'URL': 'bluh'}, | ||
'INDEX_STRATEGY_CLASS': 'share.search.index_strategy.sharev2_elastic8.Sharev2Elastic8IndexStrategy', | ||
}, | ||
}, | ||
} | ||
index_strategy = IndexStrategy.get_by_name(elastic_test_index_name) | ||
try: | ||
index_strategy.pls_delete() | ||
index_strategy.pls_setup() | ||
try: | ||
yield | ||
finally: | ||
index_strategy.pls_delete() | ||
except Exception as error: | ||
raise pytest.skip(f'Elasticsearch unavailable? (error: {error})') | ||
return tuple(settings.ELASTICSEARCH['INDEX_STRATEGIES'].keys()) | ||
|
||
|
||
@pytest.fixture | ||
def mock_elastic_clients(fake_elastic_strategies): | ||
with mock.patch('share.search.index_strategy.sharev2_elastic5.elasticsearch5') as es5_mockpackage: | ||
with mock.patch('share.search.index_strategy.elastic8.elasticsearch8') as es8_mockpackage: | ||
es5_mockclient = es5_mockpackage.Elasticsearch.return_value | ||
es8_mockclient = es8_mockpackage.Elasticsearch.return_value | ||
yield { | ||
'my_es5_strategy': es5_mockclient, | ||
'my_es8_strategy': es8_mockclient, | ||
'another_es8_strategy': es8_mockclient, | ||
} |
1 change: 0 additions & 1 deletion
1
tests/share/search/index_strategy/test_admin_workflow_integration.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from unittest import mock | ||
|
||
from django.test.client import Client | ||
import pytest | ||
|
||
from share.models import ShareUser | ||
from share.search.index_strategy import IndexStrategy | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_admin_search_indexes_view(fake_elastic_strategies, mock_elastic_clients): | ||
credentials = {'username': 'test-test-test', 'password': 'password-password'} | ||
ShareUser.objects.create_superuser(**credentials) | ||
client = Client() | ||
client.login(**credentials) | ||
with mock.patch('share.search.index_strategy.elastic8.elasticsearch8'): | ||
resp = client.get('/admin/search-indexes') | ||
for strategy_name in fake_elastic_strategies: | ||
index_strategy = IndexStrategy.get_by_name(strategy_name) | ||
expected_header = f'<h3>current index: <i>{index_strategy.current_indexname}</i></h3>' | ||
assert expected_header.encode() in resp.content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from share.models import IndexBackfill | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestIndexBackfillMethods: | ||
@pytest.fixture | ||
def fake_strategy(self): | ||
fake_strategy = mock.Mock() | ||
fake_strategy.name = 'foo' | ||
fake_strategy.for_current_index.return_value.indexname = 'foo_bar' | ||
return fake_strategy | ||
|
||
@pytest.fixture | ||
def index_backfill(self, fake_strategy): | ||
return IndexBackfill.objects.create( | ||
index_strategy_name=fake_strategy.name, | ||
) | ||
|
||
def test_happypath(self, index_backfill, fake_strategy): | ||
assert index_backfill.backfill_status == IndexBackfill.INITIAL | ||
assert index_backfill.specific_indexname == '' | ||
with mock.patch('share.tasks.schedule_index_backfill') as mock_task: | ||
index_backfill.pls_start(fake_strategy) | ||
mock_task.apply_async.assert_called_once_with((index_backfill.pk,)) | ||
assert index_backfill.backfill_status == IndexBackfill.WAITING | ||
assert index_backfill.specific_indexname == 'foo_bar' | ||
index_backfill.pls_note_scheduling_has_begun() | ||
assert index_backfill.backfill_status == IndexBackfill.SCHEDULING | ||
index_backfill.pls_note_scheduling_has_finished() | ||
assert index_backfill.backfill_status == IndexBackfill.INDEXING | ||
index_backfill.pls_mark_complete() | ||
assert index_backfill.backfill_status == IndexBackfill.COMPLETE | ||
|
||
def test_error(self, index_backfill): | ||
assert index_backfill.backfill_status == IndexBackfill.INITIAL | ||
assert index_backfill.error_type == '' | ||
assert index_backfill.error_message == '' | ||
assert index_backfill.error_context == '' | ||
index_backfill.pls_mark_error(ValueError('hello')) | ||
assert index_backfill.backfill_status == IndexBackfill.ERROR | ||
assert index_backfill.error_type == 'ValueError' | ||
assert index_backfill.error_message == 'hello' | ||
assert index_backfill.error_context | ||
index_backfill.pls_mark_error(None) | ||
assert index_backfill.backfill_status == IndexBackfill.ERROR # clearing error does not change status | ||
assert index_backfill.error_type == '' | ||
assert index_backfill.error_message == '' | ||
assert index_backfill.error_context == '' |