Coverage for test/esxport/_prepare_search_query_test.py: 97%
69 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-22 17:59 +0530
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-22 17:59 +0530
1"""Export testing."""
2from __future__ import annotations
4import string
5from random import choice, randint
6from typing import TYPE_CHECKING, Any
7from unittest.mock import patch
9import pytest
11from src.exceptions import IndexNotFoundError
12from src.strings import index_not_found, output_fields, sorting_by, using_indexes
14if TYPE_CHECKING:
15 from typing_extensions import Self
17 from src.esxport import EsXport
20@patch("src.esxport.EsXport._validate_fields")
21class TestSearchQuery:
22 """Tests that a search query with valid input parameters is successful."""
24 @staticmethod
25 def random_string(str_len: int = 20) -> str:
26 """Generates a random string."""
27 characters = string.ascii_letters + string.digits
28 return "".join(choice(characters) for _ in range(str_len))
30 @staticmethod
31 def random_number(upper: int = 100, lower: int = 10000) -> int:
32 """Generates a random number."""
33 return randint(upper, lower)
35 def test_index(self: Self, _: Any, esxport_obj: EsXport) -> None:
36 """Arr, matey!.
38 Let's test if our search query be successful, with valid input parameters!.
39 """
40 random_strings = [self.random_string(10) for _ in range(5)]
41 esxport_obj.opts.index_prefixes = random_strings
42 indexes = ",".join(random_strings)
44 esxport_obj._prepare_search_query()
45 assert esxport_obj.search_args["index"] == indexes
47 def test_all_index(self: Self, _: Any, esxport_obj: EsXport) -> None:
48 """Arr, matey!.
50 Let's test if our search query be successful, with valid input parameters!.
51 """
52 esxport_obj.opts.index_prefixes = ["_all", "invalid_index"]
53 esxport_obj._check_indexes()
54 assert esxport_obj.opts.index_prefixes == ["_all"]
56 def test_invalid_index(
57 self: Self,
58 _: Any,
59 esxport_obj: EsXport,
60 ) -> None:
61 """Arr, matey!.
63 Let's test if our search query be successful, with valid input parameters!.
64 """
65 esxport_obj.opts.index_prefixes = ["invalid_index"]
67 with patch.object(esxport_obj.es_client, "indices_exists", return_value=False):
68 with pytest.raises(IndexNotFoundError) as exc_info:
69 esxport_obj._check_indexes()
71 msg = index_not_found.format("invalid_index", esxport_obj.opts.url)
72 assert str(exc_info.value) == msg
74 def test_size(
75 self: Self,
76 _: Any,
77 esxport_obj: EsXport,
78 ) -> None:
79 """Arr, matey!.
81 Let's test if our search query be successful, with valid input parameters!.
82 """
83 page_size = randint(100, 9999)
84 esxport_obj.opts.scroll_size = page_size
86 esxport_obj._prepare_search_query()
87 assert esxport_obj.search_args["size"] == page_size
89 def test_query(self: Self, _: Any, esxport_obj: EsXport) -> None:
90 """Arr, matey!.
92 Let's test if our search query be successful, with valid input parameters!.
93 """
94 expected_query: dict[str, Any] = {"query": {"match_all": {}}}
95 esxport_obj.opts.query = expected_query
97 esxport_obj._prepare_search_query()
98 assert esxport_obj.search_args["body"] == expected_query
100 def test_terminate_after(self: Self, _: Any, esxport_obj: EsXport) -> None:
101 """Arr, matey!.
103 Let's test if our search query be successful, with valid input parameters!.
104 """
105 random_max = self.random_number()
106 esxport_obj.opts.max_results = random_max
107 esxport_obj._prepare_search_query()
108 assert esxport_obj.search_args["terminate_after"] == random_max
110 def test_sort(
111 self: Self,
112 _: Any,
113 esxport_obj: EsXport,
114 ) -> None:
115 """Arr, matey!.
117 Let's test if our search query be successful, with valid input parameters!.
118 """
119 random_sort = [{self.random_string(): "asc"}, {self.random_string(): "desc"}]
120 esxport_obj.opts.sort = random_sort
122 esxport_obj._prepare_search_query()
123 assert esxport_obj.search_args["sort"] == random_sort
125 def test_debug_option(
126 self: Self,
127 _: Any,
128 esxport_obj: EsXport,
129 caplog: pytest.LogCaptureFixture,
130 ) -> None:
131 """Arr, matey!.
133 Let's test if our search query be successful, with valid input parameters!.
134 """
135 esxport_obj.opts.debug = True
137 esxport_obj._prepare_search_query()
138 assert caplog.records[0].msg == using_indexes.format(indexes={", ".join(esxport_obj.opts.index_prefixes)})
139 assert caplog.records[1].msg.startswith("Using query")
140 assert caplog.records[2].msg == output_fields.format(fields={", ".join(esxport_obj.opts.fields)})
141 assert caplog.records[3].msg == sorting_by.format(sort=esxport_obj.opts.sort)
143 def test_custom_output_fields(self: Self, _: Any, esxport_obj: EsXport) -> None:
144 """Test if selection only some fields for the output works."""
145 random_strings = [self.random_string(10) for _ in range(5)]
146 esxport_obj.opts.fields = random_strings
147 esxport_obj._prepare_search_query()
148 assert esxport_obj.search_args["_source_includes"] == ",".join(random_strings)