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 sans.dataloader |
---|
17 | from sans.dataloader.loader import Loader |
---|
18 | |
---|
19 | # Check whether we should test image loading on this system |
---|
20 | HAS_IMAGE = False |
---|
21 | try: |
---|
22 | import Image |
---|
23 | HAS_IMAGE = True |
---|
24 | except: |
---|
25 | print "IMAGE TESTS WILL NOT BE PERFORMED: MISSING PIL MODULE" |
---|
26 | |
---|
27 | import os.path |
---|
28 | |
---|
29 | class designtest(unittest.TestCase): |
---|
30 | |
---|
31 | def setUp(self): |
---|
32 | self.loader = Loader() |
---|
33 | |
---|
34 | def test_singleton(self): |
---|
35 | """ |
---|
36 | Testing whether Loader is truly a singleton |
---|
37 | """ |
---|
38 | # Create a 'new' Loader |
---|
39 | b = Loader() |
---|
40 | self.assertEqual(self.loader._get_registry_creation_time(), |
---|
41 | b._get_registry_creation_time()) |
---|
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 | |
---|
56 | def testLoad0(self): |
---|
57 | """test reading empty file""" |
---|
58 | self.assertRaises(RuntimeError, self.L.load, 'empty.txt') |
---|
59 | |
---|
60 | def testLoad1(self): |
---|
61 | """test reading 2 columns""" |
---|
62 | |
---|
63 | #Testing loading a txt file of 2 columns, the only reader should be read1 |
---|
64 | output=self.L.load('test_2_columns.txt') |
---|
65 | x=[2.83954,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449,1.42857,1.63265] |
---|
66 | y=[0.6,3.44938, 5.82026,5.27591,5.2781,5.22531,7.47487,7.85852,10.2278] |
---|
67 | dx=[] |
---|
68 | dy=[] |
---|
69 | self.assertEqual(len(output.x),len(x)) |
---|
70 | self.assertEqual(len(output.y),len(y)) |
---|
71 | |
---|
72 | for i in range(len(x)): |
---|
73 | self.assertEqual(output.x[i],x[i]) |
---|
74 | self.assertEqual(output.y[i],y[i]) |
---|
75 | |
---|
76 | |
---|
77 | def testLoad2(self): |
---|
78 | """Testing loading a txt file of 3 columns""" |
---|
79 | output= self.L.load('test_3_columns.txt') |
---|
80 | x=[0,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449] |
---|
81 | y=[2.83954,3.44938,5.82026,5.27591,5.2781,5.22531,7.47487] |
---|
82 | dx=[] |
---|
83 | dy=[0.6,0.676531,0.753061,0.829592,0.906122,0.982653,1.05918] |
---|
84 | self.assertEqual(len(output.x),len(x)) |
---|
85 | self.assertEqual(len(output.y),len(y)) |
---|
86 | self.assertEqual(len(output.dy),len(dy)) |
---|
87 | for i in range(len(x)): |
---|
88 | self.assertEqual(output.x[i],x[i]) |
---|
89 | self.assertEqual(output.y[i],y[i]) |
---|
90 | self.assertEqual(output.dy[i],dy[i]) |
---|
91 | |
---|
92 | def testLoad2_uppercase(self): |
---|
93 | """Testing loading a txt file of 3 columns""" |
---|
94 | output= self.L.load('test_3_columns.TXT') |
---|
95 | x=[0,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449] |
---|
96 | y=[2.83954,3.44938,5.82026,5.27591,5.2781,5.22531,7.47487] |
---|
97 | dx=[] |
---|
98 | dy=[0.6,0.676531,0.753061,0.829592,0.906122,0.982653,1.05918] |
---|
99 | self.assertEqual(len(output.x),len(x)) |
---|
100 | self.assertEqual(len(output.y),len(y)) |
---|
101 | self.assertEqual(len(output.dy),len(dy)) |
---|
102 | for i in range(len(x)): |
---|
103 | self.assertEqual(output.x[i],x[i]) |
---|
104 | self.assertEqual(output.y[i],y[i]) |
---|
105 | self.assertEqual(output.dy[i],dy[i]) |
---|
106 | |
---|
107 | |
---|
108 | def testload3(self): |
---|
109 | """ Testing loading Igor data""" |
---|
110 | #tested good file.asc |
---|
111 | output= self.L.load('MAR07232_rest.ASC') |
---|
112 | self.assertEqual(output.xmin,-0.018558945804750416) |
---|
113 | self.assertEqual(output.xmax, 0.016234058202440633,) |
---|
114 | self.assertEqual(output.ymin,-0.01684257151702391) |
---|
115 | self.assertEqual(output.ymax,0.017950440578015116) |
---|
116 | |
---|
117 | #tested corrupted file.asc |
---|
118 | try:self.L.load('AR07232_rest.ASC') |
---|
119 | except ValueError,msg: |
---|
120 | #logging.log(10,str(msg)) |
---|
121 | logging.error(str(msg)) |
---|
122 | |
---|
123 | def testload3_lowercase(self): |
---|
124 | """ Testing loading Igor data""" |
---|
125 | #tested good file.asc |
---|
126 | output= self.L.load('MAR07232_rest.asc') |
---|
127 | self.assertEqual(output.xmin,-0.018558945804750416) |
---|
128 | self.assertEqual(output.xmax, 0.016234058202440633,) |
---|
129 | self.assertEqual(output.ymin,-0.01684257151702391) |
---|
130 | self.assertEqual(output.ymax,0.017950440578015116) |
---|
131 | |
---|
132 | #tested corrupted file.asc |
---|
133 | try:self.L.load('AR07232_rest.ASC') |
---|
134 | except ValueError,msg: |
---|
135 | #logging.log(10,str(msg)) |
---|
136 | logging.error(str(msg)) |
---|
137 | def testload4(self): |
---|
138 | """ Testing loading danse file""" |
---|
139 | #tested good file.sans |
---|
140 | output=self.L.load('MP_New.sans') |
---|
141 | |
---|
142 | self.assertEqual(output.source.wavelength,7.5) |
---|
143 | |
---|
144 | #tested corrupted file.sans |
---|
145 | try: self.L.load('P_New.sans') |
---|
146 | except ValueError,msg: |
---|
147 | #logging.log(40,str(msg)) |
---|
148 | logging.error(str(msg)) |
---|
149 | #else: raise ValueError,"No error raised for missing extension" |
---|
150 | |
---|
151 | def testload5(self): |
---|
152 | """ Testing loading image file""" |
---|
153 | if HAS_IMAGE: |
---|
154 | output=self.L.load('angles_flat.png') |
---|
155 | self.assertEqual(output.xbins ,200) |
---|
156 | |
---|
157 | def testload6(self): |
---|
158 | """test file with unknown extension""" |
---|
159 | self.assertRaises(ValueError, self.L.load, 'hello.missing') |
---|
160 | |
---|
161 | # Lookup is not supported as a public method |
---|
162 | #self.assertRaises(ValueError, self.L.lookup, 'hello.missing') |
---|
163 | |
---|
164 | |
---|
165 | def testload7(self): |
---|
166 | """ test file containing an image but as extension .txt""" |
---|
167 | self.assertRaises(RuntimeError, self.L.load, 'angles_flat.txt') |
---|
168 | |
---|
169 | if __name__ == '__main__': |
---|
170 | unittest.main() |
---|
171 | |
---|