From 0cbc17f6484f3b2198d203b11eab0b67a83d0174 Mon Sep 17 00:00:00 2001 From: amercader Date: Thu, 31 Aug 2017 12:50:10 +0100 Subject: [PATCH] Remove no longer applicable section on Flask migration docs No longer needed after #3782 --- doc/extensions/flask-migration.rst | 84 ------------------------------ 1 file changed, 84 deletions(-) diff --git a/doc/extensions/flask-migration.rst b/doc/extensions/flask-migration.rst index 0df03344899..d535d5d4dfd 100644 --- a/doc/extensions/flask-migration.rst +++ b/doc/extensions/flask-migration.rst @@ -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 `_). 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(...)