Skip to content

Commit

Permalink
Use default_import_mapping() in Importer to get a blank mapping
Browse files Browse the repository at this point in the history
Importer was using legacy functionality in spinedb_api to get blank
mappings when user changes the mapping type. This does not work e.g.
with Entity group mappings since those did not exist in pre-0.7 world.

Re spine-tools/Spine-Toolbox#2662
  • Loading branch information
soininen committed Apr 2, 2024
1 parent 7cf1886 commit 623f2fb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions spine_items/importer/mvcmodels/mappings_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
70 changes: 70 additions & 0 deletions tests/importer/mvcmodels/test_mappings_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 623f2fb

Please sign in to comment.