Skip to content

Commit

Permalink
Merge pull request #6735 from ales-erjavec/test-owcsvimport-cleanup
Browse files Browse the repository at this point in the history
[FIX] test_owcsvimport: Workaround for segfault in tests on macos
  • Loading branch information
PrimozGodec committed Feb 29, 2024
2 parents 194572b + febf466 commit ba951fa
Showing 1 changed file with 53 additions and 40 deletions.
93 changes: 53 additions & 40 deletions Orange/widgets/data/tests/test_owcsvimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,34 @@ def test_browse_prefix_parent(self):
mb.assert_called()
self.assertIsNone(widget.current_item())

@staticmethod
@contextmanager
def activate_recent_and_get_dialog(
widget: OWCSVFileImport, recent_index: int = 0
) -> QFileDialog:
"""
Activate the recent item (which MUST be missing on FS) and
yield the QFileDialog which is shown.
"""
browse_dialog = widget._browse_dialog
with mock.patch.object(widget, "_browse_dialog") as r:
dlg = browse_dialog()
# segfaults in tests when using 'sheet' dialog on macos when parent
# is destroyed (before the dialog fully hides - animation)?
dlg.setParent(None)
# calling selectFile when using native (macOS) dialog does not have
# an effect - at least not immediately;
dlg.setOption(QFileDialog.DontUseNativeDialog)
r.return_value = dlg
with mock.patch.object(dlg, "open") as r:
widget.activate_recent(recent_index)
r.assert_called()
try:
yield dlg
finally:
dlg.deleteLater()
return

def test_browse_for_missing(self):
missing = os.path.dirname(__file__) + "/this file does not exist.csv"
widget = self.create_widget(
Expand All @@ -292,19 +320,14 @@ def test_browse_for_missing(self):
]
}
)
widget.activate_recent(0)
dlg = widget.findChild(QFileDialog)
assert dlg is not None
# calling selectFile when using native (macOS) dialog does not have
# an effect - at least not immediately;
dlg.setOption(QFileDialog.DontUseNativeDialog)
dlg.selectFile(self.data_regions_path)
dlg.accept()
cur = widget.current_item()
self.assertTrue(samepath(self.data_regions_path, cur.path()))
self.assertEqual(
self.data_regions_options.as_dict(), cur.options().as_dict()
)
with self.activate_recent_and_get_dialog(widget) as dlg:
dlg.selectFile(self.data_regions_path)
dlg.accept()
cur = widget.current_item()
self.assertTrue(samepath(self.data_regions_path, cur.path()))
self.assertEqual(
self.data_regions_options.as_dict(), cur.options().as_dict()
)

def test_browse_for_missing_prefixed(self):
path = self.data_regions_path
Expand All @@ -318,21 +341,16 @@ def test_browse_for_missing_prefixed(self):
},
env={"basedir": basedir}
)
widget.activate_recent(0)
dlg = widget.findChild(QFileDialog)
assert dlg is not None
# calling selectFile when using native (macOS) dialog does not have
# an effect - at least not immediately;
dlg.setOption(QFileDialog.DontUseNativeDialog)
dlg.selectFile(path)
dlg.accept()
cur = widget.current_item()
self.assertTrue(samepath(path, cur.path()))
self.assertEqual(
cur.varPath(), PathItem.VarPath("basedir", "data-regions.tab"))
self.assertEqual(
self.data_regions_options.as_dict(), cur.options().as_dict()
)
with self.activate_recent_and_get_dialog(widget) as dlg:
dlg.selectFile(path)
dlg.accept()
cur = widget.current_item()
self.assertTrue(samepath(path, cur.path()))
self.assertEqual(
cur.varPath(), PathItem.VarPath("basedir", "data-regions.tab"))
self.assertEqual(
self.data_regions_options.as_dict(), cur.options().as_dict()
)

def test_browse_for_missing_prefixed_parent(self):
path = self.data_regions_path
Expand All @@ -348,18 +366,13 @@ def test_browse_for_missing_prefixed_parent(self):
env={"basedir": basedir}
)
mb = widget._path_must_be_relative_mb = mock.Mock()
widget.activate_recent(0)
dlg = widget.findChild(QFileDialog)
assert dlg is not None
# calling selectFile when using native (macOS) dialog does not have
# an effect - at least not immediately;
dlg.setOption(QFileDialog.DontUseNativeDialog)
dlg.selectFile(path)
dlg.accept()
mb.assert_called()
cur = widget.current_item()
self.assertEqual(item[0], cur.varPath())
self.assertEqual(item[1].as_dict(), cur.options().as_dict())
with self.activate_recent_and_get_dialog(widget) as dlg:
dlg.selectFile(path)
dlg.accept()
mb.assert_called()
cur = widget.current_item()
self.assertEqual(item[0], cur.varPath())
self.assertEqual(item[1].as_dict(), cur.options().as_dict())

def test_long_data(self):
with tempfile.TemporaryDirectory() as tmp:
Expand Down

0 comments on commit ba951fa

Please sign in to comment.