def test_select_reader(self): filename = FileFormat.locate("iris.tab", dataset_dirs) # a setting which adds a new qualified name to the reader combo no_class = RecentPath(filename, None, None, file_format="not.a.file.reader.class") self.widget = self.create_widget(OWFile, stored_settings={"recent_paths": [no_class]}) self.widget.load_data() len_with_qname = len(self.widget.reader_combo) self.assertEqual(self.widget.reader_combo.currentText(), "not.a.file.reader.class") self.assertEqual(self.widget.reader, None) # select the last option, the same reader self.widget.reader_combo.activated.emit(len_with_qname - 1) self.assertEqual(len(self.widget.reader_combo), len_with_qname) self.assertEqual(self.widget.reader_combo.currentText(), "not.a.file.reader.class") self.assertEqual(self.widget.reader, None) # select the tab reader for i in range(len_with_qname): text = self.widget.reader_combo.itemText(i) if text.startswith("Tab-separated"): break self.widget.reader_combo.activated.emit(i) self.assertEqual(len(self.widget.reader_combo), len_with_qname - 1) self.assertTrue(self.widget.reader_combo.currentText().startswith("Tab-separated")) self.assertIsInstance(self.widget.reader, TabReader) # select the default reader self.widget.reader_combo.activated.emit(0) self.assertEqual(len(self.widget.reader_combo), len_with_qname - 1) self.assertEqual(self.widget.reader_combo.currentText(), DEFAULT_READER_TEXT) self.assertIsInstance(self.widget.reader, TabReader)
def test_select_reader_errors(self): filename = FileFormat.locate("iris.tab", dataset_dirs) no_class = RecentPath(filename, None, None, file_format="Orange.data.io.ExcelReader") self.widget = self.create_widget(OWFile, stored_settings={"recent_paths": [no_class]}) self.widget.load_data() self.assertIn("Excel", self.widget.reader_combo.currentText()) self.assertTrue(self.widget.Error.unknown.is_shown()) self.assertFalse(self.widget.Error.missing_reader.is_shown())
def test_files_relocated_on_saved_workflow(self): tempdir = tempfile.mkdtemp() try: oiris = FileFormat.locate("iris.tab", dataset_dirs) ciris = os.path.join(tempdir, "iris.tab") shutil.copy(oiris, ciris) with patch("Orange.widgets.widget.OWWidget.workflowEnv", Mock(return_value={"basedir": tempdir})): self.load_files(ciris) self.assertEqual(self.widget.recent_paths[0].relpath, "iris.tab") finally: shutil.rmtree(tempdir)
def load_files(self, *files, reader=None): files = [FileFormat.locate(name, dataset_dirs) for name in files] def open_with_no_specific_format(a, b, c, filters, e): return files, filters.split(";;")[0] def open_with_specific_format(a, b, c, filters, e): return files, format_filter(reader) patchfn = open_with_no_specific_format if reader is None else open_with_specific_format # pretend that files were chosen in the open dialog with patch("AnyQt.QtWidgets.QFileDialog.getOpenFileNames", patchfn): self.widget.browse_files()
def test_missing_files_do_not_disappear(self): tempdir = tempfile.mkdtemp() try: oiris = FileFormat.locate("iris.tab", dataset_dirs) ciris = os.path.join(tempdir, "iris.tab") shutil.copy(oiris, ciris) self.load_files(ciris) settings = self.widget.settingsHandler.pack_data(self.widget) finally: shutil.rmtree(tempdir) self.widget = self.create_widget(OWMultifile, stored_settings=settings) assert not os.path.exists(ciris) self.assertEqual(1, len(self.widget.recent_paths)) self.assertTrue(self.widget.Error.file_not_found.is_shown()) self.assertIsNone(self.get_output(OWMultifile.Outputs.data)) self.assertEqual("File not found.", self.widget.lb.item(0).toolTip()) self.assertEqual(Qt.red, self.widget.lb.item(0).foreground())
def open_dataset(self, name): filename = FileFormat.locate(name, dataset_dirs) self.widget.add_path(filename) self.widget.load_data()
def load_files(self, *files): files = [FileFormat.locate(name, dataset_dirs) for name in files] self.widget.load_files(files)
def locate_dataset(fn): return FileFormat.locate(fn, dataset_dirs)