1- # pylint: disable=no-self-use,protected-access
1+ # pylint: disable=no-self-use,protected-access,invalid-name,arguments-differ
22import unittest
33from unittest import mock
4- from contextlib import ExitStack
4+ from contextlib import ExitStack , contextmanager
55
66import os
77import io
2424from Orange .widgets .tests .base import WidgetTest , GuiTest
2525from Orange .widgets .data import owcsvimport
2626from Orange .widgets .data .owcsvimport import (
27- pandas_to_table , ColumnType , RowSpec
27+ OWCSVFileImport , pandas_to_table , ColumnType , RowSpec ,
2828)
2929from Orange .widgets .utils .pathutils import PathItem , samepath
3030from Orange .widgets .utils .settings import QSettings_writeArray
@@ -223,9 +223,9 @@ def test_backward_compatibility(self):
223223 self .assertIsInstance (domain ["numeric2" ], ContinuousVariable )
224224 self .assertIsInstance (domain ["string" ], StringVariable )
225225
226- def test_browse ( self ):
227- widget = self . widget
228- path = self . data_regions_path
226+ @ staticmethod
227+ @ contextmanager
228+ def _browse_setup ( widget : OWCSVFileImport , path : str ):
229229 browse_dialog = widget ._browse_dialog
230230 with mock .patch .object (widget , "_browse_dialog" ) as r :
231231 dlg = browse_dialog ()
@@ -235,33 +235,44 @@ def test_browse(self):
235235 r .return_value = dlg
236236 with mock .patch .object (owcsvimport .CSVImportDialog , "exec_" ,
237237 lambda _ : QFileDialog .Accepted ):
238- widget .browse ()
238+ yield
239+
240+ def test_browse (self ):
241+ widget = self .widget
242+ path = self .data_regions_path
243+ with self ._browse_setup (widget , path ):
244+ widget .browse ()
239245 cur = widget .current_item ()
240246 self .assertIsNotNone (cur )
241247 self .assertTrue (samepath (cur .path (), path ))
242248
243249 def test_browse_prefix (self ):
244250 widget = self .widget
245251 path = self .data_regions_path
246- browse_dialog = widget ._browse_dialog
247- with mock .patch .object (widget , "_browse_dialog" ) as r :
248- dlg = browse_dialog ()
249- dlg .setOption (QFileDialog .DontUseNativeDialog )
250- dlg .selectFile (path )
251- dlg .exec_ = dlg .exec = lambda : QFileDialog .Accepted
252- r .return_value = dlg
253- with mock .patch .object (owcsvimport .CSVImportDialog , "exec_" ,
254- lambda _ : QFileDialog .Accepted ):
255- dir = os .path .dirname (__file__ )
256- widget .workflowEnv = lambda : {"basedir" : dir }
257- widget .workflowEnvChanged ("basedir" , dir , "" )
258- widget .browse_relative (prefixname = "basedir" )
252+ with self ._browse_setup (widget , path ):
253+ basedir = os .path .dirname (__file__ )
254+ widget .workflowEnv = lambda : {"basedir" : basedir }
255+ widget .workflowEnvChanged ("basedir" , basedir , "" )
256+ widget .browse_relative (prefixname = "basedir" )
259257
260258 cur = widget .current_item ()
261259 self .assertIsNotNone (cur )
262260 self .assertTrue (samepath (cur .path (), path ))
263261 self .assertIsInstance (cur .varPath (), PathItem .VarPath )
264262
263+ def test_browse_prefix_parent (self ):
264+ widget = self .widget
265+ path = self .data_regions_path
266+
267+ with self ._browse_setup (widget , path ):
268+ basedir = os .path .join (os .path .dirname (__file__ ), "bs" )
269+ widget .workflowEnv = lambda : {"basedir" : basedir }
270+ widget .workflowEnvChanged ("basedir" , basedir , "" )
271+ mb = widget ._path_must_be_relative_mb = mock .Mock ()
272+ widget .browse_relative (prefixname = "basedir" )
273+ mb .assert_called ()
274+ self .assertIsNone (widget .current_item ())
275+
265276 def test_browse_for_missing (self ):
266277 missing = os .path .dirname (__file__ ) + "/this file does not exist.csv"
267278 widget = self .create_widget (
@@ -279,13 +290,10 @@ def test_browse_for_missing(self):
279290 dlg .setOption (QFileDialog .DontUseNativeDialog )
280291 dlg .selectFile (self .data_regions_path )
281292 dlg .accept ()
282- self .assertTrue (samepath (
283- self .data_regions_path ,
284- widget .recent_combo .currentData (owcsvimport .ImportItem .PathRole )
285- ))
293+ cur = widget .current_item ()
294+ self .assertTrue (samepath (self .data_regions_path , cur .path ()))
286295 self .assertEqual (
287- self .data_regions_options .as_dict (),
288- widget .recent_combo .currentData (owcsvimport .ImportItem .OptionsRole ).as_dict ()
296+ self .data_regions_options .as_dict (), cur .options ().as_dict ()
289297 )
290298
291299
0 commit comments