[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 |
---|
[ca3b9c5d] | 16 | import sans.dataloader |
---|
| 17 | from sans.dataloader.loader import Loader |
---|
[daa56d0] | 18 | |
---|
| 19 | # Check whether we should test image loading on this system |
---|
| 20 | HAS_IMAGE = False |
---|
| 21 | try: |
---|
| 22 | import Image |
---|
| 23 | HAS_IMAGE = True |
---|
| 24 | except: |
---|
| 25 | print "IMAGE TESTS WILL NOT BE PERFORMED: MISSING PIL MODULE" |
---|
| 26 | |
---|
[96510c8] | 27 | import os.path |
---|
[cc37c818] | 28 | |
---|
| 29 | class designtest(unittest.TestCase): |
---|
| 30 | |
---|
| 31 | def setUp(self): |
---|
| 32 | self.loader = Loader() |
---|
| 33 | |
---|
| 34 | def test_singleton(self): |
---|
| 35 | """ |
---|
| 36 | Testing whether Loader is truly a singleton |
---|
| 37 | """ |
---|
| 38 | # Create a 'new' Loader |
---|
| 39 | b = Loader() |
---|
[daa56d0] | 40 | self.assertEqual(self.loader._get_registry_creation_time(), |
---|
| 41 | b._get_registry_creation_time()) |
---|
[cc37c818] | 42 | |
---|
[aa749ac] | 43 | class testLoader(unittest.TestCase): |
---|
[d3619421] | 44 | logging.debug("Inside testLoad module") |
---|
[c125e0c] | 45 | |
---|
[cc37c818] | 46 | """ test fitting """ |
---|
| 47 | def setUp(self): |
---|
| 48 | """ |
---|
| 49 | Set up the initial conditions before _each_ test |
---|
| 50 | so that they all start from the same well-defined state. |
---|
| 51 | """ |
---|
| 52 | #Creating a loader |
---|
| 53 | self.L=Loader() |
---|
| 54 | |
---|
[5f0f3d2] | 55 | |
---|
[d22da51] | 56 | def testLoad0(self): |
---|
| 57 | """test reading empty file""" |
---|
[daa56d0] | 58 | self.assertRaises(RuntimeError, self.L.load, 'empty.txt') |
---|
[d22da51] | 59 | |
---|
| 60 | def testLoad1(self): |
---|
| 61 | """test reading 2 columns""" |
---|
| 62 | |
---|
[c125e0c] | 63 | #Testing loading a txt file of 2 columns, the only reader should be read1 |
---|
[16d8e5f] | 64 | output=self.L.load('test_2_columns.txt') |
---|
[c125e0c] | 65 | x=[2.83954,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449,1.42857,1.63265] |
---|
[aa749ac] | 66 | y=[0.6,3.44938, 5.82026,5.27591,5.2781,5.22531,7.47487,7.85852,10.2278] |
---|
| 67 | dx=[] |
---|
| 68 | dy=[] |
---|
[16d8e5f] | 69 | self.assertEqual(len(output.x),len(x)) |
---|
| 70 | self.assertEqual(len(output.y),len(y)) |
---|
| 71 | |
---|
[c125e0c] | 72 | for i in range(len(x)): |
---|
[16d8e5f] | 73 | self.assertEqual(output.x[i],x[i]) |
---|
| 74 | self.assertEqual(output.y[i],y[i]) |
---|
[d22da51] | 75 | |
---|
[c125e0c] | 76 | |
---|
| 77 | def testLoad2(self): |
---|
[d22da51] | 78 | """Testing loading a txt file of 3 columns""" |
---|
[16d8e5f] | 79 | output= self.L.load('test_3_columns.txt') |
---|
[c125e0c] | 80 | x=[0,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449] |
---|
| 81 | y=[2.83954,3.44938,5.82026,5.27591,5.2781,5.22531,7.47487] |
---|
| 82 | dx=[] |
---|
| 83 | dy=[0.6,0.676531,0.753061,0.829592,0.906122,0.982653,1.05918] |
---|
[16d8e5f] | 84 | self.assertEqual(len(output.x),len(x)) |
---|
| 85 | self.assertEqual(len(output.y),len(y)) |
---|
| 86 | self.assertEqual(len(output.dy),len(dy)) |
---|
[c125e0c] | 87 | for i in range(len(x)): |
---|
[16d8e5f] | 88 | self.assertEqual(output.x[i],x[i]) |
---|
| 89 | self.assertEqual(output.y[i],y[i]) |
---|
| 90 | self.assertEqual(output.dy[i],dy[i]) |
---|
[d22da51] | 91 | |
---|
[daa56d0] | 92 | def testLoad2_uppercase(self): |
---|
| 93 | """Testing loading a txt file of 3 columns""" |
---|
| 94 | output= self.L.load('test_3_columns.TXT') |
---|
| 95 | x=[0,0.204082,0.408163,0.612245,0.816327,1.02041,1.22449] |
---|
| 96 | y=[2.83954,3.44938,5.82026,5.27591,5.2781,5.22531,7.47487] |
---|
| 97 | dx=[] |
---|
| 98 | dy=[0.6,0.676531,0.753061,0.829592,0.906122,0.982653,1.05918] |
---|
| 99 | self.assertEqual(len(output.x),len(x)) |
---|
| 100 | self.assertEqual(len(output.y),len(y)) |
---|
| 101 | self.assertEqual(len(output.dy),len(dy)) |
---|
| 102 | for i in range(len(x)): |
---|
| 103 | self.assertEqual(output.x[i],x[i]) |
---|
| 104 | self.assertEqual(output.y[i],y[i]) |
---|
| 105 | self.assertEqual(output.dy[i],dy[i]) |
---|
| 106 | |
---|
[c125e0c] | 107 | |
---|
| 108 | def testload3(self): |
---|
| 109 | """ Testing loading Igor data""" |
---|
| 110 | #tested good file.asc |
---|
[16d8e5f] | 111 | output= self.L.load('MAR07232_rest.ASC') |
---|
| 112 | self.assertEqual(output.xmin,-0.018558945804750416) |
---|
| 113 | self.assertEqual(output.xmax, 0.016234058202440633,) |
---|
| 114 | self.assertEqual(output.ymin,-0.01684257151702391) |
---|
| 115 | self.assertEqual(output.ymax,0.017950440578015116) |
---|
[d22da51] | 116 | |
---|
[c125e0c] | 117 | #tested corrupted file.asc |
---|
[d22da51] | 118 | try:self.L.load('AR07232_rest.ASC') |
---|
| 119 | except ValueError,msg: |
---|
| 120 | #logging.log(10,str(msg)) |
---|
| 121 | logging.error(str(msg)) |
---|
[daa56d0] | 122 | |
---|
| 123 | def testload3_lowercase(self): |
---|
| 124 | """ Testing loading Igor data""" |
---|
| 125 | #tested good file.asc |
---|
| 126 | output= self.L.load('MAR07232_rest.asc') |
---|
| 127 | self.assertEqual(output.xmin,-0.018558945804750416) |
---|
| 128 | self.assertEqual(output.xmax, 0.016234058202440633,) |
---|
| 129 | self.assertEqual(output.ymin,-0.01684257151702391) |
---|
| 130 | self.assertEqual(output.ymax,0.017950440578015116) |
---|
| 131 | |
---|
| 132 | #tested corrupted file.asc |
---|
| 133 | try:self.L.load('AR07232_rest.ASC') |
---|
| 134 | except ValueError,msg: |
---|
| 135 | #logging.log(10,str(msg)) |
---|
| 136 | logging.error(str(msg)) |
---|
[c125e0c] | 137 | def testload4(self): |
---|
| 138 | """ Testing loading danse file""" |
---|
| 139 | #tested good file.sans |
---|
[16d8e5f] | 140 | output=self.L.load('MP_New.sans') |
---|
[aa749ac] | 141 | |
---|
[b99ac227] | 142 | self.assertEqual(output.source.wavelength,7.5) |
---|
[d22da51] | 143 | |
---|
[c125e0c] | 144 | #tested corrupted file.sans |
---|
[d22da51] | 145 | try: self.L.load('P_New.sans') |
---|
| 146 | except ValueError,msg: |
---|
| 147 | #logging.log(40,str(msg)) |
---|
| 148 | logging.error(str(msg)) |
---|
| 149 | #else: raise ValueError,"No error raised for missing extension" |
---|
[8d6440f] | 150 | |
---|
[c125e0c] | 151 | def testload5(self): |
---|
| 152 | """ Testing loading image file""" |
---|
[daa56d0] | 153 | if HAS_IMAGE: |
---|
| 154 | output=self.L.load('angles_flat.png') |
---|
| 155 | self.assertEqual(output.xbins ,200) |
---|
[d22da51] | 156 | |
---|
| 157 | def testload6(self): |
---|
| 158 | """test file with unknown extension""" |
---|
[daa56d0] | 159 | self.assertRaises(ValueError, self.L.load, 'hello.missing') |
---|
| 160 | |
---|
| 161 | # Lookup is not supported as a public method |
---|
| 162 | #self.assertRaises(ValueError, self.L.lookup, 'hello.missing') |
---|
[d22da51] | 163 | |
---|
| 164 | |
---|
| 165 | def testload7(self): |
---|
| 166 | """ test file containing an image but as extension .txt""" |
---|
[daa56d0] | 167 | self.assertRaises(RuntimeError, self.L.load, 'angles_flat.txt') |
---|
[cc37c818] | 168 | |
---|
| 169 | if __name__ == '__main__': |
---|
| 170 | unittest.main() |
---|
[1b0b3ca] | 171 | |
---|