Skip to content

Commit

Permalink
Fix import mapping when pivoting by header
Browse files Browse the repository at this point in the history
Import mappings should ignore the position of last mapping if
the mappings are pivoted. This did not happen, however, when
one of the other mappings was marked as header and there were
no explicitly set pivoted mappings. We now take this special
case into account.

Re spine-tools/Spine-Toolbox#2224
  • Loading branch information
soininen committed Aug 9, 2023
1 parent 4168c30 commit f23e417
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion spinedb_api/import_mapping/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def _split_mapping(mapping):
non_pivoted = []
pivoted_from_header = []
for m in flattened:
if pivoted and m is flattened[-1]:
if (pivoted or pivoted_from_header) and m is flattened[-1]:
# If any other mapping is pivoted, ignore last mapping's position
break
if m.position == Position.header and m.value is None:
Expand Down
1 change: 1 addition & 0 deletions spinedb_api/import_mapping/import_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ImportKey(Enum):

def __str__(self):
name = {
self.ALTERNATIVE_NAME.value: "Alternative names",
self.CLASS_NAME.value: "Class names",
self.OBJECT_CLASS_NAME.value: "Object class names",
self.OBJECT_NAME.value: "Object names",
Expand Down
37 changes: 37 additions & 0 deletions tests/import_mapping/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,43 @@ def test_arrays_get_imported_correctly_when_objects_are_in_header_and_alternativ
},
)

def test_header_position_is_ignored_in_last_mapping_if_other_mappings_are_in_header(self):
header = ["Dimension", "parameter1", "parameter2"]
data_source = iter([["d1", 1.1, -2.3], ["d2", -1.1, 2.3]])
mappings = [
[
{"map_type": "ObjectClass", "position": "table_name"},
{"map_type": "Object", "position": 0},
{"map_type": "ObjectMetadata", "position": "hidden"},
{"map_type": "ParameterDefinition", "position": "header"},
{"map_type": "Alternative", "position": "hidden", "value": "Base"},
{"map_type": "ParameterValueMetadata", "position": "hidden"},
{"map_type": "ParameterValue", "position": "header"},
]
]
convert_function_specs = {0: "string", 1: "float", 2: "float"}
convert_functions = {column: value_to_convert_spec(spec) for column, spec in convert_function_specs.items()}

mapped_data, errors = get_mapped_data(
data_source, mappings, header, table_name="Data", column_convert_fns=convert_functions
)
self.assertEqual(errors, [])
self.assertEqual(
mapped_data,
{
"alternatives": {"Base"},
"object_classes": {"Data"},
"object_parameter_values": [
["Data", "d1", "parameter1", 1.1, "Base"],
["Data", "d1", "parameter2", -2.3, "Base"],
["Data", "d2", "parameter1", -1.1, "Base"],
["Data", "d2", "parameter2", 2.3, "Base"],
],
"object_parameters": [("Data", "parameter1"), ("Data", "parameter2")],
"objects": {("Data", "d1"), ("Data", "d2")},
},
)


if __name__ == '__main__':
unittest.main()

0 comments on commit f23e417

Please sign in to comment.