Ignore:
Timestamp:
Jun 14, 2016 4:51:18 AM (8 years ago)
Author:
Piotr Rozyczko <piotr.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:
a281ab8
Parents:
488c49d
Message:

threaded file load, data object related fixes, more unit tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/UnitTesting/DataExplorerTest.py

    r488c49d r5032ea68  
    11import sys 
    22import unittest 
     3#from twisted.trial import unittest 
     4#from twisted.internet import reactor, defer, interfaces, threads, protocol, error 
    35 
    46from PyQt4.QtGui import * 
     
    2022    def setUp(self): 
    2123        '''Create the GUI''' 
     24        class MyPerspective(object): 
     25            def communicator(self): 
     26                return Communicate() 
     27            def allowBatch(self): 
     28                return False 
     29            def setData(self, data_list=None): 
     30                return None 
     31            def title(self): 
     32                return "Dummy Perspective" 
     33 
    2234        class dummy_manager(object): 
    2335            def communicator(self): 
    2436                return Communicate() 
     37            def perspective(self): 
     38                return MyPerspective() 
    2539 
    2640        self.form = DataExplorerWindow(None, dummy_manager()) 
     
    6781 
    6882    def testDeleteButton(self): 
     83        """ 
     84        Functionality of the delete button 
     85        """ 
    6986 
    7087        deleteButton = self.form.cmdDelete 
    7188 
    7289        # Mock the confirmation dialog with return=Yes 
     90        QtGui.QMessageBox.question = MagicMock(return_value=QtGui.QMessageBox.No) 
    7391 
    7492        # Populate the model 
    75  
    76         # Assure the checkbox is on 
     93        filename = ["cyl_400_20.txt", "Dec07031.ASC", "cyl_400_20.txt"] 
     94        self.form.readData(filename) 
     95 
     96        # Assure the model contains three items 
     97        self.assertEqual(self.form.model.rowCount(), 3) 
     98 
     99        # Assure the checkboxes are on 
     100        item1 = self.form.model.item(0) 
     101        item2 = self.form.model.item(1) 
     102        item3 = self.form.model.item(2) 
     103        self.assertTrue(item1.checkState() == QtCore.Qt.Checked) 
     104        self.assertTrue(item2.checkState() == QtCore.Qt.Checked) 
     105        self.assertTrue(item3.checkState() == QtCore.Qt.Checked) 
    77106 
    78107        # Click on the delete  button 
     
    80109 
    81110        # Test the warning dialog called once 
    82         # self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) 
     111        self.assertTrue(QtGui.QMessageBox.question.called) 
     112 
     113        # Assure the model still contains the items 
     114        self.assertEqual(self.form.model.rowCount(), 3) 
     115 
     116        # Now, mock the confirmation dialog with return=Yes 
     117        QtGui.QMessageBox.question = MagicMock(return_value=QtGui.QMessageBox.Yes) 
     118 
     119        # Click on the delete  button 
     120        QTest.mouseClick(deleteButton, Qt.LeftButton) 
     121 
     122        # Test the warning dialog called once 
     123        self.assertTrue(QtGui.QMessageBox.question.called) 
    83124 
    84125        # Assure the model contains no items 
    85  
    86     def testSendToButton(self):         
    87         sendToButton = self.form.cmdSendTo 
    88  
    89         # Mock the current perspective set_data method 
    90  
     126        self.assertEqual(self.form.model.rowCount(), 0) 
     127 
     128        # Click delete once again to assure no nasty behaviour on empty model 
     129        QTest.mouseClick(deleteButton, Qt.LeftButton) 
     130 
     131 
     132    def testSendToButton(self): 
     133        """ 
     134        Test that clicking the Send To button sends checked data to a perspective 
     135        """ 
     136         
    91137        # Populate the model 
     138        filename = ["cyl_400_20.txt"] 
     139        self.form.readData(filename) 
     140 
     141        # setData is the method we want to see called 
     142        mocked = self.form.parent.perspective().setData 
     143        mocked = MagicMock(filename) 
    92144 
    93145        # Assure the checkbox is on 
     146        self.form.cbSelect.setCurrentIndex(0) 
    94147 
    95148        # Click on the Send To  button 
    96         QTest.mouseClick(sendToButton, Qt.LeftButton) 
     149        QTest.mouseClick(self.form.cmdSendTo, Qt.LeftButton) 
    97150 
    98151        # Test the set_data method called once 
    99         # self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) 
    100  
    101         # Assure the model contains no items 
     152        # self.assertTrue(mocked.called) 
     153 
     154        # open another file 
     155        filename = ["cyl_400_20.txt"] 
     156        self.form.readData(filename) 
     157 
     158        # Mock the warning message 
     159        QtGui.QMessageBox = MagicMock() 
     160 
     161        # Click on the button 
     162        QTest.mouseClick(self.form.cmdSendTo, Qt.LeftButton) 
     163 
     164        # Assure the message box popped up 
     165        QtGui.QMessageBox.assert_called_once() 
     166 
    102167 
    103168    def testDataSelection(self): 
     
    159224    def testReadData(self): 
    160225        """ 
    161         Test the readData() method 
     226        Test the low level readData() method 
    162227        """ 
    163228        filename = ["cyl_400_20.txt"] 
     
    172237 
    173238        # Expected two status bar updates 
    174         self.assertEqual(spy_status_update.count(), 2) 
     239        self.assertEqual(spy_status_update.count(), 1) 
    175240        self.assertIn(filename[0], str(spy_status_update.called()[0]['args'][0])) 
    176         self.assertIn("Loading Data Complete", str(spy_status_update.called()[1]['args'][0])) 
    177  
    178         # Expect one Data Received signal 
    179         self.assertEqual(spy_data_received.count(), 1) 
    180  
    181         # Assure returned dictionary has correct data 
    182         # We don't know the data ID, so need to iterate over dict 
    183         data_dict = spy_data_received.called()[0]['args'][0] 
    184         for data_key, data_value in data_dict.iteritems(): 
    185             self.assertIsInstance(data_value, Data1D) 
     241 
    186242 
    187243        # Check that the model contains the item 
     
    194250        self.assertEqual(model_name, filename[0]) 
    195251 
    196         # Assure add_data on data_manager was called (last call) 
    197         self.assertTrue(self.form.manager.add_data.called) 
     252    def testLoadFile(self): 
     253        """ 
     254        Test the threaded call to readData() 
     255        """ 
     256        pass 
    198257 
    199258    def testGetWList(self): 
     
    206265            'HFIR 1D files (*.d1d);;DANSE files (*.sans);;NXS files (*.nxs)' 
    207266        self.assertEqual(defaults, list) 
    208  
     267        
    209268    def testLoadComplete(self): 
    210269        """ 
    211         """ 
    212         # Initialize signal spy instances         
     270        Test the callback method updating the data object 
     271        """ 
     272 
     273        data_dict = {"a1":Data1D()} 
     274 
     275        self.form.manager.add_data = MagicMock() 
     276 
     277        # Initialize signal spy instances 
    213278        spy_status_update = QtSignalSpy(self.form, self.form.communicate.statusBarUpdateSignal) 
    214279        spy_data_received = QtSignalSpy(self.form, self.form.communicate.fileDataReceivedSignal) 
    215280 
    216         # Need an empty Data1D object 
    217         mockData = Data1D() 
    218  
    219         # Call the tested method 
    220         self.form.loadComplete({"a":mockData}, message="test message") 
    221  
    222         # test the signals  
    223         self.assertEqual(spy_status_update.count(), 1) 
    224         self.assertIn("message", str(spy_status_update.called()[0]['args'][0])) 
     281        # Read in the file 
     282        self.form.loadComplete(data_dict, message="Loading Data Complete") 
     283 
     284        # "Loading data complete" no longer sent in LoadFile but in callback 
     285        self.assertIn("Loading Data Complete", str(spy_status_update.called()[0]['args'][0])) 
     286 
     287        # Expect one Data Received signal 
    225288        self.assertEqual(spy_data_received.count(), 1) 
    226289 
    227         # The data_manager update is going away, so don't bother testing 
    228         
     290        # Assure returned dictionary has correct data 
     291        # We don't know the data ID, so need to iterate over dict 
     292        data_dict = spy_data_received.called()[0]['args'][0] 
     293        for data_key, data_value in data_dict.iteritems(): 
     294            self.assertIsInstance(data_value, Data1D) 
     295 
     296        # Assure add_data on data_manager was called (last call) 
     297        self.assertTrue(self.form.manager.add_data.called) 
     298 
     299 
    229300if __name__ == "__main__": 
    230301    unittest.main() 
Note: See TracChangeset for help on using the changeset viewer.