Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #4123 from hypothesis/search-cli
Browse files Browse the repository at this point in the history
Improve CLI for search-related operations
  • Loading branch information
chdorner committed Nov 25, 2016
2 parents 0a5fff3 + f8a73fd commit 9b43e29
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 19 deletions.
2 changes: 1 addition & 1 deletion h/cli/__init__.py
Expand Up @@ -21,7 +21,7 @@
'h.cli.commands.migrate.migrate',
'h.cli.commands.move_uri.move_uri',
'h.cli.commands.normalize_uris.normalize_uris',
'h.cli.commands.reindex.reindex',
'h.cli.commands.search.search',
'h.cli.commands.shell.shell',
'h.cli.commands.user.user',
)
Expand Down
17 changes: 0 additions & 17 deletions h/cli/commands/reindex.py

This file was deleted.

46 changes: 46 additions & 0 deletions h/cli/commands/search.py
@@ -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)
2 changes: 1 addition & 1 deletion src/memex/search/config.py
Expand Up @@ -291,7 +291,7 @@ def _update_index_mappings(conn, name, mappings):

message = ("Elasticsearch index mapping cannot be automatically "
"updated! Please reindex it. You may find the `hypothesis "
"reindex` command helpful.")
"search reindex` command helpful.")
log.critical(message)
raise RuntimeError(message)

Expand Down
48 changes: 48 additions & 0 deletions tests/h/cli/commands/search_test.py
@@ -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)}

0 comments on commit 9b43e29

Please sign in to comment.