|
10 | 10 |
|
11 | 11 |
|
12 | 12 | @pytest.mark.parametrize(
|
13 |
| - ["input_record", "field_pointers", "expected"], |
| 13 | + ["input_record", "field_pointers", "condition", "expected"], |
14 | 14 | [
|
15 |
| - pytest.param({"k1": "v", "k2": "v"}, [["k1"]], {"k2": "v"}, id="remove a field that exists (flat dict)"), |
16 |
| - pytest.param({"k1": "v", "k2": "v"}, [["k3"]], {"k1": "v", "k2": "v"}, id="remove a field that doesn't exist (flat dict)"), |
17 |
| - pytest.param({"k1": "v", "k2": "v"}, [["k1"], ["k2"]], {}, id="remove multiple fields that exist (flat dict)"), |
| 15 | + pytest.param({"k1": "v", "k2": "v"}, [["k1"]], None, {"k2": "v"}, id="remove a field that exists (flat dict), condition = None"), |
| 16 | + pytest.param({"k1": "v", "k2": "v"}, [["k1"]], "", {"k2": "v"}, id="remove a field that exists (flat dict)"), |
| 17 | + pytest.param({"k1": "v", "k2": "v"}, [["k3"]], "", {"k1": "v", "k2": "v"}, id="remove a field that doesn't exist (flat dict)"), |
| 18 | + pytest.param({"k1": "v", "k2": "v"}, [["k1"], ["k2"]], "", {}, id="remove multiple fields that exist (flat dict)"), |
18 | 19 | # TODO: should we instead splice the element out of the array? I think that's the more intuitive solution
|
19 | 20 | # Otherwise one could just set the field's value to null.
|
20 |
| - pytest.param({"k1": [1, 2]}, [["k1", 0]], {"k1": [None, 2]}, id="remove field inside array (int index)"), |
21 |
| - pytest.param({"k1": [1, 2]}, [["k1", "0"]], {"k1": [None, 2]}, id="remove field inside array (string index)"), |
| 21 | + pytest.param({"k1": [1, 2]}, [["k1", 0]], "", {"k1": [None, 2]}, id="remove field inside array (int index)"), |
| 22 | + pytest.param({"k1": [1, 2]}, [["k1", "0"]], "", {"k1": [None, 2]}, id="remove field inside array (string index)"), |
22 | 23 | pytest.param(
|
23 | 24 | {"k1": "v", "k2": "v", "k3": [0, 1], "k4": "v"},
|
24 | 25 | [["k1"], ["k2"], ["k3", 0]],
|
| 26 | + "", |
25 | 27 | {"k3": [None, 1], "k4": "v"},
|
26 | 28 | id="test all cases (flat)",
|
27 | 29 | ),
|
28 |
| - pytest.param({"k1": [0, 1]}, [[".", "k1", 10]], {"k1": [0, 1]}, id="remove array index that doesn't exist (flat)"), |
29 |
| - pytest.param({".": {"k1": [0, 1]}}, [[".", "k1", 10]], {".": {"k1": [0, 1]}}, id="remove array index that doesn't exist (nested)"), |
30 |
| - pytest.param({".": {"k2": "v", "k1": "v"}}, [[".", "k1"]], {".": {"k2": "v"}}, id="remove nested field that exists"), |
| 30 | + pytest.param({"k1": [0, 1]}, [[".", "k1", 10]], "", {"k1": [0, 1]}, id="remove array index that doesn't exist (flat)"), |
31 | 31 | pytest.param(
|
32 |
| - {".": {"k2": "v", "k1": "v"}}, [[".", "k3"]], {".": {"k2": "v", "k1": "v"}}, id="remove field that doesn't exist (nested)" |
| 32 | + {".": {"k1": [0, 1]}}, [[".", "k1", 10]], "", {".": {"k1": [0, 1]}}, id="remove array index that doesn't exist (nested)" |
33 | 33 | ),
|
34 |
| - pytest.param({".": {"k2": "v", "k1": "v"}}, [[".", "k1"], [".", "k2"]], {".": {}}, id="remove multiple fields that exist (nested)"), |
| 34 | + pytest.param({".": {"k2": "v", "k1": "v"}}, [[".", "k1"]], "", {".": {"k2": "v"}}, id="remove nested field that exists"), |
35 | 35 | pytest.param(
|
36 |
| - {".": {"k1": [0, 1]}}, [[".", "k1", 0]], {".": {"k1": [None, 1]}}, id="remove multiple fields that exist in arrays (nested)" |
| 36 | + {".": {"k2": "v", "k1": "v"}}, [[".", "k3"]], "", {".": {"k2": "v", "k1": "v"}}, id="remove field that doesn't exist (nested)" |
| 37 | + ), |
| 38 | + pytest.param( |
| 39 | + {".": {"k2": "v", "k1": "v"}}, [[".", "k1"], [".", "k2"]], "", {".": {}}, id="remove multiple fields that exist (nested)" |
| 40 | + ), |
| 41 | + pytest.param( |
| 42 | + {".": {"k1": [0, 1]}}, |
| 43 | + [[".", "k1", 0]], |
| 44 | + "", |
| 45 | + {".": {"k1": [None, 1]}}, |
| 46 | + id="remove multiple fields that exist in arrays (nested)", |
37 | 47 | ),
|
38 | 48 | pytest.param(
|
39 | 49 | {".": {"k1": [{"k2": "v", "k3": "v"}, {"k4": "v"}]}},
|
40 | 50 | [[".", "k1", 0, "k2"], [".", "k1", 1, "k4"]],
|
| 51 | + "", |
41 | 52 | {".": {"k1": [{"k3": "v"}, {}]}},
|
42 | 53 | id="remove fields that exist in arrays (deeply nested)",
|
43 | 54 | ),
|
| 55 | + pytest.param( |
| 56 | + {"k1": "v", "k2": "v"}, |
| 57 | + [["**"]], |
| 58 | + "{{ False }}", |
| 59 | + {"k1": "v", "k2": "v"}, |
| 60 | + id="do not remove any field if condition is boolean False", |
| 61 | + ), |
| 62 | + pytest.param({"k1": "v", "k2": "v"}, [["**"]], "{{ True }}", {}, id="remove all field if condition is boolean True"), |
| 63 | + pytest.param( |
| 64 | + {"k1": "v", "k2": "v1", "k3": "v1", "k4": {"k_nested": "v1", "k_nested2": "v2"}}, |
| 65 | + [["**"]], |
| 66 | + "{{ property == 'v1' }}", |
| 67 | + {"k1": "v", "k4": {"k_nested2": "v2"}}, |
| 68 | + id="recursively remove any field that matches property condition and leave that does not", |
| 69 | + ), |
| 70 | + pytest.param( |
| 71 | + {"k1": "v", "k2": "some_long_string", "k3": "some_long_string", "k4": {"k_nested": "v1", "k_nested2": "v2"}}, |
| 72 | + [["**"]], |
| 73 | + "{{ property|length > 5 }}", |
| 74 | + {"k1": "v", "k4": {"k_nested": "v1", "k_nested2": "v2"}}, |
| 75 | + id="remove any field that have length > 5 and leave that does not", |
| 76 | + ), |
| 77 | + pytest.param( |
| 78 | + {"k1": 255, "k2": "some_string", "k3": "some_long_string", "k4": {"k_nested": 123123, "k_nested2": "v2"}}, |
| 79 | + [["**"]], |
| 80 | + "{{ property is integer }}", |
| 81 | + {"k2": "some_string", "k3": "some_long_string", "k4": {"k_nested2": "v2"}}, |
| 82 | + id="recursively remove any field that of type integer and leave that does not", |
| 83 | + ), |
44 | 84 | ],
|
45 | 85 | )
|
46 |
| -def test_remove_fields(input_record: Mapping[str, Any], field_pointers: List[FieldPointer], expected: Mapping[str, Any]): |
47 |
| - transformation = RemoveFields(field_pointers=field_pointers, parameters={}) |
| 86 | +def test_remove_fields(input_record: Mapping[str, Any], field_pointers: List[FieldPointer], condition: str, expected: Mapping[str, Any]): |
| 87 | + transformation = RemoveFields(field_pointers=field_pointers, condition=condition, parameters={}) |
48 | 88 | assert transformation.transform(input_record) == expected
|
0 commit comments