Coverage for tests/utility/test_dict.py: 100%
102 statements
« prev ^ index » next coverage.py v7.10.5, created at 2025-08-28 09:13 +0000
« prev ^ index » next coverage.py v7.10.5, created at 2025-08-28 09:13 +0000
1"""Test module for the functions in the `utility/dict.py` module.
3This module contains unit tests for the functions implemented in the `dict.py` module. The purpose of these tests is to
4ensure the correct functionality of each function in different scenarios and to validate that the expected outputs are
5returned.
7Tests should cover various edge cases, valid inputs, and any other conditions that are necessary to confirm the
8robustness of the functions."""
10import pytest
12from app.utility.dict import *
15class TestMergeDicts:
17 def test_empty_input(self) -> None:
18 """Test with empty input."""
19 assert merge_dicts() == {}
21 def test_single_dict(self) -> None:
22 """Test with a single dictionary."""
23 assert merge_dicts({"a": 1, "b": 2}) == {"a": 1, "b": 2}
25 def test_two_dicts_no_overlap(self) -> None:
26 """Test with two dictionaries that don't have overlapping keys."""
27 dict1 = {"a": 1, "b": 2}
28 dict2 = {"c": 3, "d": 4}
29 assert merge_dicts(dict1, dict2) == {"a": 1, "b": 2, "c": 3, "d": 4}
31 def test_two_dicts_with_overlap(self) -> None:
32 """Test with two dictionaries that have overlapping keys."""
33 dict1 = {"a": 1, "b": 2}
34 dict2 = {"b": 3, "c": 4}
35 assert merge_dicts(dict1, dict2) == {"a": 1, "b": 2, "c": 4}
37 def test_nested_dictionaries(self) -> None:
38 """Test with nested dictionaries."""
39 dict1 = {"a": {"nested": 1}}
40 dict2 = {"a": {"different": 2}, "b": 3}
41 # The entire nested dict should be kept from dict1
42 assert merge_dicts(dict1, dict2) == {"a": {"nested": 1}, "b": 3}
45class TestFilterDicts:
47 def test_empty_inequations(self) -> None:
48 """Test with empty list of inequations."""
49 dicts = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
50 result = filter_dicts(dicts, [])
51 assert result == dicts
53 def test_basic_less_than(self) -> None:
54 """Test basic less than operation."""
55 dicts = [{"a": 4, "b": 2}, {"a": 2, "b": 3}, {"a": 8, "b": 4}, {"a": 4, "b": 5}]
56 result = filter_dicts(dicts, ["b < a"])
57 assert result == [{"a": 4, "b": 2}, {"a": 8, "b": 4}]
59 def test_basic_greater_than(self) -> None:
60 """Test basic greater than operation."""
61 dicts = [{"a": 4, "b": 2}, {"a": 2, "b": 3}, {"a": 8, "b": 4}, {"a": 4, "b": 5}]
62 result = filter_dicts(dicts, ["b > a"])
63 assert result == [{"a": 2, "b": 3}, {"a": 4, "b": 5}]
65 def test_multiple_inequations(self) -> None:
66 """Test multiple inequations."""
67 dicts = [{"a": 4, "b": 2, "c": 3}, {"a": 2, "b": 3, "c": 1}, {"a": 8, "b": 4, "c": 5}, {"a": 4, "b": 5, "c": 2}]
68 result = filter_dicts(dicts, ["b < a", "c > b"])
69 assert result == [{"a": 4, "b": 2, "c": 3}, {"a": 8, "b": 4, "c": 5}]
71 def test_fixed_values_less_than(self) -> None:
72 """Test with fixed values and less than operation."""
73 dicts = [{"a": 4}, {"a": 2}, {"a": 8}, {"a": 4}]
74 result = filter_dicts(dicts, ["a < b"], {"b": 4.1})
75 assert result == [{"a": 4}, {"a": 2}, {"a": 4}]
77 def test_fixed_values_greater_than(self) -> None:
78 """Test with fixed values and greater than operation."""
79 dicts = [{"a": 4}, {"a": 2}, {"a": 8}, {"a": 4}]
80 result = filter_dicts(dicts, ["a > b"], {"b": 4.1})
81 assert result == [{"a": 8}]
83 def test_reversed_fixed_values(self) -> None:
84 """Test with fixed values on the left side of the inequation."""
85 dicts = [{"b": 4}, {"b": 2}, {"b": 8}, {"b": 4}]
86 result = filter_dicts(dicts, ["a < b"], {"a": 4.1})
87 assert result == [{"b": 8}]
89 def test_whitespace_handling(self) -> None:
90 """Test with whitespace in inequations."""
91 dicts = [{"a": 4, "b": 2}, {"a": 2, "b": 3}]
92 result = filter_dicts(dicts, ["b < a"]) # Extra whitespace
93 assert result == [{"a": 4, "b": 2}]
95 def test_equal_values(self) -> None:
96 """Test with equal values (should not be included)."""
97 dicts = [{"a": 4, "b": 4}, {"a": 2, "b": 3}]
98 result = filter_dicts(dicts, ["b < a"])
99 assert result == []
101 result = filter_dicts(dicts, ["b > a"])
102 assert result == [{"a": 2, "b": 3}]
104 def test_nonexistent_key(self) -> None:
105 """Test with nonexistent key (should not affect the result)."""
106 dicts = [{"a": 4, "b": 2}, {"a": 2, "b": 3}]
107 result = filter_dicts(dicts, ["c < a"])
108 assert result == dicts
110 def test_multiple_fixed_values(self) -> None:
111 """Test with multiple fixed values."""
112 dicts = [{"a": 4}, {"a": 2}, {"a": 8}, {"a": 4}]
113 result = filter_dicts(dicts, ["a > b", "a < c"], {"b": 3, "c": 5})
114 assert result == [{"a": 4}, {"a": 4}]
116 def test_combined_inequations(self) -> None:
117 """Test combining multiple inequations with <= and >=."""
118 dicts = [{"a": 4, "b": 4, "c": 5}, {"a": 2, "b": 3, "c": 4}, {"a": 8, "b": 4, "c": 8}]
119 result = filter_dicts(dicts, ["b <= a", "c >= b"])
120 assert result == [{"a": 4, "b": 4, "c": 5}, {"a": 8, "b": 4, "c": 8}]
122 def test_no_matching_results(self) -> None:
123 """Test when no dictionaries match the condition."""
124 dicts = [{"a": 1, "b": 5}, {"a": 3, "b": 7}]
125 result = filter_dicts(dicts, ["a >= b"])
126 assert result == []
128 def test_invalid_inequality(self) -> None:
130 with pytest.raises(ValueError):
131 dicts = [{"a": 1, "b": 5}, {"a": 3, "b": 7}]
132 filter_dicts(dicts, ["a f b"])
135class TestListToDict:
137 def test_basic_case(self) -> None:
138 """Test with a basic list of dictionaries."""
139 dicts = [{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6}]
140 result = list_to_dict(dicts)
141 expected = {"a": [1, 3, 5], "b": [2, 4, 6]}
142 assert result == expected
144 def test_single_element_dict(self) -> None:
145 """Test with a list containing a single dictionary."""
146 dicts = [{"a": 1, "b": 2}]
147 result = list_to_dict(dicts)
148 expected = {"a": [1], "b": [2]}
149 assert result == expected
151 def test_missing_key(self) -> None:
152 """Test when a dictionary in the list is missing a key."""
153 dicts = [{"a": 1, "b": 2}, {"a": 3}, {"a": 5, "b": 6}]
154 with pytest.raises(KeyError):
155 list_to_dict(dicts)
157 def test_non_dict_elements(self) -> None:
158 """Test when the input contains non-dictionary elements."""
159 dicts = [{"a": 1, "b": 2}, [3, 4], {"a": 5, "b": 6}]
160 with pytest.raises(TypeError):
161 list_to_dict(dicts)
163 def test_empty_dicts(self) -> None:
164 """Test when the dictionaries in the list are empty."""
165 dicts = [{}, {}, {}]
166 result = list_to_dict(dicts)
167 expected = {} # No keys, so the result should be an empty dict
168 assert result == expected