From a487cfad5e0fc1f7b9c6d86d4ee624ed6a53df33 Mon Sep 17 00:00:00 2001 From: Jacques Fize <4259846+jacquesfize@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:44:21 +0200 Subject: [PATCH] fix(celery): make new FlaskCelery class work with import tests (#3237) --- backend/geonature/app.py | 3 +-- backend/geonature/tests/fixtures.py | 2 ++ backend/geonature/tests/test_gn_meta.py | 6 +++++- backend/geonature/utils/celery.py | 5 +++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/backend/geonature/app.py b/backend/geonature/app.py index 2c0ac65efb..8cbf39a60e 100755 --- a/backend/geonature/app.py +++ b/backend/geonature/app.py @@ -140,8 +140,7 @@ def create_app(with_external_mods=True): if "CELERY" in app.config: from geonature.utils.celery import celery_app - # celery_app.init_app(app) - celery_app.conf.update(app.config["CELERY"]) + celery_app.init_app(app) # Emails configuration if app.config["MAIL_CONFIG"]: diff --git a/backend/geonature/tests/fixtures.py b/backend/geonature/tests/fixtures.py index 90342f4df3..585bbd9956 100644 --- a/backend/geonature/tests/fixtures.py +++ b/backend/geonature/tests/fixtures.py @@ -21,6 +21,7 @@ TaxrefBdcStatutText, ) from geonature import create_app +from geonature.utils.config import config from geonature.core.gn_commons.models import BibTablesLocation, TMedias, TModules from geonature.core.gn_meta.models import ( CorAcquisitionFrameworkActor, @@ -96,6 +97,7 @@ def open(self, *args, **kwargs): @pytest.fixture(scope="session", autouse=True) def app(): + config["CELERY"]["task_always_eager"] = True app = create_app() app.testing = True app.test_client_class = GeoNatureClient diff --git a/backend/geonature/tests/test_gn_meta.py b/backend/geonature/tests/test_gn_meta.py index b9387eaba2..90f0a545cd 100644 --- a/backend/geonature/tests/test_gn_meta.py +++ b/backend/geonature/tests/test_gn_meta.py @@ -589,7 +589,11 @@ def test_datasets_permissions(self, app, datasets, users): qs = select(TDatasets).where(TDatasets.id_dataset.in_(ds_ids)) assert set(sc(dsc.filter_by_scope(0, query=qs)).unique().all()) == set([]) assert set(sc(dsc.filter_by_scope(1, query=qs)).unique().all()) == set( - [datasets["own_dataset"], datasets["own_dataset_not_activated"]] + [ + datasets["own_dataset"], + datasets["own_dataset_not_activated"], + datasets["private"], + ] ) assert set(sc(dsc.filter_by_scope(2, query=qs)).unique().all()) == set( [ diff --git a/backend/geonature/utils/celery.py b/backend/geonature/utils/celery.py index 20e203c1eb..1e1f6c6b48 100644 --- a/backend/geonature/utils/celery.py +++ b/backend/geonature/utils/celery.py @@ -7,7 +7,6 @@ class FlaskCelery(Celery): def __init__(self, *args, **kwargs): super(FlaskCelery, self).__init__(*args, **kwargs) - self.patch_task() if "app" in kwargs: self.init_app(kwargs["app"]) @@ -33,6 +32,8 @@ def __call__(self, *args, **kwargs): def init_app(self, app): self.app = app self.config_from_object(app.config["CELERY"]) + if not self.conf.task_always_eager: + self.patch_task() -celery_app = Celery("geonature") # FIX ME +celery_app = FlaskCelery("geonature")