def test_save_uncompressed(self): widget = self.widget widget.auto_save = False spiris = Table("iris") spiris.X = sp.csr_matrix(spiris.X) for selected_filter, writer in widget.filters.items(): widget.write = writer ext = writer.EXTENSIONS[0] with named_file("", suffix=ext) as filename: widget.get_save_filename = Mock( return_value=(filename, selected_filter)) self.send_signal(widget.Inputs.data, self.iris) widget.save_file_as() self.assertEqual(len(Table(filename)), 150) if writer.SUPPORT_SPARSE_DATA: self.send_signal(widget.Inputs.data, spiris) widget.save_file() self.assertEqual(len(Table(filename)), 150)
def test_domain_editor_conversions(self): dat = """V0\tV1\tV2\tV3\tV4\tV5\tV6 c\tc\td\td\tc\td\td \t \t \t \t \t \t 3.0\t1.0\t4\ta\t0.0\tx\t1.0 1.0\t2.0\t4\tb\t0.0\ty\t2.0 2.0\t1.0\t7\ta\t0.0\ty\t2.0 0.0\t2.0\t7\ta\t0.0\tz\t2.0""" with named_file(dat, suffix=".tab") as filename: self.open_dataset(filename) data1 = self.get_output(self.widget.Outputs.data) model = self.widget.domain_editor.model() # check the ordering of attributes for i, a in enumerate(data1.domain.attributes): self.assertEqual( str(a), model.data(model.createIndex(i, 0), Qt.DisplayRole)) # make conversions model.setData(model.createIndex(0, 1), "categorical", Qt.EditRole) model.setData(model.createIndex(1, 1), "text", Qt.EditRole) model.setData(model.createIndex(2, 1), "numeric", Qt.EditRole) model.setData(model.createIndex(3, 1), "numeric", Qt.EditRole) model.setData(model.createIndex(6, 1), "numeric", Qt.EditRole) self.widget.apply_button.click() data2 = self.get_output(self.widget.Outputs.data) # round continuous values should be converted to integers (3.0 -> 3, "3") self.assertEqual(len(data2.domain.attributes[0].values[0]), 1) self.assertEqual(len(data2[0].metas[0]), 1) # discrete integer values should stay the same after conversion to continuous self.assertAlmostEqual(float(data1[0][2].value), data2[0][1]) # discrete round floats should stay the same after conversion to continuous self.assertAlmostEqual(float(data1[0][6].value), data2[0][5])
def test_no_reader_extension(self): with named_file("", suffix=".xyz_unknown") as fn: no_reader = RecentPath(fn, None, None) self.widget = self.create_widget(OWFile, stored_settings={"recent_paths": [no_reader]}) self.widget.load_data() self.assertTrue(self.widget.Error.missing_reader.is_shown())
def test_no_reader_extension(self): with named_file("", suffix=".xyz_unknown") as fn: no_reader = RecentPath(fn, None, None) self.widget = self.create_widget( OWFile, stored_settings={"recent_paths": [no_reader]}) self.widget.load_data() self.assertTrue(self.widget.Error.missing_reader.is_shown())
def test_dragEnterEvent_rejects_binary(self): with named_file("", suffix=".42") as fn: with open(fn, "wb") as f: f.write(b"\xc3\x28") event = self._drag_enter_event(QUrl.fromLocalFile(fn)) self.widget.dragEnterEvent(event) self.assertFalse(event.isAccepted())
def test_check_datetime_disabled(self): """ Datetime option is disable if numerical is disabled as well. GH-2050 (code fixes) GH-2120 """ dat = """\ 01.08.16\t42.15\tneumann\t2017-02-20 03.08.16\t16.08\tneumann\t2017-02-21 04.08.16\t23.04\tneumann\t2017-02-22 03.09.16\t48.84\tturing\t2017-02-23 02.02.17\t23.16\tturing\t2017-02-24""" with named_file(dat, suffix=".tab") as filename: self.open_dataset(filename) domain_editor = self.widget.domain_editor idx = lambda x: self.widget.domain_editor.model().createIndex(x, 1) qcombobox = QComboBox() combo = ComboDelegate(domain_editor, VarTableModel.typenames).createEditor( qcombobox, None, idx(2)) vartype_delegate = VarTypeDelegate(domain_editor, VarTableModel.typenames) vartype_delegate.setEditorData(combo, idx(2)) counts = [4, 2, 4, 2] for i in range(4): vartype_delegate.setEditorData(combo, idx(i)) self.assertEqual(combo.count(), counts[i])
def test_no_specified_reader(self): with named_file("", suffix=".tab") as fn: no_class = RecentPath(fn, 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() self.assertTrue(self.widget.Error.missing_reader.is_shown())
def test_domain_editor_conversions(self): dat = """V0\tV1\tV2\tV3\tV4\tV5\tV6 c\tc\td\td\tc\td\td \t \t \t \t \t \t 3.0\t1.0\t4\ta\t0.0\tx\t1.0 1.0\t2.0\t4\tb\t0.0\ty\t2.0 2.0\t1.0\t7\ta\t0.0\ty\t2.0 0.0\t2.0\t7\ta\t0.0\tz\t2.0""" with named_file(dat, suffix=".tab") as filename: self.open_dataset(filename) data1 = self.get_output(self.widget.Outputs.data) model = self.widget.domain_editor.model() # check the ordering of attributes for i, a in enumerate(data1.domain.attributes): self.assertEqual(str(a), model.data(model.createIndex(i, 0), Qt.DisplayRole)) # make conversions model.setData(model.createIndex(0, 1), "categorical", Qt.EditRole) model.setData(model.createIndex(1, 1), "text", Qt.EditRole) model.setData(model.createIndex(2, 1), "numeric", Qt.EditRole) model.setData(model.createIndex(3, 1), "numeric", Qt.EditRole) model.setData(model.createIndex(6, 1), "numeric", Qt.EditRole) self.widget.apply_button.click() data2 = self.get_output(self.widget.Outputs.data) # round continuous values should be converted to integers (3.0 -> 3, "3") self.assertEqual(len(data2.domain.attributes[0].values[0]), 1) self.assertEqual(len(data2[0].metas[0]), 1) # discrete integer values should stay the same after conversion to continuous self.assertAlmostEqual(float(data1[0][2].value), data2[0][1]) # discrete round floats should stay the same after conversion to continuous self.assertAlmostEqual(float(data1[0][6].value), data2[0][5])
def test_save_uncompressed(self): widget = self.widget widget.auto_save = False spiris = Table("iris") with spiris.unlocked(): spiris.X = sp.csr_matrix(spiris.X) for selected_filter, writer in widget.get_filters().items(): widget.write = writer ext = writer.EXTENSIONS[0] with named_file("", suffix=ext) as filename: widget.get_save_filename = Mock(return_value=(filename, selected_filter)) self.send_signal(widget.Inputs.data, self.iris) widget.save_file_as() if hasattr(writer, "read"): self.assertEqual(len(writer(filename).read()), 150) if writer.SUPPORT_SPARSE_DATA: self.send_signal(widget.Inputs.data, spiris) widget.save_file() if hasattr(writer, "read"): self.assertEqual(len(writer(filename).read()), 150)
def test_check_datetime_disabled(self): """ Datetime option is disable if numerical is disabled as well. GH-2050 (code fixes) GH-2120 """ dat = """\ 01.08.16\t42.15\tneumann\t2017-02-20 03.08.16\t16.08\tneumann\t2017-02-21 04.08.16\t23.04\tneumann\t2017-02-22 03.09.16\t48.84\tturing\t2017-02-23 02.02.17\t23.16\tturing\t2017-02-24""" with named_file(dat, suffix=".tab") as filename: self.open_dataset(filename) domain_editor = self.widget.domain_editor idx = lambda x: self.widget.domain_editor.model().createIndex(x, 1) qcombobox = QComboBox() combo = ComboDelegate(domain_editor, VarTableModel.typenames).createEditor(qcombobox, None, idx(2)) vartype_delegate = VarTypeDelegate(domain_editor, VarTableModel.typenames) vartype_delegate.setEditorData(combo, idx(2)) counts = [4, 2, 4, 2] for i in range(4): vartype_delegate.setEditorData(combo, idx(i)) self.assertEqual(combo.count(), counts[i])
def test_dropEvent_replaces_file(self): with named_file("test", suffix=".42") as fn: previous = self.widget.text.toPlainText() event = self._drop_event(QUrl.fromLocalFile(fn)) self.widget.dropEvent(event) self.assertEqual("test", self.widget.text.toPlainText()) self.widget.text.undo() self.assertEqual(previous, self.widget.text.toPlainText())
def test_roundtrip(self): d1 = Orange.data.Table("map_test.xyz") with named_file("", suffix=".xyz") as fn: d1.save(fn) d2 = Orange.data.Table(fn) np.testing.assert_equal(d1.X, d2.X) np.testing.assert_equal(getx(d1), getx(d2)) np.testing.assert_equal(d1.metas, d2.metas)
def setUp(self): self._stack = ExitStack().__enter__() # patch `_local_settings` to avoid side effects, across tests fname = self._stack.enter_context(named_file("")) s = QSettings(fname, QSettings.IniFormat) self._stack.enter_context( mock.patch.object(owcsvimport.OWCSVFileImport, "_local_settings", lambda *a: s)) self.widget = self.create_widget(owcsvimport.OWCSVFileImport)
def test_write_defaults_handles_permission_error(self): handler = SettingsHandler() with named_file("") as f: handler._get_settings_filename = lambda: f with patch('Orange.widgets.settings.open', create=True) as mocked_open: mocked_open.side_effect = PermissionError() handler.write_defaults()
def test_open_compressed(self): content = 'abc' for ext in ["txt", "gz", "bz2", "xz", "zip"]: with named_file('', suffix=f".{ext}") as fname: with _open_write(fname, "wt", encoding="ascii") as f: f.write(content) f.close() with owcsvimport._open(fname, "rt", encoding="ascii") as f: self.assertEqual(content, f.read())
def test_read_file_content(self): with named_file("Content", suffix=".42") as fn: # valid file opens content = read_file_content(fn) self.assertEqual("Content", content) # invalid utf-8 file does not with open(fn, "wb") as f: f.write(b"\xc3\x28") content = read_file_content(fn) self.assertIsNone(content)
def test_reader_custom_tab(self): with named_file("", suffix=".tab") as fn: qname = MyCustomTabReader.qualified_name() reader = RecentPath(fn, None, None, file_format=qname) self.widget = self.create_widget( OWFile, stored_settings={"recent_paths": [reader]}) self.widget.load_data() self.assertFalse(self.widget.Error.missing_reader.is_shown()) outdata = self.get_output(self.widget.Outputs.data) self.assertEqual(len(outdata), 150) # loaded iris
def test_script_insert_mime_file(self): with named_file("test", suffix=".42") as fn: previous = self.widget.text.toPlainText() mime = QMimeData() url = QUrl.fromLocalFile(fn) mime.setUrls([url]) self.widget.text.insertFromMimeData(mime) self.assertEqual("test", self.widget.text.toPlainText()) self.widget.text.undo() self.assertEqual(previous, self.widget.text.toPlainText())
def test_ordinary_save(self): self.send_signal(self.widget.Inputs.data, Table("iris")) for ext, writer in [('.tab', TabReader), ('.pickle', PickleReader)]: with named_file("", suffix=ext) as filename: def choose_file(a, b, c, d, e, fn=filename, w=writer): return fn, format_filter(w) with patch("AnyQt.QtWidgets.QFileDialog.getSaveFileName", choose_file): self.widget.save_file_as() self.assertEqual(len(Table(filename)), 150)
def test_reader_custom_tab(self): with named_file("", suffix=".tab") as fn: qname = MyCustomTabReader.qualified_name() reader = RecentPath(fn, None, None, file_format=qname) self.widget = self.create_widget(OWFile, stored_settings={"recent_paths": [reader]}) self.widget.load_data() self.assertFalse(self.widget.Error.missing_reader.is_shown()) outdata = self.get_output(self.widget.Outputs.data) self.assertEqual(len(outdata), 150) # loaded iris
def test_write_defaults_handles_permission_error(self): handler = SettingsHandler() with named_file("") as f: handler._get_settings_filename = lambda: f with patch("Orange.widgets.settings.log.error") as log, \ patch('Orange.widgets.settings.open', create=True, side_effect=PermissionError): handler.write_defaults() log.assert_called()
def test_pdf(self): iris = Orange.data.Table("iris") self.widget = self.create_widget(OWScatterPlot) self.send_signal(OWScatterPlot.Inputs.data, iris[::10]) with named_file("", suffix=".pdf") as fname: with patch("Orange.widgets.utils.filedialogs.open_filename_dialog_save", lambda *x: (fname, MatplotlibPDFFormat, None)): self.widget.save_graph() with open(fname, "rb") as f: code = f.read() self.assertTrue(code.startswith(b"%PDF"))
def _save_and_load(self): widget = self.widget widget.auto_save = False with named_file("", suffix=".dst") as filename: widget.get_save_filename = Mock(return_value=(filename, widget.filters[0])) self.send_signal(widget.Inputs.distances, self.distances) widget.save_file_as() return DistMatrix.from_file(filename)
def test_add_new_format(self): # test adding file formats after registering the widget called = False with named_file("", suffix=".tab") as filename: def test_format(_sd, _sf, ff, **_): nonlocal called called = True self.assertIn(FailedSheetsFormat, ff) return filename, TabReader, "" with patch("Orange.widgets.data.owfile.open_filename_dialog", test_format): self.widget.browse_file() self.assertTrue(called)
def test_add_new_format(self): # test adding file formats after registering the widget called = False with named_file("", suffix=".tab") as filename: def test_format(sd, sf, ff, **kwargs): nonlocal called called = True self.assertIn(FailedSheetsFormat, ff) return filename, TabReader, "" with patch("Orange.widgets.data.owfile.open_filename_dialog", test_format): self.widget.browse_file() self.assertTrue(called)
def test_script_insert_mime_file(self): with named_file("test", suffix=".42") as fn: previous = self.widget.text.toPlainText() mime = QMimeData() url = QUrl.fromLocalFile(fn) mime.setUrls([url]) self.widget.text.insertFromMimeData(mime) text = self.widget.text.toPlainText().split( "print('Hello world')")[0] self.assertTrue("'" + fn + "'", text) self.widget.text.undo() self.assertEqual(previous, self.widget.text.toPlainText())
def test_domaineditor_continuous_to_string(self): # GH 2744 dat = """V0\nc\n\n1.0\nnan\n3.0""" with named_file(dat, suffix=".tab") as filename: self.open_dataset(filename) model = self.widget.domain_editor.model() model.setData(model.createIndex(0, 1), "text", Qt.EditRole) self.widget.apply_button.click() data = self.get_output(self.widget.Outputs.data) self.assertSequenceEqual(data.metas.ravel().tolist(), ['1', '', '3'])
def test_roundtrip(self): with named_file("", suffix=".dat") as fn: # a single spectrum d1 = Orange.data.Table("peach_juice.dpt") d1.save(fn) d2 = Orange.data.Table(fn) np.testing.assert_equal(d1.X, d2.X) # multiple spectra d1 = Orange.data.Table("collagen.csv") d1.save(fn) d2 = Orange.data.Table(fn) np.testing.assert_equal(d1.X, d2.X)
def test_added_format(self): """Test that a format added after widget initialization is recognized""" self.send_signal(self.widget.Inputs.data, Table("iris")) called = False with named_file("", suffix=".tab") as filename: def test_format(sd, sf, ff, **kwargs): nonlocal called called = True self.assertIn(AddedFormat, ff) return filename, TabReader, "" with patch("Orange.widgets.utils.filedialogs.open_filename_dialog", test_format): self.widget.save_file_as() self.assertTrue(called)
def test_context_match_includes_variable_values(self): file1 = """\ var a b a """ file2 = """\ var a b c a """ editor = self.widget.domain_editor idx = self.widget.domain_editor.model().createIndex(0, 3) with named_file(file1, suffix=".tab") as filename: self.open_dataset(filename) self.assertEqual(editor.model().data(idx, Qt.DisplayRole), "a, b") with named_file(file2, suffix=".tab") as filename: self.open_dataset(filename) self.assertEqual(editor.model().data(idx, Qt.DisplayRole), "a, b, c")
def test_ordinary_save(self): self.send_signal(self.widget.Inputs.data, Table("iris")) for ext, suffix, _ in FILE_TYPES: self.widget.filetype = ext self.widget.update_extension() writer = self.widget.get_writer_selected() with named_file("", suffix=suffix) as filename: def choose_file(a, b, c, d, e, fn=filename, w=writer): return fn, format_filter(w) with patch("AnyQt.QtWidgets.QFileDialog.getSaveFileName", choose_file): self.widget.save_file_as() self.assertEqual(len(Table(filename)), 150)
def test_domain_edit_on_sparse_data(self): iris = Table("iris").to_sparse() with named_file("", suffix='.pickle') as fn: with open(fn, "wb") as f: pickle.dump(iris, f) self.widget.add_path(fn) self.widget.load_data() output = self.get_output(self.widget.Outputs.data) self.assertIsInstance(output, Table) self.assertEqual(iris.X.shape, output.X.shape) self.assertTrue(sp.issparse(output.X))
def test_python(self): iris = Orange.data.Table("iris") self.widget = self.create_widget(OWScatterPlot) self.send_signal(OWScatterPlot.Inputs.data, iris[::10]) with named_file("", suffix=".py") as fname: with patch("Orange.widgets.utils.filedialogs.open_filename_dialog_save", lambda *x: (fname, MatplotlibFormat, None)): self.widget.save_graph() with open(fname, "rt") as f: code = f.read() self.assertIn("plt.show()", code) self.assertIn("plt.scatter", code) # test if the runs exec(code.replace("plt.show()", ""), {})
def test_save(self): with named_file( """3 axis=1 row_labels danny 0.12 3.45 6.78 eve 9.01 2.34 5.67 frank 8.90""") as name: m = DistMatrix.from_file(name) m.save(name) m = DistMatrix.from_file(name) np.testing.assert_almost_equal(m, np.array([[0.12, 9.01, 8.90], [9.01, 2.34, 0], [8.90, 0, 0]])) self.assertIsInstance(m.row_items, Table) self.assertIsNone(m.col_items) self.assertEqual([e.metas[0] for e in m.row_items], ["danny", "eve", "frank"]) self.assertEqual(m.axis, 1) with named_file( """3 axis=0 asymmetric col_labels row_labels ann bert chad danny 0.12 3.45 6.78 eve 9.01 2.34 5.67 frank 8.90 1.23 4.56""") as name: m = DistMatrix.from_file(name) m.save(name) m = DistMatrix.from_file(name) np.testing.assert_almost_equal(m, np.array([[0.12, 3.45, 6.78], [9.01, 2.34, 5.67], [8.90, 1.23, 4.56]])) self.assertIsInstance(m.row_items, Table) self.assertIsInstance(m.col_items, Table) self.assertEqual([e.metas[0] for e in m.col_items], ["ann", "bert", "chad"]) self.assertEqual([e.metas[0] for e in m.row_items], ["danny", "eve", "frank"]) self.assertEqual(m.axis, 0)
def test_compression(self): self.send_signal(self.widget.Inputs.data, Table("iris")) self.widget.compress = True for type, compression in itertools.product([[x, ext] for x, ext, _ in FILE_TYPES], COMPRESSIONS): self.widget.filetype = type[0] self.widget.compression = compression[0] self.widget.update_extension() writer = self.widget.get_writer_selected() with named_file("", suffix=type[1] + compression[1]) as filename: def choose_file(a, b, c, d, e, fn=filename, w=writer): return fn, format_filter(w) with patch("AnyQt.QtWidgets.QFileDialog.getSaveFileName", choose_file): self.widget.save_file_as() self.assertEqual(len(Table(filename)), 150)
def test_domaineditor_makes_variables(self): # Variables created with domain editor should be interchangeable # with variables read from file. dat = """V0\tV1\nc\td\n\n1.0\t2""" v0 = StringVariable.make("V0") v1 = ContinuousVariable.make("V1") with named_file(dat, suffix=".tab") as filename: self.open_dataset(filename) model = self.widget.domain_editor.model() model.setData(model.createIndex(0, 1), "text", Qt.EditRole) model.setData(model.createIndex(1, 1), "numeric", Qt.EditRole) self.widget.apply_button.click() data = self.get_output(self.widget.Outputs.data) self.assertEqual(data.domain["V0"], v0) self.assertEqual(data.domain["V1"], v1)
def test_from_file(self): with named_file( """3 axis=0 asymmetric col_labels row_labels ann bert chad danny 0.12 3.45 6.78 eve 9.01 2.34 5.67 frank 8.90 1.23 4.56""") as name: m = DistMatrix.from_file(name) np.testing.assert_almost_equal(m, np.array([[0.12, 3.45, 6.78], [9.01, 2.34, 5.67], [8.90, 1.23, 4.56]])) self.assertIsInstance(m.row_items, Table) self.assertIsInstance(m.col_items, Table) self.assertEqual([e.metas[0] for e in m.col_items], ["ann", "bert", "chad"]) self.assertEqual([e.metas[0] for e in m.row_items], ["danny", "eve", "frank"]) self.assertEqual(m.axis, 0) with named_file( """3 axis=1 row_labels danny 0.12 3.45 6.78 eve 9.01 2.34 5.67 frank 8.90""") as name: m = DistMatrix.from_file(name) np.testing.assert_almost_equal(m, np.array([[0.12, 9.01, 8.90], [9.01, 2.34, 0], [8.90, 0, 0]])) self.assertIsInstance(m.row_items, Table) self.assertIsNone(m.col_items) self.assertEqual([e.metas[0] for e in m.row_items], ["danny", "eve", "frank"]) self.assertEqual(m.axis, 1) with named_file( """3 axis=1 symmetric 0.12 3.45 6.78 9.01 2.34 5.67 8.90""") as name: m = DistMatrix.from_file(name) np.testing.assert_almost_equal(m, np.array([[0.12, 9.01, 8.90], [9.01, 2.34, 0], [8.90, 0, 0]])) with named_file( """3 row_labels starič 0.12 3.45 6.78 aleš 9.01 2.34 5.67 anže 8.90""", encoding="utf-8""") as name: m = DistMatrix.from_file(name) np.testing.assert_almost_equal(m, np.array([[0.12, 9.01, 8.90], [9.01, 2.34, 0], [8.90, 0, 0]])) self.assertIsInstance(m.row_items, Table) self.assertIsNone(m.col_items) self.assertEqual([e.metas[0] for e in m.row_items], ["starič", "aleš", "anže"]) self.assertEqual(m.axis, 1) def assertErrorMsg(content, msg): with named_file(content) as name: with self.assertRaises(ValueError) as cm: DistMatrix.from_file(name) self.assertEqual(str(cm.exception), msg) assertErrorMsg("", "empty file") assertErrorMsg("axis=1\n1\t3\n4", "distance file must begin with dimension") assertErrorMsg("3 col_labels\na\tb\n1\n\2\n3", "mismatching number of column labels") assertErrorMsg("3 col_labels\na\tb\tc\td\n1\n\2\n3", "mismatching number of column labels") assertErrorMsg("2\n 1\t2\t3\n 5", "too many columns in matrix row 1") assertErrorMsg("2 row_labels\na\t1\t2\t3\nb\t5", "too many columns in matrix row 'a'") assertErrorMsg("2 noflag\n 1\t2\t3\n 5", "invalid flag 'noflag'") assertErrorMsg("2 noflag=5\n 1\t2\t3\n 5", "invalid flag 'noflag=5'") assertErrorMsg("2\n1\n2\n3", "too many rows") assertErrorMsg("2\n1\nasd", "invalid element at row 2, column 1") assertErrorMsg("2 row_labels\na\t1\nb\tasd", "invalid element at row 'b', column 1") assertErrorMsg("2 col_labels row_labels\nd\te\na\t1\nb\tasd", "invalid element at row 'b', column 'd'") assertErrorMsg("2 col_labels\nd\te\n1\nasd", "invalid element at row 2, column 'd'")
def set_png_graph_save(): with named_file("", suffix=".png") as fname: with patch("Orange.widgets.utils.filedialogs.open_filename_dialog_save", lambda *x: (fname, PngFormat, None)): yield fname
def test_fail(self): with named_file("name\nc\n\nstring", suffix=".tab") as fn: self.open_dataset(fn) self.assertTrue(self.widget.Error.unknown.is_shown())
def test_with_warnings(self): with named_file("", suffix=".with_warning") as fn: self.open_dataset(fn) self.assertTrue(self.widget.Warning.load_warning.is_shown())
def test_fail_sheets(self): with named_file("", suffix=".failed_sheet") as fn: self.open_dataset(fn) self.assertTrue(self.widget.Error.sheet_error.is_shown())