From d774c55af16a20253c72b58812475b35d5a5175f Mon Sep 17 00:00:00 2001 From: Tomer Nosrati Date: Tue, 24 Oct 2023 22:30:17 +0300 Subject: [PATCH] Refactored fixture usage in unit tests --- tests/unit/api/test_backend.py | 15 +++++++++++---- tests/unit/api/test_broker.py | 15 +++++++++++---- tests/unit/api/test_worker.py | 20 +++++++++++++------- tests/unit/vendors/test_memcached.py | 11 ++++++++--- tests/unit/vendors/test_rabbitmq.py | 12 +++++++++--- tests/unit/vendors/test_redis.py | 19 +++++++++++++------ tests/unit/vendors/test_worker.py | 18 +++++++++++------- 7 files changed, 76 insertions(+), 34 deletions(-) diff --git a/tests/unit/api/test_backend.py b/tests/unit/api/test_backend.py index b2b8e6d1..44f31bcd 100644 --- a/tests/unit/api/test_backend.py +++ b/tests/unit/api/test_backend.py @@ -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 diff --git a/tests/unit/api/test_broker.py b/tests/unit/api/test_broker.py index b2428289..e97a8175 100644 --- a/tests/unit/api/test_broker.py +++ b/tests/unit/api/test_broker.py @@ -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 diff --git a/tests/unit/api/test_worker.py b/tests/unit/api/test_worker.py index 1724ed9f..bec0df0f 100644 --- a/tests/unit/api/test_worker.py +++ b/tests/unit/api/test_worker.py @@ -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} diff --git a/tests/unit/vendors/test_memcached.py b/tests/unit/vendors/test_memcached.py index 26d15480..4088bf05 100644 --- a/tests/unit/vendors/test_memcached.py +++ b/tests/unit/vendors/test_memcached.py @@ -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 @@ -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() diff --git a/tests/unit/vendors/test_rabbitmq.py b/tests/unit/vendors/test_rabbitmq.py index 5c3fa0b6..a60b1ec9 100644 --- a/tests/unit/vendors/test_rabbitmq.py +++ b/tests/unit/vendors/test_rabbitmq.py @@ -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" @@ -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() diff --git a/tests/unit/vendors/test_redis.py b/tests/unit/vendors/test_redis.py index 082c09fc..ba933d79 100644 --- a/tests/unit/vendors/test_redis.py +++ b/tests/unit/vendors/test_redis.py @@ -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 @@ -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() diff --git a/tests/unit/vendors/test_worker.py b/tests/unit/vendors/test_worker.py index aa43efb7..3a3fee0d 100644 --- a/tests/unit/vendors/test_worker.py +++ b/tests/unit/vendors/test_worker.py @@ -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 @@ -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()