From 0fb627f0728ca593caaf7d50c496352b5c58eb98 Mon Sep 17 00:00:00 2001 From: "Esteban J. G. Gabancho" Date: Thu, 29 Nov 2018 16:23:56 -0500 Subject: [PATCH] fixtures: fix celery_config recursive error * Fixes recursive error on `def celery_config(celery_config)` when celery is not installed as the fixture is not yet defined. (closes #22) --- pytest_invenio/fixtures.py | 69 ++++++++++++++++++++++++++------------ tests/test_fixtures.py | 12 +++++++ 2 files changed, 60 insertions(+), 21 deletions(-) diff --git a/pytest_invenio/fixtures.py b/pytest_invenio/fixtures.py index 0728c32..7e1483c 100644 --- a/pytest_invenio/fixtures.py +++ b/pytest_invenio/fixtures.py @@ -17,6 +17,7 @@ import tempfile from datetime import datetime +import pkg_resources import pytest from pytest_flask.plugin import _make_test_response_class from selenium import webdriver @@ -92,33 +93,59 @@ def broker_uri(): yield os.environ.get('BROKER_URL', 'amqp://guest:guest@localhost:5672//') -@pytest.fixture(scope='module') -def celery_config(celery_config): - """Celery configuration (defaults to eager tasks). - - Scope: module - - This fixture provides the default Celery configuration (eager tasks, - in-memory result backend and exception propagation). It can easily be - overwritten in a specific test module: +def _celery_config(): + """Factory/Helper function to create the ``celery_config`` fixture. - .. code-block:: python - - # test_something.py - import pytest - - pytest.fixture(scope='module') - def celery_config(celery_config): - celery_config['CELERY_ALWAYS_EAGER'] = False - return celery_config + When Celery is installed it provides with this same fixture via + `celery.contrib.pytest + `_, + in this is the case we overwrite this fixture and update the configuration + with Invenio's default configuration. + If it is not installed, then we just define a new fixture which returns the + default Invenio Celery configuration. """ - celery_config.update(dict( + default_config = dict( CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND='memory', CELERY_EAGER_PROPAGATES_EXCEPTIONS=True, CELERY_RESULT_BACKEND='cache', - )) - return celery_config + ) + + try: + pkg_resources.get_distribution('celery') + + # Celery is installed, overwrite fixture + def inner(celery_config): + celery_config.update(default_config) + return celery_config + except pkg_resources.DistributionNotFound: + # No Celery, return the default config + def inner(): + return default_config + + return inner + + +celery_config = pytest.fixture( + scope='module', name='celery_config')(_celery_config()) +"""Celery configuration (defaults to eager tasks). + +Scope: module + +This fixture provides the default Celery configuration (eager tasks, +in-memory result backend and exception propagation). It can easily be +overwritten in a specific test module: + +.. code-block:: python + + # test_something.py + import pytest + + pytest.fixture(scope='module') + def celery_config(celery_config): + celery_config['CELERY_ALWAYS_EAGER'] = False + return celery_config +""" @pytest.fixture(scope='module') diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index 9369d76..40b2b25 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -377,3 +377,15 @@ def test_browser_fail(live_server, browser): assert os.path.exists( os.path.join(str(conftest_testdir.tmpdir), '.e2e_screenshots')) monkeypatch.undo() + + +def test_celery_config(testdir): + """Test celery config.""" + testdir.makepyfile(test_app=""" + def test_celery_config_with_celery(celery_config): + assert celery_config['CELERY_ALWAYS_EAGER'] == True + assert celery_config['CELERY_CACHE_BACKEND'] == 'memory' + assert celery_config['CELERY_EAGER_PROPAGATES_EXCEPTIONS'] == True + assert celery_config['CELERY_RESULT_BACKEND'] == 'cache' + """) + testdir.runpytest().assert_outcomes(passed=1)