forked from canvas-medical/pydian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mapper.py
193 lines (154 loc) · 5.75 KB
/
test_mapper.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from typing import Any
import pytest
from pydian import Mapper, get
from pydian.lib.types import DROP, KEEP
def test_drop(simple_data: dict[str, Any]) -> None:
source = simple_data
def mapping(d: dict[str, Any]) -> dict[str, Any]:
return {
"CASE_parent_keep": {
"CASE_curr_drop": {
"a": DROP.THIS_OBJECT,
"b": "someValue",
},
"CASE_curr_keep": {"id": get(d, "data.patient.id")},
},
"CASE_list": [DROP.THIS_OBJECT],
"CASE_list_of_objects": [
{"a": DROP.PARENT, "b": "someValue"},
{"a": "someValue", "b": "someValue"},
],
}
mapper = Mapper(mapping, remove_empty=True)
res = mapper(source)
assert res == {"CASE_parent_keep": {"CASE_curr_keep": {"id": get(source, "data.patient.id")}}}
def test_drop_out_of_bounds() -> None:
source: dict[str, Any] = {}
def mapping(_: dict[str, Any]) -> dict[str, Any]:
return {"parent": {"CASE_no_grandparent": DROP.GREATGRANDPARENT}}
mapper = Mapper(mapping)
with pytest.raises(RuntimeError):
_ = mapper(source)
def test_drop_exact_level() -> None:
source: dict[str, Any] = {}
def mapping(_: dict[str, Any]) -> dict[str, Any]:
return {
"parent": {"CASE_has_parent_object": DROP.PARENT},
"other_data": 123,
}
mapper = Mapper(mapping)
res = mapper(source)
assert res == {}
def test_drop_repeat() -> None:
source: dict[str, Any] = {}
def mapping(_: dict[str, Any]) -> dict[str, Any]:
return {
"dropped_direct": [DROP.THIS_OBJECT, DROP.THIS_OBJECT],
"also_dropped": [{"parent_key": DROP.PARENT}, DROP.THIS_OBJECT],
"partially_dropped": [
"first_kept",
{"second_dropped": DROP.THIS_OBJECT},
"third_kept",
{"fourth_dropped": DROP.THIS_OBJECT},
],
}
mapper = Mapper(mapping)
res = mapper(source)
assert res == {"partially_dropped": ["first_kept", "third_kept"]}
def test_keep_empty_value() -> None:
source: dict[str, Any] = {}
def mapping(_: dict[str, Any]) -> dict[str, Any]:
return {
"empty_vals": [KEEP({}), KEEP([]), KEEP(""), KEEP(None)],
"nested_vals": {
"dict": KEEP({}),
"list": KEEP([]),
"str": KEEP(""),
"none": KEEP(None),
"other_static_val": "Abc",
},
"static_val": "Def",
"empty_list": KEEP([]),
"removed_empty_list": [],
}
mapper = Mapper(mapping)
res = mapper(source)
assert KEEP({}).value == dict()
assert KEEP([]).value == list()
assert KEEP("").value == ""
assert KEEP(None).value == None
assert res == {
"empty_vals": [{}, [], "", None],
"nested_vals": {"dict": {}, "list": [], "str": "", "none": None, "other_static_val": "Abc"},
"static_val": "Def",
"empty_list": [],
}
def test_strict(simple_data: dict[str, Any]) -> None:
source = simple_data
# Test `strict` flag (independent of `get`)
def mapping(d: dict[str, Any]) -> dict[str, Any]:
return {
"CASE_parent_keep": {
"CASE_curr_drop": {
"a": DROP.THIS_OBJECT,
"b": "someValue",
},
"CASE_curr_keep": {"id": get(d, "data.patient.id")},
},
"CASE_missing": get(d, "key.nope.not.there"),
}
strict_mapper = Mapper(mapping, strict=True)
with pytest.raises(ValueError) as exc_info:
strict_mapper(source)
mapper = Mapper(mapping, strict=False)
assert mapper(source) == {
"CASE_parent_keep": {"CASE_curr_keep": {"id": get(source, "data.patient.id")}}
}
# Test `strict` flag with specific `get` calls
def strict_get_mapping(d: dict[str, Any]) -> dict[str, Any]:
return {
"CASE_parent_keep": {
"CASE_curr_drop": {
"a": DROP.THIS_OBJECT,
"b": "someValue",
},
"CASE_curr_keep": {"id": get(d, "data.patient.id")},
},
"CASE_missing": get(d, "key.nope.not.there"),
}
strict_get_mapper = Mapper(strict_get_mapping, strict=True)
get_mapper = Mapper(strict_get_mapping, strict=False)
with pytest.raises(ValueError) as exc_info:
get_mapper(source)
strict_get_mapper(source)
def test_script_deliberate_none() -> None:
source = {
"has_None": None,
"nested_None": {"has_None": None, "has_value": "value"},
"nested_list_None": {
"some_list": [
{"has_None": None},
"value",
None,
],
},
}
def mapping_success(d: dict[str, Any]) -> dict[str, Any]:
return {
"CASE_keep_None": get(d, "has_None"),
"CASE_keep_None_nested": get(d, "nested_None.has_None"),
"CASE_keep_None_list": get(d, "nested_list_None.some_list[-1]"),
"CASE_keep_None_list_nested": get(d, "nested_list_None.some_list[0].has_None"),
}
mapper = Mapper(mapping_success, strict=True, remove_empty=False)
assert mapper(source) == {
"CASE_keep_None": None,
"CASE_keep_None_nested": None,
"CASE_keep_None_list": None,
"CASE_keep_None_list_nested": None,
}
def mapping_err(d: dict[str, Any]) -> dict[str, Any]:
return {"CASE_keep_None": get(d, "has_None"), "CASE_throw_err": get(d, "key.not.found")}
err_mapper = Mapper(mapping_err, strict=True)
with pytest.raises(ValueError) as exc_info:
err_mapper(source)