From c448fefd1e6a62adaf9f1938ebeb2829444de761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Bouttier?= Date: Thu, 22 Feb 2024 16:30:12 +0100 Subject: [PATCH] tests(import): assert errors entity --- .../tests/imports/test_imports_occhab.py | 25 ++++++++++--------- .../tests/imports/test_imports_synthese.py | 6 ++++- backend/geonature/tests/imports/utils.py | 23 ++++++++++++++--- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/backend/geonature/tests/imports/test_imports_occhab.py b/backend/geonature/tests/imports/test_imports_occhab.py index a2c220a4d9..95b6ae4de5 100644 --- a/backend/geonature/tests/imports/test_imports_occhab.py +++ b/backend/geonature/tests/imports/test_imports_occhab.py @@ -187,20 +187,21 @@ def test_import_valid_file(self, imported_import): imported_import, { # Stations errors - ("DUPLICATE_UUID", "unique_id_sinp_station", frozenset({4, 5})), - ("DATASET_NOT_FOUND", "unique_dataset_id", frozenset({6})), - ("DATASET_NOT_AUTHORIZED", "unique_dataset_id", frozenset({7})), - ("INVALID_UUID", "unique_dataset_id", frozenset({8})), - ("NO-GEOM", "Champs géométriques", frozenset({9})), - ("MISSING_VALUE", "date_min", frozenset({10})), - ("DUPLICATE_ENTITY_SOURCE_PK", "id_station_source", frozenset({14, 15})), - ("INVALID_UUID", "unique_id_sinp_station", frozenset({20, 21})), + ("DUPLICATE_UUID", "station", "unique_id_sinp_station", frozenset({4, 5})), + ("DATASET_NOT_FOUND", "station", "unique_dataset_id", frozenset({6})), + ("DATASET_NOT_AUTHORIZED", "station", "unique_dataset_id", frozenset({7})), + ("INVALID_UUID", "station", "unique_dataset_id", frozenset({8})), + ("NO-GEOM", "station", "Champs géométriques", frozenset({9})), + ("MISSING_VALUE", "station", "date_min", frozenset({10})), + ("DUPLICATE_ENTITY_SOURCE_PK", "station", "id_station_source", frozenset({14, 15})), + ("INVALID_UUID", "station", "unique_id_sinp_station", frozenset({20})), # Habitats errors - ("ERRONEOUS_PARENT_ENTITY", "", frozenset({4, 5, 6, 7, 10, 14, 15, 19, 20})), - ("NO_PARENT_ENTITY", "id_station", frozenset({11})), + ("INVALID_UUID", "habitat", "unique_id_sinp_station", frozenset({20,21})), + ("ERRONEOUS_PARENT_ENTITY", "habitat", "", frozenset({4, 5, 6, 7, 10, 14, 15, 19, 20})), + ("NO_PARENT_ENTITY", "habitat", "id_station", frozenset({11})), # Other errors - ("ORPHAN_ROW", "unique_id_sinp_station", frozenset({12})), - ("ORPHAN_ROW", "id_station_source", frozenset({13})), + ("ORPHAN_ROW", None, "unique_id_sinp_station", frozenset({12})), + ("ORPHAN_ROW", None, "id_station_source", frozenset({13})), }, ) assert imported_import.statistics == {"station_count": 3, "habitat_count": 5} diff --git a/backend/geonature/tests/imports/test_imports_synthese.py b/backend/geonature/tests/imports/test_imports_synthese.py index 7705cc1726..df3432873f 100644 --- a/backend/geonature/tests/imports/test_imports_synthese.py +++ b/backend/geonature/tests/imports/test_imports_synthese.py @@ -39,7 +39,7 @@ from geonature.core.imports.utils import insert_import_data_in_transient_table from .jsonschema_definitions import jsonschema_definitions -from .utils import assert_import_errors +from .utils import assert_import_errors as _assert_import_errors tests_path = Path(__file__).parent @@ -54,6 +54,10 @@ valid_file_taxa_count = 2 +def assert_import_errors(imprt, expected_errors): + return _assert_import_errors(imprt, expected_errors, entity_code="observation") + + @pytest.fixture(scope="class") def g_permissions(): """ diff --git a/backend/geonature/tests/imports/utils.py b/backend/geonature/tests/imports/utils.py index 1bab88b603..95458e1a43 100644 --- a/backend/geonature/tests/imports/utils.py +++ b/backend/geonature/tests/imports/utils.py @@ -5,13 +5,30 @@ from geonature.core.imports.models import ImportUserErrorType -def assert_import_errors(imprt, expected_errors): +def assert_import_errors(imprt, expected_errors, entity_code=None): + """ + expected_errors must be a set of 4-elements tuples: + (error_name: str, entity_code: str, error_column:str , error_rows: frozenset) + if entity_code is set, expected_errors must be a set of 3-elements tuples: + (error_name: str, error_column:str , error_rows: frozenset) + """ + if entity_code is not None: + expected_errors = { + (error_name, entity.code, error_column, error_rows) + for error_name, error_column, error_rows in expected_errors + } errors = { - (error.type.name, error.column, frozenset(error.rows or [])) for error in imprt.errors + ( + error.type.name, + error.entity.code if error.entity else None, + error.column, + frozenset(error.rows or []), + ) + for error in imprt.errors } assert errors == expected_errors expected_erroneous_rows = set() - for error_type, _, rows in expected_errors: + for error_type, _, _, rows in expected_errors: error_type = ImportUserErrorType.query.filter_by(name=error_type).one() if error_type.level == "ERROR": expected_erroneous_rows |= set(rows)