Remove unicode prefix from output of datasource-list

When we execute command 'congress datasource list', the output content
of 'config' column is string with unicode prefix, that make users
confusion, we should output the string format rather than unicode.

Change-Id: I26f46fb8eef3a89908ec388e2046218480ab560d
Closes-Bug: #1508320
This commit is contained in:
Rui Chen
2015-10-21 15:42:02 +08:00
parent f3e9d77cbf
commit 3efb993e40
3 changed files with 41 additions and 1 deletions

View File

@@ -71,6 +71,15 @@ def format_long_dict_list(data):
return ',\n'.join(newdata) + '\n'
def format_dict(data):
"""Return a formatted string.
:param data: a dict
:rtype: a string formatted to {a:b, c:d}
"""
return str({str(key): str(value) for key, value in data.items()})
def format_list(data):
"""Return a formatted strings

View File

@@ -38,7 +38,7 @@ class ListDatasources(lister.Lister):
client = self.app.client_manager.congressclient
data = client.list_datasources()['results']
columns = ['id', 'name', 'enabled', 'type', 'config']
formatters = {'Datasources': utils.format_list}
formatters = {'config': utils.format_dict}
return (columns,
(utils.get_dict_properties(s, columns,
formatters=formatters)

View File

@@ -43,6 +43,37 @@ class TestListDatasources(common.TestCongressBase):
self.assertEqual(['id', 'name', 'enabled', 'type', 'config'],
result[0])
def test_list_datasource_output_not_unicode(self):
# response json string is converted to dict by oslo jsonutils.loads(),
# so the key and value in the dict should be unicode type.
response = {
u"results": [{u"id": u"neutron",
u"name": u"my_name",
u"enabled": True,
u"type": None,
u"config": {
u'username': u'admin',
u'tenant_name': u'admin',
u'poll_time': u'10',
u'password': u'<hidden>',
u'auth_url': u'http://127.0.0.1:5000/v2.0'
}}]
}
lister = mock.Mock(return_value=response)
self.app.client_manager.congressclient.list_datasources = lister
cmd = datasource.ListDatasources(self.app, self.namespace)
parsed_args = self.check_parser(cmd, [], [])
result = cmd.take_action(parsed_args)
lister.assert_called_with()
self.assertEqual(['id', 'name', 'enabled', 'type', 'config'],
result[0])
# get 'config' column
config = list(result[1])[0][-1]
self.assertIn("'username': 'admin'", config)
self.assertNotIn("u'username': u'admin'", config)
class TestListDatasourceTables(common.TestCongressBase):
def test_list_datasource_tables(self):