Skip to content

Commit

Permalink
✅ Added debug test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilbadyal committed Sep 18, 2023
1 parent fb4a377 commit cb81b1b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/esxport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions src/strings.py
Original file line number Diff line number Diff line change
@@ -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}."
22 changes: 21 additions & 1 deletion test/esxport/_prepare_search_query_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit cb81b1b

Please sign in to comment.