Ignore:
Timestamp:
Mar 17, 2017 4:22:23 AM (8 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:
4d457df
Parents:
cd31251
Message:

Started with unit tests for fitting widget SASVIEW-499

Location:
src/sas/qtgui/Perspectives/Fitting/UnitTesting
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Perspectives/Fitting/UnitTesting/FittingPerspectiveTest.py

    • Property mode changed from 100644 to 100755
    r6ee82dd r811bec1  
    11import sys 
    22import unittest 
     3import webbrowser 
    34 
    45from PyQt4 import QtGui 
     
    78from mock import MagicMock 
    89 
     10# set up import paths 
     11import sas.qtgui.path_prepare 
     12 
    913# Local 
    10 from FittingPerspective import FittingWindow 
     14import sas.qtgui.GuiUtils as GuiUtils 
     15from sas.sasgui.guiframe.dataFitting import Data1D 
     16 
     17from sas.qtgui.Perspectives.Fitting.FittingPerspective import FittingWindow 
    1118 
    1219app = QtGui.QApplication(sys.argv) 
    1320 
    1421class FittingPerspectiveTest(unittest.TestCase): 
    15     """Test the Main Window GUI""" 
     22    '''Test the Fitting Perspective''' 
    1623    def setUp(self): 
    17         """Create the GUI""" 
     24        class dummy_manager(object): 
     25            def communicator(self): 
     26                return GuiUtils.Communicate() 
     27            def communicate(self): 
     28                return GuiUtils.Communicate() 
    1829 
    19         self.widget = FittingWindow(None) 
     30        '''Create the perspective''' 
     31        self.widget = FittingWindow(dummy_manager()) 
    2032 
    2133    def tearDown(self): 
    22         """Destroy the GUI""" 
     34        '''Destroy the perspective''' 
    2335        self.widget.close() 
    2436        self.widget = None 
    2537 
    2638    def testDefaults(self): 
    27         """Test the GUI in its default state""" 
     39        '''Test the GUI in its default state''' 
    2840        self.assertIsInstance(self.widget, QtGui.QWidget) 
    29         self.assertEqual(self.widget.windowTitle(), "Fitting") 
    30         self.assertEqual(self.widget.sizePolicy().Policy(), QtGui.QSizePolicy.Fixed) 
     41        self.assertIn("Fit panel", self.widget.windowTitle()) 
     42        self.assertEqual(self.widget.optimizer, "DREAM") 
     43        self.assertEqual(len(self.widget.tabs), 1) 
     44        self.assertEqual(self.widget.maxIndex, 1) 
     45        self.assertEqual(self.widget.tabName(), "FitPage1") 
    3146 
    32     def testSelectCategory(self): 
    33         """ 
    34         Test if categories have been load properly 
    35         :return: 
    36         """ 
    37         fittingWindow =  FittingWindow(None) 
     47    def testAddTab(self): 
     48        '''Add a tab and test it''' 
    3849 
    39         #Test loading from json categories 
    40         category_list = fittingWindow.master_category_dict.keys() 
    41         self.assertTrue("Cylinder" in category_list) 
    42         self.assertTrue("Ellipsoid" in category_list) 
    43         self.assertTrue("Lamellae" in category_list) 
    44         self.assertTrue("Paracrystal" in category_list) 
    45         self.assertTrue("Parallelepiped" in category_list) 
    46         self.assertTrue("Shape Independent" in category_list) 
    47         self.assertTrue("Sphere" in category_list) 
     50        # Add an empty tab 
     51        self.widget.addFit(None) 
     52        self.assertEqual(len(self.widget.tabs), 2) 
     53        self.assertEqual(self.widget.tabName(), "FitPage2") 
     54        self.assertEqual(self.widget.maxIndex, 2) 
    4855 
    49         #Test for existence in combobox 
    50         self.assertNotEqual(fittingWindow.cbCategory.findText("Cylinder"),-1) 
    51         self.assertNotEqual(fittingWindow.cbCategory.findText("Ellipsoid"),-1) 
    52         self.assertNotEqual(fittingWindow.cbCategory.findText("Lamellae"),-1) 
    53         self.assertNotEqual(fittingWindow.cbCategory.findText("Paracrystal"),-1) 
    54         self.assertNotEqual(fittingWindow.cbCategory.findText("Parallelepiped"),-1) 
    55         self.assertNotEqual(fittingWindow.cbCategory.findText("Shape Independent"),-1) 
    56         self.assertNotEqual(fittingWindow.cbCategory.findText("Sphere"),-1) 
     56    def testCloseTab(self): 
     57        '''Delete a tab and test''' 
     58        # Add an empty tab 
     59        self.widget.addFit(None) 
    5760 
    58         #Test what is current text in the combobox 
    59         self.assertTrue(fittingWindow.cbCategory.currentText(), "Cylinder") 
     61        # Remove the original tab 
     62        self.widget.tabCloses(1) 
     63        self.assertEqual(len(self.widget.tabs), 1) 
     64        self.assertEqual(self.widget.maxIndex, 2) 
     65        self.assertEqual(self.widget.tabName(), "FitPage2") 
    6066 
    61     def testSelectModel(self): 
    62         """ 
    63         Test if models have been loaded properly 
    64         :return: 
    65         """ 
    66         fittingWindow =  FittingWindow(None) 
     67        # Attemtp to remove the last tab 
     68        self.widget.tabCloses(1) 
     69        # The tab should still be there 
     70        self.assertEqual(len(self.widget.tabs), 1) 
     71        self.assertEqual(self.widget.maxIndex, 2) 
     72        self.assertEqual(self.widget.tabName(), "FitPage2") 
    6773 
    68         #Test loading from json categories 
    69         model_list = fittingWindow.master_category_dict["Cylinder"] 
    70         self.assertTrue(['cylinder', True] in model_list) 
    71         self.assertTrue(['core_shell_cylinder', True] in model_list) 
    72         self.assertTrue(['barbell', True] in model_list) 
    73         self.assertTrue(['core_shell_bicelle', True] in model_list) 
    74         self.assertTrue(['flexible_cylinder', True] in model_list) 
    75         self.assertTrue(['flexible_cylinder_elliptical', True] in model_list) 
    76         self.assertTrue(['pearl_necklace', True] in model_list) 
    77         self.assertTrue(['capped_cylinder', True] in model_list) 
    78         self.assertTrue(['elliptical_cylinder', True] in model_list) 
    79         self.assertTrue(['pringle', True] in model_list) 
    80         self.assertTrue(['hollow_cylinder', True] in model_list) 
    81         self.assertTrue(['core_shell_bicelle_elliptical', True] in model_list) 
    82         self.assertTrue(['stacked_disks', True] in model_list) 
     74    def testAllowBatch(self): 
     75        '''Assure the perspective allows multiple datasets''' 
     76        self.assertTrue(self.widget.allowBatch()) 
    8377 
    84         #Test for existence in combobox 
    85         self.assertNotEqual(fittingWindow.cbModel.findText("cylinder"),-1) 
    86         self.assertNotEqual(fittingWindow.cbModel.findText("core_shell_cylinder"),-1) 
    87         self.assertNotEqual(fittingWindow.cbModel.findText("barbell"),-1) 
    88         self.assertNotEqual(fittingWindow.cbModel.findText("core_shell_bicelle"),-1) 
    89         self.assertNotEqual(fittingWindow.cbModel.findText("flexible_cylinder"),-1) 
    90         self.assertNotEqual(fittingWindow.cbModel.findText("flexible_cylinder_elliptical"),-1) 
    91         self.assertNotEqual(fittingWindow.cbModel.findText("pearl_necklace"),-1) 
    92         self.assertNotEqual(fittingWindow.cbModel.findText("capped_cylinder"),-1) 
    93         self.assertNotEqual(fittingWindow.cbModel.findText("elliptical_cylinder"),-1) 
    94         self.assertNotEqual(fittingWindow.cbModel.findText("pringle"),-1) 
    95         self.assertNotEqual(fittingWindow.cbModel.findText("hollow_cylinder"),-1) 
    96         self.assertNotEqual(fittingWindow.cbModel.findText("core_shell_bicelle_elliptical"),-1) 
    97         self.assertNotEqual(fittingWindow.cbModel.findText("stacked_disks"),-1) 
     78    def testSetData(self): 
     79        ''' Assure that setting data is correct''' 
     80        with self.assertRaises(AssertionError): 
     81            self.widget.setData(None) 
    9882 
     83        with self.assertRaises(AttributeError): 
     84            self.widget.setData("BOOP") 
    9985 
    100     def testSelectPolydispersity(self): 
    101         """ 
    102         Test if models have been loaded properly 
    103         :return: 
    104         """ 
    105         fittingWindow =  FittingWindow(None) 
     86        # Mock the datafromitem() call from FittingWidget 
     87        data = Data1D(x=[1,2], y=[1,2]) 
     88        GuiUtils.dataFromItem = MagicMock(return_value=data) 
    10689 
    107         #Test loading from json categories 
    108         fittingWindow.setModelModel("cylinder") 
    109         pd_index = fittingWindow.tableView_2.model().index(0,0) 
    110         self.assertEqual(str(pd_index.data().toString()), "Distribution of radius") 
    111         pd_index = fittingWindow.tableView_2.model().index(1,0) 
    112         self.assertEqual(str(pd_index.data().toString()), "Distribution of length") 
     90        item = QtGui.QStandardItem("test") 
     91        self.widget.setData([item]) 
    11392 
    114     def testSelectStructureFactor(self): 
    115         """ 
    116         Test if structure factors have been loaded properly 
    117         :return: 
    118         """ 
    119         fittingWindow =  FittingWindow(None) 
     93        # Look at the data in tab 
     94        self.assertEqual(self.widget._model_item, item) 
    12095 
    121         #Test for existence in combobox 
    122         self.assertNotEqual(fittingWindow.cbStructureFactor.findText("stickyhardsphere"),-1) 
    123         self.assertNotEqual(fittingWindow.cbStructureFactor.findText("hayter_msa"),-1) 
    124         self.assertNotEqual(fittingWindow.cbStructureFactor.findText("squarewell"),-1) 
    125         self.assertNotEqual(fittingWindow.cbStructureFactor.findText("hardsphere"),-1) 
     96        # First tab should accept data 
     97        self.assertEqual(len(self.widget.tabs), 1) 
    12698 
    127         #Test what is current text in the combobox 
    128         self.assertTrue(fittingWindow.cbCategory.currentText(), "None") 
     99        # Add another set of data 
     100        self.widget.setData([item]) 
     101 
     102        # Now we should have two tabs 
     103        self.assertEqual(len(self.widget.tabs), 2) 
     104 
    129105 
    130106if __name__ == "__main__": 
Note: See TracChangeset for help on using the changeset viewer.