From e1742a51303f5abeeeeebe9920758a0f9b325bf1 Mon Sep 17 00:00:00 2001 From: Antti Soininen Date: Tue, 17 Mar 2026 10:50:25 +0200 Subject: [PATCH] Fix importing ND entities Entity mapping is not necessarily hidden when its children are element mappings. This can happen e.g. in Toolbox importer when user first builds a 0D mapping but then changes it to ND; in this case the entity mapping is left to whatever position it originally had. --- spinedb_api/import_mapping/import_mapping.py | 2 +- tests/import_mapping/test_generator.py | 48 ++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/spinedb_api/import_mapping/import_mapping.py b/spinedb_api/import_mapping/import_mapping.py index 827dcd32..99a709c1 100644 --- a/spinedb_api/import_mapping/import_mapping.py +++ b/spinedb_api/import_mapping/import_mapping.py @@ -509,7 +509,7 @@ class EntityMapping(ImportMapping): MAP_TYPE = "Entity" def _import_row(self, source_data, state, mapped_data): - if self.position == Position.hidden and isinstance(self._child, ElementMapping): + if isinstance(self._child, ElementMapping): return entity_class_name = state[ImportKey.ENTITY_CLASS_NAME] entity_name = state[ImportKey.ENTITY_NAME] = str(source_data) diff --git a/tests/import_mapping/test_generator.py b/tests/import_mapping/test_generator.py index 09c24bd5..87c69fe7 100644 --- a/tests/import_mapping/test_generator.py +++ b/tests/import_mapping/test_generator.py @@ -1381,3 +1381,51 @@ def test_import_different_relationships_in_same_table(self): ], }, ) + + def test_import_element_2_names_from_headers(self): + header = ["Food", "VitA", "VitC"] + data_source = iter( + [ + ["hamburger", 6.0, 2.0], + ["orange juice", 2.0, 120.0], + ] + ) + mappings = [ + [ + {"map_type": "EntityClass", "position": "hidden", "value": "NutrientContent"}, + {"map_type": "Dimension", "position": "hidden", "value": "Food"}, + {"map_type": "Dimension", "position": "hidden", "value": "Nutrient"}, + {"map_type": "Entity", "position": 0}, + {"map_type": "Element", "position": 0}, + {"map_type": "Element", "position": "header"}, + {"map_type": "Alternative", "position": "hidden", "value": "Base"}, + {"map_type": "ParameterDefinition", "position": "hidden", "value": "content"}, + {"map_type": "ParameterValue", "position": "hidden"}, + ] + ] + mapped_data, errors = get_mapped_data(data_source, mappings, header) + self.assertEqual(errors, []) + self.assertEqual( + mapped_data, + { + "alternatives": {"Base"}, + "entity_classes": [ + ["NutrientContent", ["Food", "Nutrient"]], + ], + "entities": [ + ["NutrientContent", ["hamburger", "VitA"]], + ["NutrientContent", ["hamburger", "VitC"]], + ["NutrientContent", ["orange juice", "VitA"]], + ["NutrientContent", ["orange juice", "VitC"]], + ], + "parameter_definitions": [ + ["NutrientContent", "content"], + ], + "parameter_values": [ + ["NutrientContent", ("hamburger", "VitA"), "content", 6.0, "Base"], + ["NutrientContent", ("hamburger", "VitC"), "content", 2.0, "Base"], + ["NutrientContent", ("orange juice", "VitA"), "content", 2.0, "Base"], + ["NutrientContent", ("orange juice", "VitC"), "content", 120.0, "Base"], + ], + }, + )