From 5cfb9e01451d2108eaafb66a039ebba5aab0aad4 Mon Sep 17 00:00:00 2001 From: Nikhil Badyal Date: Mon, 18 Sep 2023 22:40:39 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Added=20debug=20test=20cases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/esxport.py | 11 ++++++----- src/strings.py | 4 ++++ test/esxport/_prepare_search_query_test.py | 22 +++++++++++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/esxport.py b/src/esxport.py index 5260b54..1dbbe24 100644 --- a/src/esxport.py +++ b/src/esxport.py @@ -12,7 +12,7 @@ from src.constant import FLUSH_BUFFER, TIMES_TO_TRY from src.exceptions import FieldNotFoundError, IndexNotFoundError -from src.strings import index_not_found +from src.strings import index_not_found, output_fields, sorting_by, using_indexes, using_query from src.utils import retry from src.writer import Writer @@ -92,10 +92,11 @@ def _prepare_search_query(self: Self) -> None: self.search_args["_source_includes"] = ",".join(self.opts.fields) if self.opts.debug: - logger.debug(f'Using these indices: {", ".join(self.opts.index_prefixes)}.') - logger.debug(f"Query {self.opts.query}") - logger.debug(f'Output field(s): {", ".join(self.opts.fields)}.') - logger.debug(f"Sorting by: {self.opts.sort}.") + logger.debug(using_indexes.format(indexes={", ".join(self.opts.index_prefixes)})) + query = json.dumps(self.opts.query) + logger.debug(using_query.format(query={query})) + logger.debug(output_fields.format(fields={", ".join(self.opts.fields)})) + logger.debug(sorting_by.format(sort=self.opts.sort)) @retry(ConnectionError, tries=TIMES_TO_TRY) def next_scroll(self: Self, scroll_id: str) -> Any: diff --git a/src/strings.py b/src/strings.py index f78d791..b29f6ae 100644 --- a/src/strings.py +++ b/src/strings.py @@ -1,2 +1,6 @@ """Common used strings.""" index_not_found = "Any of index(es) {} does not exist in {}." +using_indexes = "Using indices: {indexes}." +using_query = "Using query: {query}." +output_fields = "Output fields : {fields}." +sorting_by = "Sorting by: {sort}." diff --git a/test/esxport/_prepare_search_query_test.py b/test/esxport/_prepare_search_query_test.py index 4c4f78a..33cb0b0 100644 --- a/test/esxport/_prepare_search_query_test.py +++ b/test/esxport/_prepare_search_query_test.py @@ -10,7 +10,7 @@ from src.esxport import EsXport from src.exceptions import IndexNotFoundError -from src.strings import index_not_found +from src.strings import index_not_found, output_fields, sorting_by, using_indexes if TYPE_CHECKING: from typing_extensions import Self @@ -155,3 +155,23 @@ def test_sort( es_export = EsXport(cli_options, mock_es_client) es_export._prepare_search_query() assert es_export.search_args["sort"] == random_sort + + def test_debug_option( + self: Self, + _: Any, + mock_es_client: ElasticsearchClient, + cli_options: CliOptions, + caplog: pytest.LogCaptureFixture, + ) -> None: + """Arr, matey!. + + Let's test if our search query be successful, with valid input parameters!. + """ + cli_options.debug = True + + es_export = EsXport(cli_options, mock_es_client) + es_export._prepare_search_query() + assert caplog.records[0].msg == using_indexes.format(indexes={", ".join(cli_options.index_prefixes)}) + assert caplog.records[1].msg.startswith("Using query") + assert caplog.records[2].msg == output_fields.format(fields={", ".join(cli_options.fields)}) + assert caplog.records[3].msg == sorting_by.format(sort=cli_options.sort)