Skip to content

Commit

Permalink
Refactored fixture usage in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nusnus committed Oct 24, 2023
1 parent 891944b commit d774c55
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 34 deletions.
15 changes: 11 additions & 4 deletions tests/unit/api/test_backend.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import pytest
from pytest_lazyfixture import lazy_fixture

from pytest_celery import CELERY_BACKEND
from pytest_celery import CELERY_BACKEND_CLUSTER
from pytest_celery import CeleryBackendCluster
from pytest_celery import CeleryTestBackend


@pytest.mark.parametrize("backend", [lazy_fixture(CELERY_BACKEND)])
class test_celey_test_backend:
def test_default_config_format(self, celery_backend: CeleryTestBackend):
def test_default_config_format(self, backend: CeleryTestBackend):
expected_format = {"url", "local_url"}
assert set(celery_backend.default_config().keys()) == expected_format
assert set(backend.default_config().keys()) == expected_format


@pytest.mark.parametrize("cluster", [lazy_fixture(CELERY_BACKEND_CLUSTER)])
class test_celery_backend_cluster:
def test_default_config_format(self, celery_backend_cluster: CeleryBackendCluster):
def test_default_config_format(self, cluster: CeleryBackendCluster):
expected_format = {"urls", "local_urls"}
assert set(celery_backend_cluster.default_config().keys()) == expected_format
assert set(cluster.default_config().keys()) == expected_format
15 changes: 11 additions & 4 deletions tests/unit/api/test_broker.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import pytest
from pytest_lazyfixture import lazy_fixture

from pytest_celery import CELERY_BROKER
from pytest_celery import CELERY_BROKER_CLUSTER
from pytest_celery import CeleryBrokerCluster
from pytest_celery import CeleryTestBroker


@pytest.mark.parametrize("broker", [lazy_fixture(CELERY_BROKER)])
class test_celey_test_broker:
def test_default_config_format(self, celery_broker: CeleryTestBroker):
def test_default_config_format(self, broker: CeleryTestBroker):
expected_format = {"url", "local_url"}
assert set(celery_broker.default_config().keys()) == expected_format
assert set(broker.default_config().keys()) == expected_format


@pytest.mark.parametrize("cluster", [lazy_fixture(CELERY_BROKER_CLUSTER)])
class test_celery_broker_cluster:
def test_default_config_format(self, celery_broker_cluster: CeleryBrokerCluster):
def test_default_config_format(self, cluster: CeleryBrokerCluster):
expected_format = {"urls", "local_urls"}
assert set(celery_broker_cluster.default_config().keys()) == expected_format
assert set(cluster.default_config().keys()) == expected_format
20 changes: 13 additions & 7 deletions tests/unit/api/test_worker.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import pytest
from celery import Celery
from pytest_lazyfixture import lazy_fixture

from pytest_celery import CELERY_WORKER
from pytest_celery import CELERY_WORKER_CLUSTER
from pytest_celery import DEFAULT_WORKER_VERSION
from pytest_celery import CeleryTestWorker
from pytest_celery import CeleryWorkerCluster


@pytest.mark.parametrize("worker", [lazy_fixture(CELERY_WORKER)])
class test_celey_test_worker:
def test_ready(self, celery_worker: CeleryTestWorker):
assert celery_worker.ready()
celery_worker.container.ready.assert_called_once()
def test_ready(self, worker: CeleryTestWorker):
assert worker.ready()
worker.container.ready.assert_called_once()


@pytest.mark.parametrize("cluster", [lazy_fixture(CELERY_WORKER_CLUSTER)])
class test_celery_worker_cluster:
def test_app(self, celery_worker_cluster: CeleryWorkerCluster, celery_setup_app: Celery):
def test_app(self, cluster: CeleryWorkerCluster, celery_setup_app: Celery):
node: CeleryTestWorker
for node in celery_worker_cluster:
for node in cluster:
assert node.app is celery_setup_app

def test_versions(self, celery_worker_cluster: CeleryWorkerCluster):
assert celery_worker_cluster.versions == {DEFAULT_WORKER_VERSION}
def test_versions(self, cluster: CeleryWorkerCluster):
assert cluster.versions == {DEFAULT_WORKER_VERSION}
11 changes: 8 additions & 3 deletions tests/unit/vendors/test_memcached.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import pytest
from pytest_lazyfixture import lazy_fixture

from pytest_celery import CELERY_MEMCACHED_BACKEND
from pytest_celery import MEMCACHED_ENV
from pytest_celery import MEMCACHED_IMAGE
from pytest_celery import MemcachedContainer
Expand All @@ -15,7 +19,8 @@ def test_image(self):
assert MemcachedContainer.image() == MEMCACHED_IMAGE


@pytest.mark.parametrize("backend", [lazy_fixture(CELERY_MEMCACHED_BACKEND)])
class test_memcached_backend_api:
def test_ready(self, celery_memcached_backend: MemcachedTestBackend):
celery_memcached_backend.ready()
celery_memcached_backend.container.ready.assert_called_once()
def test_ready(self, backend: MemcachedTestBackend):
backend.ready()
backend.container.ready.assert_called_once()
12 changes: 9 additions & 3 deletions tests/unit/vendors/test_rabbitmq.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import pytest
from pytest_lazyfixture import lazy_fixture

from pytest_celery import CELERY_RABBITMQ_BROKER
from pytest_celery import RABBITMQ_ENV
from pytest_celery import RABBITMQ_IMAGE
from pytest_celery import RabbitMQContainer
from pytest_celery import RabbitMQTestBroker


# from pytest_celery import RabbitMQTestBroker
class test_rabbitmq_container:
def test_version(self):
assert RabbitMQContainer.version() == "latest"
Expand All @@ -15,7 +20,8 @@ def test_image(self):
assert RabbitMQContainer.image() == RABBITMQ_IMAGE


@pytest.mark.parametrize("broker", [lazy_fixture(CELERY_RABBITMQ_BROKER)])
class test_rabbitmq_broker_api:
def test_ready(self, celery_rabbitmq_broker: RabbitMQTestBroker):
celery_rabbitmq_broker.ready()
celery_rabbitmq_broker.container.ready.assert_called_once()
def test_ready(self, broker: RabbitMQTestBroker):
broker.ready()
broker.container.ready.assert_called_once()
19 changes: 13 additions & 6 deletions tests/unit/vendors/test_redis.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import pytest
from pytest_lazyfixture import lazy_fixture

from pytest_celery import CELERY_REDIS_BACKEND
from pytest_celery import CELERY_REDIS_BROKER
from pytest_celery import REDIS_ENV
from pytest_celery import REDIS_IMAGE
from pytest_celery import RedisContainer
Expand All @@ -16,13 +21,15 @@ def test_image(self):
assert RedisContainer.image() == REDIS_IMAGE


@pytest.mark.parametrize("backend", [lazy_fixture(CELERY_REDIS_BACKEND)])
class test_redis_backend_api:
def test_ready(self, celery_redis_backend: RedisTestBackend):
celery_redis_backend.ready()
celery_redis_backend.container.ready.assert_called_once()
def test_ready(self, backend: RedisTestBackend):
backend.ready()
backend.container.ready.assert_called_once()


@pytest.mark.parametrize("broker", [lazy_fixture(CELERY_REDIS_BROKER)])
class test_redis_broker_api:
def test_ready(self, celery_redis_broker: RedisTestBroker):
celery_redis_broker.ready()
celery_redis_broker.container.ready.assert_called_once()
def test_ready(self, broker: RedisTestBroker):
broker.ready()
broker.container.ready.assert_called_once()
18 changes: 11 additions & 7 deletions tests/unit/vendors/test_worker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import inspect

import pytest
from celery import Celery
from pytest_lazyfixture import lazy_fixture

from pytest_celery import CELERY_SETUP_WORKER
from pytest_celery import DEFAULT_WORKER_ENV
from pytest_celery import DEFAULT_WORKER_VERSION
from pytest_celery import CeleryTestWorker
Expand Down Expand Up @@ -40,13 +43,14 @@ def test_task_modules(self):
assert CeleryWorkerContainer.tasks_modules() == set()


@pytest.mark.parametrize("worker", [lazy_fixture(CELERY_SETUP_WORKER)])
class test_base_worker_api:
def test_ready(self, celery_setup_worker: CeleryTestWorker):
celery_setup_worker.ready()
celery_setup_worker.container.ready.assert_called_once()
def test_ready(self, worker: CeleryTestWorker):
worker.ready()
worker.container.ready.assert_called_once()

def test_app(self, celery_setup_worker: CeleryTestWorker, celery_setup_app: Celery):
assert celery_setup_worker.app is celery_setup_app
def test_app(self, worker: CeleryTestWorker, celery_setup_app: Celery):
assert worker.app is celery_setup_app

def test_version(self, celery_setup_worker: CeleryTestWorker):
assert celery_setup_worker.version == CeleryWorkerContainer.version()
def test_version(self, worker: CeleryTestWorker):
assert worker.version == CeleryWorkerContainer.version()

0 comments on commit d774c55

Please sign in to comment.