Skip to content

Commit aa0bfc1

Browse files
authored
Merge pull request #59 from release-engineering/mergefix
Bugfix: Merge inner dictionary values
2 parents 6384d4b + 1b0e915 commit aa0bfc1

7 files changed

Lines changed: 218 additions & 1 deletion

File tree

starmap_client/utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,16 @@ def dict_merge(a: Dict[str, Any], b: Dict[str, Any]) -> Dict[str, Any]:
2020
"""
2121
for x in [a, b]:
2222
assert_is_dict(x)
23+
24+
# Process the inner values before merging
25+
for k, v in a.items():
26+
# Merge two inner dictionaries
27+
if b.get(k) and all([isinstance(x, dict) for x in [v, b.get(k)]]):
28+
b[k] = dict_merge(v, b[k])
29+
30+
# Merge left inner dictionary
31+
elif isinstance(v, dict) and not b.get(k):
32+
b[k] = dict_merge(v, {})
33+
34+
# Default merge of dictionaries
2335
return a | b
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"meta": {
3+
"mapping": "key",
4+
"combined": {
5+
"mapping": "mapping"
6+
},
7+
"merged": {
8+
"name": "mapping"
9+
}
10+
},
11+
"destinations": [
12+
{
13+
"architecture": "x86_64",
14+
"destination": "fake_destination",
15+
"overwrite": false,
16+
"restrict_version": true,
17+
"meta": {
18+
"destination": "key",
19+
"combined": {
20+
"destination": "destination"
21+
},
22+
"merged": {
23+
"name": "destination"
24+
}
25+
}
26+
}
27+
]
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"destination": "key",
3+
"combined": {
4+
"mapping": "mapping",
5+
"destination": "destination"
6+
},
7+
"mapping": "key",
8+
"merged": {
9+
"name": "destination"
10+
}
11+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"cloud": "test",
3+
"meta": {
4+
"global": "key",
5+
"combined": {
6+
"package": "package"
7+
},
8+
"merged": {
9+
"name": "package"
10+
}
11+
},
12+
"mappings": {
13+
"test-marketplace": {
14+
"destinations": [
15+
{
16+
"destination": "test-destination/foo/bar",
17+
"overwrite": false,
18+
"restrict_version": false,
19+
"meta": {
20+
"destination": "key",
21+
"combined": {
22+
"destination": "destination"
23+
},
24+
"merged": {
25+
"name": "destination"
26+
}
27+
}
28+
},
29+
{
30+
"destination": "second-test-destination/foo/bar",
31+
"overwrite": true,
32+
"restrict_version": false,
33+
"meta": {
34+
"destination": "key",
35+
"combined": {
36+
"destination": "destination"
37+
},
38+
"merged": {
39+
"name": "destination"
40+
}
41+
}
42+
}
43+
],
44+
"provider": null,
45+
"meta": {
46+
"mapping": "key",
47+
"combined": {
48+
"mapping": "mapping"
49+
},
50+
"merged": {
51+
"name": "mapping"
52+
}
53+
}
54+
},
55+
"another-marketplace": {
56+
"destinations": [
57+
{
58+
"destination": "aaaaaaaaaaaaaaa",
59+
"overwrite": false,
60+
"restrict_version": false,
61+
"meta": {
62+
"destination": "key",
63+
"combined": {
64+
"destination": "destination"
65+
},
66+
"merged": {
67+
"name": "destination"
68+
},
69+
"last": {
70+
"test": true
71+
}
72+
}
73+
},
74+
{
75+
"destination": "bbbbbbbbbbbbb",
76+
"overwrite": true,
77+
"restrict_version": false,
78+
"meta": {
79+
"destination": "key",
80+
"combined": {
81+
"destination": "destination"
82+
},
83+
"merged": {
84+
"name": "destination"
85+
},
86+
"last": {
87+
"test": true
88+
}
89+
}
90+
}
91+
],
92+
"provider": null,
93+
"meta": {
94+
"mapping": "key",
95+
"combined": {
96+
"mapping": "mapping"
97+
},
98+
"merged": {
99+
"name": "mapping"
100+
},
101+
"another": {
102+
"foo": "bar"
103+
}
104+
}
105+
}
106+
},
107+
"workflow": "stratosphere",
108+
"name": "sample-product"
109+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"test-marketplace": {
3+
"destination": "key",
4+
"combined": {
5+
"package": "package",
6+
"mapping": "mapping",
7+
"destination": "destination"
8+
},
9+
"global": "key",
10+
"mapping": "key",
11+
"merged": {
12+
"name": "destination"
13+
}
14+
},
15+
"another-marketplace": {
16+
"destination": "key",
17+
"combined": {
18+
"package": "package",
19+
"mapping": "mapping",
20+
"destination": "destination"
21+
},
22+
"global": "key",
23+
"mapping": "key",
24+
"merged": {
25+
"name": "destination"
26+
},
27+
"another": {
28+
"foo": "bar"
29+
},
30+
"last": {
31+
"test": true
32+
}
33+
}
34+
}

tests/test_models.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ class TestV2MappingResponseObject:
262262
"tests/data/query_v2/mapping_response_obj/valid_mro3_meta.json",
263263
None,
264264
),
265+
(
266+
"tests/data/query_v2/mapping_response_obj/valid_mro4.json",
267+
"tests/data/query_v2/mapping_response_obj/valid_mro4_meta.json",
268+
None,
269+
),
265270
],
266271
)
267272
def test_valid_mapping_response_obj(self, json_file, meta, provider) -> None:
@@ -310,6 +315,10 @@ class TestV2QueryResponseEntity:
310315
"tests/data/query_v2/query_response_entity/valid_qre4.json",
311316
"tests/data/query_v2/query_response_entity/valid_qre4_meta.json",
312317
),
318+
(
319+
"tests/data/query_v2/query_response_entity/valid_qre5.json",
320+
"tests/data/query_v2/query_response_entity/valid_qre5_meta.json",
321+
),
313322
],
314323
)
315324
def test_valid_query_response_entity(self, json_file, meta) -> None:
@@ -318,9 +327,13 @@ def test_valid_query_response_entity(self, json_file, meta) -> None:
318327
expected_meta_dict = load_json(meta)
319328

320329
q = QueryResponseEntity.from_json(data)
330+
331+
# Test the merged meta attributes on destinations
321332
for account_name in q.account_names:
322-
assert q.mappings[account_name].meta == expected_meta_dict[account_name]
333+
for dest in q.mappings[account_name].destinations:
334+
assert dest.meta == expected_meta_dict[account_name]
323335

336+
# Test the billing_code_config
324337
if q.billing_code_config:
325338
bc_asdict = {k: asdict(v) for k, v in q.billing_code_config.items()}
326339
assert bc_asdict == d["billing-code-config"]

tests/test_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ def test_assert_is_dict() -> None:
2424
({"1": 1, "3": 3}, {"2": 2}, {"1": 1, "2": 2, "3": 3}),
2525
({"1": 1, "3": 3}, {"2": 2, "3": 4}, {"1": 1, "2": 2, "3": 4}),
2626
({"A": True, "B": True, "C": True}, {"B": False}, {"A": True, "B": False, "C": True}),
27+
(
28+
{"dic1": {"foo": "bar"}, "dic2": {"key": "value"}},
29+
{"dic1": {"bar": "foo"}},
30+
{"dic1": {"foo": "bar", "bar": "foo"}, "dic2": {"key": "value"}},
31+
),
32+
(
33+
{"dic1": {"foo": "bar"}},
34+
{"dic1": {"bar": "foo"}, "dic2": {"key": "value"}},
35+
{"dic1": {"foo": "bar", "bar": "foo"}, "dic2": {"key": "value"}},
36+
),
2737
],
2838
)
2939
def test_dict_merge(a: Dict[str, Any], b: Dict[str, Any], expected: Dict[str, Any]) -> None:

0 commit comments

Comments
 (0)