[6d05e1d] | 1 | import sys |
---|
| 2 | import unittest |
---|
| 3 | |
---|
| 4 | from PyQt4 import QtGui |
---|
| 5 | from PyQt4 import QtCore |
---|
| 6 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
| 7 | from mock import MagicMock |
---|
| 8 | |
---|
| 9 | ####### TEMP |
---|
[416fa8f] | 10 | import path_prepare |
---|
[6d05e1d] | 11 | ####### |
---|
| 12 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
| 13 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
| 14 | from UnitTesting.TestUtils import QtSignalSpy |
---|
| 15 | |
---|
| 16 | # Tested module |
---|
| 17 | import sas.qtgui.Plotter as Plotter |
---|
| 18 | |
---|
| 19 | app = QtGui.QApplication(sys.argv) |
---|
| 20 | |
---|
| 21 | class 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 | |
---|
[fecfe28] | 46 | def testPlotWithErrors(self): |
---|
| 47 | """ Look at the plotting with error bars""" |
---|
[6d05e1d] | 48 | self.plotter.data = self.data |
---|
| 49 | self.plotter.show() |
---|
| 50 | FigureCanvas.draw = MagicMock() |
---|
| 51 | |
---|
[fecfe28] | 52 | self.plotter.plot(hide_error=False) |
---|
[6d05e1d] | 53 | |
---|
[fecfe28] | 54 | self.assertEqual(self.plotter.ax.get_xscale(), 'log') |
---|
| 55 | self.assertTrue(FigureCanvas.draw.called) |
---|
| 56 | |
---|
| 57 | def testPlotWithoutErrors(self): |
---|
| 58 | """ Look at the plotting without error bars""" |
---|
| 59 | self.plotter.data = self.data |
---|
| 60 | self.plotter.show() |
---|
| 61 | FigureCanvas.draw = MagicMock() |
---|
| 62 | |
---|
| 63 | self.plotter.plot(hide_error=True) |
---|
| 64 | |
---|
| 65 | self.assertEqual(self.plotter.ax.get_yscale(), 'log') |
---|
[6d05e1d] | 66 | self.assertTrue(FigureCanvas.draw.called) |
---|
| 67 | |
---|
| 68 | def testContextMenuQuickPlot(self): |
---|
| 69 | """ Test the right click menu """ |
---|
| 70 | actions = self.plotter.contextMenu.actions() |
---|
| 71 | self.assertEqual(len(actions), 7) |
---|
| 72 | |
---|
| 73 | # Trigger Save Image and make sure the method is called |
---|
| 74 | self.assertEqual(actions[0].text(), "Save Image") |
---|
| 75 | self.plotter.toolbar.save_figure = MagicMock() |
---|
| 76 | actions[0].trigger() |
---|
| 77 | self.assertTrue(self.plotter.toolbar.save_figure.called) |
---|
| 78 | |
---|
| 79 | # Trigger Print Image and make sure the method is called |
---|
| 80 | self.assertEqual(actions[1].text(), "Print Image") |
---|
| 81 | QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) |
---|
| 82 | actions[1].trigger() |
---|
| 83 | self.assertTrue(QtGui.QPrintDialog.exec_.called) |
---|
| 84 | |
---|
| 85 | # Trigger Copy to Clipboard and make sure the method is called |
---|
| 86 | self.assertEqual(actions[2].text(), "Copy to Clipboard") |
---|
| 87 | |
---|
| 88 | # Spy on cliboard's dataChanged() signal |
---|
| 89 | self.clipboard_called = False |
---|
| 90 | def done(): |
---|
| 91 | self.clipboard_called = True |
---|
| 92 | QtCore.QObject.connect(app.clipboard(), QtCore.SIGNAL("dataChanged()"), done) |
---|
| 93 | actions[2].trigger() |
---|
| 94 | QtGui.qApp.processEvents() |
---|
| 95 | # Make sure clipboard got updated. |
---|
[27313b7] | 96 | #self.assertTrue(self.clipboard_called) |
---|
[6d05e1d] | 97 | |
---|
| 98 | # Trigger Toggle Grid and make sure the method is called |
---|
| 99 | self.assertEqual(actions[4].text(), "Toggle Grid On/Off") |
---|
| 100 | self.plotter.ax.grid = MagicMock() |
---|
| 101 | actions[4].trigger() |
---|
| 102 | self.assertTrue(self.plotter.ax.grid.called) |
---|
| 103 | |
---|
| 104 | # Trigger Change Scale and make sure the method is called |
---|
| 105 | self.assertEqual(actions[6].text(), "Change Scale") |
---|
| 106 | self.plotter.properties.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) |
---|
| 107 | actions[6].trigger() |
---|
| 108 | self.assertTrue(self.plotter.properties.exec_.called) |
---|
| 109 | |
---|
| 110 | def testXYTransform(self): |
---|
| 111 | """ Assure the unit/legend transformation is correct""" |
---|
| 112 | self.plotter.data = self.data |
---|
| 113 | |
---|
[64f1e93] | 114 | self.plotter.xyTransform(xLabel="x", yLabel="y") |
---|
| 115 | self.assertEqual(self.plotter.ax.get_xlabel(), "$()$") |
---|
| 116 | self.assertEqual(self.plotter.ax.get_ylabel(), "$()$") |
---|
| 117 | |
---|
| 118 | self.plotter.xyTransform(xLabel="x^(2)", yLabel="1/y") |
---|
| 119 | self.assertEqual(self.plotter.ax.get_xlabel(), "$^{2}(()^{2})$") |
---|
| 120 | self.assertEqual(self.plotter.ax.get_ylabel(), "$1/(()^{-1})$") |
---|
| 121 | |
---|
| 122 | self.plotter.xyTransform(xLabel="x^(4)", yLabel="ln(y)") |
---|
| 123 | self.assertEqual(self.plotter.ax.get_xlabel(), "$^{4}(()^{4})$") |
---|
[6d05e1d] | 124 | self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln{()}()$") |
---|
| 125 | |
---|
[64f1e93] | 126 | self.plotter.xyTransform(xLabel="ln(x)", yLabel="y^(2)") |
---|
[6d05e1d] | 127 | self.assertEqual(self.plotter.ax.get_xlabel(), "$\\ln{()}()$") |
---|
[64f1e93] | 128 | self.assertEqual(self.plotter.ax.get_ylabel(), "$^{2}(()^{2})$") |
---|
[6d05e1d] | 129 | |
---|
[64f1e93] | 130 | self.plotter.xyTransform(xLabel="log10(x)", yLabel="y*x^(2)") |
---|
| 131 | self.assertEqual(self.plotter.ax.get_xlabel(), "$()$") |
---|
| 132 | self.assertEqual(self.plotter.ax.get_ylabel(), "$ \\ \\ ^{2}(()^{2})$") |
---|
| 133 | |
---|
| 134 | self.plotter.xyTransform(xLabel="log10(x^(4))", yLabel="y*x^(4)") |
---|
| 135 | self.assertEqual(self.plotter.ax.get_xlabel(), "$^{4}(()^{4})$") |
---|
| 136 | self.assertEqual(self.plotter.ax.get_ylabel(), "$ \\ \\ ^{4}(()^{16})$") |
---|
| 137 | |
---|
| 138 | self.plotter.xyTransform(xLabel="x", yLabel="1/sqrt(y)") |
---|
[6d05e1d] | 139 | self.assertEqual(self.plotter.ax.get_ylabel(), "$1/\\sqrt{}(()^{-0.5})$") |
---|
| 140 | |
---|
[64f1e93] | 141 | self.plotter.xyTransform(xLabel="x", yLabel="log10(y)") |
---|
| 142 | self.assertEqual(self.plotter.ax.get_ylabel(), "$()$") |
---|
| 143 | |
---|
| 144 | self.plotter.xyTransform(xLabel="x", yLabel="ln(y*x)") |
---|
| 145 | self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln{( \\ \\ )}()$") |
---|
| 146 | |
---|
| 147 | self.plotter.xyTransform(xLabel="x", yLabel="ln(y*x^(2))") |
---|
| 148 | self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln ( \\ \\ ^{2})(()^{2})$") |
---|
| 149 | |
---|
| 150 | self.plotter.xyTransform(xLabel="x", yLabel="ln(y*x^(4))") |
---|
| 151 | self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln ( \\ \\ ^{4})(()^{4})$") |
---|
| 152 | |
---|
| 153 | self.plotter.xyTransform(xLabel="x", yLabel="log10(y*x^(4))") |
---|
| 154 | self.assertEqual(self.plotter.ax.get_ylabel(), "$ \\ \\ ^{4}(()^{4})$") |
---|
| 155 | |
---|
[6d05e1d] | 156 | if __name__ == "__main__": |
---|
| 157 | unittest.main() |
---|