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