source: sasview/DataLoader/test/testLoad.py @ 442f42f

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 442f42f was b99ac227, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Updates and tests for readers

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[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]6import logging
7logging.basicConfig(level=logging.DEBUG,
8                    format='%(asctime)s %(levelname)s %(message)s',
9                    filename='test_log.txt',
10                    filemode='w')
11
12
[aa749ac]13
14import unittest
15import math
16import DataLoader
[1b0b3ca]17from DataLoader.loader import  Loader
[16d8e5f]18 
[96510c8]19import os.path
[cc37c818]20
21class 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]40class 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
140if __name__ == '__main__':
141    unittest.main()
[1b0b3ca]142   
Note: See TracBrowser for help on using the repository browser.