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