diff --git a/spine_items/importer/mvcmodels/mappings_model.py b/spine_items/importer/mvcmodels/mappings_model.py index 352cc34c..d6785473 100644 --- a/spine_items/importer/mvcmodels/mappings_model.py +++ b/spine_items/importer/mvcmodels/mappings_model.py @@ -20,7 +20,7 @@ from spinetoolbox.helpers import plain_to_rich, list_to_rich_text, unique_name from spinedb_api.parameter_value import join_value_and_type, split_value_and_type from spinedb_api import from_database, ParameterValueFormatError -from spinedb_api.import_mapping.import_mapping import ScenarioBeforeAlternativeMapping +from spinedb_api.import_mapping.import_mapping import default_import_mapping, ScenarioBeforeAlternativeMapping from spinedb_api.import_mapping.import_mapping_compat import ( parse_named_mapping_spec, import_mapping_from_dict, @@ -733,7 +733,7 @@ def set_mappings_type(self, table_row, list_row, new_type): "Scenario alternative": "ScenarioAlternative", "Parameter value list": "ParameterValueList", }[new_type] - root_mapping = import_mapping_from_dict({"map_type": map_type}) + root_mapping = default_import_mapping(map_type) self.set_root_mapping(table_row, list_row, root_mapping) def _set_mapping_data(self, flattened_mappings, index, value, role): diff --git a/tests/importer/mvcmodels/test_mappings_model.py b/tests/importer/mvcmodels/test_mappings_model.py index e08fcebd..9131adce 100644 --- a/tests/importer/mvcmodels/test_mappings_model.py +++ b/tests/importer/mvcmodels/test_mappings_model.py @@ -63,6 +63,76 @@ def test_set_time_series_repeat_flag(self): waiter.wait() self.assertTrue(flattened_mappings.value_mapping().options["repeat"]) + def test_change_mappings_type(self): + model = MappingsModel(self._undo_stack, self._model_parent) + model.restore( + { + "table_mappings": { + "Sheet1": [ + { + "Mapping 1": { + "mapping": [ + {"map_type": "EntityClass", "position": "hidden", "value": "Object"}, + {"map_type": "Entity", "position": 0}, + {"map_type": "EntityMetadata", "position": "hidden"}, + {"map_type": "ParameterDefinition", "position": "hidden", "value": "size"}, + {"map_type": "Alternative", "position": "hidden", "value": "Base"}, + {"map_type": "ParameterValueMetadata", "position": "hidden"}, + {"map_type": "ParameterValue", "position": 1}, + ] + } + } + ] + }, + "selected_tables": ["Sheet1"], + "table_options": {"Sheet1": {}}, + "table_types": {"Sheet1": {"0": "string", "1": "float"}}, + "table_default_column_type": {}, + "table_row_types": {}, + "source_type": "ExcelConnector", + } + ) + self.assertEqual(model.index(0, 0).data(), "Select all") + table_index = model.index(1, 0) + self.assertEqual(table_index.data(), "Sheet1") + list_index = model.index(0, 0, table_index) + self.assertEqual(list_index.data(), "Mapping 1") + expected = [ + ["Entity class names", "Constant", "Object", ""], + ["Entity names", "Column", 1, ""], + ["Entity metadata", "None", None, ""], + ["Parameter names", "Constant", "size", ""], + ["Alternative names", "Constant", "Base", ""], + ["Parameter value metadata", "None", None, ""], + ["Parameter values", "Column", 2, ""], + ] + rows = model.rowCount(list_index) + self.assertEqual(rows, len(expected)) + for row in range(model.rowCount(list_index)): + expected_row = expected[row] + columns = model.columnCount(list_index) + self.assertEqual(columns, len(expected_row)) + for column in range(columns): + with self.subTest(row=row, column=column): + index = model.index(row, column, list_index) + self.assertEqual(index.data(), expected_row[column]) + model.set_mappings_type(1, 0, "Entity group") + expected = [ + ["Entity class names", "None", None, ""], + ["Group names", "None", None, ""], + ["Member names", "None", None, ""], + ] + rows = model.rowCount(list_index) + self.assertEqual(rows, len(expected)) + for row in range(model.rowCount(list_index)): + expected_row = expected[row] + columns = model.columnCount(list_index) + self.assertEqual(columns, len(expected_row)) + for column in range(columns): + with self.subTest(row=row, column=column): + index = model.index(row, column, list_index) + self.assertEqual(index.data(), expected_row[column]) + class TestTableList(unittest.TestCase): @classmethod