[2162fa0] | 1 | import sys |
---|
| 2 | import unittest |
---|
[811bec1] | 3 | import webbrowser |
---|
[2162fa0] | 4 | |
---|
[53c771e] | 5 | from PyQt5 import QtGui |
---|
| 6 | from PyQt5 import QtWidgets |
---|
| 7 | from PyQt5 import QtTest |
---|
| 8 | from PyQt5 import QtCore |
---|
[7fb471d] | 9 | from unittest.mock import MagicMock |
---|
[2162fa0] | 10 | |
---|
[811bec1] | 11 | # set up import paths |
---|
| 12 | import sas.qtgui.path_prepare |
---|
| 13 | |
---|
[2162fa0] | 14 | # Local |
---|
[83eb5208] | 15 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
[dc5ef15] | 16 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
[811bec1] | 17 | from sas.qtgui.Perspectives.Fitting.FittingPerspective import FittingWindow |
---|
[2162fa0] | 18 | |
---|
[53c771e] | 19 | if not QtWidgets.QApplication.instance(): |
---|
| 20 | app = QtWidgets.QApplication(sys.argv) |
---|
[2162fa0] | 21 | |
---|
[63319b0] | 22 | |
---|
[2162fa0] | 23 | class FittingPerspectiveTest(unittest.TestCase): |
---|
[811bec1] | 24 | '''Test the Fitting Perspective''' |
---|
[2162fa0] | 25 | def setUp(self): |
---|
[811bec1] | 26 | class dummy_manager(object): |
---|
| 27 | def communicator(self): |
---|
| 28 | return GuiUtils.Communicate() |
---|
[80468f6] | 29 | communicate = GuiUtils.Communicate() |
---|
[2162fa0] | 30 | |
---|
[811bec1] | 31 | '''Create the perspective''' |
---|
| 32 | self.widget = FittingWindow(dummy_manager()) |
---|
[2162fa0] | 33 | |
---|
| 34 | def tearDown(self): |
---|
[811bec1] | 35 | '''Destroy the perspective''' |
---|
[b9c4c1e1] | 36 | self.widget.close() |
---|
[2162fa0] | 37 | self.widget = None |
---|
| 38 | |
---|
| 39 | def testDefaults(self): |
---|
[811bec1] | 40 | '''Test the GUI in its default state''' |
---|
[53c771e] | 41 | self.assertIsInstance(self.widget, QtWidgets.QWidget) |
---|
[811bec1] | 42 | self.assertIn("Fit panel", self.widget.windowTitle()) |
---|
[1bc27f1] | 43 | self.assertEqual(self.widget.optimizer, "Levenberg-Marquardt") |
---|
[811bec1] | 44 | self.assertEqual(len(self.widget.tabs), 1) |
---|
| 45 | self.assertEqual(self.widget.maxIndex, 1) |
---|
[676f137] | 46 | self.assertEqual(self.widget.getTabName(), "FitPage1") |
---|
[811bec1] | 47 | |
---|
| 48 | def testAddTab(self): |
---|
| 49 | '''Add a tab and test it''' |
---|
| 50 | |
---|
| 51 | # Add an empty tab |
---|
| 52 | self.widget.addFit(None) |
---|
| 53 | self.assertEqual(len(self.widget.tabs), 2) |
---|
[676f137] | 54 | self.assertEqual(self.widget.getTabName(), "FitPage2") |
---|
[811bec1] | 55 | self.assertEqual(self.widget.maxIndex, 2) |
---|
[0e4d87c] | 56 | # Add an empty batch tab |
---|
| 57 | self.widget.addFit(None, is_batch=True) |
---|
| 58 | self.assertEqual(len(self.widget.tabs), 3) |
---|
[676f137] | 59 | self.assertEqual(self.widget.getTabName(2), "BatchPage3") |
---|
[0e4d87c] | 60 | self.assertEqual(self.widget.maxIndex, 3) |
---|
| 61 | |
---|
[676f137] | 62 | def testAddCSTab(self): |
---|
| 63 | ''' Add a constraint/simult tab''' |
---|
| 64 | self.widget.addConstraintTab() |
---|
| 65 | self.assertEqual(len(self.widget.tabs), 2) |
---|
[80468f6] | 66 | self.assertEqual(self.widget.getCSTabName(), "Const. & Simul. Fit") |
---|
[676f137] | 67 | |
---|
[0e4d87c] | 68 | def testResetTab(self): |
---|
| 69 | ''' Remove data from last tab''' |
---|
| 70 | self.assertEqual(len(self.widget.tabs), 1) |
---|
[676f137] | 71 | self.assertEqual(self.widget.getTabName(), "FitPage1") |
---|
[0e4d87c] | 72 | self.assertEqual(self.widget.maxIndex, 1) |
---|
| 73 | |
---|
| 74 | # Attempt to remove the last tab |
---|
| 75 | self.widget.resetTab(0) |
---|
| 76 | |
---|
| 77 | # see that the tab didn't disappear, just changed the name/id |
---|
| 78 | self.assertEqual(len(self.widget.tabs), 1) |
---|
[676f137] | 79 | self.assertEqual(self.widget.getTabName(), "FitPage2") |
---|
[0e4d87c] | 80 | self.assertEqual(self.widget.maxIndex, 2) |
---|
| 81 | |
---|
| 82 | # Now, add data |
---|
| 83 | data = Data1D(x=[1,2], y=[1,2]) |
---|
| 84 | GuiUtils.dataFromItem = MagicMock(return_value=data) |
---|
| 85 | item = QtGui.QStandardItem("test") |
---|
| 86 | self.widget.setData([item]) |
---|
| 87 | # Assert data is on widget |
---|
| 88 | self.assertEqual(len(self.widget.tabs[0].all_data), 1) |
---|
| 89 | # Reset the tab |
---|
| 90 | self.widget.resetTab(0) |
---|
| 91 | # See that the tab contains data no more |
---|
| 92 | self.assertEqual(len(self.widget.tabs[0].all_data), 0) |
---|
[811bec1] | 93 | |
---|
| 94 | def testCloseTab(self): |
---|
| 95 | '''Delete a tab and test''' |
---|
| 96 | # Add an empty tab |
---|
| 97 | self.widget.addFit(None) |
---|
| 98 | |
---|
| 99 | # Remove the original tab |
---|
| 100 | self.widget.tabCloses(1) |
---|
| 101 | self.assertEqual(len(self.widget.tabs), 1) |
---|
| 102 | self.assertEqual(self.widget.maxIndex, 2) |
---|
[676f137] | 103 | self.assertEqual(self.widget.getTabName(), "FitPage2") |
---|
[811bec1] | 104 | |
---|
| 105 | # Attemtp to remove the last tab |
---|
| 106 | self.widget.tabCloses(1) |
---|
| 107 | # The tab should still be there |
---|
| 108 | self.assertEqual(len(self.widget.tabs), 1) |
---|
[377ade1] | 109 | self.assertEqual(self.widget.maxIndex, 3) |
---|
[676f137] | 110 | self.assertEqual(self.widget.getTabName(), "FitPage3") |
---|
[811bec1] | 111 | |
---|
| 112 | def testAllowBatch(self): |
---|
| 113 | '''Assure the perspective allows multiple datasets''' |
---|
| 114 | self.assertTrue(self.widget.allowBatch()) |
---|
| 115 | |
---|
| 116 | def testSetData(self): |
---|
| 117 | ''' Assure that setting data is correct''' |
---|
| 118 | with self.assertRaises(AssertionError): |
---|
| 119 | self.widget.setData(None) |
---|
| 120 | |
---|
| 121 | with self.assertRaises(AttributeError): |
---|
| 122 | self.widget.setData("BOOP") |
---|
| 123 | |
---|
| 124 | # Mock the datafromitem() call from FittingWidget |
---|
| 125 | data = Data1D(x=[1,2], y=[1,2]) |
---|
| 126 | GuiUtils.dataFromItem = MagicMock(return_value=data) |
---|
| 127 | |
---|
| 128 | item = QtGui.QStandardItem("test") |
---|
| 129 | self.widget.setData([item]) |
---|
| 130 | |
---|
| 131 | # First tab should accept data |
---|
| 132 | self.assertEqual(len(self.widget.tabs), 1) |
---|
| 133 | |
---|
| 134 | # Add another set of data |
---|
| 135 | self.widget.setData([item]) |
---|
| 136 | |
---|
| 137 | # Now we should have two tabs |
---|
| 138 | self.assertEqual(len(self.widget.tabs), 2) |
---|
| 139 | |
---|
[2155824] | 140 | # Add two more items in a list |
---|
| 141 | self.widget.setData([item, item]) |
---|
| 142 | |
---|
| 143 | # Check for 4 tabs |
---|
| 144 | self.assertEqual(len(self.widget.tabs), 4) |
---|
| 145 | |
---|
[63319b0] | 146 | def testSetBatchData(self): |
---|
| 147 | ''' Assure that setting batch data is correct''' |
---|
| 148 | |
---|
| 149 | # Mock the datafromitem() call from FittingWidget |
---|
| 150 | data1 = Data1D(x=[1,2], y=[1,2]) |
---|
| 151 | data2 = Data1D(x=[1,2], y=[1,2]) |
---|
| 152 | data_batch = [data1, data2] |
---|
| 153 | GuiUtils.dataFromItem = MagicMock(return_value=data1) |
---|
| 154 | |
---|
| 155 | item = QtGui.QStandardItem("test") |
---|
| 156 | self.widget.setData([item, item], is_batch=True) |
---|
| 157 | |
---|
| 158 | # First tab should not accept data |
---|
| 159 | self.assertEqual(len(self.widget.tabs), 2) |
---|
| 160 | |
---|
| 161 | # Add another set of data |
---|
| 162 | self.widget.setData([item, item], is_batch=True) |
---|
| 163 | |
---|
| 164 | # Now we should have two batch tabs |
---|
| 165 | self.assertEqual(len(self.widget.tabs), 3) |
---|
| 166 | |
---|
| 167 | # Check the names of the new tabs |
---|
| 168 | self.assertEqual(self.widget.tabText(1), "BatchPage1") |
---|
| 169 | self.assertEqual(self.widget.tabText(2), "BatchPage2") |
---|
[2162fa0] | 170 | |
---|
| 171 | if __name__ == "__main__": |
---|
| 172 | unittest.main() |
---|