diff --git a/src/pytest_celery/api/setup.py b/src/pytest_celery/api/setup.py index 1b9a19e0b..bb15a1f72 100644 --- a/src/pytest_celery/api/setup.py +++ b/src/pytest_celery/api/setup.py @@ -64,34 +64,34 @@ def app(self) -> Celery: return self._app @property - def backend_cluster(self) -> CeleryBackendCluster: + def backend_cluster(self) -> CeleryBackendCluster | None: """The backend cluster of this setup.""" return self._backend_cluster @property - def backend(self) -> CeleryTestBackend: + def backend(self) -> CeleryTestBackend | None: """The first backend node of the backend cluster.""" - return self._backend_cluster[0] # type: ignore + return self._backend_cluster[0] if self._backend_cluster else None @property - def broker_cluster(self) -> CeleryBrokerCluster: + def broker_cluster(self) -> CeleryBrokerCluster | None: """The broker cluster of this setup.""" return self._broker_cluster @property - def broker(self) -> CeleryTestBroker: + def broker(self) -> CeleryTestBroker | None: """The first broker node of the broker cluster.""" - return self._broker_cluster[0] # type: ignore + return self._broker_cluster[0] if self._broker_cluster else None @property - def worker_cluster(self) -> CeleryWorkerCluster: + def worker_cluster(self) -> CeleryWorkerCluster | None: """The worker cluster of this setup.""" return self._worker_cluster @property - def worker(self) -> CeleryTestWorker: + def worker(self) -> CeleryTestWorker | None: """The first worker node of the worker cluster.""" - return self._worker_cluster[0] # type: ignore + return self._worker_cluster[0] if self._worker_cluster else None @classmethod def name(cls) -> str: diff --git a/tests/integration/api/test_setup.py b/tests/integration/api/test_setup.py index 26ed7b0d7..c8d3ce2fc 100644 --- a/tests/integration/api/test_setup.py +++ b/tests/integration/api/test_setup.py @@ -4,6 +4,7 @@ from celery import Celery from pytest_celery import RESULT_TIMEOUT +from pytest_celery import CeleryBackendCluster from pytest_celery import CeleryTestSetup from pytest_celery import CeleryTestWorker from tests.tasks import identity @@ -59,3 +60,12 @@ def test_apply_async(self, celery_setup: CeleryTestSetup): queue = worker.worker_queue r = identity.s("test_ready").apply_async(queue=queue) assert r.get(timeout=RESULT_TIMEOUT) == "test_ready" + + class test_disabling_backend_cluster: + @pytest.fixture + def celery_backend_cluster(self) -> CeleryBackendCluster: + return None + + def test_disabling_backend_cluster(self, celery_setup: CeleryTestSetup): + assert celery_setup.backend_cluster is None + assert celery_setup.backend is None diff --git a/tests/unit/api/test_setup.py b/tests/unit/api/test_setup.py index ebdb95fb0..11d4752b6 100644 --- a/tests/unit/api/test_setup.py +++ b/tests/unit/api/test_setup.py @@ -3,6 +3,7 @@ import pytest from celery import Celery +from pytest_celery import CeleryBackendCluster from pytest_celery import CeleryTestBackend from pytest_celery import CeleryTestBroker from pytest_celery import CeleryTestSetup @@ -67,3 +68,12 @@ def test_ready(self, celery_setup: CeleryTestSetup, confirmation: dict): **confirmation, control=False, # Not supported in unit tests ) + + class test_disabling_backend_cluster: + @pytest.fixture + def celery_backend_cluster(self) -> CeleryBackendCluster: + return None + + def test_disabling_backend_cluster(self, celery_setup: CeleryTestSetup): + assert celery_setup.backend_cluster is None + assert celery_setup.backend is None