Skip to content

Commit

Permalink
Fix opening DB editor tab when editor window has a empty tab only (#3003
Browse files Browse the repository at this point in the history
)
  • Loading branch information
soininen authored Nov 7, 2024
2 parents 554ef57 + d2275e9 commit 3599115
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def _get_existing_spine_db_editor(db_urls):
for multi_db_editor in db_editor_registry.windows():
for k in range(multi_db_editor.tab_widget.count()):
db_editor = multi_db_editor.tab_widget.widget(k)
if all(url in db_urls for url in db_editor.db_urls):
if db_editor.db_urls and all(url in db_urls for url in db_editor.db_urls):
return multi_db_editor, db_editor
return None

Expand Down
2 changes: 1 addition & 1 deletion spinetoolbox/ui_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ def open_specification_file(self, index):

@Slot(bool)
def new_db_editor(self):
editor = MultiSpineDBEditor(self.db_mngr, {})
editor = MultiSpineDBEditor(self.db_mngr, [])
editor.show()

@Slot()
Expand Down
54 changes: 43 additions & 11 deletions tests/spine_db_editor/widgets/test_multi_spine_db_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"""Unit tests for SpineDBEditor classes."""
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch
from PySide6.QtCore import QPoint, QSettings
from PySide6.QtWidgets import QApplication
from spinetoolbox.spine_db_editor.editors import db_editor_registry
from spinetoolbox.multi_tab_windows import MultiTabWindowRegistry
from spinetoolbox.spine_db_editor.widgets.multi_spine_db_editor import MultiSpineDBEditor, open_db_editor
from spinetoolbox.spine_db_manager import SpineDBManager
from tests.mock_helpers import FakeDataStore, TestCaseWithQApplication, clean_up_toolbox, create_toolboxui_with_project
Expand Down Expand Up @@ -54,6 +54,7 @@ def setUp(self):
self._db_url = "sqlite:///" + str(db_path)
self._db_mngr = SpineDBManager(QSettings(), None)
self._logger = MagicMock()
self._db_editor_registry = MultiTabWindowRegistry()

def tearDown(self):
self._db_mngr.close_all_sessions()
Expand All @@ -69,14 +70,45 @@ def tearDown(self):
else:
running = False

def test_open_db_editor(self):
self.assertFalse(db_editor_registry.has_windows())
open_db_editor([self._db_url], self._db_mngr, reuse_existing_editor=True)
self.assertEqual(len(db_editor_registry.windows()), 1)
open_db_editor([self._db_url], self._db_mngr, reuse_existing_editor=True)
self.assertEqual(len(db_editor_registry.windows()), 1)
editor = db_editor_registry.windows()[0]
self.assertEqual(editor.tab_widget.count(), 1)
for editor in db_editor_registry.windows():
def _close_windows(self):
for editor in self._db_editor_registry.windows():
QApplication.processEvents()
editor.close()
self.assertFalse(self._db_editor_registry.has_windows())

def test_open_db_editor(self):
with (
patch(
"spinetoolbox.spine_db_editor.widgets.multi_spine_db_editor.db_editor_registry",
self._db_editor_registry,
),
patch("spinetoolbox.spine_db_editor.widgets.multi_spine_db_editor.MultiSpineDBEditor.show") as mock_show,
):
self.assertFalse(self._db_editor_registry.has_windows())
open_db_editor([self._db_url], self._db_mngr, reuse_existing_editor=True)
mock_show.assert_called_once()
self.assertEqual(len(self._db_editor_registry.windows()), 1)
open_db_editor([self._db_url], self._db_mngr, reuse_existing_editor=True)
self.assertEqual(len(self._db_editor_registry.windows()), 1)
editor = self._db_editor_registry.windows()[0]
self.assertEqual(editor.tab_widget.count(), 1)
self._close_windows()

def test_open_db_in_tab_when_editor_has_an_empty_tab(self):
with (
patch(
"spinetoolbox.spine_db_editor.widgets.multi_spine_db_editor.db_editor_registry",
self._db_editor_registry,
),
patch("spinetoolbox.spine_db_editor.widgets.multi_spine_db_editor.MultiSpineDBEditor.show") as mock_show,
):
self.assertFalse(self._db_editor_registry.has_windows())
window = MultiSpineDBEditor(self._db_mngr, [])
self.assertEqual(window.tab_widget.count(), 1)
tab = window.tab_widget.widget(0)
self.assertEqual(tab.db_urls, [])
open_db_editor([self._db_url], self._db_mngr, reuse_existing_editor=True)
self.assertEqual(window.tab_widget.count(), 2)
tab = window.tab_widget.widget(1)
self.assertEqual(tab.db_urls, [self._db_url])
self._close_windows()

0 comments on commit 3599115

Please sign in to comment.