Merge "Update cut_dict() to return no more than specified by length"

This commit is contained in:
Zuul 2018-04-04 01:05:28 +00:00 committed by Gerrit Code Review
commit f58df93bdc
2 changed files with 45 additions and 20 deletions
mistral
tests/unit/utils
utils

@ -157,15 +157,15 @@ class UtilsTest(base.BaseTest):
def test_cut_dict_with_strings(self):
d = {'key1': 'value1', 'key2': 'value2'}
s = utils.cut_dict(d, 9)
s = utils.cut_dict(d, 13)
self.assertIn(s, ["{'key1': '...", "{'key2': '..."])
s = utils.cut_dict(d, 13)
s = utils.cut_dict(d, 15)
self.assertIn(s, ["{'key1': 'va...", "{'key2': 'va..."])
s = utils.cut_dict(d, 19)
s = utils.cut_dict(d, 22)
self.assertIn(
s,
@ -183,14 +183,42 @@ class UtilsTest(base.BaseTest):
def test_cut_dict_with_digits(self):
d = {1: 2, 3: 4}
s = utils.cut_dict(d, 6)
s = utils.cut_dict(d, 10)
self.assertIn(s, ["{1: 2, ...", "{3: 4, ..."])
s = utils.cut_dict(d, 8)
s = utils.cut_dict(d, 11)
self.assertIn(s, ["{1: 2, 3...", "{3: 4, 1..."])
s = utils.cut_dict(d, 100)
self.assertIn(s, ["{1: 2, 3: 4}", "{3: 4, 1: 2}"])
def test_cut_dict_with_large_dict_of_str(self):
d = {}
for i in range(65535):
d[str(i)] = str(i)
s = utils.cut(d, 65535)
assert len(s) <= 65535, len(s)
def test_cut_dict_with_large_dict_of_int(self):
d = {}
for i in range(65535):
d[i] = i
s = utils.cut(d, 65535)
assert len(s) <= 65535, len(s)
def test_cut_dict_with_large_dict_of_dict(self):
d = {}
for i in range(65535):
d[i] = {'value': str(i)}
s = utils.cut(d, 65535)
assert len(s) <= 65535, len(s)
def test_cut_dict_for_state_info(self):
d = {}
for i in range(2000):
d[i] = {'value': 'This is a string that exceeds 35 characters'}
s = utils.cut(d, 65500)
assert len(s) <= 65500, str(len(s)) + ' : ' + s[:-30]

@ -216,44 +216,41 @@ def cut_dict(d, length=100):
v = str(value)
# Processing key.
new_len = len(res) + len(k)
new_len = len(k)
is_str = isinstance(key, str)
if is_str:
new_len += 2
if new_len >= length:
res += "'%s..." % k[:length - new_len] if is_str else "%s..." % k
new_len += 2 # Account for the quotation marks
if new_len + len(res) >= length:
res += "'%s" % k if is_str else k
break
else:
res += "'%s'" % k if is_str else k
res += ": "
res += ": "
# Processing value.
new_len = len(res) + len(v)
new_len = len(v)
is_str = isinstance(value, str)
if is_str:
new_len += 2
if new_len >= length:
res += "'%s..." % v[:length - new_len] if is_str else "%s..." % v
if new_len + len(res) >= length:
res += "'%s" % v if is_str else v
break
else:
res += "'%s'" % v if is_str else v
res += ', ' if idx < len(d) - 1 else '}'
if len(res) >= length:
res += '...'
break
res += ', ' if idx < len(d) - 1 else '}'
idx += 1
if len(res) >= length and res[length - 1] is not '}':
res = res[:length - 3] + '...'
return res