Ignore:
Timestamp:
Jan 13, 2017 2:28:30 AM (7 years ago)
Author:
Piotr Rozyczko <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:
239214f
Parents:
db5cd8d
Message:

Code review issues addressed.
More unit tests for plotting.

File:
1 edited

Legend:

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

    rdb5cd8d r2e3e959  
    1414from sas.sasgui.guiframe.dataFitting import Data2D 
    1515from UnitTesting.TestUtils import WarningTestNotImplemented 
     16from sas.qtgui.LinearFit import LinearFit 
     17from sas.qtgui.PlotProperties import PlotProperties 
    1618 
    1719# Tested module 
     
    224226    def testOnLinearFit(self): 
    225227        """ Checks the response to LinearFit call """ 
    226         WarningTestNotImplemented() 
     228        self.plotter.plot(self.data) 
     229        self.plotter.show() 
     230        QtGui.QDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Accepted) 
     231 
     232        # Just this one plot 
     233        self.assertEqual(len(self.plotter.plot_dict.keys()), 1) 
     234        self.plotter.onLinearFit(1) 
     235 
     236        # Check that exec_ got called 
     237        self.assertTrue(QtGui.QDialog.exec_.called) 
    227238 
    228239    def testOnRemovePlot(self): 
    229240        """ Assure plots get removed when requested """ 
    230         WarningTestNotImplemented() 
     241        # Add two plots 
     242        self.plotter.show() 
     243        self.plotter.plot(self.data) 
     244        data2 = Data1D(x=[1.0, 2.0, 3.0], 
     245                       y=[11.0, 12.0, 13.0], 
     246                       dx=[0.1, 0.2, 0.3], 
     247                       dy=[0.1, 0.2, 0.3]) 
     248        data2.title="Test data 2" 
     249        data2.name="Test name 2" 
     250        data2.id = 2 
     251        self.plotter.plot(data2) 
     252 
     253        # Assure the plotter window is visible 
     254        self.assertTrue(self.plotter.isVisible()) 
     255 
     256        # Assure we have two sets 
     257        self.assertEqual(len(self.plotter.plot_dict.keys()), 2) 
     258 
     259        # Delete one set 
     260        self.plotter.onRemovePlot(2) 
     261        # Assure we have two sets 
     262        self.assertEqual(len(self.plotter.plot_dict.keys()), 1) 
     263 
     264        self.plotter.manager = MagicMock() 
     265 
     266        # Delete the remaining set 
     267        self.plotter.onRemovePlot(1) 
     268        # Assure we have no plots 
     269        self.assertEqual(len(self.plotter.plot_dict.keys()), 0) 
     270        # Assure the plotter window is closed 
     271        self.assertFalse(self.plotter.isVisible()) 
     272 
    231273 
    232274    def testRemovePlot(self): 
    233275        """ Test plot removal """ 
    234         WarningTestNotImplemented() 
     276        # Add two plots 
     277        self.plotter.show() 
     278        self.plotter.plot(self.data) 
     279        data2 = Data1D(x=[1.0, 2.0, 3.0], 
     280                       y=[11.0, 12.0, 13.0], 
     281                       dx=[0.1, 0.2, 0.3], 
     282                       dy=[0.1, 0.2, 0.3]) 
     283        data2.title="Test data 2" 
     284        data2.name="Test name 2" 
     285        data2.id = 2 
     286        data2._xaxis = "XAXIS" 
     287        data2._xunit = "furlong*fortnight^{-1}" 
     288        data2._yaxis = "YAXIS" 
     289        data2._yunit = "cake" 
     290        data2.hide_error = True 
     291        self.plotter.plot(data2) 
     292 
     293        # delete plot 1 
     294        self.plotter.removePlot(1) 
     295 
     296        # See that the labels didn't change 
     297        xl = self.plotter.ax.xaxis.label.get_text() 
     298        yl = self.plotter.ax.yaxis.label.get_text() 
     299        self.assertEqual(xl, "$XAXIS(furlong*fortnight^{-1})$") 
     300        self.assertEqual(yl, "$YAXIS(cake)$") 
     301        # The hide_error flag should also remain 
     302        self.assertTrue(self.plotter.plot_dict[2].hide_error) 
    235303 
    236304    def testOnToggleHideError(self): 
    237305        """ Test the error bar toggle on plots """ 
    238         WarningTestNotImplemented() 
     306        # Add two plots 
     307        self.plotter.show() 
     308        self.plotter.plot(self.data) 
     309        data2 = Data1D(x=[1.0, 2.0, 3.0], 
     310                       y=[11.0, 12.0, 13.0], 
     311                       dx=[0.1, 0.2, 0.3], 
     312                       dy=[0.1, 0.2, 0.3]) 
     313        data2.title="Test data 2" 
     314        data2.name="Test name 2" 
     315        data2.id = 2 
     316        data2._xaxis = "XAXIS" 
     317        data2._xunit = "furlong*fortnight^{-1}" 
     318        data2._yaxis = "YAXIS" 
     319        data2._yunit = "cake" 
     320        error_status = True 
     321        data2.hide_error = error_status 
     322        self.plotter.plot(data2) 
     323 
     324        # Reverse the toggle 
     325        self.plotter.onToggleHideError(2) 
     326        # See that the labels didn't change 
     327        xl = self.plotter.ax.xaxis.label.get_text() 
     328        yl = self.plotter.ax.yaxis.label.get_text() 
     329        self.assertEqual(xl, "$XAXIS(furlong*fortnight^{-1})$") 
     330        self.assertEqual(yl, "$YAXIS(cake)$") 
     331        # The hide_error flag should toggle 
     332        self.assertEqual(self.plotter.plot_dict[2].hide_error, not error_status) 
    239333 
    240334    def testOnFitDisplay(self): 
    241335        """ Test the fit line display on the chart """ 
    242         WarningTestNotImplemented() 
     336        self.assertIsInstance(self.plotter.fit_result, Data1D) 
     337        self.assertEqual(self.plotter.fit_result.symbol, 13) 
     338        self.assertEqual(self.plotter.fit_result.name, "Fit") 
     339 
     340        # fudge some init data 
     341        fit_data = [[0.0,0.0], [5.0,5.0]] 
     342        # Call the method 
     343        self.plotter.plot = MagicMock() 
     344        self.plotter.onFitDisplay(fit_data) 
     345        self.assertTrue(self.plotter.plot.called) 
     346        # Look at arguments passed to .plot() 
     347        self.plotter.plot.assert_called_with(data=self.plotter.fit_result, 
     348                                             hide_error=True, marker='-') 
    243349 
    244350    def testReplacePlot(self): 
    245351        """ Test the plot refresh functionality """ 
    246         WarningTestNotImplemented() 
    247  
    248     def testReplacePlot(self): 
    249         """ Test the plot refresh functionality """ 
    250         WarningTestNotImplemented(sys._getframe().f_code.co_name) 
     352        # Add original data 
     353        self.plotter.show() 
     354        self.plotter.plot(self.data) 
     355        # See the default labels 
     356        xl = self.plotter.ax.xaxis.label.get_text() 
     357        yl = self.plotter.ax.yaxis.label.get_text() 
     358        self.assertEqual(xl, "$()$") 
     359        self.assertEqual(yl, "$()$") 
     360 
     361        # Prepare new data 
     362        data2 = Data1D(x=[1.0, 2.0, 3.0], 
     363                       y=[11.0, 12.0, 13.0], 
     364                       dx=[0.1, 0.2, 0.3], 
     365                       dy=[0.1, 0.2, 0.3]) 
     366        data2.title="Test data 2" 
     367        data2.name="Test name 2" 
     368        data2.id = 2 
     369        data2._xaxis = "XAXIS" 
     370        data2._xunit = "furlong*fortnight^{-1}" 
     371        data2._yaxis = "YAXIS" 
     372        data2._yunit = "cake" 
     373        error_status = True 
     374        data2.hide_error = error_status 
     375 
     376        # Replace data in plot 
     377        self.plotter.replacePlot(1, data2) 
     378 
     379        # See that the labels changed 
     380        xl = self.plotter.ax.xaxis.label.get_text() 
     381        yl = self.plotter.ax.yaxis.label.get_text() 
     382        self.assertEqual(xl, "$XAXIS(furlong*fortnight^{-1})$") 
     383        self.assertEqual(yl, "$YAXIS(cake)$") 
     384        # The hide_error flag should be as set 
     385        self.assertEqual(self.plotter.plot_dict[2].hide_error, error_status) 
    251386 
    252387    def testOnModifyPlot(self): 
    253388        """ Test the functionality for changing plot properties """ 
    254         WarningTestNotImplemented() 
     389        # Prepare new data 
     390        data2 = Data1D(x=[1.0, 2.0, 3.0], 
     391                       y=[11.0, 12.0, 13.0], 
     392                       dx=[0.1, 0.2, 0.3], 
     393                       dy=[0.1, 0.2, 0.3]) 
     394        data2.title="Test data 2" 
     395        data2.name="Test name 2" 
     396        data2.id = 2 
     397        data2.custom_color = None 
     398        data2.symbol = 1 
     399        data2.markersize = 11 
     400 
     401        self.plotter.plot(data2) 
     402 
     403        with patch('PlotProperties.PlotProperties') as mock: 
     404            instance = mock.return_value 
     405            QtGui.QDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Accepted) 
     406            instance.symbol.return_value = 7 
     407 
     408            self.plotter.onModifyPlot(2) 
     409 
     410            #print self.plotter.plot_dict[2].symbol 
     411 
    255412 
    256413if __name__ == "__main__": 
Note: See TracChangeset for help on using the changeset viewer.