Skip to content

Commit

Permalink
fixtures: fix celery_config recursive error
Browse files Browse the repository at this point in the history
* Fixes recursive error on `def celery_config(celery_config)` when
  celery is not installed as the fixture is not yet defined.
  (closes #22)
  • Loading branch information
egabancho authored and lnielsen committed Dec 3, 2018
1 parent b5624ac commit 0fb627f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
69 changes: 48 additions & 21 deletions pytest_invenio/fixtures.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
<https://github.com/celery/celery/blob/master/celery/contrib/pytest.py>`_,
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')
Expand Down
12 changes: 12 additions & 0 deletions tests/test_fixtures.py
Expand Up @@ -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)

0 comments on commit 0fb627f

Please sign in to comment.