|
8 | 8 | FlattenFields,
|
9 | 9 | )
|
10 | 10 |
|
| 11 | +_FLATTEN_LISTS = True |
| 12 | +_DO_NOT_FLATTEN_LISTS = False |
| 13 | + |
11 | 14 |
|
12 | 15 | @pytest.mark.parametrize(
|
13 |
| - "input_record, expected_output", |
| 16 | + "flatten_lists, input_record, expected_output", |
14 | 17 | [
|
15 |
| - ({"FirstName": "John", "LastName": "Doe"}, {"FirstName": "John", "LastName": "Doe"}), |
16 |
| - ({"123Number": 123, "456Another123": 456}, {"123Number": 123, "456Another123": 456}), |
17 |
| - ( |
| 18 | + pytest.param( |
| 19 | + _FLATTEN_LISTS, |
| 20 | + {"FirstName": "John", "LastName": "Doe"}, |
| 21 | + {"FirstName": "John", "LastName": "Doe"}, |
| 22 | + id="flatten simple record with string values", |
| 23 | + ), |
| 24 | + pytest.param( |
| 25 | + _FLATTEN_LISTS, |
| 26 | + {"123Number": 123, "456Another123": 456}, |
| 27 | + {"123Number": 123, "456Another123": 456}, |
| 28 | + id="flatten simple record with int values", |
| 29 | + ), |
| 30 | + pytest.param( |
| 31 | + _FLATTEN_LISTS, |
18 | 32 | {
|
19 | 33 | "NestedRecord": {"FirstName": "John", "LastName": "Doe"},
|
20 | 34 | "456Another123": 456,
|
|
24 | 38 | "LastName": "Doe",
|
25 | 39 | "456Another123": 456,
|
26 | 40 | },
|
| 41 | + id="flatten record with nested dict", |
27 | 42 | ),
|
28 |
| - ( |
| 43 | + pytest.param( |
| 44 | + _FLATTEN_LISTS, |
29 | 45 | {"ListExample": [{"A": "a"}, {"A": "b"}]},
|
30 | 46 | {"ListExample.0.A": "a", "ListExample.1.A": "b"},
|
| 47 | + id="flatten record with list values of dict items", |
31 | 48 | ),
|
32 |
| - ( |
| 49 | + pytest.param( |
| 50 | + _FLATTEN_LISTS, |
33 | 51 | {
|
34 | 52 | "MixedCase123": {
|
35 | 53 | "Nested": [{"Key": {"Value": "test1"}}, {"Key": {"Value": "test2"}}]
|
|
41 | 59 | "Nested.1.Key.Value": "test2",
|
42 | 60 | "SimpleKey": "SimpleValue",
|
43 | 61 | },
|
| 62 | + id="flatten record with nested dict of both list and string values", |
44 | 63 | ),
|
45 |
| - ( |
| 64 | + pytest.param( |
| 65 | + _FLATTEN_LISTS, |
46 | 66 | {"List": ["Item1", "Item2", "Item3"]},
|
47 | 67 | {"List.0": "Item1", "List.1": "Item2", "List.2": "Item3"},
|
| 68 | + id="flatten record with list of str values", |
| 69 | + ), |
| 70 | + pytest.param( |
| 71 | + _DO_NOT_FLATTEN_LISTS, |
| 72 | + {"List": ["Item1", "Item2", "Item3"]}, |
| 73 | + {"List": ["Item1", "Item2", "Item3"]}, |
| 74 | + id="flatten record with dict of list values, flatten_lists=False", |
| 75 | + ), |
| 76 | + pytest.param( |
| 77 | + _DO_NOT_FLATTEN_LISTS, |
| 78 | + { |
| 79 | + "RootField": { |
| 80 | + "NestedList": [{"Key": {"Value": "test1"}}, {"Key": {"Value": "test2"}}] |
| 81 | + }, |
| 82 | + "SimpleKey": "SimpleValue", |
| 83 | + }, |
| 84 | + { |
| 85 | + "NestedList": [{"Key": {"Value": "test1"}}, {"Key": {"Value": "test2"}}], |
| 86 | + "SimpleKey": "SimpleValue", |
| 87 | + }, |
| 88 | + id="flatten record with dict of list values and simple key, flatten_lists=False", |
| 89 | + ), |
| 90 | + pytest.param( |
| 91 | + _DO_NOT_FLATTEN_LISTS, |
| 92 | + { |
| 93 | + "RootField": {"List": [{"Key": {"Value": "test1"}}, {"Key": {"Value": "test2"}}]}, |
| 94 | + "List": [1, 3, 6], |
| 95 | + "SimpleKey": "SimpleValue", |
| 96 | + }, |
| 97 | + { |
| 98 | + "RootField.List": [{"Key": {"Value": "test1"}}, {"Key": {"Value": "test2"}}], |
| 99 | + "List": [1, 3, 6], |
| 100 | + "SimpleKey": "SimpleValue", |
| 101 | + }, |
| 102 | + id="flatten record with dict of list values and simple key with duplicated keys, flatten_lists=False", |
48 | 103 | ),
|
49 | 104 | ],
|
50 | 105 | )
|
51 |
| -def test_flatten_fields(input_record, expected_output): |
52 |
| - flattener = FlattenFields() |
| 106 | +def test_flatten_fields(flatten_lists, input_record, expected_output): |
| 107 | + flattener = FlattenFields(flatten_lists=flatten_lists) |
53 | 108 | flattener.transform(input_record)
|
54 | 109 | assert input_record == expected_output
|
0 commit comments