refactor(print_routes): Clean up test code and DRY traverse() (#1080)

This commit is contained in:
Kurt Griffiths 2017-07-07 08:03:43 -06:00 committed by John Vrbanac
parent 60fa5f0327
commit adc22e1a43
2 changed files with 26 additions and 21 deletions

View File

@ -55,15 +55,18 @@ def traverse(roots, parent='', verbose=False):
for method, func in root.method_map.items():
if func.__name__ != 'method_not_allowed':
if isinstance(func, partial):
print('-->{0} {1}:{2}'.format(
method,
inspect.getsourcefile(func.func),
inspect.getsourcelines(func.func)[1]))
real_func = func.func
else:
print('-->{0} {1}:{2}'.format(
method,
inspect.getsourcefile(func),
inspect.getsourcelines(func)[1]))
real_func = func
source_file = inspect.getsourcefile(real_func)
print('-->{0} {1}:{2}'.format(
method,
source_file,
source_file[1]
))
if root.children:
traverse(root.children, parent + '/' + root.raw_segment, verbose)

View File

@ -17,27 +17,29 @@ _api.add_route('/test', DummyResource())
def test_traverse_with_verbose():
"""Ensure traverse finds the proper routes and adds verbose output
for a method function as well as the OPTIONS partial."""
"""Ensure traverse() finds the proper routes and outputs verbose info."""
output = six.moves.StringIO()
with redirected(stdout=output):
print_routes.traverse(_api._router._roots, verbose=True)
route, method, options = output.getvalue().strip().split('\n')
route, get_info, options_info = output.getvalue().strip().split('\n')
assert '-> /test' == route
# Check in both methods and options for the GET method
# because method map is not ordered
assert 'GET' in method + options
if 'GET' in method:
assert 'OPTIONS' in options
assert 'falcon/responders.py:' in options
else:
assert 'OPTIONS' in method
assert 'falcon/responders.py:' in method
# NOTE(kgriffs) We might receive these in either order, since the
# method map is not ordered, so check and swap if necessary.
if options_info.startswith('-->GET'):
get_info, options_info = options_info, get_info
assert options_info.startswith('-->OPTIONS')
assert 'falcon/responders.py:' in options_info
assert get_info.startswith('-->GET')
assert 'tests/test_cmd_print_api.py:' in get_info
def test_traverse():
"""Ensure traverse finds the proper routes."""
"""Ensure traverse() finds the proper routes."""
output = six.moves.StringIO()
with redirected(stdout=output):
print_routes.traverse(_api._router._roots, verbose=False)