Changeset debf5c3 in sasview


Ignore:
Timestamp:
Dec 14, 2016 2:19:57 AM (8 years ago)
Author:
wojciech
Branches:
ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
ab9984e
Parents:
3a0ce4f
Message:

Addded tests for raised exceptions

Location:
src/sas/qtgui
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/SlitSizeCalculator.py

    r3a0ce4f rdebf5c3  
    33from UI.SlitSizeCalculator import Ui_SlitSizeCalculator 
    44from sas.sascalc.dataloader.loader import Loader 
    5 from sas.sasgui.guiframe.dataFitting import Data1D 
    6 from sas.sasgui.guiframe.dataFitting import Data2D 
    75from sas.sascalc.calculator.slit_length_calculator import SlitlengthCalculator 
    86 
     
    119 
    1210class SlitSizeCalculator(QtGui.QDialog, Ui_SlitSizeCalculator): 
     11    """ 
     12    Provides the slit length calculator GUI. 
     13    """ 
    1314    def __init__(self, parent=None): 
    1415        super(SlitSizeCalculator, self).__init__() 
     
    3132    def onHelp(self): 
    3233        """ 
    33         Bring up the Kiessig fringe calculator Documentation whenever 
     34        Bring up the Slit Size Calculator calculator Documentation whenever 
    3435        the HELP button is clicked. 
    3536        Calls DocumentationWindow with the path of the location within the 
     
    4849    def onBrowse(self): 
    4950        """ 
    50         Execute the computation of thickness 
     51        Browse the file and calculate slit lenght upon loading 
    5152        """ 
    5253        path_str = self.chooseFile() 
     
    5758 
    5859        self.data_file.setText(os.path.basename(path_str)) 
    59         #We are loading data for one model only therefor index 0 
    6060        self.calculateSlitSize(data) 
    6161 
     
    7070                "SAXSess 1D data (*.txt *.TXT *.dat *.DAT)", None, 
    7171                QtGui.QFileDialog.DontUseNativeDialog) 
     72 
    7273        if path is None: 
    7374            return 
     
    8687    def calculateSlitSize(self, data=None): 
    8788        """ 
    88             Complete the loading and compute the slit size 
     89        Computes slit lenght from given 1D data 
    8990        """ 
    9091 
     
    9394            raise RuntimeError, msg 
    9495 
    95         if isinstance(data, Data2D) or data.__class__.__name__ == 'Data2D': 
     96        if data.__class__.__name__ == 'Data2D': 
    9697            msg = "Slit Length cannot be computed for 2D Data" 
    9798            raise Exception, msg 
     
    101102             x = data.x 
    102103             y = data.y 
    103              if x == [] or  x is None or y == [] or y is None: 
     104             if x == [] or x is None or y == [] or y is None: 
    104105                 msg = "The current data is empty please check x and y" 
    105106                 raise ValueError, msg 
     
    113114        slit_length_str = "{:.5f}".format(slit_length) 
    114115        self.slit_length_out.setText(slit_length_str) 
    115         #Display unit 
     116 
     117        #Display unit, which most likely needs to be 1/Ang but needs to be confirmed 
    116118        self.unit_out.setText("[Unknown]") 
    117119 
  • src/sas/qtgui/UnitTesting/SlitSizeCalculatorTest.py

    r3a0ce4f rdebf5c3  
    3838        self.widget.onHelp() 
    3939 
    40     def testCalculateSlitSize(self): 
    41         """ User entered compound calculations and subsequent reset""" 
    42  
    43         filename = "beam_profile.DAT" 
    44         loader = Loader() 
    45         data = loader.load(filename) 
    46  
    47         self.widget.calculateSlitSize(data) 
    48         # The value "5.5858" was obtained by manual calculation. 
    49         # It turns out our slit length is FWHM/2 
    50         self.assertAlmostEqual(float(self.widget.slit_length_out.text()), 5.5858/2, 3) 
    51  
    52     def testCalculateSlitSize2D(self): 
    53         """ User entered compound calculations and subsequent reset""" 
    54  
    55         #TODO: Test for 2D and check if exception is being raised 
    56         filename = "beam_profile.DAT" 
    57         loader = Loader() 
    58         data = loader.load(filename) 
    59  
    60         self.widget.calculateSlitSize(data) 
    61         # The value "5.5858" was obtained by manual calculation. 
    62         # It turns out our slit length is FWHM/2 
    63         self.assertAlmostEqual(float(self.widget.slit_length_out.text()), 5.5858/2, 3) 
    64  
    65     # def testDataEntryNumbers(self): 
    66     #      """ User entered compound calculations and subsequent reset""" 
    67     # 
    68     #      self.widget.data_file.clear() 
    69     #      self.widget.data_file.insert("beam profile.DAT") 
    70     #      # 
    71     #      # Push Compute with the left mouse button 
    72     #      computeButton = self.widget.computeButton 
    73     #      QTest.mouseClick(computeButton, Qt.LeftButton) 
    74     #      self.assertEqual(self.widget.lengthscale_out.text(), '6.283') 
    75  
    7640    def testBrowseButton(self): 
    7741        browseButton = self.widget.browseButton 
     
    10064        QtGui.QFileDialog.getOpenFileName.assert_called_once() 
    10165 
    102     # def testDataEntryNumbers(self): 
    103     #     """ User entered compound calculations and subsequent reset""" 
    104     # 
    105     #     self.widget.data_file.clear() 
    106     #     self.widget.data_file.insert("beam profile.DAT") 
    107     #     # 
    108     #     # Push Compute with the left mouse button 
    109     #     computeButton = self.widget.computeButton 
    110     #     QTest.mouseClick(computeButton, Qt.LeftButton) 
    111     #     self.assertEqual(self.widget.lengthscale_out.text(), '6.283') 
    112     # 
    113     # 
    114     # def testComplexEntryLetters(self): 
    115     #     """ User entered compound calculations and subsequent reset""" 
    116     # 
    117     #     self.widget.deltaq_in.insert("xyz") 
    118     # 
    119     #     # Push Compute with the left mouse button 
    120     #     computeButton = self.widget.computeButton 
    121     #     QTest.mouseClick(computeButton, Qt.LeftButton) 
    122     #     self.assertEqual(self.widget.lengthscale_out.text(), '') 
     66 
     67    def testCalculateSlitSize(self): 
     68        """ Test slit size calculated value """ 
     69 
     70        filename = "beam_profile.DAT" 
     71        loader = Loader() 
     72        data = loader.load(filename) 
     73 
     74        self.widget.calculateSlitSize(data) 
     75        # The value "5.5858" was obtained by manual calculation. 
     76        # It turns out our slit length is FWHM/2 
     77        self.assertAlmostEqual(float(self.widget.slit_length_out.text()), 5.5858/2, 3) 
     78 
     79    def testWrongInput(self): 
     80        """ Test on wrong input data """ 
     81 
     82        filename = "Dec07031.ASC" 
     83        loader = Loader() 
     84        data = loader.load(filename) 
     85        self.assertRaisesRegexp(Exception, 
     86                                "Slit Length cannot be computed for 2D Data", 
     87                                self.widget.calculateSlitSize, data) 
     88 
     89        filename = "empty_file.txt" 
     90        loader = Loader() 
     91        data = loader.load(filename) 
     92        self.assertRaisesRegexp(RuntimeError, 
     93                                "ERROR: Data hasn't been loaded correctly", 
     94                                self.widget.calculateSlitSize, data) 
     95 
     96        data = None 
     97        self.assertRaisesRegexp(RuntimeError, 
     98                                "ERROR: Data hasn't been loaded correctly", 
     99                                self.widget.calculateSlitSize, data) 
     100 
    123101 
    124102if __name__ == "__main__": 
Note: See TracChangeset for help on using the changeset viewer.