miguelgrinberg/api-pycon2014 — pull request #4 by kile (2014-11-27 08:59:04+00:00)
Title: Update test_client.py to handle exceptions

I'm trying to use your test suite for a project that uses flask-restful and sqlalchemy.
I've noticed that I'm getting correctly every http code from the server but I got stuck with the 404 from sqlalchemy.

My code looks something like:
```python
class UserAPI(restful.Resource):
def get(self, id):
user = User.query.get_or_404(id)
return user.to_json()
....
```

If I send a non-valid ```id``` test will just break without catching the correct error code as it call ```abort(404)``` and throws an exception making the app to fail:

```bash
ERROR: test_user (tests.test_api.TestAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/vagrant/serverside/tests/test_api.py", line 68, in test_user
rv, json = self.client.get('/user/%d' % user.id)
File "/vagrant/serverside/tests/test_client.py", line 31, in get
return self.send(url, 'GET', headers=headers)
File "/vagrant/serverside/tests/test_client.py", line 25, in send
rv = self.app.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 397, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 487, in dispatch_request
resp = meth(*args, **kwargs)
File "/vagrant/serverside/app/api/v1/patient.py", line 13, in get
patient = Patient.query.get_or_404(100)
File "/usr/local/lib/python2.7/dist-packages/flask_sqlalchemy/__init__.py", line 410, in get_or_404
abort(404)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/exceptions.py", line 576, in __call__
raise self.mapping[code](*args, **kwargs)
NotFound: 404: Not Found
```

I've added an ```try/catch``` to handle this exceptions by creating a ```ExceptionResponse``` class that will fill the ```status_code``` in order to get correctly in your testing suite.

I hope it will be useful for someone.