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