1 | """ |
---|
2 | Unit tests for the ascii (n-column) reader |
---|
3 | """ |
---|
4 | |
---|
5 | import os.path |
---|
6 | import warnings |
---|
7 | warnings.simplefilter("ignore") |
---|
8 | |
---|
9 | import unittest |
---|
10 | from sas.sascalc.dataloader.loader import Loader |
---|
11 | |
---|
12 | |
---|
13 | def find(filename): |
---|
14 | return os.path.join(os.path.dirname(__file__), filename) |
---|
15 | |
---|
16 | |
---|
17 | class ABSReaderTests(unittest.TestCase): |
---|
18 | |
---|
19 | def setUp(self): |
---|
20 | self.loader = Loader() |
---|
21 | self.f1_list = self.loader.load(find("ascii_test_1.txt")) |
---|
22 | self.f1 = self.f1_list[0] |
---|
23 | self.f2_list = self.loader.load(find("ascii_test_2.txt")) |
---|
24 | self.f2 = self.f2_list[0] |
---|
25 | self.f3_list = self.loader.load(find("ascii_test_3.txt")) |
---|
26 | self.f3 = self.f3_list[0] |
---|
27 | self.f4_list = self.loader.load(find("ascii_test_4.abs")) |
---|
28 | self.f4 = self.f4_list[0] |
---|
29 | self.f5_list = self.loader.load(find("ascii_test_5.txt")) |
---|
30 | self.f5 = self.f5_list[0] |
---|
31 | |
---|
32 | def test_checkdata(self): |
---|
33 | """ |
---|
34 | Test .ABS file loaded as ascii |
---|
35 | """ |
---|
36 | # The length of the data is 10 |
---|
37 | self.assertEqual(len(self.f1_list), 1) |
---|
38 | self.assertEqual(len(self.f2_list), 1) |
---|
39 | self.assertEqual(len(self.f3_list), 1) |
---|
40 | self.assertEqual(len(self.f4_list), 1) |
---|
41 | self.assertEqual(len(self.f5_list), 1) |
---|
42 | self.assertEqual(len(self.f1.x), 10) |
---|
43 | self.assertEqual(self.f1.x[0],0.002618) |
---|
44 | self.assertEqual(self.f1.x[9],0.0497) |
---|
45 | self.assertTrue(self.f1.x_unit == 'A^{-1}') |
---|
46 | self.assertTrue(self.f1.y_unit == 'cm^{-1}') |
---|
47 | |
---|
48 | self.assertEqual(self.f1.meta_data['loader'],"ASCII") |
---|
49 | |
---|
50 | def test_truncated_1(self): |
---|
51 | """ |
---|
52 | Test an ascii file with header and a |
---|
53 | comment line in the middle of the data section. |
---|
54 | The business rule says that we should stop |
---|
55 | reading at the first comment once the data |
---|
56 | section has started (and treat the comment |
---|
57 | as though it were the start of a footer). |
---|
58 | """ |
---|
59 | # The length of the data is 5 |
---|
60 | self.assertEqual(len(self.f2.x), 5) |
---|
61 | self.assertEqual(self.f2.x[0],0.002618) |
---|
62 | self.assertEqual(self.f2.x[4],0.02356) |
---|
63 | |
---|
64 | def test_truncated_2(self): |
---|
65 | """ |
---|
66 | Test a 6-col ascii file with header and a |
---|
67 | line with only 2 columns in the middle of the data section. |
---|
68 | The business rule says that we should stop |
---|
69 | reading at the first inconsitent line. |
---|
70 | """ |
---|
71 | # The length of the data is 5 |
---|
72 | self.assertEqual(len(self.f3.x), 5) |
---|
73 | self.assertEqual(self.f3.x[0],0.002618) |
---|
74 | self.assertEqual(self.f3.x[4],0.02356) |
---|
75 | |
---|
76 | def test_truncated_3(self): |
---|
77 | """ |
---|
78 | Test a 6-col ascii file with complex header and |
---|
79 | many lines with 2 or 2 columns in the middle of the data section. |
---|
80 | The business rule says that we should stop |
---|
81 | reading at the last line of header. |
---|
82 | """ |
---|
83 | # The length of the data is 5 |
---|
84 | self.assertEqual(len(self.f4.x), 5) |
---|
85 | self.assertEqual(self.f4.x[0],0.012654) |
---|
86 | self.assertEqual(self.f4.x[4],0.02654) |
---|
87 | |
---|
88 | def test_truncated_4(self): |
---|
89 | """ |
---|
90 | Test mix of 6-col and 2-col. |
---|
91 | Only the last 5 2-col lines should be read. |
---|
92 | """ |
---|
93 | # The length of the data is 5 |
---|
94 | self.assertEqual(len(self.f5.x), 5) |
---|
95 | self.assertEqual(self.f5.x[0],0.02879) |
---|
96 | self.assertEqual(self.f5.x[4],0.0497) |
---|
97 | |
---|
98 | def test_truncated_5(self): |
---|
99 | """ |
---|
100 | Test a 6-col ascii file with complex header where one of them has a |
---|
101 | letter and many lines with 2 or 2 columns in the middle of the data |
---|
102 | section. Will be rejected because fewer than 5 lines. |
---|
103 | """ |
---|
104 | # Test .ABS file loaded as ascii |
---|
105 | f = None |
---|
106 | try: |
---|
107 | f = self.loader.load(find("ascii_test_6.txt")) |
---|
108 | # The length of the data is 5 |
---|
109 | except: |
---|
110 | self.assertEqual(f, None) |
---|
111 | |
---|
112 | if __name__ == '__main__': |
---|
113 | unittest.main() |
---|
114 | |
---|