[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 |
---|
[16d8e5f] | 18 | |
---|
[96510c8] | 19 | import os.path |
---|
[cc37c818] | 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 | |
---|
[aa749ac] | 40 | class testLoader(unittest.TestCase): |
---|
[d3619421] | 41 | logging.debug("Inside testLoad module") |
---|
[c125e0c] | 42 | |
---|
[cc37c818] | 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 | |
---|
[5f0f3d2] | 52 | |
---|
[d22da51] | 53 | def testLoad0(self): |
---|
| 54 | """test reading empty file""" |
---|
[c125e0c] | 55 | self.assertEqual(self.L.load('empty.txt'),None) |
---|
[d22da51] | 56 | |
---|
| 57 | def testLoad1(self): |
---|
| 58 | """test reading 2 columns""" |
---|
| 59 | |
---|
[c125e0c] | 60 | #Testing loading a txt file of 2 columns, the only reader should be read1 |
---|
[16d8e5f] | 61 | output=self.L.load('test_2_columns.txt') |
---|
[c125e0c] | 62 | x=[2.83954,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449,1.42857,1.63265] |
---|
[aa749ac] | 63 | y=[0.6,3.44938, 5.82026,5.27591,5.2781,5.22531,7.47487,7.85852,10.2278] |
---|
| 64 | dx=[] |
---|
| 65 | dy=[] |
---|
[16d8e5f] | 66 | self.assertEqual(len(output.x),len(x)) |
---|
| 67 | self.assertEqual(len(output.y),len(y)) |
---|
| 68 | |
---|
[c125e0c] | 69 | for i in range(len(x)): |
---|
[16d8e5f] | 70 | self.assertEqual(output.x[i],x[i]) |
---|
| 71 | self.assertEqual(output.y[i],y[i]) |
---|
[d22da51] | 72 | |
---|
[c125e0c] | 73 | |
---|
| 74 | def testLoad2(self): |
---|
[d22da51] | 75 | """Testing loading a txt file of 3 columns""" |
---|
[16d8e5f] | 76 | output= self.L.load('test_3_columns.txt') |
---|
[c125e0c] | 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] |
---|
[16d8e5f] | 81 | self.assertEqual(len(output.x),len(x)) |
---|
| 82 | self.assertEqual(len(output.y),len(y)) |
---|
| 83 | self.assertEqual(len(output.dy),len(dy)) |
---|
[c125e0c] | 84 | for i in range(len(x)): |
---|
[16d8e5f] | 85 | self.assertEqual(output.x[i],x[i]) |
---|
| 86 | self.assertEqual(output.y[i],y[i]) |
---|
| 87 | self.assertEqual(output.dy[i],dy[i]) |
---|
[d22da51] | 88 | |
---|
[c125e0c] | 89 | |
---|
| 90 | def testload3(self): |
---|
| 91 | """ Testing loading Igor data""" |
---|
| 92 | #tested good file.asc |
---|
[16d8e5f] | 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) |
---|
[d22da51] | 98 | |
---|
[c125e0c] | 99 | #tested corrupted file.asc |
---|
[d22da51] | 100 | try:self.L.load('AR07232_rest.ASC') |
---|
| 101 | except ValueError,msg: |
---|
| 102 | #logging.log(10,str(msg)) |
---|
| 103 | logging.error(str(msg)) |
---|
[c125e0c] | 104 | def testload4(self): |
---|
| 105 | """ Testing loading danse file""" |
---|
| 106 | #tested good file.sans |
---|
[16d8e5f] | 107 | output=self.L.load('MP_New.sans') |
---|
[aa749ac] | 108 | |
---|
[b99ac227] | 109 | self.assertEqual(output.source.wavelength,7.5) |
---|
[d22da51] | 110 | |
---|
[c125e0c] | 111 | #tested corrupted file.sans |
---|
[d22da51] | 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" |
---|
[8d6440f] | 117 | |
---|
[c125e0c] | 118 | def testload5(self): |
---|
| 119 | """ Testing loading image file""" |
---|
[16d8e5f] | 120 | output=self.L.load('angles_flat.png') |
---|
| 121 | self.assertEqual(output.xbins ,200) |
---|
[d22da51] | 122 | |
---|
| 123 | def testload6(self): |
---|
| 124 | """test file with unknown extension""" |
---|
| 125 | try:self.L.load('hello.missing') |
---|
[8d6440f] | 126 | except RuntimeError,msg: |
---|
[d22da51] | 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') |
---|
[8d6440f] | 132 | except RuntimeError,msg: |
---|
[d22da51] | 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) |
---|
[cc37c818] | 139 | |
---|
| 140 | if __name__ == '__main__': |
---|
| 141 | unittest.main() |
---|
[1b0b3ca] | 142 | |
---|