From f294977c2f35ae17532051a960fb1cbb959b47bd Mon Sep 17 00:00:00 2001 From: Jianing Yang Date: Thu, 5 Dec 2013 17:34:31 +0800 Subject: [PATCH] Fix str2dict and dict2str's incorrect behavior Closes-bug: #1258065 Change-Id: Idf14c077eeeda6f18f534ad8f62cd741d0c0a2eb --- neutron/common/utils.py | 2 +- neutron/tests/unit/test_common_utils.py | 27 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/neutron/common/utils.py b/neutron/common/utils.py index e72fcee14f..5e8cab756f 100644 --- a/neutron/common/utils.py +++ b/neutron/common/utils.py @@ -180,7 +180,7 @@ def dict2str(dic): def str2dict(string): res_dict = {} - for keyvalue in string.split(',', 1): + for keyvalue in string.split(','): (key, value) = keyvalue.split('=', 1) res_dict[key] = value return res_dict diff --git a/neutron/tests/unit/test_common_utils.py b/neutron/tests/unit/test_common_utils.py index 364d3ed210..3ea9ff0389 100644 --- a/neutron/tests/unit/test_common_utils.py +++ b/neutron/tests/unit/test_common_utils.py @@ -287,3 +287,30 @@ class TestParseVlanRangeList(UtilTestParseVlanRanges): (1000, 1099)], "net2": [(200, 299)]} self.assertEqual(self.parse_list(config_list), expected_networks) + + +class TestDictUtils(base.BaseTestCase): + def test_dict2str(self): + dic = {"key1": "value1", "key2": "value2", "key3": "value3"} + expected = "key1=value1,key2=value2,key3=value3" + self.assertEqual(utils.dict2str(dic), expected) + + def test_str2dict(self): + string = "key1=value1,key2=value2,key3=value3" + expected = {"key1": "value1", "key2": "value2", "key3": "value3"} + self.assertEqual(utils.str2dict(string), expected) + + def test_dict_str_conversion(self): + dic = {"key1": "value1", "key2": "value2"} + self.assertEqual(utils.str2dict(utils.dict2str(dic)), dic) + + def test_diff_list_of_dict(self): + old_list = [{"key1": "value1"}, + {"key2": "value2"}, + {"key3": "value3"}] + new_list = [{"key1": "value1"}, + {"key2": "value2"}, + {"key4": "value4"}] + added, removed = utils.diff_list_of_dict(old_list, new_list) + self.assertEqual(added, [dict(key4="value4")]) + self.assertEqual(removed, [dict(key3="value3")])