1 | import sys |
---|
2 | import unittest |
---|
3 | import webbrowser |
---|
4 | |
---|
5 | from PyQt5 import QtGui |
---|
6 | from PyQt5 import QtWidgets |
---|
7 | from PyQt5 import QtTest |
---|
8 | from PyQt5 import QtCore |
---|
9 | from unittest.mock import MagicMock |
---|
10 | |
---|
11 | # set up import paths |
---|
12 | import sas.qtgui.path_prepare |
---|
13 | |
---|
14 | # Local |
---|
15 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
16 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
17 | from sas.qtgui.Perspectives.Fitting.FittingPerspective import FittingWindow |
---|
18 | |
---|
19 | if not QtWidgets.QApplication.instance(): |
---|
20 | app = QtWidgets.QApplication(sys.argv) |
---|
21 | |
---|
22 | class FittingPerspectiveTest(unittest.TestCase): |
---|
23 | '''Test the Fitting Perspective''' |
---|
24 | def setUp(self): |
---|
25 | class dummy_manager(object): |
---|
26 | def communicator(self): |
---|
27 | return GuiUtils.Communicate() |
---|
28 | def communicate(self): |
---|
29 | return GuiUtils.Communicate() |
---|
30 | |
---|
31 | '''Create the perspective''' |
---|
32 | self.widget = FittingWindow(dummy_manager()) |
---|
33 | |
---|
34 | def tearDown(self): |
---|
35 | '''Destroy the perspective''' |
---|
36 | self.widget.close() |
---|
37 | self.widget = None |
---|
38 | |
---|
39 | def testDefaults(self): |
---|
40 | '''Test the GUI in its default state''' |
---|
41 | self.assertIsInstance(self.widget, QtWidgets.QWidget) |
---|
42 | self.assertIn("Fit panel", self.widget.windowTitle()) |
---|
43 | self.assertEqual(self.widget.optimizer, "Levenberg-Marquardt") |
---|
44 | self.assertEqual(len(self.widget.tabs), 1) |
---|
45 | self.assertEqual(self.widget.maxIndex, 1) |
---|
46 | self.assertEqual(self.widget.tabName(), "FitPage1") |
---|
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) |
---|
54 | self.assertEqual(self.widget.tabName(), "FitPage2") |
---|
55 | self.assertEqual(self.widget.maxIndex, 2) |
---|
56 | # Add an empty batch tab |
---|
57 | self.widget.addFit(None, is_batch=True) |
---|
58 | self.assertEqual(len(self.widget.tabs), 3) |
---|
59 | self.assertEqual(self.widget.tabName(2), "BatchPage3") |
---|
60 | self.assertEqual(self.widget.maxIndex, 3) |
---|
61 | |
---|
62 | def testResetTab(self): |
---|
63 | ''' Remove data from last tab''' |
---|
64 | self.assertEqual(len(self.widget.tabs), 1) |
---|
65 | self.assertEqual(self.widget.tabName(), "FitPage1") |
---|
66 | self.assertEqual(self.widget.maxIndex, 1) |
---|
67 | |
---|
68 | # Attempt to remove the last tab |
---|
69 | self.widget.resetTab(0) |
---|
70 | |
---|
71 | # see that the tab didn't disappear, just changed the name/id |
---|
72 | self.assertEqual(len(self.widget.tabs), 1) |
---|
73 | self.assertEqual(self.widget.tabName(), "FitPage2") |
---|
74 | self.assertEqual(self.widget.maxIndex, 2) |
---|
75 | |
---|
76 | # Now, add data |
---|
77 | data = Data1D(x=[1,2], y=[1,2]) |
---|
78 | GuiUtils.dataFromItem = MagicMock(return_value=data) |
---|
79 | item = QtGui.QStandardItem("test") |
---|
80 | self.widget.setData([item]) |
---|
81 | # Assert data is on widget |
---|
82 | self.assertEqual(len(self.widget.tabs[0].all_data), 1) |
---|
83 | # Reset the tab |
---|
84 | self.widget.resetTab(0) |
---|
85 | # See that the tab contains data no more |
---|
86 | self.assertEqual(len(self.widget.tabs[0].all_data), 0) |
---|
87 | |
---|
88 | def testCloseTab(self): |
---|
89 | '''Delete a tab and test''' |
---|
90 | # Add an empty tab |
---|
91 | self.widget.addFit(None) |
---|
92 | |
---|
93 | # Remove the original tab |
---|
94 | self.widget.tabCloses(1) |
---|
95 | self.assertEqual(len(self.widget.tabs), 1) |
---|
96 | self.assertEqual(self.widget.maxIndex, 2) |
---|
97 | self.assertEqual(self.widget.tabName(), "FitPage2") |
---|
98 | |
---|
99 | # Attemtp to remove the last tab |
---|
100 | self.widget.tabCloses(1) |
---|
101 | # The tab should still be there |
---|
102 | self.assertEqual(len(self.widget.tabs), 1) |
---|
103 | self.assertEqual(self.widget.maxIndex, 3) |
---|
104 | self.assertEqual(self.widget.tabName(), "FitPage3") |
---|
105 | |
---|
106 | def testAllowBatch(self): |
---|
107 | '''Assure the perspective allows multiple datasets''' |
---|
108 | self.assertTrue(self.widget.allowBatch()) |
---|
109 | |
---|
110 | def testSetData(self): |
---|
111 | ''' Assure that setting data is correct''' |
---|
112 | with self.assertRaises(AssertionError): |
---|
113 | self.widget.setData(None) |
---|
114 | |
---|
115 | with self.assertRaises(AttributeError): |
---|
116 | self.widget.setData("BOOP") |
---|
117 | |
---|
118 | # Mock the datafromitem() call from FittingWidget |
---|
119 | data = Data1D(x=[1,2], y=[1,2]) |
---|
120 | GuiUtils.dataFromItem = MagicMock(return_value=data) |
---|
121 | |
---|
122 | item = QtGui.QStandardItem("test") |
---|
123 | self.widget.setData([item]) |
---|
124 | |
---|
125 | # First tab should accept data |
---|
126 | self.assertEqual(len(self.widget.tabs), 1) |
---|
127 | |
---|
128 | # Add another set of data |
---|
129 | self.widget.setData([item]) |
---|
130 | |
---|
131 | # Now we should have two tabs |
---|
132 | self.assertEqual(len(self.widget.tabs), 2) |
---|
133 | |
---|
134 | # Add two more items in a list |
---|
135 | self.widget.setData([item, item]) |
---|
136 | |
---|
137 | # Check for 4 tabs |
---|
138 | self.assertEqual(len(self.widget.tabs), 4) |
---|
139 | |
---|
140 | |
---|
141 | if __name__ == "__main__": |
---|
142 | unittest.main() |
---|