Fix str2dict and dict2str's incorrect behavior

Closes-bug: #1258065

Change-Id: Idf14c077eeeda6f18f534ad8f62cd741d0c0a2eb
This commit is contained in:
Jianing Yang 2013-12-05 17:34:31 +08:00
parent 31e9e0d5ea
commit f294977c2f
2 changed files with 28 additions and 1 deletions

View File

@ -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

View File

@ -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")])