Skip to content

Commit 3f08a58

Browse files
authored
Bugfix: Disabled clusters raised exception on access from setup instead of returning None (#196)
1 parent 8e8a8d6 commit 3f08a58

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

src/pytest_celery/api/setup.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,34 @@ def app(self) -> Celery:
6464
return self._app
6565

6666
@property
67-
def backend_cluster(self) -> CeleryBackendCluster:
67+
def backend_cluster(self) -> CeleryBackendCluster | None:
6868
"""The backend cluster of this setup."""
6969
return self._backend_cluster
7070

7171
@property
72-
def backend(self) -> CeleryTestBackend:
72+
def backend(self) -> CeleryTestBackend | None:
7373
"""The first backend node of the backend cluster."""
74-
return self._backend_cluster[0] # type: ignore
74+
return self._backend_cluster[0] if self._backend_cluster else None # type: ignore
7575

7676
@property
77-
def broker_cluster(self) -> CeleryBrokerCluster:
77+
def broker_cluster(self) -> CeleryBrokerCluster | None:
7878
"""The broker cluster of this setup."""
7979
return self._broker_cluster
8080

8181
@property
82-
def broker(self) -> CeleryTestBroker:
82+
def broker(self) -> CeleryTestBroker | None:
8383
"""The first broker node of the broker cluster."""
84-
return self._broker_cluster[0] # type: ignore
84+
return self._broker_cluster[0] if self._broker_cluster else None # type: ignore
8585

8686
@property
87-
def worker_cluster(self) -> CeleryWorkerCluster:
87+
def worker_cluster(self) -> CeleryWorkerCluster | None:
8888
"""The worker cluster of this setup."""
8989
return self._worker_cluster
9090

9191
@property
92-
def worker(self) -> CeleryTestWorker:
92+
def worker(self) -> CeleryTestWorker | None:
9393
"""The first worker node of the worker cluster."""
94-
return self._worker_cluster[0] # type: ignore
94+
return self._worker_cluster[0] if self._worker_cluster else None # type: ignore
9595

9696
@classmethod
9797
def name(cls) -> str:

tests/integration/api/test_setup.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
from celery import Celery
55

66
from pytest_celery import RESULT_TIMEOUT
7+
from pytest_celery import CeleryBackendCluster
8+
from pytest_celery import CeleryBrokerCluster
79
from pytest_celery import CeleryTestSetup
810
from pytest_celery import CeleryTestWorker
11+
from pytest_celery import CeleryWorkerCluster
912
from tests.tasks import identity
1013

1114

@@ -59,3 +62,34 @@ def test_apply_async(self, celery_setup: CeleryTestSetup):
5962
queue = worker.worker_queue
6063
r = identity.s("test_ready").apply_async(queue=queue)
6164
assert r.get(timeout=RESULT_TIMEOUT) == "test_ready"
65+
66+
class test_disabling_backend_cluster:
67+
@pytest.fixture
68+
def celery_backend_cluster(self) -> CeleryBackendCluster:
69+
return None
70+
71+
def test_disabling_backend_cluster(self, celery_setup: CeleryTestSetup):
72+
assert celery_setup.backend_cluster is None
73+
assert celery_setup.backend is None
74+
75+
class test_disabling_broker_cluster:
76+
@pytest.fixture
77+
def celery_broker_cluster(self) -> CeleryBrokerCluster:
78+
return None
79+
80+
@pytest.fixture
81+
def celery_worker_cluster(self) -> CeleryWorkerCluster:
82+
return None
83+
84+
def test_disabling_broker_cluster(self, celery_setup: CeleryTestSetup):
85+
assert celery_setup.broker_cluster is None
86+
assert celery_setup.broker is None
87+
88+
class test_disabling_worker_cluster:
89+
@pytest.fixture
90+
def celery_worker_cluster(self) -> CeleryWorkerCluster:
91+
return None
92+
93+
def test_disabling_worker_cluster(self, celery_setup: CeleryTestSetup):
94+
assert celery_setup.worker_cluster is None
95+
assert celery_setup.worker is None

tests/unit/api/test_setup.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import pytest
44
from celery import Celery
55

6+
from pytest_celery import CeleryBackendCluster
7+
from pytest_celery import CeleryBrokerCluster
68
from pytest_celery import CeleryTestBackend
79
from pytest_celery import CeleryTestBroker
810
from pytest_celery import CeleryTestSetup
911
from pytest_celery import CeleryTestWorker
12+
from pytest_celery import CeleryWorkerCluster
1013

1114

1215
class test_celery_test_setup_unit:
@@ -67,3 +70,30 @@ def test_ready(self, celery_setup: CeleryTestSetup, confirmation: dict):
6770
**confirmation,
6871
control=False, # Not supported in unit tests
6972
)
73+
74+
class test_disabling_backend_cluster:
75+
@pytest.fixture
76+
def celery_backend_cluster(self) -> CeleryBackendCluster:
77+
return None
78+
79+
def test_disabling_backend_cluster(self, celery_setup: CeleryTestSetup):
80+
assert celery_setup.backend_cluster is None
81+
assert celery_setup.backend is None
82+
83+
class test_disabling_broker_cluster:
84+
@pytest.fixture
85+
def celery_broker_cluster(self) -> CeleryBrokerCluster:
86+
return None
87+
88+
def test_disabling_broker_cluster(self, celery_setup: CeleryTestSetup):
89+
assert celery_setup.broker_cluster is None
90+
assert celery_setup.broker is None
91+
92+
class test_disabling_worker_cluster:
93+
@pytest.fixture
94+
def celery_worker_cluster(self) -> CeleryWorkerCluster:
95+
return None
96+
97+
def test_disabling_worker_cluster(self, celery_setup: CeleryTestSetup):
98+
assert celery_setup.worker_cluster is None
99+
assert celery_setup.worker is None

0 commit comments

Comments
 (0)