[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 | |
---|
| 10 | from sas.sascalc.dataloader.data_info import DataInfo, plottable_1D |
---|
[3ee0451] | 11 | from sas.sascalc.dataloader.loader import Loader |
---|
[beba407] | 12 | from sas.sascalc.dataloader.file_reader_base_class import FileReader |
---|
| 13 | |
---|
| 14 | logger = logging.getLogger(__name__) |
---|
| 15 | |
---|
| 16 | |
---|
[f53d684] | 17 | def find(filename): |
---|
| 18 | return os.path.join(os.path.dirname(__file__), filename) |
---|
| 19 | |
---|
| 20 | |
---|
[beba407] | 21 | class GenericFileReaderTests(unittest.TestCase): |
---|
| 22 | |
---|
| 23 | def setUp(self): |
---|
[3648cbf] | 24 | self.reader = TestFileReader() |
---|
[f53d684] | 25 | self.bad_file = find("ACB123.txt") |
---|
| 26 | self.good_file = find("123ABC.txt") |
---|
[3ee0451] | 27 | self.generic_reader = Loader() |
---|
| 28 | self.deprecated_file_type = find("FEB18012.ASC") |
---|
[beba407] | 29 | |
---|
| 30 | def test_bad_file_path(self): |
---|
| 31 | output = self.reader.read(self.bad_file) |
---|
[3648cbf] | 32 | self.assertEqual(output, []) |
---|
[beba407] | 33 | |
---|
| 34 | def test_good_file_path(self): |
---|
[3648cbf] | 35 | f = open(self.good_file, 'w') |
---|
| 36 | f.write('123ABC exists!') |
---|
| 37 | f.close() |
---|
[beba407] | 38 | output = self.reader.read(self.good_file) |
---|
[3648cbf] | 39 | self.assertEqual(len(output), 1) |
---|
| 40 | self.assertEqual(output[0].meta_data["blah"], '123ABC exists!') |
---|
[beba407] | 41 | |
---|
[3ee0451] | 42 | def test_old_file_types(self): |
---|
| 43 | f = self.generic_reader.load(self.deprecated_file_type) |
---|
| 44 | last_f = f[0] |
---|
| 45 | if hasattr(last_f, "errors"): |
---|
| 46 | self.assertEquals(len(last_f.errors), 1) |
---|
| 47 | else: |
---|
| 48 | self.fail("Errors did not propogate to the file properly.") |
---|
| 49 | |
---|
[beba407] | 50 | def tearDown(self): |
---|
| 51 | if os.path.isfile(self.bad_file): |
---|
| 52 | os.remove(self.bad_file) |
---|
| 53 | if os.path.isfile(self.good_file): |
---|
| 54 | os.remove(self.good_file) |
---|
[3648cbf] | 55 | |
---|
| 56 | class TestFileReader(FileReader): |
---|
| 57 | def get_file_contents(self): |
---|
| 58 | """ |
---|
| 59 | Reader specific class to access the contents of the file |
---|
| 60 | All reader classes that inherit from FileReader must implement |
---|
| 61 | """ |
---|
| 62 | x = np.zeros(0) |
---|
| 63 | y = np.zeros(0) |
---|
| 64 | self.current_dataset = plottable_1D(x,y) |
---|
| 65 | self.current_datainfo = DataInfo() |
---|
| 66 | self.current_datainfo.meta_data["blah"] = self.nextline() |
---|
[f53d684] | 67 | self.send_to_output() |
---|