Skip to content

Commit

Permalink
test: add test for auto_validation function
Browse files Browse the repository at this point in the history
Test to check changes for "STATUT_VALID" on synthese observation with
score = 3 and auto_validation = True and validation.id_validator is None

Reviewed-by: andriacap
  • Loading branch information
andriacap committed Oct 30, 2023
1 parent d52b239 commit 57d4dd6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
6 changes: 6 additions & 0 deletions backend/geonature/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"celery_eager",
"sources_modules",
"modules",
"auto_validation_enabled",
]


Expand Down Expand Up @@ -663,3 +664,8 @@ def create_report(id_synthese, id_role, content, id_type, deleted):
@pytest.fixture()
def notifications_enabled(monkeypatch):
monkeypatch.setitem(current_app.config, "NOTIFICATIONS_ENABLED", True)


@pytest.fixture()
def auto_validation_enabled(monkeypatch):
monkeypatch.setitem(current_app.config["VALIDATION"], "AUTO_VALIDATION_ENABLED", True)
75 changes: 75 additions & 0 deletions backend/geonature/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from werkzeug.exceptions import Unauthorized, BadRequest

from geonature.core.gn_synthese.models import Synthese
from geonature.core.gn_commons.models import TValidations, VLatestValidations
from geonature.core.gn_profiles.models import VConsistancyData
from gn_module_validation.tasks import set_auto_validation
from geonature.utils.env import db
from geonature.utils.config import config

Expand All @@ -20,6 +23,33 @@
)


@pytest.fixture()
def validation_with_max_score_and_wait_validation_status():
id_nomenclature_attente_validation = db.session.execute(
""" select tn.id_nomenclature from ref_nomenclatures.t_nomenclatures tn where tn.mnemonique = 'En attente de validation' """
).scalar()

validations_to_update = (
db.session.query(
TValidations.id_validation,
VLatestValidations.uuid_attached_row,
VConsistancyData.id_synthese,
)
.join(TValidations, TValidations.id_validation == VLatestValidations.id_validation)
.join(VConsistancyData, VConsistancyData.id_sinp == VLatestValidations.uuid_attached_row)
.filter(
TValidations.validation_auto == True,
VLatestValidations.id_nomenclature_valid_status == id_nomenclature_attente_validation,
VLatestValidations.id_validator == None,
VConsistancyData.valid_phenology == True,
VConsistancyData.valid_altitude == True,
VConsistancyData.valid_distribution == True,
)
.all()
)
return validations_to_update


@pytest.mark.usefixtures("client_class", "temporary_transaction", "app")
class TestValidation:
def test_get_synthese_data(self, users, synthese_data):
Expand Down Expand Up @@ -77,3 +107,48 @@ def test_get_validation_history(self, users, synthese_data):
url_for("gn_commons.get_hist", uuid_attached_row=s.unique_id_sinp)
)
assert response.status_code == 200

def test_auto_validation(
self,
users,
app,
auto_validation_enabled,
validation_with_max_score_and_wait_validation_status,
):
fct_auto_validation_name = app.config["VALIDATION"][
"AUTO_VALIDATION_SQL_FUNCTION"
] # config["VALIDATION"]["AUTO_VALIDATION_SQL_FUNCTION"]
set_logged_user(self.client, users["user"])

id_nomenclature_probable = db.session.execute(
""" select tn.id_nomenclature from ref_nomenclatures.t_nomenclatures tn where tn.mnemonique = 'Probable' """
).scalar()
id_nomenclature_attente_validation = db.session.execute(
""" select tn.id_nomenclature from ref_nomenclatures.t_nomenclatures tn where tn.mnemonique = 'En attente de validation' """
).scalar()

list_synthese_to_update = []
for row in validation_with_max_score_and_wait_validation_status:
list_synthese_to_update.append(row[2])

synthese_valid_statut_before_update = (
db.session.query(Synthese.id_nomenclature_valid_status)
.filter(Synthese.id_synthese.in_(list_synthese_to_update))
.all()
)
assert all(
synthese_valid_statut[0] == id_nomenclature_attente_validation
for synthese_valid_statut in synthese_valid_statut_before_update
)

# On applique la fonction
set_auto_validation() # list_synthese_updated = TValidations.auto_validation(fct_auto_validation_name)
synthese_valid_statut_after_update = (
db.session.query(Synthese.id_nomenclature_valid_status)
.filter(Synthese.id_synthese.in_(list_synthese_to_update))
.all()
)
assert all(
synthese_valid_statut[0] == id_nomenclature_probable
for synthese_valid_statut in synthese_valid_statut_after_update
)

0 comments on commit 57d4dd6

Please sign in to comment.