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