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 | #logger.info('oops I did it again') |
---|
14 | |
---|
15 | import unittest |
---|
16 | import math |
---|
17 | import DataLoader |
---|
18 | from DataLoader.loader import Loader |
---|
19 | from DataLoader.readers import TXT3_Reader,TXT2_Reader |
---|
20 | from DataLoader.readers import IgorReader,danse_reader,tiff_reader |
---|
21 | |
---|
22 | import os.path |
---|
23 | |
---|
24 | class designtest(unittest.TestCase): |
---|
25 | |
---|
26 | def setUp(self): |
---|
27 | self.loader = Loader() |
---|
28 | |
---|
29 | |
---|
30 | def test_singleton(self): |
---|
31 | """ |
---|
32 | Testing whether Loader is truly a singleton |
---|
33 | """ |
---|
34 | # Set a new data member |
---|
35 | self.loader._test_data_member = 1.45 |
---|
36 | |
---|
37 | # Create a 'new' Loader |
---|
38 | b = Loader() |
---|
39 | |
---|
40 | # Test that the new loader has the new data member |
---|
41 | self.assertEqual(b._test_data_member, self.loader._test_data_member) |
---|
42 | |
---|
43 | class testLoader(unittest.TestCase): |
---|
44 | logging.debug("Inside testLoad module") |
---|
45 | |
---|
46 | """ test fitting """ |
---|
47 | def setUp(self): |
---|
48 | """ |
---|
49 | Set up the initial conditions before _each_ test |
---|
50 | so that they all start from the same well-defined state. |
---|
51 | """ |
---|
52 | #Creating a loader |
---|
53 | self.L=Loader() |
---|
54 | |
---|
55 | #creating readers |
---|
56 | read1=TXT2_Reader.Reader() |
---|
57 | read2=TXT3_Reader.Reader() |
---|
58 | read3=IgorReader.Reader() |
---|
59 | read4=danse_reader.Reader() |
---|
60 | read5=tiff_reader.Reader() |
---|
61 | #for each readers set an extensions inside the loader |
---|
62 | |
---|
63 | #TODO: should not call __setitem__ from outside |
---|
64 | # the class. That's not the purpose of __setitem__ |
---|
65 | self.L.__setitem__("plugins",'.txt',read2) |
---|
66 | self.L.__setitem__(None,'.txt',read1) |
---|
67 | self.L.__setitem__(None,'.dat',read1) |
---|
68 | |
---|
69 | self.L.__setitem__(None,'.dat',read2) |
---|
70 | self.L.__setitem__(None,'.ASC',read3) |
---|
71 | self.L.__setitem__(None,'.sans',read4) |
---|
72 | self.L.__setitem__(None,'.tif',read5) |
---|
73 | self.L.__setitem__(None,'.jpg',read5) |
---|
74 | self.L.__setitem__(None,'.png',read5) |
---|
75 | self.L.__setitem__(None,'.jpeg',read5) |
---|
76 | self.L.__setitem__(None,'.gif',read5) |
---|
77 | self.L.__setitem__(None,'.bmp',read5) |
---|
78 | |
---|
79 | def testLoad0(self): |
---|
80 | """test reading empty file""" |
---|
81 | self.assertEqual(self.L.load('empty.txt'),None) |
---|
82 | |
---|
83 | def testLoad1(self): |
---|
84 | """test reading 2 columns""" |
---|
85 | |
---|
86 | #Testing loading a txt file of 2 columns, the only reader should be read1 |
---|
87 | output=self.L.load('test_2_columns.txt') |
---|
88 | x=[2.83954,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449,1.42857,1.63265] |
---|
89 | y=[0.6,3.44938, 5.82026,5.27591,5.2781,5.22531,7.47487,7.85852,10.2278] |
---|
90 | dx=[] |
---|
91 | dy=[] |
---|
92 | self.assertEqual(len(output.x),len(x)) |
---|
93 | self.assertEqual(len(output.y),len(y)) |
---|
94 | |
---|
95 | for i in range(len(x)): |
---|
96 | self.assertEqual(output.x[i],x[i]) |
---|
97 | self.assertEqual(output.y[i],y[i]) |
---|
98 | |
---|
99 | |
---|
100 | def testLoad2(self): |
---|
101 | """Testing loading a txt file of 3 columns""" |
---|
102 | output= self.L.load('test_3_columns.txt') |
---|
103 | x=[0,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449] |
---|
104 | y=[2.83954,3.44938,5.82026,5.27591,5.2781,5.22531,7.47487] |
---|
105 | dx=[] |
---|
106 | dy=[0.6,0.676531,0.753061,0.829592,0.906122,0.982653,1.05918] |
---|
107 | self.assertEqual(len(output.x),len(x)) |
---|
108 | self.assertEqual(len(output.y),len(y)) |
---|
109 | self.assertEqual(len(output.dy),len(dy)) |
---|
110 | for i in range(len(x)): |
---|
111 | self.assertEqual(output.x[i],x[i]) |
---|
112 | self.assertEqual(output.y[i],y[i]) |
---|
113 | self.assertEqual(output.dy[i],dy[i]) |
---|
114 | |
---|
115 | |
---|
116 | def testload3(self): |
---|
117 | """ Testing loading Igor data""" |
---|
118 | #tested good file.asc |
---|
119 | output= self.L.load('MAR07232_rest.ASC') |
---|
120 | self.assertEqual(output.xmin,-0.018558945804750416) |
---|
121 | self.assertEqual(output.xmax, 0.016234058202440633,) |
---|
122 | self.assertEqual(output.ymin,-0.01684257151702391) |
---|
123 | self.assertEqual(output.ymax,0.017950440578015116) |
---|
124 | |
---|
125 | #tested corrupted file.asc |
---|
126 | try:self.L.load('AR07232_rest.ASC') |
---|
127 | except ValueError,msg: |
---|
128 | #logging.log(10,str(msg)) |
---|
129 | logging.error(str(msg)) |
---|
130 | def testload4(self): |
---|
131 | """ Testing loading danse file""" |
---|
132 | #tested good file.sans |
---|
133 | output=self.L.load('MP_New.sans') |
---|
134 | |
---|
135 | self.assertEqual(output.wavelength,7.5) |
---|
136 | |
---|
137 | #tested corrupted file.sans |
---|
138 | try: self.L.load('P_New.sans') |
---|
139 | except ValueError,msg: |
---|
140 | #logging.log(40,str(msg)) |
---|
141 | logging.error(str(msg)) |
---|
142 | #else: raise ValueError,"No error raised for missing extension" |
---|
143 | |
---|
144 | def testload5(self): |
---|
145 | """ Testing loading image file""" |
---|
146 | output=self.L.load('angles_flat.png') |
---|
147 | self.assertEqual(output.xbins ,200) |
---|
148 | |
---|
149 | def testload6(self): |
---|
150 | """test file with unknown extension""" |
---|
151 | try:self.L.load('hello.missing') |
---|
152 | except RuntimeError,msg: |
---|
153 | self.assertEqual( str(msg),"Unknown file type '.missing'") |
---|
154 | else: raise ValueError,"No error raised for missing extension" |
---|
155 | |
---|
156 | #self.L.lookup('hello.missing') |
---|
157 | try: self.L.lookup('hello.missing') |
---|
158 | except RuntimeError,msg: |
---|
159 | self.assertEqual( str(msg),"Unknown file type '.missing'") |
---|
160 | else: raise ValueError,"No error raised for missing extension" |
---|
161 | |
---|
162 | def testload7(self): |
---|
163 | """ test file containing an image but as extension .txt""" |
---|
164 | self.assertEqual(self.L.load('angles_flat.txt'),None) |
---|
165 | |
---|
166 | if __name__ == '__main__': |
---|
167 | unittest.main() |
---|
168 | |
---|