diff --git a/spine_items/importer/commands.py b/spine_items/importer/commands.py index b0801e4a..dd876c11 100644 --- a/spine_items/importer/commands.py +++ b/spine_items/importer/commands.py @@ -194,7 +194,7 @@ def undo(self): class SetMappingPositionType(QUndoCommand): - """Sets the type of a component mapping.""" + """Sets the type of component mapping.""" def __init__(self, table_row, list_row, row, model, position_type, previous_type, previous_position): """ @@ -504,7 +504,7 @@ def __init__(self, table_row, list_row, model, import_entities): model (MappingsModel): model import_entities (bool): new flag value """ - super().__init__("import objects flag change") + super().__init__(("check" if import_entities else "uncheck") + " import entities") self._table_row = table_row self._list_row = list_row self._model = model @@ -519,8 +519,38 @@ def undo(self): self._model.set_import_entities(self._table_row, self._list_row, not self._import_entities) +class SetImportEntityAlternatives(QUndoCommand): + """Command to add/remove entity alternative imports to/from mappings.""" + + def __init__(self, table_row, list_row, model, import_entity_alternatives, previous_mapping): + """ + Args: + table_row (int): source table row index + list_row (int): mapping list row index + model (MappingsModel): model + import_entity_alternatives (bool): new flag value + previous_mapping (ImportMapping): previous mapping root + """ + super().__init__(("check" if import_entity_alternatives else "uncheck") + " import entities") + self._table_row = table_row + self._list_row = list_row + self._model = model + self._import_entity_alternatives = import_entity_alternatives + self._previous_mapping_dict = [m.to_dict() for m in previous_mapping.flatten()] + + def redo(self): + """Changes the .""" + self._model.set_import_entity_alternatives(self._table_row, self._list_row, self._import_entity_alternatives) + + def undo(self): + """Restores the mappings to previous state.""" + self._model.set_root_mapping( + self._table_row, self._list_row, import_mapping_from_dict(self._previous_mapping_dict) + ) + + class SetParameterType(QUndoCommand): - """Command to change the parameter type of an item mapping.""" + """Command to change the parameter type of item mapping.""" def __init__(self, table_row, list_row, model, new_type, previous_mapping): """ @@ -550,7 +580,7 @@ def undo(self): class SetValueType(QUndoCommand): - """Command to change the value type of an item mapping.""" + """Command to change the value type of item mapping.""" def __init__(self, table_row, list_row, model, new_type, old_type): """ diff --git a/spine_items/importer/flattened_mappings.py b/spine_items/importer/flattened_mappings.py index 409c0c8c..664a64d6 100644 --- a/spine_items/importer/flattened_mappings.py +++ b/spine_items/importer/flattened_mappings.py @@ -12,17 +12,19 @@ """Contains a model to handle source tables and import mapping.""" from enum import Enum, unique -from spinedb_api.mapping import Position +from spinedb_api.mapping import Position, unflatten from spinedb_api.parameter_value import split_value_and_type from spinedb_api import from_database, ParameterValueFormatError -from spinedb_api.helpers import fix_name_ambiguity, string_to_bool +from spinedb_api.helpers import fix_name_ambiguity from spinedb_api.import_mapping.import_mapping import ( EntityClassMapping, EntityMapping, DimensionMapping, ElementMapping, EntityGroupMapping, + EntityAlternativeActivityMapping, AlternativeMapping, + EntityMetadataMapping, ScenarioMapping, ScenarioAlternativeMapping, ParameterValueListMapping, @@ -45,7 +47,6 @@ @unique class MappingType(Enum): EntityClass = "Entity class" - Entity = "Entity" EntityGroup = "Entity group" Alternative = "Alternative" Scenario = "Scenario" @@ -69,6 +70,7 @@ class MappingType(Enum): "EntityGroup": "Member names", "Dimension": "Dimension names", "Element": "Element names", + "EntityAlternativeActivity": "Entity activities", "Alternative": "Alternative names", "Scenario": "Scenario names", "ScenarioActiveFlag": "Scenario active flags", @@ -470,13 +472,13 @@ def _resolve_map_type(flattened): return None head_mapping = flattened[0] if isinstance(head_mapping, EntityClassMapping): - if any(isinstance(m, EntityGroupMapping) for m in flattened): + if any(isinstance(m, EntityGroupMapping) for m in flattened[1:]): return MappingType.EntityGroup return MappingType.EntityClass if isinstance(head_mapping, AlternativeMapping): return MappingType.Alternative if isinstance(head_mapping, ScenarioMapping): - if any(isinstance(m, ScenarioAlternativeMapping) for m in flattened): + if any(isinstance(m, ScenarioAlternativeMapping) for m in flattened[1:]): return MappingType.ScenarioAlternative return MappingType.Scenario if isinstance(head_mapping, ParameterValueListMapping): @@ -526,7 +528,7 @@ def set_dimension_count(self, dimension_count): dimension_count (int): new dimension count """ if not self.can_have_dimensions(): - return None, None + return current_dimension_count = self.dimension_count() last_dim_mapping = next( m for m in reversed(self._components) if isinstance(m, (DimensionMapping, EntityClassMapping)) @@ -549,13 +551,74 @@ def set_dimension_count(self, dimension_count): self.set_root_mapping(self._components[0]) self._ensure_consistent_import_entities() + def may_import_entity_alternatives(self): + """Checks if the mappings can optionally import entity alternatives. + + Returns: + bool: True if mappings can import entity alternatives, False otherwise + """ + return self._map_type == MappingType.EntityClass + + def import_entity_alternatives(self): + """Returns the import entity alternatives flag. + + Returns: + bool: True if import entity alternatives is set, False otherwise + """ + return any(isinstance(m, EntityAlternativeActivityMapping) for m in self._components) + + def set_import_entity_alternatives(self, import_entity_alternatives): + """Adds or removes AlternativeMapping and EntityAlternativeActivityMapping. + + Legacy mappings may already have AlternativeMapping as part of parameter value mappings; + in this case we move the AlternativeMapping before EntityAlternativeActivityMapping. + + Args: + import_entity_alternatives (bool): if True, mappings will be added; if False they will be removed + """ + if not self.may_import_entity_alternatives(): + return + if import_entity_alternatives: + for i in range(len(self._components)): + if isinstance(self._components[i], AlternativeMapping): + alternative_mapping = self._components.pop(i) + break + else: + alternative_mapping = AlternativeMapping(Position.hidden) + for i, m in enumerate(self._components): + if isinstance(m, EntityMetadataMapping): + insertion_point = i + break + else: + raise RuntimeError("Logic error: expected to find EntityMetadataMapping") + new_mappings = [] + for i, m in enumerate(self._components): + new_mappings.append(m) + if i == insertion_point: + new_mappings.append(alternative_mapping) + new_mappings.append(EntityAlternativeActivityMapping(Position.hidden)) + else: + new_mappings = [] + has_value = any(isinstance(m, (ParameterValueMapping, ParameterValueTypeMapping)) for m in self._components) + for m in self._components: + if isinstance(m, EntityAlternativeActivityMapping): + continue + if isinstance(m, AlternativeMapping) and not has_value: + continue + new_mappings.append(m) + self.set_root_mapping(unflatten(new_mappings)) + def may_import_entities(self): """Checks if the mappings can optionally import entities. Returns: bool: True if mappings can import entities, False otherwise """ - return self._map_type in (MappingType.EntityClass, MappingType.EntityGroup) + if self._map_type == MappingType.EntityGroup: + return True + if self._map_type == MappingType.EntityClass: + return bool(self._import_entities_mappings()) + return False def _import_entities_mappings(self): """Collects a list of mapping components that have an import_entities attribute. @@ -571,7 +634,8 @@ def import_entities(self): Returns: bool: True if imports entities is set, False otherwise """ - return all(m.import_entities for m in self._import_entities_mappings()) + import_enabled_mappings = self._import_entities_mappings() + return bool(import_enabled_mappings) and all(m.import_entities for m in self._import_entities_mappings()) def set_import_entities(self, import_entities): """Sets the import entities flag for components that support it. @@ -585,7 +649,7 @@ def set_import_entities(self, import_entities): def _ensure_consistent_import_entities(self): """If any mapping has the import entities flag set, sets the flag also for all other mappings.""" mappings = self._import_entities_mappings() - if any(mapping.import_entities for mapping in mappings): + if mappings and any(mapping.import_entities for mapping in mappings): for m in mappings: m.import_entities = True @@ -612,9 +676,20 @@ def set_parameter_components(self, parameter_definition_component): parameter_definition_component (ImportMapping, optional): root of parameter mappings; None removes the mappings """ + if parameter_definition_component is not None and any( + isinstance(m, AlternativeMapping) for m in self._components + ): + parameter_definition_component = unflatten( + filter(lambda m: not isinstance(m, AlternativeMapping), parameter_definition_component.flatten()) + ) m = self._parameter_definition_component() parent = m.parent if m is not None else self._components[-1] parent.child = parameter_definition_component + if parameter_definition_component is None and not self.import_entity_alternatives(): + for c in self._components: + if isinstance(c, AlternativeMapping): + c.parent.child = c.child + break self.set_root_mapping(self._components[0]) def display_parameter_type(self): diff --git a/spine_items/importer/mvcmodels/mappings_model.py b/spine_items/importer/mvcmodels/mappings_model.py index 237a3ae5..157e021d 100644 --- a/spine_items/importer/mvcmodels/mappings_model.py +++ b/spine_items/importer/mvcmodels/mappings_model.py @@ -26,9 +26,6 @@ from spinedb_api import from_database, ParameterValueFormatError from spinedb_api.import_mapping.import_mapping import ( default_import_mapping, - EntityClassMapping, - EntityMapping, - EntityMetadataMapping, ScenarioBeforeAlternativeMapping, ) from spinedb_api.import_mapping.import_mapping_compat import ( @@ -1199,6 +1196,27 @@ def set_value_type(self, table_row, list_row, new_type): self.endInsertRows() self.dataChanged.emit(list_index, list_index, [Role.FLATTENED_MAPPINGS]) + def set_import_entity_alternatives(self, table_row, list_row, import_entity_alternatives): + """Adds or removes entity alternative imports. + + Args: + table_row (int): source table row index + list_row (int): mapping list row index + import_entity_alternatives (bool): True to import entity alternatives, False to disable importing + """ + table_index = self.index(table_row, 0) + list_index = self.index(list_row, 0, table_index) + mapping_list = self._mappings[table_row].mapping_list[list_row] + flattened_mappings = mapping_list.flattened_mappings + self.beginRemoveRows(list_index, 0, len(flattened_mappings.display_names) - 1) + mapping_list.flattened_mappings = None + self.endRemoveRows() + flattened_mappings.set_import_entity_alternatives(import_entity_alternatives) + self.beginInsertRows(list_index, 0, len(flattened_mappings.display_names) - 1) + mapping_list.flattened_mappings = flattened_mappings + self.endInsertRows() + self.dataChanged.emit(list_index, list_index, [Role.FLATTENED_MAPPINGS]) + def change_component_mapping(self, flattened_mappings, index, new_type, new_ref): """ Pushes :class:`SetComponentMappingType` to the undo stack. @@ -1611,10 +1629,7 @@ def create_default_mapping(): Returns: ImportMapping: new mapping root """ - root_mapping = EntityClassMapping(0) - object_mapping = root_mapping.child = EntityMapping(1) - object_mapping.child = EntityMetadataMapping(Position.hidden) - return root_mapping + return default_import_mapping("EntityClass") def has_mapping_name(self, table_row, name): """Checks if a name exists in mapping list. diff --git a/spine_items/importer/ui/import_editor_window.py b/spine_items/importer/ui/import_editor_window.py index a5c19c65..524401f8 100644 --- a/spine_items/importer/ui/import_editor_window.py +++ b/spine_items/importer/ui/import_editor_window.py @@ -14,7 +14,7 @@ ################################################################################ ## Form generated from reading UI file 'import_editor_window.ui' ## -## Created by: Qt User Interface Compiler version 6.6.3 +## Created by: Qt User Interface Compiler version 6.7.2 ## ## WARNING! All changes made in this file will be lost when recompiling UI file! ################################################################################ @@ -73,7 +73,7 @@ def setupUi(self, MainWindow): self.browse_source_button = QToolButton(self.centralwidget) self.browse_source_button.setObjectName(u"browse_source_button") icon = QIcon() - icon.addFile(u":/icons/folder-open-solid.svg", QSize(), QIcon.Normal, QIcon.Off) + icon.addFile(u":/icons/folder-open-solid.svg", QSize(), QIcon.Mode.Normal, QIcon.State.Off) self.browse_source_button.setIcon(icon) self.horizontalLayout_2.addWidget(self.browse_source_button) @@ -236,63 +236,84 @@ def setupUi(self, MainWindow): self.mapping_options_contents.setSpacing(3) self.mapping_options_contents.setObjectName(u"mapping_options_contents") self.mapping_options_contents.setContentsMargins(3, 3, 3, 3) - self.ignore_columns_button = QPushButton(self.frame_2) - self.ignore_columns_button.setObjectName(u"ignore_columns_button") + self.class_type_combo_box = QComboBox(self.frame_2) + self.class_type_combo_box.addItem("") + self.class_type_combo_box.addItem("") + self.class_type_combo_box.addItem("") + self.class_type_combo_box.addItem("") + self.class_type_combo_box.addItem("") + self.class_type_combo_box.addItem("") + self.class_type_combo_box.setObjectName(u"class_type_combo_box") - self.mapping_options_contents.addWidget(self.ignore_columns_button, 9, 1, 1, 1) + self.mapping_options_contents.addWidget(self.class_type_combo_box, 0, 1, 1, 1) self.value_type_label = QLabel(self.frame_2) self.value_type_label.setObjectName(u"value_type_label") - self.mapping_options_contents.addWidget(self.value_type_label, 4, 0, 1, 1) + self.mapping_options_contents.addWidget(self.value_type_label, 3, 0, 1, 1) + + self.value_type_combo_box = QComboBox(self.frame_2) + self.value_type_combo_box.addItem("") + self.value_type_combo_box.addItem("") + self.value_type_combo_box.addItem("") + self.value_type_combo_box.addItem("") + self.value_type_combo_box.addItem("") + self.value_type_combo_box.setObjectName(u"value_type_combo_box") + + self.mapping_options_contents.addWidget(self.value_type_combo_box, 3, 1, 1, 1) + + self.start_read_row_spin_box = QSpinBox(self.frame_2) + self.start_read_row_spin_box.setObjectName(u"start_read_row_spin_box") + self.start_read_row_spin_box.setMinimum(1) + + self.mapping_options_contents.addWidget(self.start_read_row_spin_box, 7, 1, 1, 1) self.class_type_label = QLabel(self.frame_2) self.class_type_label.setObjectName(u"class_type_label") self.mapping_options_contents.addWidget(self.class_type_label, 0, 0, 1, 1) - self.read_start_row_label = QLabel(self.frame_2) - self.read_start_row_label.setObjectName(u"read_start_row_label") + self.dimension_spin_box = QSpinBox(self.frame_2) + self.dimension_spin_box.setObjectName(u"dimension_spin_box") + self.dimension_spin_box.setValue(0) - self.mapping_options_contents.addWidget(self.read_start_row_label, 8, 0, 1, 1) + self.mapping_options_contents.addWidget(self.dimension_spin_box, 4, 1, 1, 1) - self.map_dimensions_label = QLabel(self.frame_2) - self.map_dimensions_label.setObjectName(u"map_dimensions_label") + self.parameter_type_label = QLabel(self.frame_2) + self.parameter_type_label.setObjectName(u"parameter_type_label") - self.mapping_options_contents.addWidget(self.map_dimensions_label, 6, 0, 1, 1) + self.mapping_options_contents.addWidget(self.parameter_type_label, 2, 0, 1, 1) - self.before_alternative_check_box = QCheckBox(self.frame_2) - self.before_alternative_check_box.setObjectName(u"before_alternative_check_box") + self.dimension_label = QLabel(self.frame_2) + self.dimension_label.setObjectName(u"dimension_label") - self.mapping_options_contents.addWidget(self.before_alternative_check_box, 10, 1, 1, 1) + self.mapping_options_contents.addWidget(self.dimension_label, 4, 0, 1, 1) - self.ignore_columns_label = QLabel(self.frame_2) - self.ignore_columns_label.setObjectName(u"ignore_columns_label") + self.ignore_columns_button = QPushButton(self.frame_2) + self.ignore_columns_button.setObjectName(u"ignore_columns_button") - self.mapping_options_contents.addWidget(self.ignore_columns_label, 9, 0, 1, 1) + self.mapping_options_contents.addWidget(self.ignore_columns_button, 8, 1, 1, 1) - self.class_type_combo_box = QComboBox(self.frame_2) - self.class_type_combo_box.addItem("") - self.class_type_combo_box.addItem("") - self.class_type_combo_box.addItem("") - self.class_type_combo_box.addItem("") - self.class_type_combo_box.addItem("") - self.class_type_combo_box.addItem("") - self.class_type_combo_box.setObjectName(u"class_type_combo_box") + self.time_series_repeat_check_box = QCheckBox(self.frame_2) + self.time_series_repeat_check_box.setObjectName(u"time_series_repeat_check_box") - self.mapping_options_contents.addWidget(self.class_type_combo_box, 0, 1, 1, 1) + self.mapping_options_contents.addWidget(self.time_series_repeat_check_box, 12, 0, 1, 1) - self.start_read_row_spin_box = QSpinBox(self.frame_2) - self.start_read_row_spin_box.setObjectName(u"start_read_row_spin_box") - self.start_read_row_spin_box.setMinimum(1) + self.import_entity_alternatives_check_box = QCheckBox(self.frame_2) + self.import_entity_alternatives_check_box.setObjectName(u"import_entity_alternatives_check_box") - self.mapping_options_contents.addWidget(self.start_read_row_spin_box, 8, 1, 1, 1) + self.mapping_options_contents.addWidget(self.import_entity_alternatives_check_box, 9, 0, 1, 1) + + self.map_compression_check_box = QCheckBox(self.frame_2) + self.map_compression_check_box.setObjectName(u"map_compression_check_box") + + self.mapping_options_contents.addWidget(self.map_compression_check_box, 12, 1, 1, 1) self.map_dimension_spin_box = QSpinBox(self.frame_2) self.map_dimension_spin_box.setObjectName(u"map_dimension_spin_box") self.map_dimension_spin_box.setMinimum(1) - self.mapping_options_contents.addWidget(self.map_dimension_spin_box, 6, 1, 1, 1) + self.mapping_options_contents.addWidget(self.map_dimension_spin_box, 5, 1, 1, 1) self.parameter_type_combo_box = QComboBox(self.frame_2) self.parameter_type_combo_box.addItem("") @@ -300,48 +321,32 @@ def setupUi(self, MainWindow): self.parameter_type_combo_box.addItem("") self.parameter_type_combo_box.setObjectName(u"parameter_type_combo_box") - self.mapping_options_contents.addWidget(self.parameter_type_combo_box, 3, 1, 1, 1) + self.mapping_options_contents.addWidget(self.parameter_type_combo_box, 2, 1, 1, 1) - self.map_compression_check_box = QCheckBox(self.frame_2) - self.map_compression_check_box.setObjectName(u"map_compression_check_box") - - self.mapping_options_contents.addWidget(self.map_compression_check_box, 11, 1, 1, 1) - - self.import_entities_check_box = QCheckBox(self.frame_2) - self.import_entities_check_box.setObjectName(u"import_entities_check_box") - - self.mapping_options_contents.addWidget(self.import_entities_check_box, 10, 0, 1, 1) - - self.value_type_combo_box = QComboBox(self.frame_2) - self.value_type_combo_box.addItem("") - self.value_type_combo_box.addItem("") - self.value_type_combo_box.addItem("") - self.value_type_combo_box.addItem("") - self.value_type_combo_box.addItem("") - self.value_type_combo_box.setObjectName(u"value_type_combo_box") + self.read_start_row_label = QLabel(self.frame_2) + self.read_start_row_label.setObjectName(u"read_start_row_label") - self.mapping_options_contents.addWidget(self.value_type_combo_box, 4, 1, 1, 1) + self.mapping_options_contents.addWidget(self.read_start_row_label, 7, 0, 1, 1) - self.time_series_repeat_check_box = QCheckBox(self.frame_2) - self.time_series_repeat_check_box.setObjectName(u"time_series_repeat_check_box") + self.ignore_columns_label = QLabel(self.frame_2) + self.ignore_columns_label.setObjectName(u"ignore_columns_label") - self.mapping_options_contents.addWidget(self.time_series_repeat_check_box, 11, 0, 1, 1) + self.mapping_options_contents.addWidget(self.ignore_columns_label, 8, 0, 1, 1) - self.parameter_type_label = QLabel(self.frame_2) - self.parameter_type_label.setObjectName(u"parameter_type_label") + self.map_dimensions_label = QLabel(self.frame_2) + self.map_dimensions_label.setObjectName(u"map_dimensions_label") - self.mapping_options_contents.addWidget(self.parameter_type_label, 3, 0, 1, 1) + self.mapping_options_contents.addWidget(self.map_dimensions_label, 5, 0, 1, 1) - self.dimension_label = QLabel(self.frame_2) - self.dimension_label.setObjectName(u"dimension_label") + self.before_alternative_check_box = QCheckBox(self.frame_2) + self.before_alternative_check_box.setObjectName(u"before_alternative_check_box") - self.mapping_options_contents.addWidget(self.dimension_label, 5, 0, 1, 1) + self.mapping_options_contents.addWidget(self.before_alternative_check_box, 10, 1, 1, 1) - self.dimension_spin_box = QSpinBox(self.frame_2) - self.dimension_spin_box.setObjectName(u"dimension_spin_box") - self.dimension_spin_box.setValue(0) + self.import_entities_check_box = QCheckBox(self.frame_2) + self.import_entities_check_box.setObjectName(u"import_entities_check_box") - self.mapping_options_contents.addWidget(self.dimension_spin_box, 5, 1, 1, 1) + self.mapping_options_contents.addWidget(self.import_entities_check_box, 10, 0, 1, 1) self.verticalLayout_3.addLayout(self.mapping_options_contents) @@ -379,9 +384,7 @@ def setupUi(self, MainWindow): QWidget.setTabOrder(self.dimension_spin_box, self.map_dimension_spin_box) QWidget.setTabOrder(self.map_dimension_spin_box, self.start_read_row_spin_box) QWidget.setTabOrder(self.start_read_row_spin_box, self.ignore_columns_button) - QWidget.setTabOrder(self.ignore_columns_button, self.import_entities_check_box) - QWidget.setTabOrder(self.import_entities_check_box, self.before_alternative_check_box) - QWidget.setTabOrder(self.before_alternative_check_box, self.time_series_repeat_check_box) + QWidget.setTabOrder(self.ignore_columns_button, self.time_series_repeat_check_box) QWidget.setTabOrder(self.time_series_repeat_check_box, self.map_compression_check_box) QWidget.setTabOrder(self.map_compression_check_box, self.mapping_spec_table) @@ -410,16 +413,6 @@ def retranslateUi(self, MainWindow): self.new_button.setText(QCoreApplication.translate("MainWindow", u"Add", None)) self.remove_button.setText(QCoreApplication.translate("MainWindow", u"Remove", None)) self.duplicate_button.setText(QCoreApplication.translate("MainWindow", u"Duplicate", None)) - self.ignore_columns_button.setText("") - self.value_type_label.setText(QCoreApplication.translate("MainWindow", u"Default value type:", None)) - self.class_type_label.setText(QCoreApplication.translate("MainWindow", u"Item type:", None)) - self.read_start_row_label.setText(QCoreApplication.translate("MainWindow", u"Read data from row:", None)) - self.map_dimensions_label.setText(QCoreApplication.translate("MainWindow", u"Map dimensions:", None)) -#if QT_CONFIG(tooltip) - self.before_alternative_check_box.setToolTip(QCoreApplication.translate("MainWindow", u"Enable or disable 'Before alternative name' mapping for scenario alternative item type.", None)) -#endif // QT_CONFIG(tooltip) - self.before_alternative_check_box.setText(QCoreApplication.translate("MainWindow", u"Use before alternative", None)) - self.ignore_columns_label.setText(QCoreApplication.translate("MainWindow", u"Ignore columns:", None)) self.class_type_combo_box.setItemText(0, QCoreApplication.translate("MainWindow", u"Entity class", None)) self.class_type_combo_box.setItemText(1, QCoreApplication.translate("MainWindow", u"Entity group", None)) self.class_type_combo_box.setItemText(2, QCoreApplication.translate("MainWindow", u"Alternative", None)) @@ -427,29 +420,40 @@ def retranslateUi(self, MainWindow): self.class_type_combo_box.setItemText(4, QCoreApplication.translate("MainWindow", u"Scenario alternative", None)) self.class_type_combo_box.setItemText(5, QCoreApplication.translate("MainWindow", u"Parameter value list", None)) -#if QT_CONFIG(tooltip) - self.map_dimension_spin_box.setToolTip(QCoreApplication.translate("MainWindow", u"Number of dimensions when value type is Map.", None)) -#endif // QT_CONFIG(tooltip) - self.parameter_type_combo_box.setItemText(0, QCoreApplication.translate("MainWindow", u"Value", None)) - self.parameter_type_combo_box.setItemText(1, QCoreApplication.translate("MainWindow", u"Definition", None)) - self.parameter_type_combo_box.setItemText(2, QCoreApplication.translate("MainWindow", u"None", None)) - - self.map_compression_check_box.setText(QCoreApplication.translate("MainWindow", u"Compress Maps", None)) - self.import_entities_check_box.setText(QCoreApplication.translate("MainWindow", u"Import entities", None)) + self.value_type_label.setText(QCoreApplication.translate("MainWindow", u"Default value type:", None)) self.value_type_combo_box.setItemText(0, QCoreApplication.translate("MainWindow", u"Single value", None)) self.value_type_combo_box.setItemText(1, QCoreApplication.translate("MainWindow", u"Time series", None)) self.value_type_combo_box.setItemText(2, QCoreApplication.translate("MainWindow", u"Time pattern", None)) self.value_type_combo_box.setItemText(3, QCoreApplication.translate("MainWindow", u"Map", None)) self.value_type_combo_box.setItemText(4, QCoreApplication.translate("MainWindow", u"Array", None)) + self.class_type_label.setText(QCoreApplication.translate("MainWindow", u"Item type:", None)) #if QT_CONFIG(tooltip) - self.time_series_repeat_check_box.setToolTip(QCoreApplication.translate("MainWindow", u"Set the repeat flag for all imported time series", None)) + self.dimension_spin_box.setToolTip(QCoreApplication.translate("MainWindow", u"Number of entity dimensions.", None)) #endif // QT_CONFIG(tooltip) - self.time_series_repeat_check_box.setText(QCoreApplication.translate("MainWindow", u"Repeat time series", None)) self.parameter_type_label.setText(QCoreApplication.translate("MainWindow", u"Parameter type:", None)) self.dimension_label.setText(QCoreApplication.translate("MainWindow", u"Number of dimensions:", None)) + self.ignore_columns_button.setText("") #if QT_CONFIG(tooltip) - self.dimension_spin_box.setToolTip(QCoreApplication.translate("MainWindow", u"Number of entity dimensions.", None)) + self.time_series_repeat_check_box.setToolTip(QCoreApplication.translate("MainWindow", u"Set the repeat flag for all imported time series", None)) #endif // QT_CONFIG(tooltip) + self.time_series_repeat_check_box.setText(QCoreApplication.translate("MainWindow", u"Repeat time series", None)) + self.import_entity_alternatives_check_box.setText(QCoreApplication.translate("MainWindow", u"Import entity alternatives", None)) + self.map_compression_check_box.setText(QCoreApplication.translate("MainWindow", u"Compress Maps", None)) +#if QT_CONFIG(tooltip) + self.map_dimension_spin_box.setToolTip(QCoreApplication.translate("MainWindow", u"Number of dimensions when value type is Map.", None)) +#endif // QT_CONFIG(tooltip) + self.parameter_type_combo_box.setItemText(0, QCoreApplication.translate("MainWindow", u"Value", None)) + self.parameter_type_combo_box.setItemText(1, QCoreApplication.translate("MainWindow", u"Definition", None)) + self.parameter_type_combo_box.setItemText(2, QCoreApplication.translate("MainWindow", u"None", None)) + + self.read_start_row_label.setText(QCoreApplication.translate("MainWindow", u"Read data from row:", None)) + self.ignore_columns_label.setText(QCoreApplication.translate("MainWindow", u"Ignore columns:", None)) + self.map_dimensions_label.setText(QCoreApplication.translate("MainWindow", u"Map dimensions:", None)) +#if QT_CONFIG(tooltip) + self.before_alternative_check_box.setToolTip(QCoreApplication.translate("MainWindow", u"Enable or disable 'Before alternative name' mapping for scenario alternative item type.", None)) +#endif // QT_CONFIG(tooltip) + self.before_alternative_check_box.setText(QCoreApplication.translate("MainWindow", u"Use before alternative", None)) + self.import_entities_check_box.setText(QCoreApplication.translate("MainWindow", u"Import entities", None)) # retranslateUi diff --git a/spine_items/importer/ui/import_editor_window.ui b/spine_items/importer/ui/import_editor_window.ui index e8e01dad..d7700fe4 100644 --- a/spine_items/importer/ui/import_editor_window.ui +++ b/spine_items/importer/ui/import_editor_window.ui @@ -349,58 +349,6 @@ 3 - - - - - - - - - - - Default value type: - - - - - - - Item type: - - - - - - - Read data from row: - - - - - - - Map dimensions: - - - - - - - Enable or disable 'Before alternative name' mapping for scenario alternative item type. - - - Use before alternative - - - - - - - Ignore columns: - - - @@ -435,116 +383,175 @@ - - - - 1 - - - - - - - Number of dimensions when value type is Map. - - - 1 + + + + Default value type: - + - Value + Single value - Definition + Time series - None + Time pattern + + + Map + + + + + Array + + + + + + + + 1 + + + + + + + Item type: + + + + + + + Number of entity dimensions. + + + 0 + + + + + + + Parameter type: + + + + + + + Number of dimensions: + + + + + + + + + + + + + + Set the repeat flag for all imported time series + + + Repeat time series + + + + + + + Import entity alternatives + - + Compress Maps - - - - Import entities + + + + Number of dimensions when value type is Map. + + + 1 - - - - - Single value - - - - - Time series - - + + - Time pattern + Value - Map + Definition - Array + None - - - - Set the repeat flag for all imported time series - + + - Repeat time series + Read data from row: - - + + - Parameter type: + Ignore columns: - + - Number of dimensions: + Map dimensions: - - + + - Number of entity dimensions. + Enable or disable 'Before alternative name' mapping for scenario alternative item type. - - 0 + + Use before alternative + + + + + + + Import entities @@ -625,8 +632,6 @@ map_dimension_spin_box start_read_row_spin_box ignore_columns_button - import_entities_check_box - before_alternative_check_box time_series_repeat_check_box map_compression_check_box mapping_spec_table diff --git a/spine_items/importer/widgets/import_mapping_options.py b/spine_items/importer/widgets/import_mapping_options.py index fe425b4a..0550fc3a 100644 --- a/spine_items/importer/widgets/import_mapping_options.py +++ b/spine_items/importer/widgets/import_mapping_options.py @@ -15,6 +15,7 @@ from .custom_menus import SimpleFilterMenu from ..commands import ( SetImportEntitiesFlag, + SetImportEntityAlternatives, SetItemMappingDimensionCount, SetItemMappingType, SetMapCompressFlag, @@ -62,6 +63,7 @@ def __init__(self, mappings_model, ui, undo_stack): self._ui.import_entities_check_box.stateChanged.connect(self._change_import_entities) self._ui_ignore_columns_filtermenu.filterChanged.connect(self._change_skip_columns) self._ui.start_read_row_spin_box.valueChanged.connect(self._change_read_start_row) + self._ui.import_entity_alternatives_check_box.stateChanged.connect(self._change_import_entity_alternatives) self._ui.time_series_repeat_check_box.stateChanged.connect(self._change_time_series_repeat_flag) self._ui.map_dimension_spin_box.valueChanged.connect(self._change_map_dimension_count) self._ui.map_compression_check_box.stateChanged.connect(self._change_map_compression_flag) @@ -146,12 +148,22 @@ def _update_ui(self): self._ui.class_type_combo_box.setCurrentIndex(class_type_index) # update item mapping settings + if flattened_mappings.may_import_entity_alternatives(): + self._ui.import_entity_alternatives_check_box.setEnabled(True) + check_state = ( + Qt.CheckState.Checked if flattened_mappings.import_entity_alternatives() else Qt.CheckState.Unchecked + ) + self._ui.import_entity_alternatives_check_box.setCheckState(check_state) + else: + self._ui.import_entity_alternatives_check_box.setEnabled(False) + self._ui.import_entity_alternatives_check_box.setCheckState(Qt.CheckState.Unchecked) if flattened_mappings.may_import_entities(): self._ui.import_entities_check_box.setEnabled(True) check_state = Qt.CheckState.Checked if flattened_mappings.import_entities() else Qt.CheckState.Unchecked self._ui.import_entities_check_box.setCheckState(check_state) else: self._ui.import_entities_check_box.setEnabled(False) + self._ui.import_entities_check_box.setCheckState(Qt.CheckState.Unchecked) can_have_dimensions = flattened_mappings.can_have_dimensions() self._ui.dimension_label.setEnabled(can_have_dimensions) self._ui.dimension_spin_box.setEnabled(can_have_dimensions) @@ -340,6 +352,27 @@ def _change_read_start_row(self, row): ) ) + @Slot(int) + def _change_import_entity_alternatives(self, state): + """ + Pushes SetImportEntityAlternatives command to the undo stack. + + Args: + state (Qt.CheckState): New check box state + """ + if self._block_signals or not self._has_current_mappings(): + return + previous_mapping_root = self._list_index.data(Role.FLATTENED_MAPPINGS).root_mapping + self._undo_stack.push( + SetImportEntityAlternatives( + self._list_index.parent().row(), + self._list_index.row(), + self._mappings_model, + state == Qt.CheckState.Checked.value, + previous_mapping_root, + ) + ) + def _change_skip_columns(self, skip_cols): """Pushes :class:`SetSkipColumns` to the undo stack. diff --git a/tests/importer/mvcmodels/test_mappings_model.py b/tests/importer/mvcmodels/test_mappings_model.py index 45df2263..2e1a3026 100644 --- a/tests/importer/mvcmodels/test_mappings_model.py +++ b/tests/importer/mvcmodels/test_mappings_model.py @@ -657,9 +657,9 @@ def test_data_when_mapping_invalid_object_class_with_parameters(self): index = self._model.index(2, 0, self._list_index) self.assertEqual(index.data(), "Entity metadata") index = self._model.index(3, 0, self._list_index) - self.assertEqual(index.data(), "Parameter names") - index = self._model.index(4, 0, self._list_index) self.assertEqual(index.data(), "Alternative names") + index = self._model.index(4, 0, self._list_index) + self.assertEqual(index.data(), "Parameter names") index = self._model.index(5, 0, self._list_index) self.assertEqual(index.data(), "Parameter value metadata") index = self._model.index(6, 0, self._list_index) @@ -697,9 +697,9 @@ def test_data_when_mapping_valid_object_class_with_pivoted_parameters(self): index = self._model.index(2, 0, self._list_index) self.assertEqual(index.data(), "Entity metadata") index = self._model.index(3, 0, self._list_index) - self.assertEqual(index.data(), "Parameter names") - index = self._model.index(4, 0, self._list_index) self.assertEqual(index.data(), "Alternative names") + index = self._model.index(4, 0, self._list_index) + self.assertEqual(index.data(), "Parameter names") index = self._model.index(5, 0, self._list_index) self.assertEqual(index.data(), "Parameter value metadata") index = self._model.index(6, 0, self._list_index) @@ -713,9 +713,9 @@ def test_data_when_mapping_valid_object_class_with_pivoted_parameters(self): index = self._model.index(2, 1, self._list_index) self.assertEqual(index.data(), "None") index = self._model.index(3, 1, self._list_index) - self.assertEqual(index.data(), "Column") - index = self._model.index(4, 1, self._list_index) self.assertEqual(index.data(), "None") + index = self._model.index(4, 1, self._list_index) + self.assertEqual(index.data(), "Column") index = self._model.index(5, 1, self._list_index) self.assertEqual(index.data(), "None") index = self._model.index(6, 1, self._list_index) @@ -735,11 +735,11 @@ def test_data_when_mapping_valid_object_class_with_pivoted_parameters(self): self.assertEqual(index.data(Qt.ItemDataRole.BackgroundRole), None) self.assertFalse(index.data(Qt.ItemDataRole.ToolTipRole)) index = self._model.index(3, 2, self._list_index) - self.assertEqual(index.data(), 2 + 1) + self.assertEqual(index.data(), None) self.assertEqual(index.data(Qt.ItemDataRole.BackgroundRole), None) self.assertFalse(index.data(Qt.ItemDataRole.ToolTipRole)) index = self._model.index(4, 2, self._list_index) - self.assertEqual(index.data(), None) + self.assertEqual(index.data(), 2 + 1) self.assertEqual(index.data(Qt.ItemDataRole.BackgroundRole), None) self.assertFalse(index.data(Qt.ItemDataRole.ToolTipRole)) index = self._model.index(5, 2, self._list_index) @@ -785,9 +785,9 @@ def test_data_when_mapping_valid_object_class_with_parameters(self): index = self._model.index(2, 0, self._list_index) self.assertEqual(index.data(), "Entity metadata") index = self._model.index(3, 0, self._list_index) - self.assertEqual(index.data(), "Parameter names") - index = self._model.index(4, 0, self._list_index) self.assertEqual(index.data(), "Alternative names") + index = self._model.index(4, 0, self._list_index) + self.assertEqual(index.data(), "Parameter names") index = self._model.index(5, 0, self._list_index) self.assertEqual(index.data(), "Parameter value metadata") index = self._model.index(6, 0, self._list_index) @@ -803,9 +803,9 @@ def test_data_when_mapping_valid_object_class_with_parameters(self): index = self._model.index(2, 1, self._list_index) self.assertEqual(index.data(), "None") index = self._model.index(3, 1, self._list_index) - self.assertEqual(index.data(), "Column") - index = self._model.index(4, 1, self._list_index) self.assertEqual(index.data(), "None") + index = self._model.index(4, 1, self._list_index) + self.assertEqual(index.data(), "Column") index = self._model.index(5, 1, self._list_index) self.assertEqual(index.data(), "None") index = self._model.index(6, 1, self._list_index) @@ -827,11 +827,11 @@ def test_data_when_mapping_valid_object_class_with_parameters(self): self.assertEqual(index.data(Qt.ItemDataRole.BackgroundRole), None) self.assertFalse(index.data(Qt.ItemDataRole.ToolTipRole)) index = self._model.index(3, 2, self._list_index) - self.assertEqual(index.data(), 99 + 1) + self.assertEqual(index.data(), None) self.assertEqual(index.data(Qt.ItemDataRole.BackgroundRole), None) self.assertFalse(index.data(Qt.ItemDataRole.ToolTipRole)) index = self._model.index(4, 2, self._list_index) - self.assertEqual(index.data(), None) + self.assertEqual(index.data(), 99 + 1) self.assertEqual(index.data(Qt.ItemDataRole.BackgroundRole), None) self.assertFalse(index.data(Qt.ItemDataRole.ToolTipRole)) index = self._model.index(5, 2, self._list_index) @@ -884,9 +884,9 @@ def test_data_when_valid_object_class_with_nested_map(self): index = self._model.index(2, 0, self._list_index) self.assertEqual(index.data(), "Entity metadata") index = self._model.index(3, 0, self._list_index) - self.assertEqual(index.data(), "Parameter names") - index = self._model.index(4, 0, self._list_index) self.assertEqual(index.data(), "Alternative names") + index = self._model.index(4, 0, self._list_index) + self.assertEqual(index.data(), "Parameter names") index = self._model.index(5, 0, self._list_index) self.assertEqual(index.data(), "Parameter value metadata") index = self._model.index(6, 0, self._list_index) @@ -906,9 +906,9 @@ def test_data_when_valid_object_class_with_nested_map(self): index = self._model.index(2, 1, self._list_index) self.assertEqual(index.data(), "None") index = self._model.index(3, 1, self._list_index) - self.assertEqual(index.data(), "Column") - index = self._model.index(4, 1, self._list_index) self.assertEqual(index.data(), "None") + index = self._model.index(4, 1, self._list_index) + self.assertEqual(index.data(), "Column") index = self._model.index(5, 1, self._list_index) self.assertEqual(index.data(), "None") index = self._model.index(6, 1, self._list_index) @@ -934,11 +934,11 @@ def test_data_when_valid_object_class_with_nested_map(self): self.assertEqual(index.data(Qt.ItemDataRole.BackgroundRole), None) self.assertFalse(index.data(Qt.ItemDataRole.ToolTipRole)) index = self._model.index(3, 2, self._list_index) - self.assertEqual(index.data(), 99 + 1) + self.assertEqual(index.data(), None) self.assertEqual(index.data(Qt.ItemDataRole.BackgroundRole), None) self.assertFalse(index.data(Qt.ItemDataRole.ToolTipRole)) index = self._model.index(4, 2, self._list_index) - self.assertEqual(index.data(), None) + self.assertEqual(index.data(), 99 + 1) self.assertEqual(index.data(Qt.ItemDataRole.BackgroundRole), None) self.assertFalse(index.data(Qt.ItemDataRole.ToolTipRole)) index = self._model.index(5, 2, self._list_index) @@ -1021,9 +1021,9 @@ def test_data_when_mapping_invalid_relationship_class_with_parameters(self): index = self._model.index(3, 0, self._list_index) self.assertEqual(index.data(), "Entity metadata") index = self._model.index(4, 0, self._list_index) - self.assertEqual(index.data(), "Parameter names") - index = self._model.index(5, 0, self._list_index) self.assertEqual(index.data(), "Alternative names") + index = self._model.index(5, 0, self._list_index) + self.assertEqual(index.data(), "Parameter names") index = self._model.index(6, 0, self._list_index) self.assertEqual(index.data(), "Parameter value metadata") index = self._model.index(7, 0, self._list_index) @@ -1079,9 +1079,9 @@ def test_data_when_mapping_multidimensional_relationship_class_with_parameters(s index = self._model.index(5, 0, self._list_index) self.assertEqual(index.data(), "Entity metadata") index = self._model.index(6, 0, self._list_index) - self.assertEqual(index.data(), "Parameter names") - index = self._model.index(7, 0, self._list_index) self.assertEqual(index.data(), "Alternative names") + index = self._model.index(7, 0, self._list_index) + self.assertEqual(index.data(), "Parameter names") index = self._model.index(8, 0, self._list_index) self.assertEqual(index.data(), "Parameter value metadata") index = self._model.index(9, 0, self._list_index) @@ -1103,9 +1103,9 @@ def test_data_when_mapping_multidimensional_relationship_class_with_parameters(s index = self._model.index(5, 1, self._list_index) self.assertEqual(index.data(), "None") index = self._model.index(6, 1, self._list_index) - self.assertEqual(index.data(), "Column") - index = self._model.index(7, 1, self._list_index) self.assertEqual(index.data(), "None") + index = self._model.index(7, 1, self._list_index) + self.assertEqual(index.data(), "Column") index = self._model.index(8, 1, self._list_index) self.assertEqual(index.data(), "None") index = self._model.index(9, 1, self._list_index) @@ -1139,11 +1139,11 @@ def test_data_when_mapping_multidimensional_relationship_class_with_parameters(s self.assertEqual(index.data(Qt.ItemDataRole.BackgroundRole), None) self.assertFalse(index.data(Qt.ItemDataRole.ToolTipRole)) index = self._model.index(6, 2, self._list_index) - self.assertEqual(index.data(), 99 + 1) + self.assertEqual(index.data(), None) self.assertEqual(index.data(Qt.ItemDataRole.BackgroundRole), None) self.assertFalse(index.data(Qt.ItemDataRole.ToolTipRole)) index = self._model.index(7, 2, self._list_index) - self.assertEqual(index.data(), None) + self.assertEqual(index.data(), 99 + 1) self.assertEqual(index.data(Qt.ItemDataRole.BackgroundRole), None) self.assertFalse(index.data(Qt.ItemDataRole.ToolTipRole)) index = self._model.index(8, 2, self._list_index) diff --git a/tests/importer/test_flattened_mappings.py b/tests/importer/test_flattened_mappings.py index 773824bc..ed1cac5f 100644 --- a/tests/importer/test_flattened_mappings.py +++ b/tests/importer/test_flattened_mappings.py @@ -13,7 +13,9 @@ """Unit tests for ``flattened_mappings`` module.""" import unittest from spinedb_api import import_mapping_from_dict -from spine_items.importer.flattened_mappings import FlattenedMappings +from spine_items.importer.flattened_mappings import FlattenedMappings, MappingType +from spinedb_api.import_mapping.import_mapping import default_import_mapping +from spinedb_api.import_mapping.import_mapping_compat import parameter_mapping_from_dict class TestFlattenedMappings(unittest.TestCase): @@ -38,6 +40,323 @@ def test_increasing_relationship_dimensions_keeps_import_objects_flags_consisten flattened_mappings.set_dimension_count(2) self.assertTrue(flattened_mappings.import_entities()) + def test_default_entity_class_mapping(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + self.assertEqual(flattened_mappings.map_type, MappingType.EntityClass) + self.assertEqual(flattened_mappings.display_names, ["Entity class names", "Entity names", "Entity metadata"]) + self.assertIs(flattened_mappings.root_mapping, root_mapping) + self.assertIsNone(flattened_mappings.value_type) + self.assertFalse(flattened_mappings.is_time_series_value()) + self.assertFalse(flattened_mappings.is_map_value()) + self.assertEqual(flattened_mappings.map_dimension_count(), 1) + self.assertFalse(flattened_mappings.uses_before_alternative()) + self.assertEqual(flattened_mappings.read_start_row(), 0) + self.assertEqual(flattened_mappings.skip_columns(), []) + for row in range(len(flattened_mappings.display_names)): + with self.subTest(row=row): + self.assertEqual(flattened_mappings.display_position_type(row), "None") + self.assertIsNone(flattened_mappings.display_position(row)) + self.assertEqual(flattened_mappings.display_row_issues(row), []) + self.assertTrue(flattened_mappings.can_have_dimensions()) + self.assertFalse(flattened_mappings.has_dimensions()) + self.assertEqual(flattened_mappings.dimension_count(), 0) + self.assertTrue(flattened_mappings.may_import_entity_alternatives()) + self.assertFalse(flattened_mappings.import_entity_alternatives()) + self.assertFalse(flattened_mappings.may_import_entities()) + self.assertFalse(flattened_mappings.import_entities()) + self.assertTrue(flattened_mappings.has_parameters()) + self.assertEqual(flattened_mappings.display_parameter_type(), "None") + self.assertFalse(flattened_mappings.has_value_component()) + + def test_add_dimensions_to_entity_class_mapping(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + self.assertFalse(flattened_mappings.has_dimensions()) + self.assertEqual(flattened_mappings.dimension_count(), 0) + flattened_mappings.set_dimension_count(1) + self.assertTrue(flattened_mappings.has_dimensions()) + self.assertEqual(flattened_mappings.dimension_count(), 1) + self.assertEqual( + flattened_mappings.display_names, + ["Entity class names", "Dimension names", "Element names", "Entity metadata"], + ) + self.assertTrue(flattened_mappings.may_import_entities()) + self.assertFalse(flattened_mappings.import_entities()) + flattened_mappings.set_dimension_count(2) + self.assertTrue(flattened_mappings.has_dimensions()) + self.assertEqual(flattened_mappings.dimension_count(), 2) + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Dimension names 1", + "Dimension names 2", + "Element names 1", + "Element names 2", + "Entity metadata", + ], + ) + self.assertTrue(flattened_mappings.may_import_entities()) + self.assertFalse(flattened_mappings.import_entities()) + flattened_mappings.set_dimension_count(0) + self.assertFalse(flattened_mappings.has_dimensions()) + self.assertEqual(flattened_mappings.dimension_count(), 0) + self.assertEqual(flattened_mappings.display_names, ["Entity class names", "Entity names", "Entity metadata"]) + self.assertFalse(flattened_mappings.may_import_entities()) + self.assertFalse(flattened_mappings.import_entities()) + + def test_add_parameter_definition_to_entity_class_mapping(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + self.assertEqual(flattened_mappings.display_parameter_type(), "None") + self.assertFalse(flattened_mappings.has_value_component()) + self.assertEqual(flattened_mappings.value_type_label(), "") + parameter_root = parameter_mapping_from_dict({"map_type": "ParameterDefinition"}) + flattened_mappings.set_parameter_components(parameter_root) + self.assertEqual(flattened_mappings.display_parameter_type(), "Definition") + self.assertTrue(flattened_mappings.has_value_component()) + self.assertEqual(flattened_mappings.value_type_label(), "Default value:") + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Entity names", + "Entity metadata", + "Parameter names", + "Value list names", + "Parameter default values", + ], + ) + + def test_remove_parameter_definition_from_entity_class_mapping(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + parameter_root = parameter_mapping_from_dict({"map_type": "ParameterDefinition"}) + flattened_mappings.set_parameter_components(parameter_root) + flattened_mappings.set_parameter_components(None) + self.assertEqual(flattened_mappings.display_parameter_type(), "None") + self.assertFalse(flattened_mappings.has_value_component()) + self.assertEqual(flattened_mappings.value_type_label(), "") + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Entity names", + "Entity metadata", + ], + ) + + def test_add_parameter_value_to_entity_class_mapping(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + self.assertEqual(flattened_mappings.display_parameter_type(), "None") + self.assertFalse(flattened_mappings.has_value_component()) + self.assertEqual(flattened_mappings.value_type_label(), "") + parameter_root = parameter_mapping_from_dict({"map_type": "ParameterValue"}) + flattened_mappings.set_parameter_components(parameter_root) + self.assertEqual(flattened_mappings.display_parameter_type(), "Value") + self.assertTrue(flattened_mappings.has_value_component()) + self.assertEqual(flattened_mappings.value_type_label(), "Value:") + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Entity names", + "Entity metadata", + "Alternative names", + "Parameter names", + "Parameter value metadata", + "Parameter values", + ], + ) + + def test_remove_parameter_value_from_entity_class_mapping(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + parameter_root = parameter_mapping_from_dict({"map_type": "ParameterValue"}) + flattened_mappings.set_parameter_components(parameter_root) + flattened_mappings.set_parameter_components(None) + self.assertEqual(flattened_mappings.display_parameter_type(), "None") + self.assertFalse(flattened_mappings.has_value_component()) + self.assertEqual(flattened_mappings.value_type_label(), "") + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Entity names", + "Entity metadata", + ], + ) + + def test_add_entity_alternative_to_entity_class_mapping(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + self.assertFalse(flattened_mappings.import_entity_alternatives()) + flattened_mappings.set_import_entity_alternatives(True) + self.assertTrue(flattened_mappings.import_entity_alternatives()) + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Entity names", + "Entity metadata", + "Alternative names", + "Entity activities", + ], + ) + + def test_remove_entity_alternative_from_entity_class_mapping(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + flattened_mappings.set_import_entity_alternatives(True) + flattened_mappings.set_import_entity_alternatives(False) + self.assertFalse(flattened_mappings.import_entity_alternatives()) + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Entity names", + "Entity metadata", + ], + ) + + def test_add_entity_alternative_and_parameter_value_to_entity_class_mapping(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + flattened_mappings.set_import_entity_alternatives(True) + parameter_root = parameter_mapping_from_dict({"map_type": "ParameterValue"}) + flattened_mappings.set_parameter_components(parameter_root) + self.assertTrue(flattened_mappings.import_entity_alternatives()) + self.assertEqual(flattened_mappings.display_parameter_type(), "Value") + self.assertTrue(flattened_mappings.has_value_component()) + self.assertEqual(flattened_mappings.value_type_label(), "Value:") + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Entity names", + "Entity metadata", + "Alternative names", + "Entity activities", + "Parameter names", + "Parameter value metadata", + "Parameter values", + ], + ) + + def test_add_parameter_value_and_entity_alternative_to_entity_class_mapping(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + parameter_root = parameter_mapping_from_dict({"map_type": "ParameterValue"}) + flattened_mappings.set_parameter_components(parameter_root) + flattened_mappings.set_import_entity_alternatives(True) + self.assertTrue(flattened_mappings.import_entity_alternatives()) + self.assertEqual(flattened_mappings.display_parameter_type(), "Value") + self.assertTrue(flattened_mappings.has_value_component()) + self.assertEqual(flattened_mappings.value_type_label(), "Value:") + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Entity names", + "Entity metadata", + "Alternative names", + "Entity activities", + "Parameter names", + "Parameter value metadata", + "Parameter values", + ], + ) + + def test_remove_parameter_value_from_entity_class_mapping_with_entity_alternative(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + parameter_root = parameter_mapping_from_dict({"map_type": "ParameterValue"}) + flattened_mappings.set_parameter_components(parameter_root) + flattened_mappings.set_import_entity_alternatives(True) + flattened_mappings.set_parameter_components(None) + self.assertTrue(flattened_mappings.import_entity_alternatives()) + self.assertEqual(flattened_mappings.display_parameter_type(), "None") + self.assertFalse(flattened_mappings.has_value_component()) + self.assertEqual(flattened_mappings.value_type_label(), "") + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Entity names", + "Entity metadata", + "Alternative names", + "Entity activities", + ], + ) + + def test_remove_entity_alternative_from_entity_class_mapping_with_parameter_value(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + parameter_root = parameter_mapping_from_dict({"map_type": "ParameterValue"}) + flattened_mappings.set_parameter_components(parameter_root) + flattened_mappings.set_import_entity_alternatives(True) + flattened_mappings.set_import_entity_alternatives(False) + self.assertFalse(flattened_mappings.import_entity_alternatives()) + self.assertEqual(flattened_mappings.display_parameter_type(), "Value") + self.assertTrue(flattened_mappings.has_value_component()) + self.assertEqual(flattened_mappings.value_type_label(), "Value:") + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Entity names", + "Entity metadata", + "Alternative names", + "Parameter names", + "Parameter value metadata", + "Parameter values", + ], + ) + + def test_remove_entity_alternative_and_parameter_value_from_entity_class_mapping(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + parameter_root = parameter_mapping_from_dict({"map_type": "ParameterValue"}) + flattened_mappings.set_parameter_components(parameter_root) + flattened_mappings.set_import_entity_alternatives(True) + flattened_mappings.set_import_entity_alternatives(False) + flattened_mappings.set_parameter_components(None) + self.assertFalse(flattened_mappings.import_entity_alternatives()) + self.assertEqual(flattened_mappings.display_parameter_type(), "None") + self.assertFalse(flattened_mappings.has_value_component()) + self.assertEqual(flattened_mappings.value_type_label(), "") + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Entity names", + "Entity metadata", + ], + ) + + def test_remove_parameter_value_and_entity_alternative_from_entity_class_mapping(self): + root_mapping = default_import_mapping("EntityClass") + flattened_mappings = FlattenedMappings(root_mapping) + parameter_root = parameter_mapping_from_dict({"map_type": "ParameterValue"}) + flattened_mappings.set_parameter_components(parameter_root) + flattened_mappings.set_import_entity_alternatives(True) + flattened_mappings.set_parameter_components(None) + flattened_mappings.set_import_entity_alternatives(False) + self.assertFalse(flattened_mappings.import_entity_alternatives()) + self.assertEqual(flattened_mappings.display_parameter_type(), "None") + self.assertFalse(flattened_mappings.has_value_component()) + self.assertEqual(flattened_mappings.value_type_label(), "") + self.assertEqual( + flattened_mappings.display_names, + [ + "Entity class names", + "Entity names", + "Entity metadata", + ], + ) + if __name__ == "__main__": unittest.main()