Skip to content

Commit

Permalink
tests(import): assert errors entity
Browse files Browse the repository at this point in the history
  • Loading branch information
bouttier committed Feb 22, 2024
1 parent 395e5bd commit c448fef
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
25 changes: 13 additions & 12 deletions backend/geonature/tests/imports/test_imports_occhab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
6 changes: 5 additions & 1 deletion backend/geonature/tests/imports/test_imports_synthese.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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():
"""
Expand Down
23 changes: 20 additions & 3 deletions backend/geonature/tests/imports/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>)
if entity_code is set, expected_errors must be a set of 3-elements tuples:
(error_name: str, error_column:str , error_rows: frozenset<int>)
"""
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)
Expand Down

0 comments on commit c448fef

Please sign in to comment.