1 | """ |
---|
2 | Unit tests for DataLoader module |
---|
3 | """ |
---|
4 | |
---|
5 | import unittest |
---|
6 | import math |
---|
7 | import DataLoader |
---|
8 | from DataLoader.loader import Loader |
---|
9 | from DataLoader.readers import TXT3_Reader,TXT2_Reader |
---|
10 | from DataLoader.readers import IgorReader,danse_reader,tiff_reader |
---|
11 | import os.path |
---|
12 | class testLoader(unittest.TestCase): |
---|
13 | |
---|
14 | |
---|
15 | """ test fitting """ |
---|
16 | #Creating a loader |
---|
17 | L=Loader() |
---|
18 | |
---|
19 | #creating readers |
---|
20 | read1=TXT2_Reader.Reader() |
---|
21 | read2=TXT3_Reader.Reader() |
---|
22 | read3=IgorReader.Reader() |
---|
23 | read4=danse_reader.Reader() |
---|
24 | read5=tiff_reader.Reader() |
---|
25 | #for each readers set an extensions inside the loader |
---|
26 | L.__setitem__('.txt',read2) |
---|
27 | L.__setitem__('.txt',read1) |
---|
28 | L.__setitem__('.dat',read1) |
---|
29 | |
---|
30 | L.__setitem__('.dat',read2) |
---|
31 | L.__setitem__('.ASC',read3) |
---|
32 | L.__setitem__('.sans',read4) |
---|
33 | L.__setitem__('.tif',read5) |
---|
34 | L.__setitem__('.jpg',read5) |
---|
35 | L.__setitem__('.png',read5) |
---|
36 | L.__setitem__('.jpeg',read5) |
---|
37 | L.__setitem__('.gif',read5) |
---|
38 | L.__setitem__('.bmp',read5) |
---|
39 | |
---|
40 | def testLoad1(self): |
---|
41 | """test reading empty file, no file can read it""" |
---|
42 | self.assertEqual(self.L.load('empty.txt'),None) |
---|
43 | self.assertEqual( self.L.getAcTReader('empty.txt'),None) |
---|
44 | |
---|
45 | #Testing loading a txt file of 2 columns, the only reader should be read1 |
---|
46 | xload,yload,dyload=self.L.load('test_2_columns.txt') |
---|
47 | x=[2.83954,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449,1.42857,1.63265] |
---|
48 | y=[0.6,3.44938, 5.82026,5.27591,5.2781,5.22531,7.47487,7.85852,10.2278] |
---|
49 | dx=[] |
---|
50 | dy=[] |
---|
51 | self.assertEqual(len(xload),len(x)) |
---|
52 | self.assertEqual(len(yload),len(y)) |
---|
53 | self.assertEqual(len(dyload),0) |
---|
54 | for i in range(len(x)): |
---|
55 | self.assertEqual(xload[i],x[i]) |
---|
56 | self.assertEqual(yload[i],y[i]) |
---|
57 | self.assertEqual( self.L.getAcTReader('test_2_columns.txt'),self.read1.__class__) |
---|
58 | |
---|
59 | def testLoad2(self): |
---|
60 | """Testing loading a txt file of 3 columns, the only reader should be read2""" |
---|
61 | xload,yload,dyload= self.L.load('test_3_columns.txt') |
---|
62 | x=[0,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449] |
---|
63 | y=[2.83954,3.44938,5.82026,5.27591,5.2781,5.22531,7.47487] |
---|
64 | dx=[] |
---|
65 | dy=[0.6,0.676531,0.753061,0.829592,0.906122,0.982653,1.05918] |
---|
66 | self.assertEqual(len(xload),len(x)) |
---|
67 | self.assertEqual(len(yload),len(y)) |
---|
68 | self.assertEqual(len(dyload),len(dy)) |
---|
69 | for i in range(len(x)): |
---|
70 | self.assertEqual(xload[i],x[i]) |
---|
71 | self.assertEqual(yload[i],y[i]) |
---|
72 | self.assertEqual(dyload[i],dy[i]) |
---|
73 | self.assertEqual(self.L.getAcTReader('test_3_columns.txt'),self.read2.__class__) |
---|
74 | |
---|
75 | def testload3(self): |
---|
76 | """ Testing loading Igor data""" |
---|
77 | #tested good file.asc |
---|
78 | Z,xmin, xmax, ymin, ymax= self.L.load('MAR07232_rest.ASC') |
---|
79 | self.assertEqual(xmin,-0.018558945804750416) |
---|
80 | self.assertEqual(xmax, 0.016234058202440633,) |
---|
81 | self.assertEqual(ymin,-0.01684257151702391) |
---|
82 | self.assertEqual(ymax,0.017950440578015116) |
---|
83 | self.assertEqual(self.L.getAcTReader('MAR07232_rest.ASC'),self.read3.__class__) |
---|
84 | #tested corrupted file.asc |
---|
85 | self.assertEqual(self.L.load('AR07232_rest.ASC') ,None) |
---|
86 | |
---|
87 | def testload4(self): |
---|
88 | """ Testing loading danse file""" |
---|
89 | #tested good file.sans |
---|
90 | data=self.L.load('MP_New.sans') |
---|
91 | |
---|
92 | self.assertEqual(data.__class__,danse_reader.ReaderInfo) |
---|
93 | self.assertEqual(self.L.getAcTReader('MP_New.sans'),self.read4.__class__) |
---|
94 | #tested corrupted file.sans |
---|
95 | self.assertEqual(self.L.load('P_New.sans'),None) |
---|
96 | |
---|
97 | def testload5(self): |
---|
98 | """ Testing loading image file""" |
---|
99 | data=self.L.load('angles_flat.png') |
---|
100 | self.assertEqual(data.__class__,tiff_reader.ReaderInfo) |
---|
101 | self.assertEqual(self.L.getAcTReader('angles_flat.png'),self.read5.__class__) |
---|
102 | |
---|
103 | |
---|