Merge "Avoid 'NoneType' error when format conversion"

This commit is contained in:
Jenkins 2017-03-06 18:18:35 +00:00 committed by Gerrit Code Review
commit 62a17ed479
2 changed files with 10 additions and 0 deletions

View File

@ -498,11 +498,13 @@ class TestFindResource(test_utils.TestCase):
utils.format_dict({'a': 'b', 'c': 'd', 'e': 'f'})) utils.format_dict({'a': 'b', 'c': 'd', 'e': 'f'}))
self.assertEqual(expected, self.assertEqual(expected,
utils.format_dict({'e': 'f', 'c': 'd', 'a': 'b'})) utils.format_dict({'e': 'f', 'c': 'd', 'a': 'b'}))
self.assertEqual(None, utils.format_dict(None))
def test_format_list(self): def test_format_list(self):
expected = 'a, b, c' expected = 'a, b, c'
self.assertEqual(expected, utils.format_list(['a', 'b', 'c'])) self.assertEqual(expected, utils.format_list(['a', 'b', 'c']))
self.assertEqual(expected, utils.format_list(['c', 'b', 'a'])) self.assertEqual(expected, utils.format_list(['c', 'b', 'a']))
self.assertEqual(None, utils.format_list(None))
def test_format_list_of_dicts(self): def test_format_list_of_dicts(self):
expected = "a='b', c='d'\ne='f'" expected = "a='b', c='d'\ne='f'"
@ -512,6 +514,7 @@ class TestFindResource(test_utils.TestCase):
self.assertEqual(expected, utils.format_list_of_dicts(unsorted_data)) self.assertEqual(expected, utils.format_list_of_dicts(unsorted_data))
self.assertEqual('', utils.format_list_of_dicts([])) self.assertEqual('', utils.format_list_of_dicts([]))
self.assertEqual('', utils.format_list_of_dicts([{}])) self.assertEqual('', utils.format_list_of_dicts([{}]))
self.assertEqual(None, utils.format_list_of_dicts(None))
def test_format_list_separator(self): def test_format_list_separator(self):
expected = 'a\nb\nc' expected = 'a\nb\nc'

View File

@ -192,6 +192,9 @@ def format_dict(data):
:rtype: a string formatted to key='value' :rtype: a string formatted to key='value'
""" """
if data is None:
return None
output = "" output = ""
for s in sorted(data): for s in sorted(data):
output = output + s + "='" + six.text_type(data[s]) + "', " output = output + s + "='" + six.text_type(data[s]) + "', "
@ -205,6 +208,8 @@ def format_list(data, separator=', '):
:param separator: the separator to use between strings (default: ', ') :param separator: the separator to use between strings (default: ', ')
:rtype: a string formatted based on separator :rtype: a string formatted based on separator
""" """
if data is None:
return None
return separator.join(sorted(data)) return separator.join(sorted(data))
@ -215,6 +220,8 @@ def format_list_of_dicts(data):
:param data: a list of dicts :param data: a list of dicts
:rtype: a string formatted to key='value' with dicts separated by new line :rtype: a string formatted to key='value' with dicts separated by new line
""" """
if data is None:
return None
return '\n'.join(format_dict(i) for i in data) return '\n'.join(format_dict(i) for i in data)