source: sasview/src/sas/qtgui/UnitTesting/PlotterTest.py @ 416fa8f

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

Initial implementation of the mask editor.
Refactored plotter base to QDialog and reimplemented as local widgets.
Fixed some unit tests.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1import sys
2import unittest
3
4from PyQt4 import QtGui
5from PyQt4 import QtCore
6from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
7from mock import MagicMock
8
9####### TEMP
10import path_prepare
11#######
12from sas.sasgui.guiframe.dataFitting import Data1D
13from sas.sasgui.guiframe.dataFitting import Data2D
14from UnitTesting.TestUtils import QtSignalSpy
15
16# Tested module
17import sas.qtgui.Plotter as Plotter
18
19app = QtGui.QApplication(sys.argv)
20
21class PlotterTest(unittest.TestCase):
22    '''Test the Plotter 1D class'''
23    def setUp(self):
24        '''create'''
25        self.plotter = Plotter.Plotter(None, quickplot=True)
26        self.data = Data1D(x=[1.0, 2.0, 3.0],
27                           y=[10.0, 11.0, 12.0],
28                           dx=[0.1, 0.2, 0.3],
29                           dy=[0.1, 0.2, 0.3])
30        self.data.title="Test data"
31        self.data.id = 1
32
33    def tearDown(self):
34        '''destroy'''
35        self.plotter = None
36
37    def testDataProperty(self):
38        """ Adding data """
39        self.plotter.data = self.data
40
41        self.assertEqual(self.plotter.data, self.data)
42        self.assertEqual(self.plotter._title, self.data.title)
43        self.assertEqual(self.plotter.xLabel, "$()$")
44        self.assertEqual(self.plotter.yLabel, "$()$")
45
46    def testPlot(self):
47        """ Look at the plotting """
48        self.plotter.data = self.data
49        self.plotter.show()
50        FigureCanvas.draw = MagicMock()
51
52        self.plotter.plot()
53
54        self.assertTrue(FigureCanvas.draw.called)
55
56    def testContextMenuQuickPlot(self):
57        """ Test the right click menu """
58        actions = self.plotter.contextMenu.actions()
59        self.assertEqual(len(actions), 7)
60
61        # Trigger Save Image and make sure the method is called
62        self.assertEqual(actions[0].text(), "Save Image")
63        self.plotter.toolbar.save_figure = MagicMock()
64        actions[0].trigger()
65        self.assertTrue(self.plotter.toolbar.save_figure.called)
66
67        # Trigger Print Image and make sure the method is called
68        self.assertEqual(actions[1].text(), "Print Image")
69        QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected)
70        actions[1].trigger()
71        self.assertTrue(QtGui.QPrintDialog.exec_.called)
72
73        # Trigger Copy to Clipboard and make sure the method is called
74        self.assertEqual(actions[2].text(), "Copy to Clipboard")
75
76        # Spy on cliboard's dataChanged() signal
77        self.clipboard_called = False
78        def done():
79            self.clipboard_called = True
80        QtCore.QObject.connect(app.clipboard(), QtCore.SIGNAL("dataChanged()"), done)
81        actions[2].trigger()
82        QtGui.qApp.processEvents()
83        # Make sure clipboard got updated.
84        self.assertTrue(self.clipboard_called)
85
86        # Trigger Toggle Grid and make sure the method is called
87        self.assertEqual(actions[4].text(), "Toggle Grid On/Off")
88        self.plotter.ax.grid = MagicMock()
89        actions[4].trigger()
90        self.assertTrue(self.plotter.ax.grid.called)
91
92        # Trigger Change Scale and make sure the method is called
93        self.assertEqual(actions[6].text(), "Change Scale")
94        self.plotter.properties.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected)
95        actions[6].trigger()
96        self.assertTrue(self.plotter.properties.exec_.called)
97
98    def testXYTransform(self):
99        """ Assure the unit/legend transformation is correct"""
100        self.plotter.data = self.data
101
102        self.plotter.xyTransform(xLabel="x", yLabel="y")
103        self.assertEqual(self.plotter.ax.get_xlabel(), "$()$")
104        self.assertEqual(self.plotter.ax.get_ylabel(), "$()$")
105
106        self.plotter.xyTransform(xLabel="x^(2)", yLabel="1/y")
107        self.assertEqual(self.plotter.ax.get_xlabel(), "$^{2}(()^{2})$")
108        self.assertEqual(self.plotter.ax.get_ylabel(), "$1/(()^{-1})$")
109
110        self.plotter.xyTransform(xLabel="x^(4)", yLabel="ln(y)")
111        self.assertEqual(self.plotter.ax.get_xlabel(), "$^{4}(()^{4})$")
112        self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln{()}()$")
113
114        self.plotter.xyTransform(xLabel="ln(x)", yLabel="y^(2)")
115        self.assertEqual(self.plotter.ax.get_xlabel(), "$\\ln{()}()$")
116        self.assertEqual(self.plotter.ax.get_ylabel(), "$^{2}(()^{2})$")
117
118        self.plotter.xyTransform(xLabel="log10(x)", yLabel="y*x^(2)")
119        self.assertEqual(self.plotter.ax.get_xlabel(), "$()$")
120        self.assertEqual(self.plotter.ax.get_ylabel(), "$ \\ \\ ^{2}(()^{2})$")
121
122        self.plotter.xyTransform(xLabel="log10(x^(4))", yLabel="y*x^(4)")
123        self.assertEqual(self.plotter.ax.get_xlabel(), "$^{4}(()^{4})$")
124        self.assertEqual(self.plotter.ax.get_ylabel(), "$ \\ \\ ^{4}(()^{16})$")
125
126        self.plotter.xyTransform(xLabel="x", yLabel="1/sqrt(y)")
127        self.assertEqual(self.plotter.ax.get_ylabel(), "$1/\\sqrt{}(()^{-0.5})$")
128
129        self.plotter.xyTransform(xLabel="x", yLabel="log10(y)")
130        self.assertEqual(self.plotter.ax.get_ylabel(), "$()$")
131
132        self.plotter.xyTransform(xLabel="x", yLabel="ln(y*x)")
133        self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln{( \\ \\ )}()$")
134
135        self.plotter.xyTransform(xLabel="x", yLabel="ln(y*x^(2))")
136        self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln ( \\ \\ ^{2})(()^{2})$")
137
138        self.plotter.xyTransform(xLabel="x", yLabel="ln(y*x^(4))")
139        self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln ( \\ \\ ^{4})(()^{4})$")
140
141        self.plotter.xyTransform(xLabel="x", yLabel="log10(y*x^(4))")
142        self.assertEqual(self.plotter.ax.get_ylabel(), "$ \\ \\ ^{4}(()^{4})$")
143
144if __name__ == "__main__":
145    unittest.main()
Note: See TracBrowser for help on using the repository browser.