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