Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4123 from hypothesis/search-cli
Improve CLI for search-related operations
- Loading branch information
Showing
5 changed files
with
96 additions
and
19 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import click | ||
|
||
from memex.search import config | ||
from memex.search import index | ||
|
||
|
||
@click.group() | ||
def search(): | ||
"""Manage search index.""" | ||
|
||
|
||
@search.command() | ||
@click.pass_context | ||
def reindex(ctx): | ||
""" | ||
Reindex all annotations. | ||
Creates a new search index from the data in PostgreSQL and atomically | ||
updates the index alias. This requires that the index is aliased already, | ||
and will raise an error if it is not. | ||
""" | ||
|
||
request = ctx.obj['bootstrap']() | ||
|
||
index.reindex(request.db, request.es, request) | ||
|
||
|
||
@search.command('update-settings') | ||
@click.pass_context | ||
def update_settings(ctx): | ||
""" | ||
Attempt to update mappings and settings. | ||
Attempts to update mappings and index settings. This may fail if the | ||
pending changes to mappings are not compatible with the current index. In | ||
this case you will likely need to reindex. | ||
""" | ||
|
||
request = ctx.obj['bootstrap']() | ||
|
||
try: | ||
config.update_index_settings(request.es) | ||
except RuntimeError as e: | ||
raise click.ClickException(e.message) |
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,48 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import mock | ||
import pytest | ||
|
||
from h.cli.commands import search | ||
|
||
|
||
class TestReindexCommand(object): | ||
def test_calls_reindex(self, cli, cliconfig, pyramid_request, reindex): | ||
result = cli.invoke(search.reindex, [], obj=cliconfig) | ||
|
||
assert result.exit_code == 0 | ||
reindex.assert_called_once_with(pyramid_request.db, | ||
pyramid_request.es, | ||
pyramid_request) | ||
|
||
@pytest.fixture | ||
def reindex(self, patch): | ||
index = patch('h.cli.commands.search.index') | ||
return index.reindex | ||
|
||
|
||
class TestUpdateSettingsCommand(object): | ||
def test_calls_update_index_settings(self, cli, cliconfig, pyramid_request, update_index_settings): | ||
result = cli.invoke(search.update_settings, [], obj=cliconfig) | ||
|
||
assert result.exit_code == 0 | ||
update_index_settings.assert_called_once_with(pyramid_request.es) | ||
|
||
def test_handles_runtimeerror(self, cli, cliconfig, update_index_settings): | ||
update_index_settings.side_effect = RuntimeError("asplode!") | ||
|
||
result = cli.invoke(search.update_settings, [], obj=cliconfig) | ||
|
||
assert result.exit_code == 1 | ||
assert 'asplode!' in result.output | ||
|
||
@pytest.fixture | ||
def update_index_settings(self, patch): | ||
config = patch('h.cli.commands.search.config') | ||
return config.update_index_settings | ||
|
||
|
||
@pytest.fixture | ||
def cliconfig(pyramid_request): | ||
pyramid_request.es = mock.sentinel.es | ||
return {'bootstrap': mock.Mock(return_value=pyramid_request)} |