Coverage for tests/unit/test_utils.py: 100%
59 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-26 17:59 -0600
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-26 17:59 -0600
1"""Unit tests for the utils module."""
3# pylint: disable=C0116
4import pytest
5from es_fieldusage.helpers.utils import (
6 convert_mapping,
7 detuple,
8 get_value_from_path,
9 iterate_paths,
10 output_report,
11 override_settings,
12 passthrough,
13 sort_by_name,
14 sort_by_value,
15 sum_dict_values,
16)
17from es_fieldusage.exceptions import ConfigurationException
20def test_convert_mapping():
21 data = {
22 "field1": {"properties": {"subfield1": {}}},
23 "field2": {"type": "text"},
24 }
25 expected = {"field1": {"subfield1": 0}, "field2": 0}
26 assert convert_mapping(data) == expected
29def test_detuple():
30 assert detuple([(1, 2)]) == [1, 2]
31 assert detuple([1]) == [1]
34def test_get_value_from_path():
35 data = {"a": {"b": {"c": 42}}}
36 path = ["a", "b", "c"]
37 assert get_value_from_path(data, path) == 42
40def test_iterate_paths():
41 data = {"a": {"b": {"c": 42}}, "d": 1}
42 paths = list(iterate_paths(data))
43 expected = [["a", "b", "c"], ["d"]]
44 assert paths == expected
47def test_output_report(capsys):
48 report = {
49 "indices": ["index1", "index2"],
50 "field_count": 10,
51 "accessed": {"field1": 1, "field2": 2},
52 "unaccessed": {"field3": 0},
53 }
54 output_report("test_pattern", report)
55 captured = capsys.readouterr()
56 assert "Summary Report" in captured.out
57 assert "test_pattern" in captured.out
58 assert "2 Indices Found" in captured.out
59 assert "Total Fields Found: 10" in captured.out
60 assert "Accessed Fields: 2" in captured.out
61 assert "Unaccessed Fields: 1" in captured.out
64def test_override_settings():
65 data = {"key1": "value1", "key2": "value2"}
66 new_data = {"key1": "new_value1"}
67 expected = {"key1": "new_value1", "key2": "value2"}
68 assert override_settings(data, new_data) == expected
70 with pytest.raises(ConfigurationException):
71 override_settings(data, "not_a_dict")
74def test_passthrough():
75 def sample_func(*args, **kwargs):
76 return args, kwargs
78 wrapped = passthrough(sample_func)
79 assert wrapped((1,), {"key": "value"}) == ((1,), {"key": "value"})
82def test_sort_by_name():
83 data = {"b": 2, "a": 1}
84 expected = {"a": 1, "b": 2}
85 assert sort_by_name(data) == expected
88def test_sort_by_value():
89 data = {"a": 1, "b": 2}
90 expected = {"b": 2, "a": 1}
91 assert sort_by_value(data) == expected
94def test_sum_dict_values():
95 data = {
96 "dict1": {"a": 1, "b": 2},
97 "dict2": {"a": 3, "c": 4},
98 }
99 expected = {"a": 4, "b": 2, "c": 4}
100 assert sum_dict_values(data) == expected
103@pytest.mark.parametrize(
104 "search_pattern, report, expected_strings",
105 [
106 # Test case 1: Single index as string
107 (
108 "test_index",
109 {
110 "indices": "test_index",
111 "field_count": 5,
112 "accessed": {"field1": 1},
113 "unaccessed": {"field2": 0},
114 },
115 [
116 "Summary Report",
117 "Search Pattern: test_index",
118 "Index Found: test_index",
119 "Total Fields Found: 5",
120 "Accessed Fields: 1",
121 "Unaccessed Fields: 1",
122 ],
123 ),
124 # Test case 2: Single index as list
125 (
126 "test_index",
127 {
128 "indices": ["test_index"],
129 "field_count": 5,
130 "accessed": {"field1": 1},
131 "unaccessed": {"field2": 0},
132 },
133 [
134 "Summary Report",
135 "Search Pattern: test_index",
136 "1 Indices Found: ['test_index']",
137 "Total Fields Found: 5",
138 "Accessed Fields: 1",
139 "Unaccessed Fields: 1",
140 ],
141 ),
142 # Test case 3: Multiple indices (3)
143 (
144 "test_*",
145 {
146 "indices": ["index1", "index2", "index3"],
147 "field_count": 15,
148 "accessed": {"field1": 1, "field2": 2, "field3": 3},
149 "unaccessed": {"field4": 0, "field5": 0},
150 },
151 [
152 "Summary Report",
153 "Search Pattern: test_*",
154 "3 Indices Found: ['index1', 'index2', 'index3']",
155 "Total Fields Found: 15",
156 "Accessed Fields: 3",
157 "Unaccessed Fields: 2",
158 ],
159 ),
160 # Test case 4: Many indices (4)
161 (
162 "test_*",
163 {
164 "indices": ["index1", "index2", "index3", "index4"],
165 "field_count": 20,
166 "accessed": {"field1": 1, "field2": 2, "field3": 3, "field4": 4},
167 "unaccessed": {"field5": 0, "field6": 0, "field7": 0},
168 },
169 [
170 "Summary Report",
171 "Search Pattern: test_*",
172 "4 Indices Found: (data too big)",
173 "Total Fields Found: 20",
174 "Accessed Fields: 4",
175 "Unaccessed Fields: 3",
176 ],
177 ),
178 # Test case 5: No indices
179 (
180 "test_*",
181 {
182 "indices": [],
183 "field_count": 0,
184 "accessed": {},
185 "unaccessed": {},
186 },
187 [
188 "Summary Report",
189 "Search Pattern: test_*",
190 "0 Indices Found: []",
191 "Total Fields Found: 0",
192 "Accessed Fields: 0",
193 "Unaccessed Fields: 0",
194 ],
195 ),
196 ],
197)
198def test_output_report_extended(capsys, search_pattern, report, expected_strings):
199 """Test output_report function for various report configurations."""
200 output_report(search_pattern, report)
201 captured = capsys.readouterr()
202 for string in expected_strings:
203 assert string in captured.out, f"Expected '{string}' in output:\n{captured.out}"