-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_utils_dict_support.py
173 lines (115 loc) · 6.38 KB
/
test_utils_dict_support.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from copy import deepcopy
import genet.utils.dict_support as dict_support
import pytest
from pandas import DataFrame
def test_set_nested_value_overwrites_current_value():
d = {"attributes": {"some_osm_tag": "hello"}}
value = {"attributes": {"some_osm_tag": "bye"}}
return_d = dict_support.set_nested_value(d, value)
assert return_d == {"attributes": {"some_osm_tag": "bye"}}
def test_set_nested_value_adds_new_key_val_pair():
d = {"attributes": {"some_osm_tag": "hello"}}
value = {"attributes": {"some_tag": "bye"}}
return_d = dict_support.set_nested_value(d, value)
assert return_d == {"attributes": {"some_osm_tag": "hello", "some_tag": "bye"}}
def test_set_nested_value_creates_new_nest_in_place_of_single_value():
d = {"attributes": 1}
value = {"attributes": {"some_tag": "bye"}}
return_d = dict_support.set_nested_value(d, value)
assert return_d == {"attributes": {"some_tag": "bye"}}
def test_getting_nested_value_from_dictionary():
d = {"1": {"2": {"3": {"4": "hey"}}}}
path = {"1": {"2": {"3": "4"}}}
assert dict_support.get_nested_value(d, path) == "hey"
def test_get_nested_value_throws_error_if_passed_incorrect_path():
d = {"1": {"2": {"3": {"4": "hey"}}}}
path = {"1": {"2": {"3": "5"}}}
with pytest.raises(KeyError) as e:
dict_support.get_nested_value(d, path)
assert "5" in str(e.value)
def test_finding_nested_path_on_condition():
d = {"1": {"2": {"3": {"4": "hey"}, "5": "6"}, "7": "8"}, "9": "10"}
value = "hey"
assert dict_support.find_nested_paths_to_value(d, value) == [{"1": {"2": {"3": "4"}}}]
def test_finding_nested_path_on_condition_with_list():
d = {"1": {"2": {"3": {"4": ["hey"]}, "5": "6"}, "7": "8"}, "9": "10"}
value = "hey"
assert dict_support.find_nested_paths_to_value(d, value) == [{"1": {"2": {"3": "4"}}}]
def test_finding_nested_path_on_set_condition_with_list():
d = {"1": {"2": {"3": {"4": ["hey"]}, "5": "6"}, "7": "8"}, "9": "10"}
value = {"hey"}
assert dict_support.find_nested_paths_to_value(d, value) == [{"1": {"2": {"3": "4"}}}]
def test_finding_multiple_nested_paths_on_condition():
d = {"1": {"2": {"3": {"4": ["hey"]}, "5": "6"}, "7": "hey"}, "9": "10", "11": "heyo"}
value = {"hey", "heyo"}
assert dict_support.find_nested_paths_to_value(d, value) == [
{"1": {"2": {"3": "4"}}},
{"1": "7"},
"11",
]
def test_nesting_at_leaf_with_some_value():
d = {"1": {"2": {"3": {"4": "hey"}}}}
_d = dict_support.nest_at_leaf(d, "some")
assert d == {"1": {"2": {"3": {"4": {"hey": "some"}}}}}
assert _d == {"1": {"2": {"3": {"4": {"hey": "some"}}}}}
def test_merging_simple_dictionaries():
return_d = dict_support.merge_complex_dictionaries({"a": 1, "b": 3}, {"b": 5, "c": 8})
assert return_d == {"a": 1, "b": 5, "c": 8}
def test_merging_dictionaries_with_lists(assert_semantically_equal):
return_d = dict_support.merge_complex_dictionaries(
{"a": 1, "b": [3, 6], "c": [1]}, {"b": [5], "c": [8, 90]}
)
assert_semantically_equal(return_d, {"a": 1, "b": [3, 6, 5], "c": [1, 8, 90]})
def test_preserves_duplicates_in_input_list_when_merging_dictionaries(assert_semantically_equal):
return_d = dict_support.merge_complex_dictionaries({"b": [6, 6]}, {"b": [5]})
assert_semantically_equal(return_d, {"b": [6, 6, 5]})
def test_preserves_resulting_duplicates_in_lists_when_merging_dictionaries(
assert_semantically_equal,
):
return_d = dict_support.merge_complex_dictionaries({"b": [6]}, {"b": [5, 6]})
assert_semantically_equal(return_d, {"b": [6, 5, 6]})
def test_preserves_lists_order_when_merging_dictionaries(assert_semantically_equal):
return_d = dict_support.merge_complex_dictionaries({"b": [1, 2]}, {"b": [3, 4]})
assert_semantically_equal(return_d, {"b": [1, 2, 3, 4]})
def test_combines_sets_with_or_operator_when_merging_dictionaries(assert_semantically_equal):
return_d = dict_support.merge_complex_dictionaries({"b": {1, 2}}, {"b": {2, 3}})
assert_semantically_equal(return_d, {"b": {1, 2, 3}})
def test_does_not_mutate_parameters_when_merging_complex_dictionaries(assert_semantically_equal):
A = {"a": {1, 2}, "b": [6, 6], "c": {"a": {1, 2}, "b": [6, 6]}, "d": "hey"}
B = {"a": {2, 3}, "b": [5], "c": {"a": {2, 3}, "b": [5]}, "d": "yo"}
A_before = deepcopy(A)
B_before = deepcopy(B)
dict_support.merge_complex_dictionaries(A, B)
assert_semantically_equal(A, A_before)
assert_semantically_equal(B, B_before)
def test_merging_nested_dictionaries(assert_semantically_equal):
return_d = dict_support.merge_complex_dictionaries(
{"a": 1, "b": {3: 5}, "c": {1: 4}}, {"b": {5: 3}, "c": {8: 90}}
)
assert_semantically_equal(return_d, {"a": 1, "b": {3: 5, 5: 3}, "c": {1: 4, 8: 90}})
def test_merging_dictionaries_with_nested_lists(assert_semantically_equal):
return_d = dict_support.merge_complex_dictionaries(
{"a": 1, "b": {"a": [3, 6]}, "c": {"b": [1]}}, {"b": {"a": [5]}, "c": {"b": [8, 90]}}
)
assert_semantically_equal(return_d, {"a": 1, "b": {"a": [3, 6, 5]}, "c": {"b": [8, 90, 1]}})
def test_merging_dictionaries_with_nested_sets(assert_semantically_equal):
return_d = dict_support.merge_complex_dictionaries(
{"a": 1, "b": {"a": {3, 6}}, "c": {"b": {1}}}, {"b": {"a": {5}}, "c": {"b": {8, 90}}}
)
assert_semantically_equal(return_d, {"a": 1, "b": {"a": {3, 6, 5}}, "c": {"b": {8, 90, 1}}})
def test_merging_dicts_with_lists(assert_semantically_equal):
d = dict_support.merge_complex_dictionaries({"1": [""], "2": []}, {"3": ["1"], "1": ["2"]})
assert_semantically_equal(d, {"1": ["", "2"], "2": [], "3": ["1"]})
def test_merging_dicts_with_lists_when_one_dict_is_empty(assert_semantically_equal):
d = dict_support.merge_complex_dictionaries({"1": [""], "2": []}, {})
assert_semantically_equal(d, {"1": [""], "2": []})
def test_simple_dict_to_string():
assert dict_support.dict_to_string({"simple": "nest"}) == "simple::nest"
def test_deeper_dict_to_string():
assert dict_support.dict_to_string({"deeper": {"nested": "dict"}}) == "deeper::nested::dict"
def test_dataframe_to_dict_returns_dictionary_ignoring_nan_values(assert_semantically_equal):
df = DataFrame({"id": [1, 2, 3], "value": ["6", float("nan"), 5]})
assert_semantically_equal(
dict_support.dataframe_to_dict(df.T),
{0: {"id": 1, "value": "6"}, 1: {"id": 2}, 2: {"id": 3, "value": 5}},
)