From f526fa6db146bfe95d9b803495aaa6eaff6657a3 Mon Sep 17 00:00:00 2001 From: Maxime Vergez <85738261+mvergez@users.noreply.github.com> Date: Thu, 22 Dec 2022 14:27:07 +0100 Subject: [PATCH] Feat/edit categories module (#19) * tests: make tests and fixtures work for modules By changing the way a monitoring module is created in the fixture Add two tests to check the relationship * feat(api): add categories in edit module * style: applied black --- .../config/generic/module.json | 15 ++++++++++++ .../gn_module_monitoring/routes/data_utils.py | 4 ++-- .../tests/fixtures/module.py | 23 +++++++++++-------- .../test_models/test_module.py | 18 +++++++++++---- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/backend/gn_module_monitoring/config/generic/module.json b/backend/gn_module_monitoring/config/generic/module.json index 352a5dacf..e270a49d9 100644 --- a/backend/gn_module_monitoring/config/generic/module.json +++ b/backend/gn_module_monitoring/config/generic/module.json @@ -120,6 +120,21 @@ "attribut_label": "Afficher dans le menu ?", "definition": "Afficher le module dans le menu de GeoNature. (Recharger la page pour voir les modifications)." }, + + "categories": { + "type_widget": "datalist", + "attribut_label": "Liste des catégories", + "type_util": "categorie", + "keyValue": "id_categorie", + "keyLabel": "label", + "multiple": true, + "api" : "__MONITORINGS_PATH/sites/categories", + "application": "GeoNature", + "required": true, + "data_path": "categories", + "definition": "Permet de paramétrer la compatibilité de ce module avec les catégories de sites" + }, + "medias": { "type_widget": "medias", "attribut_label": "Médias", diff --git a/backend/gn_module_monitoring/routes/data_utils.py b/backend/gn_module_monitoring/routes/data_utils.py index 2c0fae73b..2038f5f34 100644 --- a/backend/gn_module_monitoring/routes/data_utils.py +++ b/backend/gn_module_monitoring/routes/data_utils.py @@ -32,8 +32,7 @@ from ..blueprint import blueprint from ..config.repositories import get_config - -from ..monitoring.models import TMonitoringSitesGroups, TMonitoringSites +from gn_module_monitoring.monitoring.models import TMonitoringSitesGroups, TMonitoringSites, BibCategorieSite model_dict = { "habitat": Habref, @@ -41,6 +40,7 @@ "user": User, "taxonomy": Taxref, "dataset": TDatasets, + "categorie": BibCategorieSite, "observer_list": UserList, "taxonomy_list": BibListes, "sites_group": TMonitoringSitesGroups, diff --git a/backend/gn_module_monitoring/tests/fixtures/module.py b/backend/gn_module_monitoring/tests/fixtures/module.py index 6e0a5b1ac..41d946e37 100644 --- a/backend/gn_module_monitoring/tests/fixtures/module.py +++ b/backend/gn_module_monitoring/tests/fixtures/module.py @@ -1,5 +1,6 @@ +from uuid import uuid4 + import pytest -from geonature.core.gn_commons.models.base import TModules from geonature.utils.env import db from gn_module_monitoring.monitoring.models import TMonitoringModules @@ -7,15 +8,17 @@ @pytest.fixture -def monitoring_module(module, categories): - id_module = TModules.query.filter(TModules.id_module == module.id_module).one().id_module - t_monitoring_module = TMonitoringModules() +def monitoring_module(categories): + t_monitoring_module = TMonitoringModules( + module_code=uuid4(), + module_label="test", + active_frontend=True, + active_backend=False, + module_path="test", + categories=list(categories.values()), + ) - module_data = {"id_module": id_module, "categories": list(categories.values())} - t_monitoring_module.from_dict(module_data) - # monitoring = TMonitoringModules(id_module=id_module, categories=list(categories.values())) - monitoring = t_monitoring_module with db.session.begin_nested(): - db.session.add(monitoring) + db.session.add(t_monitoring_module) - return monitoring + return t_monitoring_module diff --git a/backend/gn_module_monitoring/tests/test_monitoring/test_models/test_module.py b/backend/gn_module_monitoring/tests/test_monitoring/test_models/test_module.py index e156a1614..60cca2e98 100644 --- a/backend/gn_module_monitoring/tests/test_monitoring/test_models/test_module.py +++ b/backend/gn_module_monitoring/tests/test_monitoring/test_models/test_module.py @@ -1,11 +1,21 @@ import pytest +from geonature.utils.env import db +from gn_module_monitoring.monitoring.models import TMonitoringModules from gn_module_monitoring.tests.fixtures.module import monitoring_module -from gn_module_monitoring.tests.fixtures.site import categories +from gn_module_monitoring.tests.fixtures.site import categories, site_type @pytest.mark.usefixtures("temporary_transaction") class TestModule: - def test_module(self, monitoring_module): - cateogories = monitoring_module.categories - assert False + def test_module(self, monitoring_module, categories): + cats = monitoring_module.categories + assert cats == list(categories.values()) + + def test_remove_categorie_from_module(self, monitoring_module, categories): + with db.session.begin_nested(): + monitoring_module.categories.pop(0) + + mon = TMonitoringModules.query.filter_by(id_module=monitoring_module.id_module).one() + + assert len(mon.categories) == len(categories) - 1