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(): for method, func in root.method_map.items():
if func.__name__ != 'method_not_allowed': if func.__name__ != 'method_not_allowed':
if isinstance(func, partial): if isinstance(func, partial):
print('-->{0} {1}:{2}'.format( real_func = func.func
method,
inspect.getsourcefile(func.func),
inspect.getsourcelines(func.func)[1]))
else: else:
print('-->{0} {1}:{2}'.format( real_func = func
method,
inspect.getsourcefile(func), source_file = inspect.getsourcefile(real_func)
inspect.getsourcelines(func)[1]))
print('-->{0} {1}:{2}'.format(
method,
source_file,
source_file[1]
))
if root.children: if root.children:
traverse(root.children, parent + '/' + root.raw_segment, verbose) traverse(root.children, parent + '/' + root.raw_segment, verbose)

View File

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