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