Changeset View
Changeset View
Standalone View
Standalone View
swh/search/tests/test_cli.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import tempfile | import tempfile | ||||
from unittest.mock import patch, MagicMock | from unittest.mock import patch, MagicMock | ||||
from click.testing import CliRunner | from click.testing import CliRunner | ||||
from swh.journal.serializers import value_to_kafka | from swh.journal.serializers import value_to_kafka | ||||
from swh.journal.tests.utils import MockedKafkaConsumer | |||||
from swh.search.cli import cli | from swh.search.cli import cli | ||||
from .test_elasticsearch import BaseElasticsearchTest | from .test_elasticsearch import BaseElasticsearchTest | ||||
CLI_CONFIG = ''' | CLI_CONFIG = ''' | ||||
search: | search: | ||||
cls: elasticsearch | cls: elasticsearch | ||||
Show All 24 Lines | if not catch_exceptions and result.exception: | ||||
raise result.exception | raise result.exception | ||||
return result | return result | ||||
class CliTestCase(BaseElasticsearchTest): | class CliTestCase(BaseElasticsearchTest): | ||||
def test__journal_client__origin(self): | def test__journal_client__origin(self): | ||||
"""Tests the re-indexing when origin_batch_size*task_batch_size is a | """Tests the re-indexing when origin_batch_size*task_batch_size is a | ||||
divisor of nb_origins.""" | divisor of nb_origins.""" | ||||
mock_consumer = MagicMock() | |||||
topic = 'swh.journal.objects.origin' | topic = 'swh.journal.objects.origin' | ||||
value = value_to_kafka({ | value = value_to_kafka({ | ||||
'url': 'http://foobar.baz', | 'url': 'http://foobar.baz', | ||||
}) | }) | ||||
message = MagicMock() | message = MagicMock() | ||||
message.error.return_value = None | message.error.return_value = None | ||||
message.topic.return_value = topic | message.topic.return_value = topic | ||||
message.value.return_value = value | message.value.return_value = value | ||||
mock_consumer.poll.return_value = message | |||||
mock_consumer = MockedKafkaConsumer([message]) | |||||
with patch('swh.journal.client.Consumer', | with patch('swh.journal.client.Consumer', | ||||
return_value=mock_consumer): | return_value=mock_consumer): | ||||
result = invoke(False, [ | result = invoke(False, [ | ||||
'journal-client', 'objects', | 'journal-client', 'objects', | ||||
'--max-messages', '1', | '--max-messages', '1', | ||||
], JOURNAL_OBJECTS_CONFIG, | ], JOURNAL_OBJECTS_CONFIG, | ||||
elasticsearch_host=self._elasticsearch_host) | elasticsearch_host=self._elasticsearch_host) | ||||
mock_consumer.subscribe.assert_called_once_with(topics=[topic]) | |||||
mock_consumer.poll.assert_called_once_with(timeout=1.0) | |||||
mock_consumer.commit.assert_called_once_with() | |||||
# Check the output | # Check the output | ||||
expected_output = ( | expected_output = ( | ||||
'Processed 1 messages.\n' | 'Processed 1 messages.\n' | ||||
'Done.\n' | 'Done.\n' | ||||
) | ) | ||||
assert result.exit_code == 0, result.output | assert result.exit_code == 0, result.output | ||||
assert result.output == expected_output | assert result.output == expected_output | ||||
results = self.search.origin_search(url_pattern='foobar') | results = self.search.origin_search(url_pattern='foobar') | ||||
assert results == {'scroll_token': None, 'results': [ | assert results == {'scroll_token': None, 'results': [ | ||||
{'url': 'http://foobar.baz'}]} | {'url': 'http://foobar.baz'}]} |