Skip to content

Commit

Permalink
Fix importing pivoted data with certain mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
soininen committed Oct 29, 2024
1 parent b7b5039 commit 34c8ead
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
3 changes: 1 addition & 2 deletions spinedb_api/import_mapping/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ def get_mapped_data(
)
if not unpivoted_column_pos:
continue
if not is_pivoted(last.position):
last.position = -1
last.position = -1
# Reposition row convert functions
row_convert_fns = {k: row_convert_fns[pos] for k, pos in enumerate(pivoted_pos) if pos in row_convert_fns}
# If there are only pivoted mappings, we can just feed the unpivoted rows
Expand Down
32 changes: 32 additions & 0 deletions tests/import_mapping/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,38 @@ def test_skip_first_row_when_importing_pivoted_data(self):
{"scenario_alternatives": [["Scenario1", "Base"], ["Scenario1", "fixed_prices"]]},
)

def test_leaf_mapping_with_position_on_row_is_still_considered_as_pivoted(self):
data_source = iter(
[
[None, "Scenario1", "Scenario2"],
["Base", "Base", "Base"],
["alternative 1", "alt1", "alt1"],
["alternative 1", None, "alt2"],
]
)
mappings = [
[
{"map_type": "Scenario", "position": -1},
{"map_type": "ScenarioAlternative", "position": -3},
]
]
convert_function_specs = {0: "string", 1: "string", 2: "string"}
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, column_convert_fns=convert_functions)
self.assertEqual(errors, [])
self.assertEqual(
mapped_data,
{
"scenario_alternatives": [
["Scenario1", "Base"],
["Scenario1", "alt1"],
["Scenario2", "Base"],
["Scenario2", "alt1"],
["Scenario2", "alt2"],
]
},
)


if __name__ == "__main__":
unittest.main()
5 changes: 1 addition & 4 deletions tests/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
# this program. If not, see <http://www.gnu.org/licenses/>.
######################################################################################################################

"""
Unit tests for :mod:`spinedb_api.mapping`.
"""
""" Unit tests for :mod:`spinedb_api.mapping`. """
import unittest
from spinedb_api.mapping import Mapping, Position, unflatten, value_index

Expand Down

0 comments on commit 34c8ead

Please sign in to comment.