okfn/ckan — pull request #189 by seanh (2012-11-27 21:37:22+00:00)
Title: Add shared post method for api tests

This has been bothering me for a while. Whenever I implement a new feature and write API tests for it, I'm writing repetitive code to collect params into a dict, dump the dict to a JSON string, format an action API URL, convert an API key into a string and put it into an extra_environ dict, post this to the API, get the JSON out of the response, check that 'success' is True, and finally get the 'result' or 'error' dict out of the JSON. Many API test modules either do this repeatedly or define their own helper functions to do it.

This branch adds a `ckan.tests.post()` helper function that any tests can use for this.

Maybe it should be called `action_post()` instead might be clearer.

I've converted `test_follow.py` as an example, Many other test modules could be converted to use this function, saving a lot of lines of code and making tests mode readable. Unfortunately actually converting test modules that make a lot of posts to the API takes ages.

But at least now when we write new tests we can use this.

If this function could be changed to make an actual HTTP request (e.g. using `urllib2`) instead of using paste's TestApp, then users could also import it and use it to post to the APIs of production CKAN instances (or development instances for interactive testing) from Python. Then it could be wrapped in a paster command that accepts and returns JSON, so it can be used from the command-line as well. It might mostly replace ckanclient. See https://gist.github.com/4130567