|
| 1 | +# Test methods with long descriptive names can omit docstrings |
| 2 | +# pylint: disable=missing-docstring |
| 3 | + |
| 4 | +import unittest |
| 5 | +import os |
| 6 | +import tempfile |
| 7 | +import shutil |
| 8 | + |
| 9 | +from Orange.data.io import FileFormat, TabReader, CSVReader, PickleReader |
| 10 | +from Orange.data.table import get_sample_datasets_dir |
| 11 | + |
| 12 | +class WildcardReader(FileFormat): |
| 13 | + EXTENSIONS = ('.wild', '.wild[0-9]') |
| 14 | + DESCRIPTION = "Dummy reader for testing extensions" |
| 15 | + |
| 16 | + def read(self): |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +class TestChooseReader(unittest.TestCase): |
| 21 | + |
| 22 | + def test_usual_extensions(self): |
| 23 | + self.assertIsInstance(FileFormat.get_reader("t.tab"), TabReader) |
| 24 | + self.assertIsInstance(FileFormat.get_reader("t.csv"), CSVReader) |
| 25 | + self.assertIsInstance(FileFormat.get_reader("t.pkl"), PickleReader) |
| 26 | + with self.assertRaises(OSError): |
| 27 | + FileFormat.get_reader("test.undefined_extension") |
| 28 | + |
| 29 | + def test_wildcard_extension(self): |
| 30 | + self.assertIsInstance(FileFormat.get_reader("t.wild"), |
| 31 | + WildcardReader) |
| 32 | + self.assertIsInstance(FileFormat.get_reader("t.wild2"), |
| 33 | + WildcardReader) |
| 34 | + with self.assertRaises(OSError): |
| 35 | + FileFormat.get_reader("t.wild2a") |
| 36 | + |
| 37 | + |
| 38 | +class TestLocate(unittest.TestCase): |
| 39 | + |
| 40 | + def test_locate_sample_datasets(self): |
| 41 | + with self.assertRaises(OSError): |
| 42 | + FileFormat.locate("iris.tab", |
| 43 | + search_dirs=[os.path.dirname(__file__)]) |
| 44 | + iris = FileFormat.locate("iris.tab", |
| 45 | + search_dirs=[get_sample_datasets_dir()]) |
| 46 | + self.assertEqual(os.path.basename(iris), "iris.tab") |
| 47 | + # test extension adding |
| 48 | + iris = FileFormat.locate("iris", |
| 49 | + search_dirs=[get_sample_datasets_dir()]) |
| 50 | + self.assertEqual(os.path.basename(iris), "iris.tab") |
| 51 | + |
| 52 | + |
| 53 | + def test_locate_wildcard_extension(self): |
| 54 | + tempdir = tempfile.mkdtemp() |
| 55 | + with self.assertRaises(OSError): |
| 56 | + FileFormat.locate("t.wild9", search_dirs=[tempdir]) |
| 57 | + fn = os.path.join(tempdir, "t.wild8") |
| 58 | + with open(fn, "wt") as f: |
| 59 | + f.write("\n") |
| 60 | + l = FileFormat.locate("t.wild8", search_dirs=[tempdir]) |
| 61 | + self.assertEqual(l, fn) |
| 62 | + # test extension adding |
| 63 | + l = FileFormat.locate("t", search_dirs=[tempdir]) |
| 64 | + self.assertEqual(l, fn) |
| 65 | + shutil.rmtree(tempdir) |
0 commit comments