Skip to content

Commit

Permalink
Remove no longer applicable section on Flask migration docs
Browse files Browse the repository at this point in the history
No longer needed after #3782
  • Loading branch information
amercader committed Aug 31, 2017
1 parent 42d2f01 commit 0cbc17f
Showing 1 changed file with 0 additions and 84 deletions.
84 changes: 0 additions & 84 deletions doc/extensions/flask-migration.rst
Expand Up @@ -45,87 +45,3 @@ misconfigured.
package that provides wrappers for cross-version CKAN compatibility::

from ckantoolkit import config


------------------------------------------------------------------------------
Wrap ``url_for`` calls in tests with a test request context (``RuntimeError``)
------------------------------------------------------------------------------

Starting from CKAN 2.8 you might get the following exception when running the
tests::

RuntimeError: Attempted to generate a URL without the application context being
pushed. This has to be executed when application context is available.

Users familiar with Flask may recognize this exception. Basically the Flask
router (called internally by ``ckan.lib.helpers.url_for``) requires you to be
in the context on a web request when calling it (You can learn more about this
in the `Flask documentation <http://flask.pocoo.org/docs/testing/>`_). What this
means is that a test like this will raise the above exception::

from ckan.plugins.toolkit import url_for
from ckan.tests.helpers import FunctionalTestBase

class TestSearch(FunctionalTestBase):

def test_search_page_works(self):

app = self._get_test_app()
url = url_for(controller='package', action='search')
res = app.get(url)

To fix it, we need to wrap the ``url_for`` call with a test request context::

from ckan.plugins.toolkit import url_for
from ckan.tests.helpers import FunctionalTestBase

class TestSearch(FunctionalTestBase):

def test_search_page_works(self):

app = self._get_test_app()
with app.flask_app.test_request_context():
url = url_for(controller='package', action='search')
res = app.get(url)

If you are not extending ``FunctionalTestBase`` you can get an instance of the
test app client in order to create the test request context::

from ckan.plugins.toolkit import url_for
from ckan.tests.helpers import _get_test_app

class TestSearch(object):
def test_search_page_works(self):

app = _get_test_app()
with app.flask_app.test_request_context():
url = url_for(controller='package', action='search')
res = app.get(url)

Note that the call to ``url_for`` might not be explicitly done in the test
itself but rather internally. For instance consider the following example::


from ckan.tests.helpers import FunctionalTestBase

class TestDatastore(FunctionalTestBase):

def test_create_datastore_only_view(self):
# ...
# datastore_create will call ``url_for`` internally (or trigger
# something that calls it) so we need a Flask test context
with self.app.flask_app.test_request_context():
result = helpers.call_action('datastore_create', **data)

Or this one::

import ckan.lib.dictization.model_dictize as model_dictize
from ckan.tests.helpers import _get_test_app

class TestDictize(object):

def test_resource_dictize(self):
# Internally resource_dictize calls ``url_for`` so we need a test context
app = helpers._get_test_app()
with app.flask_app.test_request_context():
resource_dict = model_dictize.resource_dictize(...)

0 comments on commit 0cbc17f

Please sign in to comment.