|
1 | | -from numpy.testing import assert_array_equal |
2 | 1 | import unittest |
| 2 | +from unittest import mock |
| 3 | +from contextlib import ExitStack |
3 | 4 |
|
4 | 5 | import os |
5 | 6 | import io |
6 | 7 | import csv |
| 8 | +import json |
7 | 9 |
|
8 | 10 | import numpy as np |
| 11 | +from numpy.testing import assert_array_equal |
9 | 12 |
|
10 | | -from Orange.widgets.tests.base import WidgetTest, GuiTest |
| 13 | +from AnyQt.QtCore import QSettings |
11 | 14 |
|
| 15 | +from Orange.tests import named_file |
| 16 | +from Orange.widgets.tests.base import WidgetTest, GuiTest |
12 | 17 | from Orange.widgets.data import owcsvimport |
13 | 18 | from Orange.widgets.data.owcsvimport import ( |
14 | 19 | pandas_to_table, ColumnType, RowSpec |
15 | 20 | ) |
| 21 | +from Orange.widgets.utils.settings import QSettings_writeArray |
16 | 22 |
|
17 | 23 |
|
18 | 24 | class TestOWCSVFileImport(WidgetTest): |
19 | 25 | def setUp(self): |
| 26 | + self._stack = ExitStack().__enter__() |
| 27 | + # patch `_local_settings` to avoid side effects, across tests |
| 28 | + fname = self._stack.enter_context(named_file("")) |
| 29 | + s = QSettings(fname, QSettings.IniFormat) |
| 30 | + self._stack.enter_context(mock.patch.object( |
| 31 | + owcsvimport.OWCSVFileImport, "_local_settings", lambda *a: s |
| 32 | + )) |
20 | 33 | self.widget = self.create_widget(owcsvimport.OWCSVFileImport) |
21 | 34 |
|
22 | 35 | def tearDown(self): |
23 | 36 | self.widgets.remove(self.widget) |
24 | 37 | self.widget.onDeleteWidget() |
25 | 38 | self.widget = None |
| 39 | + self._stack.close() |
26 | 40 |
|
27 | 41 | def test_basic(self): |
28 | 42 | w = self.widget |
29 | 43 | w.activate_recent(0) |
30 | 44 | w.cancel() |
31 | 45 |
|
| 46 | + data_regions_options = owcsvimport.Options( |
| 47 | + encoding="ascii", dialect=csv.excel_tab(), |
| 48 | + columntypes=[ |
| 49 | + (range(0, 1), ColumnType.Categorical), |
| 50 | + (range(1, 2), ColumnType.Text), |
| 51 | + (range(2, 3), ColumnType.Categorical), |
| 52 | + ], rowspec=[ |
| 53 | + (range(0, 1), RowSpec.Header), |
| 54 | + (range(1, 3), RowSpec.Skipped), |
| 55 | + ], |
| 56 | + ) |
| 57 | + |
| 58 | + def _check_data_regions(self, table): |
| 59 | + self.assertEqual(len(table), 3) |
| 60 | + self.assertEqual(len(table), 3) |
| 61 | + self.assertTrue(table.domain["id"].is_discrete) |
| 62 | + self.assertTrue(table.domain["continent"].is_discrete) |
| 63 | + self.assertTrue(table.domain["state"].is_string) |
| 64 | + assert_array_equal(table.X, [[0, 1], [1, 1], [2, 0]]) |
| 65 | + assert_array_equal(table.metas, |
| 66 | + np.array([["UK"], ["Russia"], ["Mexico"]], object)) |
| 67 | + |
| 68 | + def test_restore(self): |
| 69 | + dirname = os.path.dirname(__file__) |
| 70 | + path = os.path.join(dirname, "data-regions.tab") |
| 71 | + |
| 72 | + w = self.create_widget( |
| 73 | + owcsvimport.OWCSVFileImport, |
| 74 | + stored_settings={ |
| 75 | + "_session_items": [ |
| 76 | + (path, self.data_regions_options.as_dict()) |
| 77 | + ] |
| 78 | + } |
| 79 | + ) |
| 80 | + item = w.current_item() |
| 81 | + self.assertEqual(item.path(), path) |
| 82 | + self.assertEqual(item.options(), self.data_regions_options) |
| 83 | + out = self.get_output("Data", w) |
| 84 | + self._check_data_regions(out) |
| 85 | + |
| 86 | + def test_restore_from_local(self): |
| 87 | + dirname = os.path.dirname(__file__) |
| 88 | + path = os.path.join(dirname, "data-regions.tab") |
| 89 | + s = owcsvimport.OWCSVFileImport._local_settings() |
| 90 | + s.clear() |
| 91 | + QSettings_writeArray( |
| 92 | + s, "recent", [ |
| 93 | + {"path": path, |
| 94 | + "options": json.dumps(self.data_regions_options.as_dict())}] |
| 95 | + ) |
| 96 | + w = self.create_widget( |
| 97 | + owcsvimport.OWCSVFileImport, |
| 98 | + ) |
| 99 | + item = w.current_item() |
| 100 | + self.assertEqual(item.path(), path) |
| 101 | + self.assertEqual(item.options(), self.data_regions_options) |
| 102 | + self.assertEqual( |
| 103 | + w._session_items, [(path, self.data_regions_options.as_dict())], |
| 104 | + "local settings item must be recorded in _session_items when " |
| 105 | + "activated in __init__", |
| 106 | + ) |
| 107 | + self._check_data_regions(self.get_output("Data", w)) |
| 108 | + |
32 | 109 |
|
33 | 110 | class TestImportDialog(GuiTest): |
34 | 111 | def test_dialog(self): |
|
0 commit comments