Skip to content

Commit

Permalink
Request context fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Dec 25, 2019
1 parent b719e63 commit dcd3019
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
11 changes: 11 additions & 0 deletions ckan/tests/pytest_ckan/fixtures.py
Expand Up @@ -187,3 +187,14 @@ def client(app):
return None
flask_app = app.app._wsgi_app
return flask_app.test_client()


@pytest.fixture
def test_request_context(app):
"""Provide function for creating Flask request context.
"""
try:
flask_app = app.app._wsgi_app
except AttributeError:
flask_app = app.app.apps['flask_app']._wsgi_app
return flask_app.test_request_context
31 changes: 16 additions & 15 deletions ckanext/datapusher/tests/test.py
Expand Up @@ -21,17 +21,18 @@ class TestDatastoreCreate(object):
normal_user = None

@pytest.fixture(autouse=True)
def initial_data(self, clean_db, clean_index):
def initial_data(self, clean_db, clean_index, test_request_context):
if not tests.is_datastore_supported():
pytest.skip("Datastore not supported")
ctd.CreateTestData.create()
self.sysadmin_user = model.User.get("testsysadmin")
self.normal_user = model.User.get("annafan")
engine = db.get_write_engine()
self.Session = orm.scoped_session(orm.sessionmaker(bind=engine))
set_url_type(
model.Package.get("annakarenina").resources, self.sysadmin_user
)
with test_request_context():
set_url_type(
model.Package.get("annakarenina").resources, self.sysadmin_user
)

@pytest.mark.ckan_config("ckan.plugins", "datastore datapusher")
@pytest.mark.usefixtures("with_plugins")
Expand Down Expand Up @@ -136,7 +137,7 @@ def test_cant_provide_resource_and_resource_id(self, app):
@responses.activate
@pytest.mark.ckan_config("ckan.plugins", "datastore datapusher")
@pytest.mark.usefixtures("with_plugins")
def test_send_datapusher_creates_task(self):
def test_send_datapusher_creates_task(self, test_request_context):
responses.add(
responses.POST,
"http://datapusher.ckan.org/job",
Expand All @@ -148,10 +149,10 @@ def test_send_datapusher_creates_task(self):
resource = package.resources[0]

context = {"ignore_auth": True, "user": self.sysadmin_user.name}

p.toolkit.get_action("datapusher_submit")(
context, {"resource_id": resource.id}
)
with test_request_context():
p.toolkit.get_action("datapusher_submit")(
context, {"resource_id": resource.id}
)

context.pop("task_status", None)

Expand Down Expand Up @@ -220,15 +221,15 @@ def _call_datapusher_hook(self, user, app):

@pytest.mark.ckan_config("ckan.plugins", "datastore datapusher")
@pytest.mark.usefixtures("with_plugins")
def test_datapusher_hook_sysadmin(self, app):

self._call_datapusher_hook(self.sysadmin_user, app)
def test_datapusher_hook_sysadmin(self, app, test_request_context):
with test_request_context():
self._call_datapusher_hook(self.sysadmin_user, app)

@pytest.mark.ckan_config("ckan.plugins", "datastore datapusher")
@pytest.mark.usefixtures("with_plugins")
def test_datapusher_hook_normal_user(self, app):

self._call_datapusher_hook(self.normal_user, app)
def test_datapusher_hook_normal_user(self, app, test_request_context):
with test_request_context():
self._call_datapusher_hook(self.normal_user, app)

@pytest.mark.ckan_config("ckan.plugins", "datastore datapusher")
@pytest.mark.usefixtures("with_plugins")
Expand Down
2 changes: 0 additions & 2 deletions ckanext/example_iuploader/test/test_plugin.py
@@ -1,6 +1,5 @@
# encoding: utf-8

import paste.fileapp
import flask
import pytest
import six
Expand Down Expand Up @@ -56,7 +55,6 @@ def _get_package_new_page(app):
@pytest.mark.usefixtures("with_plugins", "clean_db")
@patch.object(ckan.lib.uploader, "os", fake_os)
@patch.object(flask, "send_file", side_effect=[CONTENT])
@patch.object(paste.fileapp, "os", fake_os)
@patch.object(config["pylons.h"], "uploads_enabled", return_value=True)
@patch.object(ckan.lib.uploader, "_storage_path", new="/doesnt_exist")
def test_resource_download_iuploader_called(
Expand Down

0 comments on commit dcd3019

Please sign in to comment.