1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
|
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring,protected-access,too-many-public-methods
from os import path, remove, getcwd
from os.path import dirname
import unittest
from threading import Thread
from unittest.mock import Mock, patch
import pickle
import tempfile
import warnings
import numpy as np
import scipy.sparse as sp
from AnyQt.QtCore import QMimeData, QPoint, Qt, QUrl, QPointF
from AnyQt.QtGui import QDragEnterEvent, QDropEvent
from AnyQt.QtTest import QTest
from AnyQt.QtWidgets import QComboBox
import Orange
from Orange.data import FileFormat, dataset_dirs, StringVariable, Table, \
Domain, DiscreteVariable, ContinuousVariable
from Orange.util import OrangeDeprecationWarning
from Orange.data.io import TabReader
from Orange.tests import named_file
from Orange.widgets.data.owfile import OWFile, OWFileDropHandler, DEFAULT_READER_TEXT
from Orange.widgets.utils.filedialogs import dialog_formats, format_filter, RecentPath
from Orange.widgets.tests.base import WidgetTest
from Orange.widgets.utils.domaineditor import ComboDelegate, VarTypeDelegate, VarTableModel
TITANIC_PATH = path.join(path.dirname(Orange.__file__), 'datasets', 'titanic.tab')
orig_path_exists = path.exists
class FailedSheetsFormat(FileFormat):
EXTENSIONS = ('.failed_sheet',)
DESCRIPTION = "Make a sheet function that fails"
def read(self):
pass
@property
def sheets(self):
# pylint: disable=broad-exception-raised
raise Exception("Not working")
class WithWarnings(FileFormat):
EXTENSIONS = ('.with_warning',)
DESCRIPTION = "Warning"
@staticmethod
def read():
warnings.warn("Some warning")
return Orange.data.Table("iris")
class MyCustomTabReader(FileFormat):
EXTENSIONS = ('.tab',)
DESCRIPTION = "Always return iris"
PRIORITY = 999999
@staticmethod
def read():
return Orange.data.Table("iris")
class TestOWFile(WidgetTest):
# Attribute used to store event data so it does not get garbage
# collected before event is processed.
event_data = None
def setUp(self):
super().setUp()
self.widget = self.create_widget(OWFile) # type: OWFile
dataset_dirs.append(dirname(__file__))
def tearDown(self):
dataset_dirs.pop()
super().tearDown()
def test_describe_call_get_nans(self):
table = Table("iris")
with patch.object(Table, "get_nan_frequency_attribute", return_value=0.) as mock:
self.widget._describe(table)
mock.assert_called()
table = Table.from_numpy(domain=None, X=np.random.random((10000, 1000)))
with patch.object(Table, "get_nan_frequency_attribute", return_value=0.) as mock:
self.widget._describe(table)
mock.assert_not_called()
def test_dragEnterEvent_accepts_urls(self):
event = self._drag_enter_event(QUrl.fromLocalFile(TITANIC_PATH))
self.widget.dragEnterEvent(event)
self.assertTrue(event.isAccepted())
def test_dragEnterEvent_skips_usupported_files(self):
event = self._drag_enter_event(QUrl.fromLocalFile('file.unsupported'))
self.widget.dragEnterEvent(event)
self.assertFalse(event.isAccepted())
def _drag_enter_event(self, url):
# make sure data does not get garbage collected before it used
self.event_data = data = QMimeData()
data.setUrls([QUrl(url)])
return QDragEnterEvent(
QPoint(0, 0), Qt.MoveAction, data,
Qt.NoButton, Qt.NoModifier)
def test_dropEvent_selects_file(self):
self.widget.load_data = Mock()
self.widget.source = OWFile.URL
event = self._drop_event(QUrl.fromLocalFile(TITANIC_PATH))
self.widget.dropEvent(event)
self.assertEqual(self.widget.source, OWFile.LOCAL_FILE)
self.assertTrue(path.samefile(self.widget.last_path(), TITANIC_PATH))
self.widget.load_data.assert_called_with()
event = self._drop_event(QUrl("https://example.com/aa.csv"))
self.widget.load_data.reset_mock()
self.widget.dropEvent(event)
self.assertEqual(self.widget.source, OWFile.URL)
self.widget.load_data.assert_called_with()
def _drop_event(self, url):
# make sure data does not get garbage collected before it used
self.event_data = data = QMimeData()
data.setUrls([QUrl(url)])
return QDropEvent(
QPointF(0, 0), Qt.MoveAction, data,
Qt.NoButton, Qt.NoModifier, QDropEvent.Drop)
def test_check_file_size(self):
self.assertFalse(self.widget.Warning.file_too_big.is_shown())
self.widget.SIZE_LIMIT = 4000
# We're avoiding __new__, pylint: disable=unnecessary-dunder-call
self.widget.__init__()
self.assertTrue(self.widget.Warning.file_too_big.is_shown())
def test_domain_changes_are_stored(self):
assert isinstance(self.widget, OWFile)
self.open_dataset("iris")
idx = self.widget.domain_editor.model().createIndex(4, 1)
self.widget.domain_editor.model().setData(idx, "text", Qt.EditRole)
self.widget.apply_button.click()
data = self.get_output(self.widget.Outputs.data)
self.assertIsInstance(data.domain["iris"], StringVariable)
self.open_dataset("zoo")
data = self.get_output(self.widget.Outputs.data)
self.assertEqual(data.name, "zoo")
self.open_dataset("iris")
data = self.get_output(self.widget.Outputs.data)
self.assertIsInstance(data.domain["iris"], StringVariable)
def test_rename_duplicates(self):
self.open_dataset("iris")
idx = self.widget.domain_editor.model().createIndex(3, 0)
self.assertFalse(self.widget.Warning.renamed_vars.is_shown())
self.widget.domain_editor.model().setData(idx, "iris", Qt.EditRole)
self.widget.apply_button.click()
data = self.get_output(self.widget.Outputs.data)
self.assertIn("iris (1)", data.domain)
self.assertIn("iris (2)", data.domain)
self.assertTrue(self.widget.Warning.renamed_vars.is_shown())
self.widget.domain_editor.model().setData(idx, "different iris", Qt.EditRole)
self.widget.apply_button.click()
self.assertFalse(self.widget.Warning.renamed_vars.is_shown())
def test_variable_name_change(self):
"""
Test whether the name of the variable is changed correctly by
the domaineditor.
"""
self.open_dataset("iris")
# just rename
idx = self.widget.domain_editor.model().createIndex(4, 0)
self.widget.domain_editor.model().setData(idx, "a", Qt.EditRole)
self.widget.apply_button.click()
data = self.get_output(self.widget.Outputs.data)
self.assertIn("a", data.domain)
idx = self.widget.domain_editor.model().createIndex(3, 0)
self.widget.domain_editor.model().setData(idx, "d", Qt.EditRole)
self.widget.apply_button.click()
data = self.get_output(self.widget.Outputs.data)
self.assertIn("d", data.domain)
# rename and change to text
idx = self.widget.domain_editor.model().createIndex(4, 0)
self.widget.domain_editor.model().setData(idx, "b", Qt.EditRole)
idx = self.widget.domain_editor.model().createIndex(4, 1)
self.widget.domain_editor.model().setData(idx, "text", Qt.EditRole)
self.widget.apply_button.click()
data = self.get_output(self.widget.Outputs.data)
self.assertIn("b", data.domain)
self.assertIsInstance(data.domain["b"], StringVariable)
# rename and change to discrete
idx = self.widget.domain_editor.model().createIndex(4, 0)
self.widget.domain_editor.model().setData(idx, "c", Qt.EditRole)
idx = self.widget.domain_editor.model().createIndex(4, 1)
self.widget.domain_editor.model().setData(
idx, "categorical", Qt.EditRole)
self.widget.apply_button.click()
data = self.get_output(self.widget.Outputs.data)
self.assertIn("c", data.domain)
self.assertIsInstance(data.domain["c"], DiscreteVariable)
# rename and change to continuous
self.open_dataset("zoo")
idx = self.widget.domain_editor.model().createIndex(0, 0)
self.widget.domain_editor.model().setData(idx, "c", Qt.EditRole)
idx = self.widget.domain_editor.model().createIndex(0, 1)
self.widget.domain_editor.model().setData(idx, "numeric", Qt.EditRole)
self.widget.apply_button.click()
data = self.get_output(self.widget.Outputs.data)
self.assertIn("c", data.domain)
self.assertIsInstance(data.domain["c"], ContinuousVariable)
def open_dataset(self, name):
filename = FileFormat.locate(name, dataset_dirs)
self.widget.add_path(filename)
self.widget.load_data()
def test_no_last_path(self):
self.widget =\
self.create_widget(OWFile, stored_settings={"recent_paths": []})
# Doesn't crash and contains a single item, (none).
self.assertEqual(self.widget.file_combo.count(), 1)
def test_file_not_found(self):
# Create a dummy file
file_name = "test_owfile_data.tab"
domainA = Domain([DiscreteVariable("d1", values=("a", "b"))],
DiscreteVariable("c1", values=("aaa", "bbb")))
dataA = Table(domainA, np.array([[0], [1], [0], [np.nan]]),
np.array([0, 1, 0, 1]))
dataA.save(file_name)
# Open the file with the widget
self.open_dataset(file_name)
self.assertEqual(self.get_output(self.widget.Outputs.data).domain, dataA.domain)
# Delete the file and try to reload it
remove(file_name)
self.widget.load_data()
self.assertEqual(file_name, path.basename(self.widget.last_path()))
self.assertTrue(self.widget.Error.file_not_found.is_shown())
self.assertIsNone(self.get_output(self.widget.Outputs.data))
self.assertEqual(self.widget.infolabel.text(), "No data.")
# Open a sample dataset
self.open_dataset("iris")
self.assertFalse(self.widget.Error.file_not_found.is_shown())
def test_nothing_selected(self):
# pylint: disable=protected-access
widget = self.widget = \
self.create_widget(OWFile, stored_settings={"recent_paths": []})
widget.Outputs.data.send = Mock()
widget.load_data()
self.assertTrue(widget.Information.no_file_selected.is_shown())
widget.Outputs.data.send.assert_called_with(None)
widget.Outputs.data.send.reset_mock()
widget.source = widget.URL
widget.load_data()
self.assertTrue(widget.Information.no_file_selected.is_shown())
widget.Outputs.data.send.assert_called_with(None)
def test_check_column_noname(self):
"""
Column name cannot be changed to an empty string or a string with whitespaces.
GH-2039
"""
self.open_dataset("iris")
idx = self.widget.domain_editor.model().createIndex(1, 0)
temp = self.widget.domain_editor.model().data(idx, Qt.DisplayRole)
self.widget.domain_editor.model().setData(idx, " ", Qt.EditRole)
self.assertEqual(self.widget.domain_editor.model().data(idx, Qt.DisplayRole), temp)
self.widget.domain_editor.model().setData(idx, "", Qt.EditRole)
self.assertEqual(self.widget.domain_editor.model().data(idx, Qt.DisplayRole), temp)
def test_invalid_role_mode(self):
self.open_dataset("iris")
model = self.widget.domain_editor.model()
idx = model.createIndex(1, 0)
self.assertFalse(model.setData(idx, Qt.StatusTipRole, ""))
self.assertIsNone(model.data(idx, Qt.StatusTipRole))
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_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
def idx(x):
return 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_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_unknown_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.select_file_type.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())
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(self):
with named_file("name\nc\n\nstring", suffix=".tab") as fn, \
patch("Orange.widgets.data.owfile.log.exception") as log:
self.open_dataset(fn)
log.assert_called()
self.assertTrue(self.widget.Error.unknown.is_shown())
def test_read_format(self):
iris = Table("iris")
def open_iris_with_no_spec_format(_a, _b, _c, filters, _e):
return iris.__file__, filters.split(";;")[0]
with patch("AnyQt.QtWidgets.QFileDialog.getOpenFileName",
open_iris_with_no_spec_format):
self.widget.browse_file()
self.assertIsNone(self.widget.recent_paths[0].file_format)
self.assertEqual(self.widget.reader_combo.currentText(), DEFAULT_READER_TEXT)
def open_iris_with_tab(*_):
return iris.__file__, format_filter(TabReader)
with patch("AnyQt.QtWidgets.QFileDialog.getOpenFileName",
open_iris_with_tab):
self.widget.browse_file()
self.assertEqual(self.widget.recent_paths[0].file_format, "Orange.data.io.TabReader")
self.assertTrue(self.widget.reader_combo.currentText().startswith("Tab-separated"))
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())
self.assertEqual(self.widget.reader_combo.currentText(), "not.a.file.reader.class")
def _select_reader(self, name):
reader_combo = self.widget.reader_combo
len_with_qname = len(reader_combo)
for i in range(len_with_qname):
text = reader_combo.itemText(i)
if text.startswith(name):
break
else:
assert f"No reader starts with {name!r}"
reader_combo.setCurrentIndex(i)
reader_combo.activated.emit(i)
def _select_tab_reader(self):
self._select_reader("Tab-separated")
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)
self._select_tab_reader()
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_auto_detect_and_override(self):
tab_as_xlsx = FileFormat.locate("actually-a-tab-file.xlsx",
[dirname(__file__)])
iris = FileFormat.locate("iris", dataset_dirs)
reader_combo = self.widget.reader_combo
reader_combo.setCurrentIndex(0)
reader_combo.activated.emit(0)
assert (self.widget.reader_combo.currentText()
== "Determine type from the file extension")
def open_file(_a, _b, _c, filters, _e):
return filename, filters.split(";;")[0]
with patch("AnyQt.QtWidgets.QFileDialog.getOpenFileName",
open_file):
# Loading a tab file with extension xlsx fails with auto-detect
filename = tab_as_xlsx
self.widget.browse_file()
self.assertEqual(self.widget.reader_combo.currentText(),
"Determine type from the file extension")
self.assertTrue(self.widget.Error.unknown_select.is_shown())
self.assertIsNone(self.get_output(self.widget.Outputs.data))
# Select the tab reader: it should work
self._select_tab_reader()
assert "Tab-separated" in self.widget.reader_combo.currentText()
self.assertFalse(self.widget.Error.unknown_select.is_shown())
self.assertIsInstance(self.widget.reader, TabReader)
self.assertIsNotNone(self.get_output(self.widget.Outputs.data))
# Switching to iris resets the combo to auto-detect
filename = iris
self.widget.browse_file()
self.assertEqual(self.widget.reader_combo.currentText(),
"Determine type from the file extension")
self.assertIsNotNone(self.get_output(self.widget.Outputs.data))
# Taking the tab-as-xlsx file from recent paths should restore
# the file type for that file
self.widget.file_combo.setCurrentIndex(1)
self.widget.file_combo.activated.emit(1)
self.assertIn("Tab-separated", self.widget.reader_combo.currentText())
self.assertIsNotNone(self.get_output(self.widget.Outputs.data))
# Reloading should work
self.widget.load_data()
self.assertIn("Tab-separated", self.widget.reader_combo.currentText())
self.assertIsNotNone(self.get_output(self.widget.Outputs.data))
# Loading this file - not from history - should fail
filename = tab_as_xlsx
self.widget.browse_file()
self.assertTrue(self.widget.Error.unknown_select.is_shown())
self.assertIsNone(self.get_output(self.widget.Outputs.data))
# Set the correct type again (preparation for the next text block)
self._select_tab_reader()
assert not self.widget.Error.unknown_select.is_shown()
assert isinstance(self.widget.reader, TabReader)
assert self.get_output(self.widget.Outputs.data) is not None
# Now load a real Excel file: this is a known excention so the combo
# should return to auto-detect
filename = FileFormat.locate("an_excel_file.xlsx", [dirname(__file__)])
self.widget.browse_file()
self.assertEqual(self.widget.reader_combo.currentText(),
"Determine type from the file extension")
self.assertFalse(self.widget.Error.unknown_select.is_shown())
self.assertIsNotNone(self.get_output(self.widget.Outputs.data))
# Load iris to prepare for the next test block
filename = iris
self.widget.browse_file()
assert (self.widget.reader_combo.currentText()
== "Determine type from the file extension")
assert self.get_output(self.widget.Outputs.data) is not None
# Files with unknown extensions require manual selection
filename = FileFormat.locate("an_excel_file.foo", [dirname(__file__)])
self.widget.browse_file()
self.assertTrue(self.widget.Error.select_file_type.is_shown())
self.assertIsNone(self.get_output(self.widget.Outputs.data))
self._select_reader("Excel")
self.assertFalse(self.widget.Error.unknown_select.is_shown())
self.assertFalse(self.widget.Error.select_file_type.is_shown())
self.assertIsNotNone(self.get_output(self.widget.Outputs.data))
# Consecutive loading of files with the same extension keeps selection
filename = FileFormat.locate("an_excel_file-too.foo", [dirname(__file__)])
self.widget.browse_file()
self.assertFalse(self.widget.Error.unknown_select.is_shown())
self.assertFalse(self.widget.Error.select_file_type.is_shown())
self.assertIsNotNone(self.get_output(self.widget.Outputs.data))
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_domain_edit_no_changes(self):
self.open_dataset("iris")
data = self.get_output(self.widget.Outputs.data)
# When no changes have been made in the domain editor,
# output data should be the same object (and not recreated)
self.assertTrue(data is self.widget.data)
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_drop_data_when_everything_skipped(self):
"""
No data when everything is skipped. Otherwise Select Rows crashes.
GH-2237
"""
self.open_dataset("iris")
data = self.get_output(self.widget.Outputs.data)
self.assertTrue(len(data), 150)
self.assertTrue(len(data.domain.variables), 5)
for i in range(5):
idx = self.widget.domain_editor.model().createIndex(i, 2)
self.widget.domain_editor.model().setData(idx, "skip", Qt.EditRole)
self.widget.apply_button.click()
data = self.get_output(self.widget.Outputs.data)
self.assertIsNone(data)
def test_call_deprecated_dialog_formats(self):
with self.assertWarns(OrangeDeprecationWarning):
self.assertIn("Tab", dialog_formats())
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_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_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_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_url_no_scheme(self):
mock_urlreader = Mock(side_effect=ValueError())
url = 'foo.bar/xxx.csv'
with patch('Orange.widgets.data.owfile.UrlReader', mock_urlreader):
self.widget.url_combo.insertItem(0, url)
self.widget.url_combo.activated.emit(0)
mock_urlreader.assert_called_once_with('http://' + url)
def test_adds_origin(self):
self.open_dataset("origin1/images")
data1 = self.get_output(self.widget.Outputs.data)
attrs = data1.domain["image"].attributes
self.assertIn("origin", attrs)
self.assertIn("origin1", attrs["origin"])
self.open_dataset("origin2/images")
data2 = self.get_output(self.widget.Outputs.data)
attrs = data2.domain["image"].attributes
self.assertIn("origin", attrs)
self.assertIn("origin2", attrs["origin"])
# Make sure that variable in data1 still contains correct origin
attrs = data1.domain["image"].attributes
self.assertIn("origin", attrs)
self.assertIn("origin1", attrs["origin"])
@patch("Orange.widgets.widget.OWWidget.workflowEnv",
Mock(return_value={"basedir": getcwd()}))
def test_open_moved_workflow(self):
"""Test opening workflow that has been moved to another location
(i.e. sent by email), considering data file is stored in the same
directory as the workflow.
"""
with tempfile.NamedTemporaryFile(dir=getcwd(), delete=False) as temp_file:
file_name = temp_file.name
base_name = path.basename(file_name)
try:
recent_path = RecentPath(
path.join("temp/datasets", base_name), "",
path.join("datasets", base_name)
)
stored_settings = {"recent_paths": [recent_path]}
w = self.create_widget(OWFile, stored_settings=stored_settings)
w.load_data()
self.assertEqual(w.file_combo.count(), 1)
self.assertFalse(w.Error.file_not_found.is_shown())
finally:
remove(file_name)
@patch("Orange.widgets.widget.OWWidget.workflowEnv",
Mock(return_value={"basedir": getcwd()}))
def test_files_relocated(self):
"""
This test testes if paths are relocated correctly
"""
with tempfile.NamedTemporaryFile(dir=getcwd(), delete=False) as temp_file:
file_name = temp_file.name
base_name = path.basename(file_name)
try:
recent_path = RecentPath(
path.join("temp/datasets", base_name), "",
path.join("datasets", base_name)
)
stored_settings = {"recent_paths": [recent_path]}
w = self.create_widget(OWFile, stored_settings=stored_settings)
w.load_data()
# relocation is called already
# if it works correctly relative path should be same than base name
self.assertEqual(w.recent_paths[0].relpath, base_name)
w.workflowEnvChanged("basedir", base_name, base_name)
self.assertEqual(w.recent_paths[0].relpath, base_name)
finally:
remove(file_name)
def test_sheets(self):
# pylint: disable=protected-access
widget = self.widget
combo = widget.sheet_combo
widget.last_path = \
lambda: path.join(path.dirname(__file__), '..', "..", '..',
'tests', 'xlsx_files', 'header_0_sheet.xlsx')
widget._try_load()
widget.reader.sheet = "my_sheet"
widget._select_active_sheet()
self.assertEqual(combo.itemText(0), "Sheet1")
self.assertEqual(combo.itemText(1), "my_sheet")
self.assertEqual(combo.itemText(2), "Sheet3")
self.assertEqual(combo.currentIndex(), 1)
widget.reader.sheet = "no such sheet"
widget._select_active_sheet()
self.assertEqual(combo.currentIndex(), 0)
@patch("os.path.exists", new=lambda _: True)
def test_warning_from_another_thread(self):
def read():
thread = Thread(
target=lambda: warnings.warn("warning from another thread")
)
thread.start()
thread.join()
return Table(TITANIC_PATH)
reader = Mock()
reader.read = read
self.widget._get_reader = lambda: reader
self.widget.last_path = lambda: "foo"
self.widget._update_sheet_combo = Mock()
# Warning must be caught by unit tests, but not the widget
with self.assertWarns(UserWarning):
self.widget._try_load()
self.assertFalse(self.widget.Warning.load_warning.is_shown())
@patch("os.path.exists", new=lambda _: True)
def test_warning_from_this_thread(self):
WARNING_MSG = "warning from this thread"
def read():
warnings.warn(WARNING_MSG)
return Table(TITANIC_PATH)
reader = Mock()
reader.read = read
self.widget._get_reader = lambda: reader
self.widget.last_path = lambda: "foo"
self.widget._update_sheet_combo = Mock()
self.widget._try_load()
self.assertTrue(self.widget.Warning.load_warning.is_shown())
self.assertIn(WARNING_MSG, str(self.widget.Warning.load_warning))
def test_recent_url_serialization(self):
with patch.object(self.widget, "load_data", lambda: None):
self.widget.url_combo.insertItem(0, "https://example.com/test.tab")
self.widget.url_combo.insertItem(1, "https://example.com/test1.tab")
self.widget.source = OWFile.URL
s = self.widget.settingsHandler.pack_data(self.widget)
self.assertEqual(s["recent_urls"],
["https://example.com/test.tab",
"https://example.com/test1.tab"])
self.widget.url_combo.lineEdit().clear()
QTest.keyClicks(self.widget.url_combo, "https://example.com/test1.tab")
QTest.keyClick(self.widget.url_combo, Qt.Key_Enter)
# must move the entered url to first position
s = self.widget.settingsHandler.pack_data(self.widget)
self.assertEqual(s["recent_urls"],
["https://example.com/test1.tab",
"https://example.com/test.tab"])
class TestOWFileDropHandler(unittest.TestCase):
def test_canDropUrl(self):
handler = OWFileDropHandler()
self.assertTrue(handler.canDropUrl(QUrl("https://example.com/test.tab")))
self.assertTrue(handler.canDropUrl(QUrl.fromLocalFile("test.tab")))
def test_parametersFromUrl(self):
handler = OWFileDropHandler()
r = handler.parametersFromUrl(QUrl("https://example.com/test.tab"))
self.assertEqual(r["source"], OWFile.URL)
self.assertEqual(r["recent_urls"], ["https://example.com/test.tab"])
r = handler.parametersFromUrl(QUrl.fromLocalFile("test.tab"))
self.assertEqual(r["source"], OWFile.LOCAL_FILE)
self.assertEqual(r["recent_paths"][0].basename, "test.tab")
defs = {
"source": OWFile.LOCAL_FILE,
"recent_paths": [
RecentPath("/foo.tab", None, None, "foo.tab"),
RecentPath(path.abspath("test.tab"), None, None, "test.tab"),
]
}
with patch.object(OWFile.settingsHandler, "defaults", defs):
r = handler.parametersFromUrl(QUrl.fromLocalFile("test.tab"))
self.assertEqual(len(r["recent_paths"]), 2)
self.assertEqual(r["recent_paths"][0].basename, "test.tab")
if __name__ == "__main__":
unittest.main()
|