Skip to content

Commit

Permalink
Bugfix: Disabled clusters raised exception on access from setup inste…
Browse files Browse the repository at this point in the history
…ad of returning None
  • Loading branch information
Nusnus committed Feb 12, 2024
1 parent 8e8a8d6 commit 8ebff37
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/pytest_celery/api/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 84 in src/pytest_celery/api/setup.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/api/setup.py#L84

Added line #L84 was not covered by tests

@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

Check warning on line 94 in src/pytest_celery/api/setup.py

View check run for this annotation

Codecov / codecov/patch

src/pytest_celery/api/setup.py#L94

Added line #L94 was not covered by tests

@classmethod
def name(cls) -> str:
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/api/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions tests/unit/api/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 8ebff37

Please sign in to comment.