[ckan-dev] Running tests that rely on a background job

Florian.Brucker at it.karlsruhe.de Florian.Brucker at it.karlsruhe.de
Wed Jan 31 07:57:58 UTC 2018


Hi Nevelina,

I think your best option is to mock the enqueue_job function so that it 
executes the task it got passed synchronously instead of asynchronously. 
Something like this (untested):

import mock

def sequential_enqueue_job(job_func, args=None, kwargs=None, title=None):
    args = args or []
    kwargs = kwargs or {}
    job_func(*args, **kwargs)

@mock.patch('ckan.plugins.toolkit.enqueue_job', 
side_effect=sequential_enqueue_job)
def test_some_background_job(enqueue_job_mock):
    some_function_that_enqueues_a_job()
    assert something

You will probably have to adjust some details, but that should get you 
going.

For an example, see the tests of ckanext-extractor, where we've used this 
approach quite a bit:

        
https://github.com/stadt-karlsruhe/ckanext-extractor/blob/9-new-background-task-system/ckanext/extractor/tests/test_plugin.py


Best regards,
Florian


"ckan-dev" <ckan-dev-bounces at lists.okfn.org> schrieb am 30.01.2018 
17:09:46:

> Von: Nevelina Aleksandrova <nevelina.aleksandrova at gmail.com>
> An: ckan-dev at lists.okfn.org, 
> Datum: 30.01.2018 17:10
> Betreff: [ckan-dev] Running tests that rely on a background job
> Gesendet von: "ckan-dev" <ckan-dev-bounces at lists.okfn.org>
> 
> Hello, 
> 
> We have been working on a plugin for CKAN that takes a column in a 
> spreadsheet which signifies a location name (e.g. Birmingham) and 
> maps that to a geo polygon. We're at the point where we have 
> refactored the mapping into a background job and consequently our 
> end-to-end tests are failing. 
> 
> I was wondering if you had any advice on how to execute RQ 
> background jobs inline in tests?
> 
> The plugin is located here https://github.com/CottageLabs/ckanext-
> string_to_location, we're using CKAN 2.7.2.
> 
> Any pointers are much appreciated.
> 
> Thanks,
> Nevelina_______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> https://lists.okfn.org/mailman/listinfo/ckan-dev
> Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20180131/64c4ad9c/attachment-0003.html>


More information about the ckan-dev mailing list