Unit tests for pretty table special formatters.

Change-Id: If07b4a5fc0c4ae2eecf486433134864270757945
This commit is contained in:
Steve Baker 2013-06-27 15:09:15 +12:00
parent 7774ac3217
commit f535cf99d0
3 changed files with 53 additions and 19 deletions

View File

@ -13,11 +13,13 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import json
import os import os
import prettytable
import sys import sys
import textwrap
import uuid import uuid
import prettytable
from heatclient import exc from heatclient import exc
from heatclient.openstack.common import importutils from heatclient.openstack.common import importutils
@ -33,8 +35,16 @@ def arg(*args, **kwargs):
return _decorator return _decorator
def pretty_choice_list(l): def link_formatter(links):
return ', '.join("'%s'" % i for i in l) return '\n'.join([l.get('href', '') for l in links or []])
def json_formatter(js):
return json.dumps(js, indent=2)
def text_wrap_formatter(d):
return '\n'.join(textwrap.wrap(d or '', 55))
def print_list(objs, fields, field_labels=None, formatters={}, sortby=0): def print_list(objs, fields, field_labels=None, formatters={}, sortby=0):

View File

@ -49,3 +49,34 @@ class shellTest(testtools.TestCase):
params = 'KeyName=heat_key;UpstreamDNS8.8.8.8' params = 'KeyName=heat_key;UpstreamDNS8.8.8.8'
self.assertRaises(exc.MalformedRequestBody, self.assertRaises(exc.MalformedRequestBody,
utils.format_parameters, params) utils.format_parameters, params)
def test_link_formatter(self):
self.assertEqual('', utils.link_formatter(None))
self.assertEqual('', utils.link_formatter([]))
self.assertEqual(
'http://foo.example.com\nhttp://bar.example.com',
utils.link_formatter([
{'href': 'http://foo.example.com'},
{'href': 'http://bar.example.com'}]))
self.assertEqual(
'\n',
utils.link_formatter([
{'hrf': 'http://foo.example.com'},
{}]))
def test_json_formatter(self):
self.assertEqual('null', utils.json_formatter(None))
self.assertEqual('{}', utils.json_formatter({}))
self.assertEqual('{\n "foo": "bar"\n}',
utils.json_formatter({"foo": "bar"}))
def test_text_wrap_formatter(self):
self.assertEqual('', utils.text_wrap_formatter(None))
self.assertEqual('', utils.text_wrap_formatter(''))
self.assertEqual('one two three',
utils.text_wrap_formatter('one two three'))
self.assertEqual(
'one two three four five six seven eight nine ten eleven\ntwelve',
utils.text_wrap_formatter(
('one two three four five six seven '
'eight nine ten eleven twelve')))

View File

@ -14,7 +14,6 @@
# under the License. # under the License.
import json import json
import textwrap
import urllib2 import urllib2
import yaml import yaml
@ -183,16 +182,13 @@ def do_stack_show(hc, args):
except exc.HTTPNotFound: except exc.HTTPNotFound:
raise exc.CommandError('Stack not found: %s' % args.id) raise exc.CommandError('Stack not found: %s' % args.id)
else: else:
text_wrap = lambda d: '\n'.join(textwrap.wrap(d, 55))
link_format = lambda links: '\n'.join([l['href'] for l in links])
json_format = lambda js: json.dumps(js, indent=2)
formatters = { formatters = {
'description': text_wrap, 'description': utils.text_wrap_formatter,
'template_description': text_wrap, 'template_description': utils.text_wrap_formatter,
'stack_status_reason': text_wrap, 'stack_status_reason': utils.text_wrap_formatter,
'parameters': json_format, 'parameters': utils.json_formatter,
'outputs': json_format, 'outputs': utils.json_formatter,
'links': link_format 'links': utils.link_formatter
} }
utils.print_dict(stack.to_dict(), formatters=formatters) utils.print_dict(stack.to_dict(), formatters=formatters)
@ -341,9 +337,8 @@ def do_resource_show(hc, args):
raise exc.CommandError('Stack or resource not found: %s %s' % raise exc.CommandError('Stack or resource not found: %s %s' %
(args.id, args.resource)) (args.id, args.resource))
else: else:
link_format = lambda links: '\n'.join([l['href'] for l in links])
formatters = { formatters = {
'links': link_format 'links': utils.link_formatter,
} }
utils.print_dict(resource.to_dict(), formatters=formatters) utils.print_dict(resource.to_dict(), formatters=formatters)
@ -410,10 +405,8 @@ def do_event_show(hc, args):
except exc.HTTPNotFound: except exc.HTTPNotFound:
raise exc.CommandError('Stack not found: %s' % args.id) raise exc.CommandError('Stack not found: %s' % args.id)
else: else:
link_format = lambda links: '\n'.join([l['href'] for l in links])
json_format = lambda js: json.dumps(js, indent=2)
formatters = { formatters = {
'links': link_format, 'links': utils.link_formatter,
'resource_properties': json_format 'resource_properties': utils.json_formatter
} }
utils.print_dict(event.to_dict(), formatters=formatters) utils.print_dict(event.to_dict(), formatters=formatters)