Skip to content

Commit

Permalink
Fix scenario alternatives not removed correctly (#2834)
Browse files Browse the repository at this point in the history
* Fix scenario alternatives not removed correctly

When deletions were made in a scenario so that the rank order changed,
all the scenario alternatives were not removed. This caused an error
when the updated scen alt with the new rank number was attempted to
be added back to the db.

* Back to default requirements
  • Loading branch information
PiispaH authored Jun 12, 2024
1 parent ee96477 commit 2854432
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/test_SpineDBManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pathlib import Path
from tempfile import TemporaryDirectory
import unittest
from unittest import mock
from unittest.mock import MagicMock
from PySide6.QtCore import Qt, QSettings
from PySide6.QtWidgets import QApplication
Expand Down Expand Up @@ -556,5 +557,54 @@ def test_updating_indexed_value_changes_the_unparsed_value_in_database(self):
self.assertEqual(update_value, Map(["a"], ["c"]))


class TestRemoveScenarioAlternative(unittest.TestCase):
@classmethod
def setUpClass(cls):
if not QApplication.instance():
QApplication()

def setUp(self):
mock_settings = MagicMock()
mock_settings.value.side_effect = lambda *args, **kwargs: 0
self._db_mngr = SpineDBManager(mock_settings, None)
self._logger = MagicMock()
self._db_map = self._db_mngr.get_db_map("sqlite://", self._logger, codename="database", create=True)

def tearDown(self):
self._db_mngr.close_all_sessions()
while not self._db_map.closed:
QApplication.processEvents()
self._db_mngr.clean_up()

def _assert_success(self, result):
item, error = result
self.assertIsNone(error)
return item

def test_removing_scenario_alternative_and_committing(self):
"""Test that removing non-rank 1 scenario alternative works."""
scenario = self._assert_success(self._db_map.add_scenario_item(name="scenario"))
base = self._db_map.get_alternative_item(name="Base")
next_level = self._assert_success(self._db_map.add_alternative_item(name="Next level"))
scen_alt_1 = self._assert_success(
self._db_map.add_scenario_alternative_item(scenario_id=scenario["id"], alternative_id=base["id"], rank=1)
)
scen_alt_2 = self._assert_success(
self._db_map.add_scenario_alternative_item(
scenario_id=scenario["id"], alternative_id=next_level["id"], rank=2
)
)
self._db_mngr.commit_session("Added data.", self._db_map)
db_map_scen_alt_data = {
self._db_map: [{"alternative_id_list": [scen_alt_2["alternative_id"]], "id": scenario["id"]}]
}
db_map_typed_data_to_rm = {self._db_map: {"scenario": set()}}
self._db_mngr.error_msg = MagicMock()
self._db_mngr.set_scenario_alternatives(db_map_scen_alt_data)
self._db_mngr.remove_items(db_map_typed_data_to_rm)
self._db_mngr.commit_session("Remove scenario alternative", self._db_map)
self._db_mngr.error_msg.emit.assert_not_called()


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

0 comments on commit 2854432

Please sign in to comment.