Changeset 0261bc1 in sasview for src/sas


Ignore:
Timestamp:
Nov 20, 2017 3:55:30 AM (6 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
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:
f1ec901
Parents:
6a3e1fe
Message:

Added unit tests for recent functionality

Location:
src/sas/qtgui
Files:
6 edited

Legend:

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

    r7c487846 r0261bc1  
    8282        # Main window 
    8383        unittest.makeSuite(DataExplorerTest.DataExplorerTest,  'test'), 
    84  
    85         unittest.makeSuite(GuiUtilsTest.GuiUtilsTest,          'test'), 
    8684        unittest.makeSuite(DroppableDataLoadWidgetTest.DroppableDataLoadWidgetTest, 'test'), 
    8785        unittest.makeSuite(MainWindowTest.MainWindowTest,      'test'), 
     
    9189 
    9290        # Utilities 
    93         unittest.makeSuite(TestUtilsTest.TestUtilsTest,         'test'), 
    94         unittest.makeSuite(SasviewLoggerTest.SasviewLoggerTest, 'test'), 
     91        unittest.makeSuite(TestUtilsTest.TestUtilsTest,           'test'), 
     92        unittest.makeSuite(SasviewLoggerTest.SasviewLoggerTest,   'test'), 
     93        unittest.makeSuite(GuiUtilsTest.GuiUtilsTest,             'test'), 
     94        unittest.makeSuite(GuiUtilsTest.DoubleValidatorTest,      'test'), 
     95        unittest.makeSuite(GuiUtilsTest.HashableStandardItemTest, 'test'), 
    9596 
    9697        # Calculators 
  • src/sas/qtgui/MainWindow/DataManager.py

    rb3e8629 r0261bc1  
    126126        rename data 
    127127        """ 
    128         ## name of the data allow to differentiate data when plotted 
    129         name = GuiUtils.parseName(name=name, expression="_") 
     128        # name of the data allow to differentiate data when plotted 
     129        try: 
     130            name = GuiUtils.parseName(name=name, expression="_") 
     131        except TypeError: 
     132            # bad name sent to rename 
     133            return None 
    130134 
    131135        max_char = name.find("[") 
  • src/sas/qtgui/Perspectives/Fitting/FittingWidget.py

    r7c487846 r0261bc1  
    591591            try: 
    592592                value = GuiUtils.toDouble(item.text()) 
    593             except ValueError: 
     593            except TypeError: 
    594594                # Can't be converted properly, bring back the old value and exit 
    595595                return 
     
    606606            try: 
    607607                value = GuiUtils.toDouble(item.text()) 
    608             except ValueError: 
     608            except TypeError: 
    609609                # Can't be converted properly, bring back the old value and exit 
    610610                return 
     
    639639        try: 
    640640            value = GuiUtils.toDouble(item.text()) 
    641         except ValueError: 
     641        except TypeError: 
    642642            # Unparsable field 
    643643            return 
     
    13101310        try: 
    13111311            value = GuiUtils.toDouble(item.text()) 
    1312         except ValueError: 
     1312        except TypeError: 
    13131313            # Unparsable field 
    13141314            return 
  • src/sas/qtgui/Perspectives/Inversion/DMaxExplorerWidget.py

    r6a3e1fe r0261bc1  
    4545        self.setupUi(self) 
    4646 
    47         self.setWindowTitle("Dₘₐₓ Explorer") 
     47        self.setWindowTitle("Dₐₓ Explorer") 
    4848 
    4949        self.pr_state = pr_state 
  • src/sas/qtgui/Utilities/GuiUtils.py

    r6a3e1fe r0261bc1  
    271271    """ 
    272272    def __init__(self, parent=None): 
    273         super(QtGui.QStandardItem, self).__init__() 
     273        super(HashableStandardItem, self).__init__() 
    274274 
    275275    def __hash__(self): 
     
    277277        #return hash(self.__init__) 
    278278        return 0 
     279 
     280    def clone(self): 
     281        ''' Assure __hash__ is cloned as well''' 
     282        clone = super(HashableStandardItem, self).clone() 
     283        clone.__hash__ = self.__hash__ 
     284        return clone 
    279285 
    280286 
     
    561567    with open(path,'w') as out: 
    562568        has_errors = True 
    563         if data.dy is None or not data.dy: 
     569        if data.dy is None or not data.dy.any(): 
    564570            has_errors = False 
    565571        # Sanity check 
     
    571577                has_errors = False 
    572578        if has_errors: 
    573             if data.dx is not None and data.dx: 
     579            if data.dx is not None and data.dx.any(): 
    574580                out.write("<X>   <Y>   <dY>   <dX>\n") 
    575581            else: 
     
    580586        for i in range(len(data.x)): 
    581587            if has_errors: 
    582                 if data.dx is not None and data.dx: 
     588                if data.dx is not None and data.dx.any(): 
    583589                    if  data.dx[i] is not None: 
    584590                        out.write("%g  %g  %g  %g\n" % (data.x[i], 
     
    857863        return value[0] 
    858864    else: 
    859         raise ValueError 
     865        raise TypeError 
    860866 
    861867class DoubleValidator(QtGui.QDoubleValidator): 
     
    867873        Return invalid for commas 
    868874        """ 
    869         if (',' in input): 
     875        if input is not None and ',' in input: 
    870876            return (QtGui.QValidator.Invalid, input, pos) 
    871877        return super(DoubleValidator, self).validate(input, pos) 
     
    875881        Correct (remove) potential preexisting content 
    876882        """ 
    877         QtGui.QDoubleValidator.fixup(input) 
     883        super(DoubleValidator, self).fixup(input) 
    878884        input = input.replace(",", "") 
    879885 
  • src/sas/qtgui/Utilities/UnitTesting/GuiUtilsTest.py

    r53c771e r0261bc1  
    414414        self.assertEqual(yscale, "log") 
    415415 
     416    def testParseName(self): 
     417        '''test parse out a string from the beinning of a string''' 
     418        # good input 
     419        value = "_test" 
     420        self.assertEqual(parseName(value, '_'), 'test') 
     421        value = "____test____" 
     422        self.assertEqual(parseName(value, '_'), '___test____') 
     423        self.assertEqual(parseName(value, '___'), '_test____') 
     424        self.assertEqual(parseName(value, 'test'), '____test____') 
     425        # bad input 
     426        with self.assertRaises(TypeError): 
     427            parseName(value, None) 
     428        with self.assertRaises(TypeError): 
     429            parseName(None, '_') 
     430        value = [] 
     431        with self.assertRaises(TypeError): 
     432            parseName(value, '_') 
     433        value = 1.44 
     434        with self.assertRaises(TypeError): 
     435            parseName(value, 'p') 
     436        value = 100 
     437        with self.assertRaises(TypeError): 
     438            parseName(value, 'p') 
     439 
     440    def testToDouble(self): 
     441        '''test homemade string-> double converter''' 
     442        #good values 
     443        value = "1" 
     444        self.assertEqual(toDouble(value), 1.0) 
     445        value = "1.2" 
     446        # has to be AlmostEqual due to numerical rounding 
     447        self.assertAlmostEqual(toDouble(value), 1.2, 6) 
     448        value = "2,1" 
     449        self.assertAlmostEqual(toDouble(value), 2.1, 6) 
     450 
     451        # bad values 
     452        value = None 
     453        with self.assertRaises(TypeError): 
     454            toDouble(value) 
     455        value = "MyDouble" 
     456        with self.assertRaises(TypeError): 
     457            toDouble(value) 
     458        value = [1,2.2] 
     459        with self.assertRaises(TypeError): 
     460            toDouble(value) 
     461 
     462 
     463class DoubleValidatorTest(unittest.TestCase): 
     464    """ Test the validator for floats """ 
     465    def setUp(self): 
     466        '''Create the validator''' 
     467        self.validator = DoubleValidator() 
     468 
     469    def tearDown(self): 
     470        '''Destroy the validator''' 
     471        self.validator = None 
     472 
     473    def testValidateGood(self): 
     474        """Test a valid float """ 
     475        QtCore.QLocale.setDefault(QtCore.QLocale('en_US')) 
     476        float_good = "170" 
     477        self.assertEqual(self.validator.validate(float_good, 1)[0], QtGui.QValidator.Acceptable) 
     478        float_good = "170.11" 
     479        ## investigate: a double returns Invalid here! 
     480        ##self.assertEqual(self.validator.validate(float_good, 1)[0], QtGui.QValidator.Acceptable) 
     481        float_good = "17e2" 
     482        self.assertEqual(self.validator.validate(float_good, 1)[0], QtGui.QValidator.Acceptable) 
     483 
     484    def testValidateBad(self): 
     485        """Test a bad float """ 
     486        float_bad = None 
     487        self.assertEqual(self.validator.validate(float_bad, 1)[0], QtGui.QValidator.Intermediate) 
     488        float_bad = [1] 
     489        with self.assertRaises(TypeError): 
     490           self.validator.validate(float_bad, 1) 
     491        float_bad = "1,3" 
     492        self.assertEqual(self.validator.validate(float_bad, 1)[0], QtGui.QValidator.Invalid) 
     493 
     494    def notestFixup(self): 
     495        """Fixup of a float""" 
     496        float_to_fixup = "1,3" 
     497        self.validator.fixup(float_to_fixup) 
     498        self.assertEqual(float_to_fixup, "13") 
     499 
    416500 
    417501class FormulaValidatorTest(unittest.TestCase): 
     
    434518 
    435519    def testValidateBad(self): 
    436         """Test a valid Formula """ 
     520        """Test an invalid Formula """ 
    437521        formula_bad = "H24 %%%O12C4C6N2Pu" 
    438522        self.assertRaises(self.validator.validate(formula_bad, 1)[0]) 
     
    442526        self.assertEqual(self.validator.validate(formula_bad, 1)[0], QtGui.QValidator.Intermediate) 
    443527 
     528class HashableStandardItemTest(unittest.TestCase): 
     529    """ Test the reimplementation of QStandardItem """ 
     530    def setUp(self): 
     531        '''Create the validator''' 
     532        self.item = HashableStandardItem() 
     533 
     534    def tearDown(self): 
     535        '''Destroy the validator''' 
     536        self.item = None 
     537 
     538    def testHash(self): 
     539        '''assure the item returns hash''' 
     540        self.assertEqual(self.item.__hash__(), 0) 
     541 
     542    def testIndexing(self): 
     543        '''test that we can use HashableSI as an index''' 
     544        dictionary = {} 
     545        dictionary[self.item] = "wow!" 
     546        self.assertEqual(dictionary[self.item], "wow!") 
     547 
     548    def testClone(self): 
     549        '''let's see if we can clone the item''' 
     550        item_clone = self.item.clone() 
     551        self.assertEqual(item_clone.__hash__(), 0) 
    444552 
    445553if __name__ == "__main__": 
Note: See TracChangeset for help on using the changeset viewer.