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