Skip to content

Commit

Permalink
Add test helper function for posting to CKAN API
Browse files Browse the repository at this point in the history
Add ckan.tests.post(), a helper function for posting to CKAN's action API and
getting the result, that handles forming the correct action API URL, converting
the apikey to a string and putting it in a headers dict like TestApp expects,
collecting params into a dict and dumping them to a JSON string, and checking
the 'success' field of the response.

This can save a lot of lines of code and make tests more readable. I've
converted test_follow.py as an example of how much can be saved.

Similar savings could be made in many other test modules:

ckan/tests/functional/api/model/test_vocabulary.py,
ckan/tests/functional/api/model/test_group.py,
ckan/tests/functional/api/test_dashboard.py,
ckan/tests/functional/api/test_activity.py,
ckan/tests/functional/test_follow.py,
ckan/tests/functional/test_tag_vocab.py,
ckan/tests/functional/test_related.py,
ckan/tests/logic/test_action.py
ckan/tests/logic/test_tag.py.

ckan/tests/functional/api/model/test_package.py could also be changed to use
this function. It currently uses a similar function defined in
ckan.tests.functional.api.base:ApiTestCase.
  • Loading branch information
Sean Hammond authored and tobes committed Nov 29, 2012
1 parent 5301384 commit 3ea1816
Show file tree
Hide file tree
Showing 2 changed files with 352 additions and 605 deletions.
58 changes: 58 additions & 0 deletions ckan/tests/__init__.py
Expand Up @@ -400,3 +400,61 @@ class StatusCodes:
STATUS_404_NOT_FOUND = 404
STATUS_409_CONFLICT = 409


def post(app, action, apikey=None, status=200, **kwargs):
'''Post to the CKAN API and return the result.
Any additional keyword arguments that you pass to this function as **kwargs
are posted as params to the API.
Usage:
package_dict = post(app, 'package_create', apikey=apikey,
name='my_package')
assert package_dict['name'] == 'my_package'
num_followers = post(app, 'user_follower_count', id='annafan')
If you are expecting an error from the API and want to check the contents
of the error dict, you have to use the status param otherwise an exception
will be raised:
error_dict = post(app, 'group_activity_list', status=403,
id='invalid_id')
assert error_dict['message'] == 'Access Denied'
:param app: the test app to post to
:type app: paste.fixture.TestApp
:param action: the action to post to, e.g. 'package_create'
:type action: string
:param apikey: the API key to put in the Authorization header of the post
(optional, default: None)
:type apikey: string
:param status: the HTTP status code expected in the response from the CKAN
API, e.g. 403, if a different status code is received an exception will
be raised (optional, default: 200)
:type status: int
:param **kwargs: any other keyword arguments passed to this function will
be posted to the API as params
:raises paste.fixture.AppError: if the HTTP status code of the response
from the CKAN API is different from the status param passed to this
function
:returns: the 'result' or 'error' dictionary from the CKAN API response
:rtype: dictionary
'''
params = json.dumps(kwargs)
response = app.post('/api/action/{0}'.format(action), params=params,
extra_environ={'Authorization': str(apikey)}, status=status)
if status in (200,):
assert response.json['success'] is True
return response.json['result']
else:
assert response.json['success'] is False
return response.json['error']

0 comments on commit 3ea1816

Please sign in to comment.