1 | """ |
---|
2 | Unit tests for DataLoader module |
---|
3 | log file "test_log.txt" contains all errors when running loader |
---|
4 | It is create in the folder where test is runned |
---|
5 | """ |
---|
6 | import logging |
---|
7 | logging.basicConfig(level=logging.DEBUG, |
---|
8 | format='%(asctime)s %(levelname)s %(message)s', |
---|
9 | filename='test_log.txt', |
---|
10 | filemode='w') |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | import unittest |
---|
15 | import math |
---|
16 | import DataLoader |
---|
17 | from DataLoader.loader import Loader |
---|
18 | from DataLoader.readers import TXT3_Reader,TXT2_Reader |
---|
19 | from DataLoader.readers import IgorReader,danse_reader,tiff_reader |
---|
20 | |
---|
21 | import os.path |
---|
22 | |
---|
23 | class designtest(unittest.TestCase): |
---|
24 | |
---|
25 | def setUp(self): |
---|
26 | self.loader = Loader() |
---|
27 | |
---|
28 | |
---|
29 | def test_singleton(self): |
---|
30 | """ |
---|
31 | Testing whether Loader is truly a singleton |
---|
32 | """ |
---|
33 | # Set a new data member |
---|
34 | self.loader._test_data_member = 1.45 |
---|
35 | |
---|
36 | # Create a 'new' Loader |
---|
37 | b = Loader() |
---|
38 | |
---|
39 | # Test that the new loader has the new data member |
---|
40 | self.assertEqual(b._test_data_member, self.loader._test_data_member) |
---|
41 | |
---|
42 | class testLoader(unittest.TestCase): |
---|
43 | logging.debug("Inside testLoad module") |
---|
44 | |
---|
45 | """ test fitting """ |
---|
46 | def setUp(self): |
---|
47 | """ |
---|
48 | Set up the initial conditions before _each_ test |
---|
49 | so that they all start from the same well-defined state. |
---|
50 | """ |
---|
51 | #Creating a loader |
---|
52 | self.L=Loader() |
---|
53 | |
---|
54 | |
---|
55 | def testLoad0(self): |
---|
56 | """test reading empty file""" |
---|
57 | self.assertEqual(self.L.load('empty.txt'),None) |
---|
58 | |
---|
59 | def testLoad1(self): |
---|
60 | """test reading 2 columns""" |
---|
61 | |
---|
62 | #Testing loading a txt file of 2 columns, the only reader should be read1 |
---|
63 | output=self.L.load('test_2_columns.txt') |
---|
64 | x=[2.83954,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449,1.42857,1.63265] |
---|
65 | y=[0.6,3.44938, 5.82026,5.27591,5.2781,5.22531,7.47487,7.85852,10.2278] |
---|
66 | dx=[] |
---|
67 | dy=[] |
---|
68 | self.assertEqual(len(output.x),len(x)) |
---|
69 | self.assertEqual(len(output.y),len(y)) |
---|
70 | |
---|
71 | for i in range(len(x)): |
---|
72 | self.assertEqual(output.x[i],x[i]) |
---|
73 | self.assertEqual(output.y[i],y[i]) |
---|
74 | |
---|
75 | |
---|
76 | def testLoad2(self): |
---|
77 | """Testing loading a txt file of 3 columns""" |
---|
78 | output= self.L.load('test_3_columns.txt') |
---|
79 | x=[0,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449] |
---|
80 | y=[2.83954,3.44938,5.82026,5.27591,5.2781,5.22531,7.47487] |
---|
81 | dx=[] |
---|
82 | dy=[0.6,0.676531,0.753061,0.829592,0.906122,0.982653,1.05918] |
---|
83 | self.assertEqual(len(output.x),len(x)) |
---|
84 | self.assertEqual(len(output.y),len(y)) |
---|
85 | self.assertEqual(len(output.dy),len(dy)) |
---|
86 | for i in range(len(x)): |
---|
87 | self.assertEqual(output.x[i],x[i]) |
---|
88 | self.assertEqual(output.y[i],y[i]) |
---|
89 | self.assertEqual(output.dy[i],dy[i]) |
---|
90 | |
---|
91 | |
---|
92 | def testload3(self): |
---|
93 | """ Testing loading Igor data""" |
---|
94 | #tested good file.asc |
---|
95 | output= self.L.load('MAR07232_rest.ASC') |
---|
96 | self.assertEqual(output.xmin,-0.018558945804750416) |
---|
97 | self.assertEqual(output.xmax, 0.016234058202440633,) |
---|
98 | self.assertEqual(output.ymin,-0.01684257151702391) |
---|
99 | self.assertEqual(output.ymax,0.017950440578015116) |
---|
100 | |
---|
101 | #tested corrupted file.asc |
---|
102 | try:self.L.load('AR07232_rest.ASC') |
---|
103 | except ValueError,msg: |
---|
104 | #logging.log(10,str(msg)) |
---|
105 | logging.error(str(msg)) |
---|
106 | def testload4(self): |
---|
107 | """ Testing loading danse file""" |
---|
108 | #tested good file.sans |
---|
109 | output=self.L.load('MP_New.sans') |
---|
110 | |
---|
111 | self.assertEqual(output.wavelength,7.5) |
---|
112 | |
---|
113 | #tested corrupted file.sans |
---|
114 | try: self.L.load('P_New.sans') |
---|
115 | except ValueError,msg: |
---|
116 | #logging.log(40,str(msg)) |
---|
117 | logging.error(str(msg)) |
---|
118 | #else: raise ValueError,"No error raised for missing extension" |
---|
119 | |
---|
120 | def testload5(self): |
---|
121 | """ Testing loading image file""" |
---|
122 | output=self.L.load('angles_flat.png') |
---|
123 | self.assertEqual(output.xbins ,200) |
---|
124 | |
---|
125 | def testload6(self): |
---|
126 | """test file with unknown extension""" |
---|
127 | try:self.L.load('hello.missing') |
---|
128 | except RuntimeError,msg: |
---|
129 | self.assertEqual( str(msg),"Unknown file type '.missing'") |
---|
130 | else: raise ValueError,"No error raised for missing extension" |
---|
131 | |
---|
132 | #self.L.lookup('hello.missing') |
---|
133 | try: self.L.lookup('hello.missing') |
---|
134 | except RuntimeError,msg: |
---|
135 | self.assertEqual( str(msg),"Unknown file type '.missing'") |
---|
136 | else: raise ValueError,"No error raised for missing extension" |
---|
137 | |
---|
138 | def testload7(self): |
---|
139 | """ test file containing an image but as extension .txt""" |
---|
140 | self.assertEqual(self.L.load('angles_flat.txt'),None) |
---|
141 | |
---|
142 | if __name__ == '__main__': |
---|
143 | unittest.main() |
---|
144 | |
---|