source: sasview/src/sas/qtgui/UnitTesting/DataExplorerTest.py @ 488c49d

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 488c49d was 488c49d, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 8 years ago

Hold data objects in model. Added more Data Explorer functionality. Added unit tests.

  • Property mode set to 100755
File size: 8.0 KB
Line 
1import sys
2import unittest
3
4from PyQt4.QtGui import *
5from PyQt4.QtTest import QTest
6from PyQt4.QtCore import *
7from mock import MagicMock
8
9# Local
10from sas.sasgui.guiframe.dataFitting import Data1D
11from DataExplorer import DataExplorerWindow
12from GuiManager import GuiManager
13from GuiUtils import *
14from UnitTesting.TestUtils import QtSignalSpy
15
16app = QApplication(sys.argv)
17
18class DataExplorerTest(unittest.TestCase):
19    '''Test the Data Explorer GUI'''
20    def setUp(self):
21        '''Create the GUI'''
22        class dummy_manager(object):
23            def communicator(self):
24                return Communicate()
25
26        self.form = DataExplorerWindow(None, dummy_manager())
27
28    def tearDown(self):
29        '''Destroy the GUI'''
30        self.form.close()
31        self.form = None
32
33    def testDefaults(self):
34        '''Test the GUI in its default state'''
35        self.assertIsInstance(self.form, QTabWidget)
36        self.assertIsInstance(self.form.treeView, QTreeView)
37        self.assertIsInstance(self.form.listView, QListView)
38        self.assertEqual(self.form.count(), 2)
39
40        self.assertEqual(self.form.cmdLoad.text(), "Load")
41        self.assertEqual(self.form.cmdDelete.text(), "Delete")
42        self.assertEqual(self.form.cmdSendTo.text(), "Send to")
43        self.assertEqual(self.form.chkBatch.text(), "Batch mode")
44        self.assertFalse(self.form.chkBatch.isChecked())
45
46        self.assertEqual(self.form.cbSelect.count(), 6)
47        self.assertEqual(self.form.cbSelect.currentIndex(), 0)
48
49        # Class is in the default state even without pressing OK
50        self.assertEqual(self.form.treeView.model().rowCount(), 0)
51        self.assertEqual(self.form.treeView.model().columnCount(), 0)
52        self.assertEqual(self.form.model.rowCount(), 0)
53        self.assertEqual(self.form.model.columnCount(), 0)
54       
55    def testLoadButton(self):
56        loadButton = self.form.cmdLoad
57
58        # Mock the system file open method
59        QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=None)
60
61        # Click on the Load button
62        QTest.mouseClick(loadButton, Qt.LeftButton)
63
64        # Test the getOpenFileName() dialog called once
65        self.assertTrue(QtGui.QFileDialog.getOpenFileName.called)
66        QtGui.QFileDialog.getOpenFileName.assert_called_once()
67
68    def testDeleteButton(self):
69
70        deleteButton = self.form.cmdDelete
71
72        # Mock the confirmation dialog with return=Yes
73
74        # Populate the model
75
76        # Assure the checkbox is on
77
78        # Click on the delete  button
79        QTest.mouseClick(deleteButton, Qt.LeftButton)
80
81        # Test the warning dialog called once
82        # self.assertTrue(QtGui.QFileDialog.getOpenFileName.called)
83
84        # 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
91        # Populate the model
92
93        # Assure the checkbox is on
94
95        # Click on the Send To  button
96        QTest.mouseClick(sendToButton, Qt.LeftButton)
97
98        # Test the set_data method called once
99        # self.assertTrue(QtGui.QFileDialog.getOpenFileName.called)
100
101        # Assure the model contains no items
102
103    def testDataSelection(self):
104        """
105        Tests the functionality of the Selection Option combobox
106        """
107        # Populate the model with 1d and 2d data
108        filename = ["cyl_400_20.txt", "Dec07031.ASC"]
109        self.form.readData(filename)
110
111        # Unselect all data
112        self.form.cbSelect.setCurrentIndex(1)
113
114        # Test the current selection
115        item1D = self.form.model.item(0)
116        item2D = self.form.model.item(1)
117        self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked)
118        self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked)       
119
120        # Select all data
121        self.form.cbSelect.setCurrentIndex(0)
122
123        # Test the current selection
124        self.assertTrue(item1D.checkState() == QtCore.Qt.Checked)
125        self.assertTrue(item2D.checkState() == QtCore.Qt.Checked)       
126
127        # select 1d data
128        self.form.cbSelect.setCurrentIndex(2)
129
130        # Test the current selection
131        self.assertTrue(item1D.checkState() == QtCore.Qt.Checked)
132        self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked)       
133
134        # unselect 1d data
135        self.form.cbSelect.setCurrentIndex(3)
136
137        # Test the current selection
138        self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked)
139        self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked)       
140
141        # select 2d data
142        self.form.cbSelect.setCurrentIndex(4)
143
144        # Test the current selection
145        self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked)
146        self.assertTrue(item2D.checkState() == QtCore.Qt.Checked)       
147
148        # unselect 2d data
149        self.form.cbSelect.setCurrentIndex(5)
150
151        # Test the current selection
152        self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked)
153        self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked)       
154
155        # choose impossible index and assure the code raises
156        #with self.assertRaises(Exception):
157        #    self.form.cbSelect.setCurrentIndex(6)
158
159    def testReadData(self):
160        """
161        Test the readData() method
162        """
163        filename = ["cyl_400_20.txt"]
164        self.form.manager.add_data = MagicMock()
165
166        # Initialize signal spy instances
167        spy_status_update = QtSignalSpy(self.form, self.form.communicate.statusBarUpdateSignal)
168        spy_data_received = QtSignalSpy(self.form, self.form.communicate.fileDataReceivedSignal)
169
170        # Read in the file
171        self.form.readData(filename)
172
173        # Expected two status bar updates
174        self.assertEqual(spy_status_update.count(), 2)
175        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)
186
187        # Check that the model contains the item
188        self.assertEqual(self.form.model.rowCount(), 1)
189        self.assertEqual(self.form.model.columnCount(), 1)
190
191        # The 0th item header should be the name of the file
192        model_item = self.form.model.index(0,0)
193        model_name = str(self.form.model.data(model_item).toString())
194        self.assertEqual(model_name, filename[0])
195
196        # Assure add_data on data_manager was called (last call)
197        self.assertTrue(self.form.manager.add_data.called)
198
199    def testGetWList(self):
200        """
201        """
202        list = self.form.getWlist()
203        defaults = 'All (*.*);;canSAS files (*.xml);;SESANS files' +\
204            ' (*.ses);;ASCII files (*.txt);;IGOR 2D files (*.asc);;' +\
205            'IGOR/DAT 2D Q_map files (*.dat);;IGOR 1D files (*.abs);;'+\
206            'HFIR 1D files (*.d1d);;DANSE files (*.sans);;NXS files (*.nxs)'
207        self.assertEqual(defaults, list)
208
209    def testLoadComplete(self):
210        """
211        """
212        # Initialize signal spy instances       
213        spy_status_update = QtSignalSpy(self.form, self.form.communicate.statusBarUpdateSignal)
214        spy_data_received = QtSignalSpy(self.form, self.form.communicate.fileDataReceivedSignal)
215
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]))
225        self.assertEqual(spy_data_received.count(), 1)
226
227        # The data_manager update is going away, so don't bother testing
228       
229if __name__ == "__main__":
230    unittest.main()
Note: See TracBrowser for help on using the repository browser.