From 4164b64b259c5a6b2cf3658a758b7d907afa4338 Mon Sep 17 00:00:00 2001 From: woutdenolf Date: Thu, 15 Feb 2024 13:40:31 +0100 Subject: [PATCH] celery extra's are not always available --- src/pytest_celery/__init__.py | 26 ++++++++---- src/pytest_celery/defaults.py | 41 ++++++++++++------- src/pytest_celery/vendors/__init__.py | 4 ++ .../vendors/memcached/__init__.py | 7 ++++ src/pytest_celery/vendors/redis/__init__.py | 7 ++++ 5 files changed, 62 insertions(+), 23 deletions(-) diff --git a/src/pytest_celery/__init__.py b/src/pytest_celery/__init__.py index a1db653e0..81a3b62f3 100644 --- a/src/pytest_celery/__init__.py +++ b/src/pytest_celery/__init__.py @@ -24,17 +24,27 @@ from pytest_celery.fixtures.broker import * from pytest_celery.fixtures.setup import * from pytest_celery.fixtures.worker import * -from pytest_celery.vendors.memcached.api import * -from pytest_celery.vendors.memcached.container import * -from pytest_celery.vendors.memcached.fixtures import * +from pytest_celery.vendors import MissingCeleryDependency from pytest_celery.vendors.rabbitmq.api import * from pytest_celery.vendors.rabbitmq.container import * from pytest_celery.vendors.rabbitmq.fixtures import * -from pytest_celery.vendors.redis.backend.api import * -from pytest_celery.vendors.redis.backend.fixtures import * -from pytest_celery.vendors.redis.broker.api import * -from pytest_celery.vendors.redis.broker.fixtures import * -from pytest_celery.vendors.redis.container import * + +try: + from pytest_celery.vendors.memcached.api import * + from pytest_celery.vendors.memcached.container import * + from pytest_celery.vendors.memcached.fixtures import * +except MissingCeleryDependency: + pass + +try: + from pytest_celery.vendors.redis.backend.api import * + from pytest_celery.vendors.redis.backend.fixtures import * + from pytest_celery.vendors.redis.broker.api import * + from pytest_celery.vendors.redis.broker.fixtures import * + from pytest_celery.vendors.redis.container import * +except MissingCeleryDependency: + pass + from pytest_celery.vendors.worker.container import * from pytest_celery.vendors.worker.fixtures import * diff --git a/src/pytest_celery/defaults.py b/src/pytest_celery/defaults.py index b67947a50..974f75018 100644 --- a/src/pytest_celery/defaults.py +++ b/src/pytest_celery/defaults.py @@ -4,18 +4,35 @@ from pytest_docker_tools import network -from pytest_celery.vendors.memcached.defaults import CELERY_MEMCACHED_BACKEND -from pytest_celery.vendors.memcached.defaults import * +from pytest_celery.vendors import MissingCeleryDependency from pytest_celery.vendors.rabbitmq.defaults import CELERY_RABBITMQ_BROKER from pytest_celery.vendors.rabbitmq.defaults import * -from pytest_celery.vendors.redis.backend.defaults import CELERY_REDIS_BACKEND -from pytest_celery.vendors.redis.backend.defaults import * -from pytest_celery.vendors.redis.broker.defaults import CELERY_REDIS_BROKER -from pytest_celery.vendors.redis.broker.defaults import * -from pytest_celery.vendors.redis.defaults import * from pytest_celery.vendors.worker.defaults import CELERY_SETUP_WORKER from pytest_celery.vendors.worker.defaults import * +try: + import pytest_celery.vendors.memcached +except MissingCeleryDependency: + _CELERY_MEMCACHED_BACKEND = None +else: + from pytest_celery.vendors.memcached.defaults import CELERY_MEMCACHED_BACKEND as _CELERY_MEMCACHED_BACKEND + from pytest_celery.vendors.memcached.defaults import * + + _CELERY_MEMCACHED_BACKEND = None # Beta support at the moment, to be used manually + +try: + import pytest_celery.vendors.redis +except MissingCeleryDependency: + _CELERY_REDIS_BACKEND = None + _CELERY_REDIS_BROKER = None +else: + from pytest_celery.vendors.redis.backend.defaults import CELERY_REDIS_BACKEND as _CELERY_REDIS_BACKEND + from pytest_celery.vendors.redis.backend.defaults import * + from pytest_celery.vendors.redis.broker.defaults import CELERY_REDIS_BROKER as _CELERY_REDIS_BROKER + from pytest_celery.vendors.redis.broker.defaults import * + from pytest_celery.vendors.redis.defaults import * + + #################################################################################### # Automatic components #################################################################################### @@ -29,14 +46,8 @@ # Tests that do not rely on default parametrization will not be affected. ALL_CELERY_WORKERS = (CELERY_SETUP_WORKER,) -ALL_CELERY_BACKENDS = ( - CELERY_REDIS_BACKEND, - # CELERY_MEMCACHED_BACKEND, # Beta support at the moment, to be used manually -) -ALL_CELERY_BROKERS = ( - CELERY_REDIS_BROKER, - CELERY_RABBITMQ_BROKER, -) +ALL_CELERY_BACKENDS = (backend for backend in [_CELERY_REDIS_BACKEND, _CELERY_MEMCACHED_BACKEND] if backend is not None) +ALL_CELERY_BROKERS = (broker for broker in [CELERY_RABBITMQ_BROKER, _CELERY_REDIS_BROKER] if broker is not None) #################################################################################### # Fixtures diff --git a/src/pytest_celery/vendors/__init__.py b/src/pytest_celery/vendors/__init__.py index b08b47eba..665b627d7 100644 --- a/src/pytest_celery/vendors/__init__.py +++ b/src/pytest_celery/vendors/__init__.py @@ -1 +1,5 @@ """See :ref:`vendors`.""" + + +class MissingCeleryDependency(ImportError): + pass diff --git a/src/pytest_celery/vendors/memcached/__init__.py b/src/pytest_celery/vendors/memcached/__init__.py index 48de88142..14ac49c34 100644 --- a/src/pytest_celery/vendors/memcached/__init__.py +++ b/src/pytest_celery/vendors/memcached/__init__.py @@ -3,3 +3,10 @@ This module is part of the Memcached Backend vendor. """ + +from .. import MissingCeleryDependency + +try: + import memcache # noqa F401 +except ImportError: + raise MissingCeleryDependency("celery extra dependency missing: celery[pymemcache]") diff --git a/src/pytest_celery/vendors/redis/__init__.py b/src/pytest_celery/vendors/redis/__init__.py index 8cc206044..8c65ac2e4 100644 --- a/src/pytest_celery/vendors/redis/__init__.py +++ b/src/pytest_celery/vendors/redis/__init__.py @@ -3,3 +3,10 @@ This module is part of the Redis vendor. """ + +from .. import MissingCeleryDependency + +try: + import redis # noqa F401 +except ImportError: + raise MissingCeleryDependency("celery extra dependency missing: celery[redis]")