From 5da8aad15bb150889621c1eb122202865ee21416 Mon Sep 17 00:00:00 2001 From: John Vrbanac Date: Thu, 25 May 2017 11:27:14 -0700 Subject: [PATCH] test: Clean up hook and print api tests (#1064) --- tests/test_after_hooks.py | 110 ++++++++++++++--------------- tests/test_before_hooks.py | 133 +++++++++++++++++++----------------- tests/test_cmd_print_api.py | 39 +++++------ 3 files changed, 146 insertions(+), 136 deletions(-) diff --git a/tests/test_after_hooks.py b/tests/test_after_hooks.py index 8281375..0105523 100644 --- a/tests/test_after_hooks.py +++ b/tests/test_after_hooks.py @@ -193,64 +193,66 @@ class ClassResourceWithAwareHooks(object): # -------------------------------------------------------------------- -class TestHooks(object): +def test_output_validator(client): + result = client.simulate_get() + assert result.status_code == 723 + assert result.text == json.dumps({'title': 'Tricky'}) - def test_output_validator(self, client): - result = client.simulate_get() - assert result.status_code == 723 - assert result.text == json.dumps({'title': 'Tricky'}) - def test_serializer(self, client): - result = client.simulate_put() - assert result.text == json.dumps({'animal': 'falcon'}) +def test_serializer(client): + result = client.simulate_put() + assert result.text == json.dumps({'animal': 'falcon'}) - def test_hook_as_callable_class(self, client): - result = client.simulate_post() - assert 'smart' == result.text - def test_wrapped_resource(self, client, wrapped_resource): - client.app.add_route('/wrapped', wrapped_resource) - result = client.simulate_get('/wrapped') +def test_hook_as_callable_class(client): + result = client.simulate_post() + assert 'smart' == result.text + + +def test_wrapped_resource(client, wrapped_resource): + client.app.add_route('/wrapped', wrapped_resource) + result = client.simulate_get('/wrapped') + assert result.status_code == 200 + assert result.text == 'fluffy and cute' + + result = client.simulate_head('/wrapped') + assert result.status_code == 200 + assert result.headers['X-Fluffiness'] == 'fluffy' + assert result.headers['X-Cuteness'] == 'cute' + + result = client.simulate_post('/wrapped') + assert result.status_code == 405 + + result = client.simulate_patch('/wrapped') + assert result.status_code == 405 + + # Decorator should not affect the default on_options responder + result = client.simulate_options('/wrapped') + assert result.status_code == 200 + assert not result.text + + +def test_wrapped_resource_with_hooks_aware_of_resource(client, wrapped_resource_aware): + client.app.add_route('/wrapped_aware', wrapped_resource_aware) + expected = 'fluffy and cute' + + result = client.simulate_get('/wrapped_aware') + assert result.status_code == 200 + assert expected == result.text + + for test in ( + client.simulate_head, + client.simulate_put, + client.simulate_post, + ): + result = test(path='/wrapped_aware') assert result.status_code == 200 - assert result.text == 'fluffy and cute' + assert wrapped_resource_aware.resp.body == expected - result = client.simulate_head('/wrapped') - assert result.status_code == 200 - assert result.headers['X-Fluffiness'] == 'fluffy' - assert result.headers['X-Cuteness'] == 'cute' + result = client.simulate_patch('/wrapped_aware') + assert result.status_code == 405 - result = client.simulate_post('/wrapped') - assert result.status_code == 405 - - result = client.simulate_patch('/wrapped') - assert result.status_code == 405 - - # Decorator should not affect the default on_options responder - result = client.simulate_options('/wrapped') - assert result.status_code == 200 - assert not result.text - - def test_wrapped_resource_with_hooks_aware_of_resource(self, client, wrapped_resource_aware): - client.app.add_route('/wrapped_aware', wrapped_resource_aware) - expected = 'fluffy and cute' - - result = client.simulate_get('/wrapped_aware') - assert result.status_code == 200 - assert expected == result.text - - for test in ( - client.simulate_head, - client.simulate_put, - client.simulate_post, - ): - result = test(path='/wrapped_aware') - assert result.status_code == 200 - assert wrapped_resource_aware.resp.body == expected - - result = client.simulate_patch('/wrapped_aware') - assert result.status_code == 405 - - # Decorator should not affect the default on_options responder - result = client.simulate_options('/wrapped_aware') - assert result.status_code == 200 - assert not result.text + # Decorator should not affect the default on_options responder + result = client.simulate_options('/wrapped_aware') + assert result.status_code == 200 + assert not result.text diff --git a/tests/test_before_hooks.py b/tests/test_before_hooks.py index 803b599..f82f3d5 100644 --- a/tests/test_before_hooks.py +++ b/tests/test_before_hooks.py @@ -77,11 +77,17 @@ def things_in_the_head(header, value, req, resp, resource, params): resp.set_header(header, value) -bunnies_in_the_head = functools.partial(things_in_the_head, - 'X-Bunnies', 'fluffy') +bunnies_in_the_head = functools.partial( + things_in_the_head, + 'X-Bunnies', + 'fluffy' +) -frogs_in_the_head = functools.partial(things_in_the_head, - 'X-Frogs', 'not fluffy') +frogs_in_the_head = functools.partial( + things_in_the_head, + 'X-Frogs', + 'not fluffy' +) class WrappedRespondersResource(object): @@ -206,80 +212,85 @@ def client(resource): return testing.TestClient(app) -class TestHooks(object): - def test_multiple_resource_hooks(self, client): - zoo_resource = ZooResource() - client.app.add_route('/', zoo_resource) +def test_multiple_resource_hooks(client): + zoo_resource = ZooResource() + client.app.add_route('/', zoo_resource) - result = client.simulate_get('/') + result = client.simulate_get('/') - assert 'not fluffy' == result.headers['X-Frogs'] - assert 'fluffy' == result.headers['X-Bunnies'] + assert 'not fluffy' == result.headers['X-Frogs'] + assert 'fluffy' == result.headers['X-Bunnies'] - assert 'fluffy' == zoo_resource.bunnies - assert 'not fluffy' == zoo_resource.frogs - assert 'slippery' == zoo_resource.fish + assert 'fluffy' == zoo_resource.bunnies + assert 'not fluffy' == zoo_resource.frogs + assert 'slippery' == zoo_resource.fish - def test_input_validator(self, client): - result = client.simulate_put('/') - assert result.status_code == 400 - def test_param_validator(self, client): - result = client.simulate_get('/', query_string='limit=10', body='{}') - assert result.status_code == 200 +def test_input_validator(client): + result = client.simulate_put('/') + assert result.status_code == 400 - result = client.simulate_get('/', query_string='limit=101') - assert result.status_code == 400 - def test_field_validator(self, client, field_resource): - client.app.add_route('/queue/{id}/messages', field_resource) - result = client.simulate_get('/queue/10/messages') - assert result.status_code == 200 - assert field_resource.id == 10 +def test_param_validator(client): + result = client.simulate_get('/', query_string='limit=10', body='{}') + assert result.status_code == 200 - result = client.simulate_get('/queue/bogus/messages') - assert result.status_code == 400 + result = client.simulate_get('/', query_string='limit=101') + assert result.status_code == 400 - def test_parser(self, client, resource): - client.simulate_get('/', body=json.dumps({'animal': 'falcon'})) - assert resource.doc == {'animal': 'falcon'} - def test_wrapped_resource(self, client, wrapped_resource): - client.app.add_route('/wrapped', wrapped_resource) - result = client.simulate_patch('/wrapped') - assert result.status_code == 405 +def test_field_validator(client, field_resource): + client.app.add_route('/queue/{id}/messages', field_resource) + result = client.simulate_get('/queue/10/messages') + assert result.status_code == 200 + assert field_resource.id == 10 - result = client.simulate_get('/wrapped', query_string='limit=10') - assert result.status_code == 200 - assert 'fuzzy' == wrapped_resource.bunnies + result = client.simulate_get('/queue/bogus/messages') + assert result.status_code == 400 - result = client.simulate_head('/wrapped') - assert result.status_code == 200 - assert 'fuzzy' == wrapped_resource.bunnies - result = client.simulate_post('/wrapped') - assert result.status_code == 200 - assert 'slippery' == wrapped_resource.fish +def test_parser(client, resource): + client.simulate_get('/', body=json.dumps({'animal': 'falcon'})) + assert resource.doc == {'animal': 'falcon'} - result = client.simulate_get('/wrapped', query_string='limit=101') - assert result.status_code == 400 - assert wrapped_resource.bunnies == 'fuzzy' - def test_wrapped_resource_with_hooks_aware_of_resource(self, client, wrapped_aware_resource): - client.app.add_route('/wrapped_aware', wrapped_aware_resource) +def test_wrapped_resource(client, wrapped_resource): + client.app.add_route('/wrapped', wrapped_resource) + result = client.simulate_patch('/wrapped') + assert result.status_code == 405 - result = client.simulate_patch('/wrapped_aware') - assert result.status_code == 405 + result = client.simulate_get('/wrapped', query_string='limit=10') + assert result.status_code == 200 + assert 'fuzzy' == wrapped_resource.bunnies - result = client.simulate_get('/wrapped_aware', query_string='limit=10') + result = client.simulate_head('/wrapped') + assert result.status_code == 200 + assert 'fuzzy' == wrapped_resource.bunnies + + result = client.simulate_post('/wrapped') + assert result.status_code == 200 + assert 'slippery' == wrapped_resource.fish + + result = client.simulate_get('/wrapped', query_string='limit=101') + assert result.status_code == 400 + assert wrapped_resource.bunnies == 'fuzzy' + + +def test_wrapped_resource_with_hooks_aware_of_resource(client, wrapped_aware_resource): + client.app.add_route('/wrapped_aware', wrapped_aware_resource) + + result = client.simulate_patch('/wrapped_aware') + assert result.status_code == 405 + + result = client.simulate_get('/wrapped_aware', query_string='limit=10') + assert result.status_code == 200 + assert wrapped_aware_resource.bunnies == 'fuzzy' + + for method in ('HEAD', 'PUT', 'POST'): + result = client.simulate_request(method, '/wrapped_aware') assert result.status_code == 200 assert wrapped_aware_resource.bunnies == 'fuzzy' - for method in ('HEAD', 'PUT', 'POST'): - result = client.simulate_request(method, '/wrapped_aware') - assert result.status_code == 200 - assert wrapped_aware_resource.bunnies == 'fuzzy' - - result = client.simulate_get('/wrapped_aware', query_string='limit=101') - assert result.status_code == 400 - assert wrapped_aware_resource.bunnies == 'fuzzy' + result = client.simulate_get('/wrapped_aware', query_string='limit=101') + assert result.status_code == 400 + assert wrapped_aware_resource.bunnies == 'fuzzy' diff --git a/tests/test_cmd_print_api.py b/tests/test_cmd_print_api.py index c4d31ad..5665204 100644 --- a/tests/test_cmd_print_api.py +++ b/tests/test_cmd_print_api.py @@ -1,7 +1,4 @@ -try: - from StringIO import StringIO -except ImportError: - from io import StringIO +import six from falcon import API from falcon.cmd import print_routes @@ -12,23 +9,23 @@ _api = API() _api.add_route('/test', None) -class TestPrintRoutes(object): - def test_traverse_with_verbose(self): - """Ensure traverse finds the proper routes and adds verbose output.""" - output = StringIO() - with redirected(stdout=output): - print_routes.traverse(_api._router._roots, verbose=True) +def test_traverse_with_verbose(): + """Ensure traverse finds the proper routes and adds verbose output.""" + output = six.moves.StringIO() + with redirected(stdout=output): + print_routes.traverse(_api._router._roots, verbose=True) - route, options = output.getvalue().strip().split('\n') - assert '-> /test' == route - assert 'OPTIONS' in options - assert 'falcon/responders.py:' in options + route, options = output.getvalue().strip().split('\n') + assert '-> /test' == route + assert 'OPTIONS' in options + assert 'falcon/responders.py:' in options - def test_traverse(self): - """Ensure traverse finds the proper routes.""" - output = StringIO() - with redirected(stdout=output): - print_routes.traverse(_api._router._roots, verbose=False) - route = output.getvalue().strip() - assert '-> /test' == route +def test_traverse(): + """Ensure traverse finds the proper routes.""" + output = six.moves.StringIO() + with redirected(stdout=output): + print_routes.traverse(_api._router._roots, verbose=False) + + route = output.getvalue().strip() + assert '-> /test' == route