[beba407] | 1 | """ |
---|
| 2 | Unit tests for the generic file reader class |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | import os |
---|
| 6 | import unittest |
---|
| 7 | import logging |
---|
| 8 | import numpy as np |
---|
| 9 | |
---|
[4a8d55c] | 10 | from sas.sascalc.dataloader.data_info import DataInfo, plottable_1D, Data1D |
---|
[3ee0451] | 11 | from sas.sascalc.dataloader.loader import Loader |
---|
[425feff] | 12 | from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException |
---|
[beba407] | 13 | from sas.sascalc.dataloader.file_reader_base_class import FileReader |
---|
| 14 | |
---|
| 15 | logger = logging.getLogger(__name__) |
---|
| 16 | |
---|
| 17 | |
---|
[f53d684] | 18 | def find(filename): |
---|
| 19 | return os.path.join(os.path.dirname(__file__), filename) |
---|
| 20 | |
---|
| 21 | |
---|
[beba407] | 22 | class GenericFileReaderTests(unittest.TestCase): |
---|
| 23 | |
---|
| 24 | def setUp(self): |
---|
[3648cbf] | 25 | self.reader = TestFileReader() |
---|
[f53d684] | 26 | self.bad_file = find("ACB123.txt") |
---|
| 27 | self.good_file = find("123ABC.txt") |
---|
[3ee0451] | 28 | self.generic_reader = Loader() |
---|
| 29 | self.deprecated_file_type = find("FEB18012.ASC") |
---|
[beba407] | 30 | |
---|
| 31 | def test_bad_file_path(self): |
---|
[425feff] | 32 | self.assertRaises(NoKnownLoaderException, self.reader.read, |
---|
| 33 | self.bad_file) |
---|
[beba407] | 34 | |
---|
| 35 | def test_good_file_path(self): |
---|
[3648cbf] | 36 | f = open(self.good_file, 'w') |
---|
| 37 | f.write('123ABC exists!') |
---|
| 38 | f.close() |
---|
[beba407] | 39 | output = self.reader.read(self.good_file) |
---|
[3648cbf] | 40 | self.assertEqual(len(output), 1) |
---|
| 41 | self.assertEqual(output[0].meta_data["blah"], '123ABC exists!') |
---|
[beba407] | 42 | |
---|
[3ee0451] | 43 | def test_old_file_types(self): |
---|
| 44 | f = self.generic_reader.load(self.deprecated_file_type) |
---|
| 45 | last_f = f[0] |
---|
| 46 | if hasattr(last_f, "errors"): |
---|
[f4e2f22] | 47 | self.assertEqual(len(last_f.errors), 1) |
---|
[3ee0451] | 48 | else: |
---|
| 49 | self.fail("Errors did not propogate to the file properly.") |
---|
| 50 | |
---|
[4a8d55c] | 51 | def test_same_file_unknown_extensions(self): |
---|
| 52 | # Five files, all with the same content, but different file extensions |
---|
[f4e2f22] | 53 | no_ext = find("test_data" + os.sep + "TestExtensions") |
---|
| 54 | not_xml = find("test_data" + os.sep + "TestExtensions.notxml") |
---|
[4a8d55c] | 55 | # Deprecated extensions |
---|
[f4e2f22] | 56 | asc_dep = find("test_data" + os.sep + "TestExtensions.asc") |
---|
| 57 | nxs_dep = find("test_data" + os.sep + "TestExtensions.nxs") |
---|
[4a8d55c] | 58 | # Native extension as a baseline |
---|
[f4e2f22] | 59 | xml_native = find("test_data" + os.sep + "TestExtensions.xml") |
---|
[4a8d55c] | 60 | # Load the files and check contents |
---|
| 61 | no_ext_load = self.generic_reader.load(no_ext) |
---|
| 62 | asc_load = self.generic_reader.load(asc_dep) |
---|
| 63 | nxs_load = self.generic_reader.load(nxs_dep) |
---|
| 64 | not_xml_load = self.generic_reader.load(not_xml) |
---|
| 65 | xml_load = self.generic_reader.load(xml_native) |
---|
| 66 | self.check_unknown_extension(no_ext_load[0]) |
---|
| 67 | self.check_unknown_extension(asc_load[0]) |
---|
| 68 | self.check_unknown_extension(nxs_load[0]) |
---|
| 69 | self.check_unknown_extension(not_xml_load[0]) |
---|
| 70 | self.check_unknown_extension(xml_load[0]) |
---|
| 71 | # Be sure the deprecation warning is passed with the file |
---|
[f4e2f22] | 72 | self.assertEqual(len(asc_load[0].errors), 1) |
---|
| 73 | self.assertEqual(len(nxs_load[0].errors), 1) |
---|
[4a8d55c] | 74 | |
---|
| 75 | def check_unknown_extension(self, data): |
---|
| 76 | self.assertTrue(isinstance(data, Data1D)) |
---|
[f4e2f22] | 77 | self.assertEqual(len(data.x), 138) |
---|
| 78 | self.assertEqual(data.sample.ID, "TK49 c10_SANS") |
---|
| 79 | self.assertEqual(data.meta_data["loader"], "CanSAS XML 1D") |
---|
[4a8d55c] | 80 | |
---|
[beba407] | 81 | def tearDown(self): |
---|
| 82 | if os.path.isfile(self.bad_file): |
---|
| 83 | os.remove(self.bad_file) |
---|
| 84 | if os.path.isfile(self.good_file): |
---|
| 85 | os.remove(self.good_file) |
---|
[3648cbf] | 86 | |
---|
| 87 | class TestFileReader(FileReader): |
---|
| 88 | def get_file_contents(self): |
---|
| 89 | """ |
---|
| 90 | Reader specific class to access the contents of the file |
---|
| 91 | All reader classes that inherit from FileReader must implement |
---|
| 92 | """ |
---|
| 93 | x = np.zeros(0) |
---|
| 94 | y = np.zeros(0) |
---|
| 95 | self.current_dataset = plottable_1D(x,y) |
---|
| 96 | self.current_datainfo = DataInfo() |
---|
| 97 | self.current_datainfo.meta_data["blah"] = self.nextline() |
---|
[f53d684] | 98 | self.send_to_output() |
---|