Merge "Refactor merge_dicts helper function tests"
This commit is contained in:
commit
970d046f1a
@ -73,12 +73,7 @@ def evaluate(value, context, freeze=True):
|
||||
def merge_lists(list1, list2):
|
||||
result = []
|
||||
for item in list1 + list2:
|
||||
exists = False
|
||||
for old_item in result:
|
||||
if item == old_item:
|
||||
exists = True
|
||||
break
|
||||
if not exists:
|
||||
if item not in result:
|
||||
result.append(item)
|
||||
return result
|
||||
|
||||
|
@ -15,29 +15,43 @@ from murano.dsl import helpers
|
||||
from murano.tests.unit import base
|
||||
|
||||
|
||||
class TestHeatStack(base.MuranoTestCase):
|
||||
def test_merge_dicts(self):
|
||||
dict1 = {"resource": "test"}
|
||||
dict2 = {"description": "Test merge dicts"}
|
||||
class TestMergeDicts(base.MuranoTestCase):
|
||||
def check(self, dict1, dict2, expected):
|
||||
result = helpers.merge_dicts(dict1, dict2)
|
||||
expected = {"resource": "test",
|
||||
"description": "Test merge dicts"}
|
||||
self.assertEqual(expected, result)
|
||||
|
||||
dict2 = {"resource": None, "description": "Test merge dicts"}
|
||||
result = helpers.merge_dicts(dict1, dict2)
|
||||
expected = {"resource": "test",
|
||||
"description": "Test merge dicts"}
|
||||
self.assertEqual(expected, result)
|
||||
def test_dicts_plain(self):
|
||||
dict1 = {"a": "1"}
|
||||
dict2 = {"a": "100", "ab": "12"}
|
||||
expected = {"a": "100", "ab": "12"}
|
||||
self.check(dict1, dict2, expected)
|
||||
|
||||
dict2 = {"resource": "abc", "description": "Test merge dicts"}
|
||||
self.assertEqual(expected, result)
|
||||
def test_different_types_none(self):
|
||||
dict1 = {"a": "1"}
|
||||
dict2 = {"a": None, "ab": "12"}
|
||||
expected = {"a": "1", "ab": "12"}
|
||||
self.check(dict1, dict2, expected)
|
||||
|
||||
dict2 = {"resource": {"test": 1}}
|
||||
def test_different_types_of_iterable(self):
|
||||
dict1 = {"a": {"ab": "1"}}
|
||||
dict2 = {"a": ["ab", "1"]}
|
||||
self.assertRaises(TypeError, helpers.merge_dicts, dict1, dict2)
|
||||
|
||||
dict1 = {"resource": None}
|
||||
dict2 = {"resource": "test", "description": "Test merge dicts"}
|
||||
expected = {"resource": None, "description": "Test merge dicts"}
|
||||
result = helpers.merge_dicts(dict1, dict2)
|
||||
def test_merge_nested_dicts(self):
|
||||
dict1 = {"a": {"ab": {}, "abc": "1"}}
|
||||
dict2 = {"a": {"abc": "123"}}
|
||||
expected = {"a": {"ab": {}, "abc": "123"}}
|
||||
self.check(dict1, dict2, expected)
|
||||
|
||||
def test_merge_nested_dicts_with_max_levels(self):
|
||||
dict1 = {"a": {"ab": {"abcd": "1234"}, "abc": "1"}}
|
||||
dict2 = {"a": {"ab": {"y": "9"}, "abc": "123"}}
|
||||
expected = {"a": {"ab": {"y": "9"}, "abc": "123"}}
|
||||
result = helpers.merge_dicts(dict1, dict2, max_levels=2)
|
||||
self.assertEqual(expected, result)
|
||||
|
||||
def test_merge_with_lists(self):
|
||||
dict1 = {"a": [1, 2]}
|
||||
dict2 = {"a": [1, 3, 2, 4]}
|
||||
expected = {"a": [1, 2, 3, 4]}
|
||||
self.check(dict1, dict2, expected)
|
||||
|
Loading…
Reference in New Issue
Block a user