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 |
---|
11 | from sas.sascalc.dataloader.loader import Loader |
---|
12 | from sas.sascalc.dataloader.file_reader_base_class import FileReader |
---|
13 | |
---|
14 | logger = logging.getLogger(__name__) |
---|
15 | |
---|
16 | |
---|
17 | def find(filename): |
---|
18 | return os.path.join(os.path.dirname(__file__), filename) |
---|
19 | |
---|
20 | |
---|
21 | class GenericFileReaderTests(unittest.TestCase): |
---|
22 | |
---|
23 | def setUp(self): |
---|
24 | self.reader = TestFileReader() |
---|
25 | self.bad_file = find("ACB123.txt") |
---|
26 | self.good_file = find("123ABC.txt") |
---|
27 | self.generic_reader = Loader() |
---|
28 | self.deprecated_file_type = find("FEB18012.ASC") |
---|
29 | |
---|
30 | def test_bad_file_path(self): |
---|
31 | output = self.reader.read(self.bad_file) |
---|
32 | self.assertEqual(output, []) |
---|
33 | |
---|
34 | def test_good_file_path(self): |
---|
35 | f = open(self.good_file, 'w') |
---|
36 | f.write('123ABC exists!') |
---|
37 | f.close() |
---|
38 | output = self.reader.read(self.good_file) |
---|
39 | self.assertEqual(len(output), 1) |
---|
40 | self.assertEqual(output[0].meta_data["blah"], '123ABC exists!') |
---|
41 | |
---|
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 | |
---|
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) |
---|
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() |
---|
67 | self.send_to_output() |
---|