source: sasview/src/sas/qtgui/Plotting/UnitTesting/Plotter2DTest.py @ 53c771e

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 53c771e was 53c771e, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

Converted unit tests

  • Property mode set to 100644
File size: 8.9 KB
RevLine 
[55d89f8]1import sys
2import unittest
[3bdbfcc]3import numpy
[b2a5042]4import platform
[55d89f8]5
[53c771e]6from PyQt5 import QtGui, QtWidgets, QtPrintSupport
7from PyQt5 import QtCore
8from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
[7fb471d]9from unittest.mock import MagicMock
[64f1e93]10from mpl_toolkits.mplot3d import Axes3D
[55d89f8]11
12####### TEMP
[416fa8f]13import path_prepare
[55d89f8]14#######
[dc5ef15]15from sas.qtgui.Plotting.PlotterData import Data1D
16from sas.qtgui.Plotting.PlotterData import Data2D
[092a3d9]17from UnitTesting.TestUtils import WarningTestNotImplemented
[55d89f8]18
19# Tested module
[83eb5208]20import sas.qtgui.Plotting.Plotter2D as Plotter2D
[55d89f8]21
[53c771e]22if not QtWidgets.QApplication.instance():
23    app = QtWidgets.QApplication(sys.argv)
[55d89f8]24
25class Plotter2DTest(unittest.TestCase):
26    '''Test the Plotter 2D class'''
27    def setUp(self):
28        '''create'''
[3bdbfcc]29        class dummy_manager(object):
30            def communicator(self):
31                return Communicate()
32            def perspective(self):
33                return MyPerspective()
34            def workspace(self):
35                return None
36
37        self.plotter = Plotter2D.Plotter2D(parent=dummy_manager(), quickplot=True)
[55d89f8]38
39        self.data = Data2D(image=[0.1]*4,
40                           qx_data=[1.0, 2.0, 3.0, 4.0],
41                           qy_data=[10.0, 11.0, 12.0, 13.0],
42                           dqx_data=[0.1, 0.2, 0.3, 0.4],
43                           dqy_data=[0.1, 0.2, 0.3, 0.4],
44                           q_data=[1,2,3,4],
45                           xmin=-1.0, xmax=5.0,
46                           ymin=-1.0, ymax=15.0,
47                           zmin=-1.0, zmax=20.0)
48
49        self.data.title="Test data"
50        self.data.id = 1
[b2a5042]51        self.isWindows = platform.system=="Windows"
[55d89f8]52
53    def tearDown(self):
54        '''destroy'''
[53c771e]55        self.plotter.figure.clf()
[55d89f8]56        self.plotter = None
57
58    def testDataProperty(self):
59        """ Adding data """
60        self.plotter.data = self.data
61
62        self.assertEqual(self.plotter.data, self.data)
63        self.assertEqual(self.plotter._title, self.data.title)
64        self.assertEqual(self.plotter.xLabel, "$\\rm{Q_{x}}(A^{-1})$")
65        self.assertEqual(self.plotter.yLabel, "$\\rm{Q_{y}}(A^{-1})$")
66
67    def testPlot(self):
68        """ Look at the plotting """
69        self.plotter.data = self.data
70        self.plotter.show()
[64f1e93]71        FigureCanvas.draw_idle = MagicMock()
[55d89f8]72
73        self.plotter.plot()
74
[64f1e93]75        self.assertTrue(FigureCanvas.draw_idle.called)
[53c771e]76        self.plotter.figure.clf()
[64f1e93]77
[092a3d9]78    def testCalculateDepth(self):
79        ''' Test the depth calculator '''
[03c372d]80        self.plotter.data = self.data
81
82        # Default, log scale
83        depth = self.plotter.calculateDepth()
84        self.assertEqual(depth, (0.1, 1.e20))
85
86        # Change the scale to linear
87        self.plotter.scale = 'linear'
88        depth = self.plotter.calculateDepth()
89        self.assertEqual(depth[0], -32.)
90        self.assertAlmostEqual(depth[1], 1.30103, 5)
[092a3d9]91
92    def testOnColorMap(self):
93        ''' Respond to the color map event '''
[03c372d]94        self.plotter.data = self.data
95        self.plotter.plot()
96        self.plotter.show()
97
[53c771e]98        QtWidgets.QDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Accepted)
[03c372d]99
100        # Just this one plot
101        self.plotter.onColorMap()
102
103        # Check that exec_ got called
[53c771e]104        self.assertTrue(QtWidgets.QDialog.exec_.called)
[03c372d]105
106        self.assertEqual(self.plotter.cmap, "jet")
[965fbd8]107        self.assertAlmostEqual(self.plotter.vmin, 0.1, 6)
108        self.assertAlmostEqual(self.plotter.vmax, 1e+20, 6)
[53c771e]109        self.plotter.figure.clf()
[092a3d9]110
[64f1e93]111    def testOnToggleScale(self):
112        """ Respond to the event by replotting """
113        self.plotter.data = self.data
114        self.plotter.show()
115        FigureCanvas.draw_idle = MagicMock()
116
117        self.plotter.onToggleScale(None)
118
119        self.assertTrue(FigureCanvas.draw_idle.called)
[53c771e]120        self.plotter.figure.clf()
[55d89f8]121
[3bdbfcc]122    def testOnBoxSum(self):
123        """ Test the box sum display and functionality """
124
125        # hacky way to make things work in manipulations._sum
126        self.data.detector = [1]
127        self.data.err_data = numpy.array([0.0, 0.0, 0.1, 0.0])
128        self.plotter.data = self.data
129        self.plotter.show()
130
131        # Mock the main window
132        self.plotter.manager.parent = MagicMock()
133
134        # Call the main tested method
135        self.plotter.onBoxSum()
136
137        # Test various properties
138        self.assertIsInstance(self.plotter.slicer.model(), QtGui.QStandardItemModel)
139        self.assertTrue(self.plotter.boxwidget.isVisible())
140        self.assertIsInstance(self.plotter.boxwidget.model, QtGui.QStandardItemModel)
[53c771e]141        self.plotter.figure.clf()
[3bdbfcc]142
[55d89f8]143    def testContextMenuQuickPlot(self):
144        """ Test the right click menu """
145        self.plotter.data = self.data
[b46f285]146        self.plotter.createContextMenuQuick()
[55d89f8]147        actions = self.plotter.contextMenu.actions()
148        self.assertEqual(len(actions), 7)
149
150        # Trigger Save Image and make sure the method is called
151        self.assertEqual(actions[0].text(), "Save Image")
152        self.plotter.toolbar.save_figure = MagicMock()
153        actions[0].trigger()
154        self.assertTrue(self.plotter.toolbar.save_figure.called)
155
156        # Trigger Print Image and make sure the method is called
157        self.assertEqual(actions[1].text(), "Print Image")
[53c771e]158        QtPrintSupport.QPrintDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Rejected)
[55d89f8]159        actions[1].trigger()
[53c771e]160        self.assertTrue(QtPrintSupport.QPrintDialog.exec_.called)
[55d89f8]161
162        # Trigger Copy to Clipboard and make sure the method is called
163        self.assertEqual(actions[2].text(), "Copy to Clipboard")
164
165        # Trigger Toggle Grid and make sure the method is called
166        self.assertEqual(actions[4].text(), "Toggle Grid On/Off")
167        self.plotter.ax.grid = MagicMock()
168        actions[4].trigger()
169        self.assertTrue(self.plotter.ax.grid.called)
170
171        # Trigger Change Scale and make sure the method is called
172        self.assertEqual(actions[6].text(), "Toggle Linear/Log Scale")
[64f1e93]173        FigureCanvas.draw_idle = MagicMock()
[55d89f8]174        actions[6].trigger()
[64f1e93]175        self.assertTrue(FigureCanvas.draw_idle.called)
176
[b2a5042]177        # Spy on cliboard's dataChanged() signal
178        if not self.isWindows:
179            return
180        self.clipboard_called = False
181        def done():
182            self.clipboard_called = True
[53c771e]183        QtCore.QObject.connect(QtWidgets.qApp.clipboard(), QtCore.SIGNAL("dataChanged()"), done)
[b2a5042]184        actions[2].trigger()
[53c771e]185        QtWidgets.qApp.processEvents()
[b2a5042]186        # Make sure clipboard got updated.
187        self.assertTrue(self.clipboard_called)
[53c771e]188        self.plotter.figure.clf()
[b2a5042]189
[64f1e93]190    def testShowNoPlot(self):
191        """ Test the plot rendering and generation """
192
193        FigureCanvas.draw_idle = MagicMock()
194        FigureCanvas.draw = MagicMock()
195
196        # Test early return on no data
197        self.plotter.showPlot(data=None,
198                              qx_data=self.data.qx_data,
199                              qy_data=self.data.qy_data,
200                              xmin=self.data.xmin,
201                              xmax=self.data.xmax,
202                              ymin=self.data.ymin, ymax=self.data.ymax,
203                              cmap=None, zmin=None,
204                              zmax=None)
205
206        self.assertFalse(FigureCanvas.draw_idle.called)
207        self.assertFalse(FigureCanvas.draw.called)
[53c771e]208        self.plotter.figure.clf()
[64f1e93]209
210    def testShow3DPlot(self):
211        """ Test the 3Dplot rendering and generation """
212        # Test 3D printout
213        FigureCanvas.draw = MagicMock()
214        Axes3D.plot_surface = MagicMock()
215        self.plotter.figure.colorbar = MagicMock()
216
217        self.plotter.dimension = 3
218        self.plotter.data = self.data
219        self.plotter.showPlot(data=self.plotter.data.data,
220                              qx_data=self.data.qx_data,
221                              qy_data=self.data.qy_data,
222                              xmin=self.data.xmin,
223                              xmax=self.data.xmax,
224                              ymin=self.data.ymin, ymax=self.data.ymax,
225                              cmap=None, zmin=None,
226                              zmax=None)
227        self.assertTrue(Axes3D.plot_surface.called)
228        self.assertTrue(FigureCanvas.draw.called)
[53c771e]229        self.plotter.figure.clf()
[64f1e93]230
231    def testShow2DPlot(self):
232        """ Test the 2Dplot rendering and generation """
233        # Test 2D printout
234        FigureCanvas.draw_idle = MagicMock()
235        self.plotter.figure.colorbar = MagicMock()
236
237        self.plotter.dimension = 2
238        self.plotter.data = self.data
239        self.plotter.showPlot(data=self.plotter.data.data,
240                              qx_data=self.data.qx_data,
241                              qy_data=self.data.qy_data,
242                              xmin=self.data.xmin,
243                              xmax=self.data.xmax,
244                              ymin=self.data.ymin, ymax=self.data.ymax,
245                              cmap=None, zmin=None,
246                              zmax=None)
247        self.assertTrue(FigureCanvas.draw_idle.called)
[53c771e]248        self.plotter.figure.clf()
[55d89f8]249
250
251if __name__ == "__main__":
252    unittest.main()
Note: See TracBrowser for help on using the repository browser.