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 | |
---|
55 | def testCloseTab(self): |
---|
56 | '''Delete a tab and test''' |
---|
57 | # Add an empty tab |
---|
58 | self.widget.addFit(None) |
---|
59 | |
---|
60 | # Remove the original tab |
---|
61 | self.widget.tabCloses(1) |
---|
62 | self.assertEqual(len(self.widget.tabs), 1) |
---|
63 | self.assertEqual(self.widget.maxIndex, 2) |
---|
64 | self.assertEqual(self.widget.tabName(), "FitPage2") |
---|
65 | |
---|
66 | # Attemtp to remove the last tab |
---|
67 | self.widget.tabCloses(1) |
---|
68 | # The tab should still be there |
---|
69 | self.assertEqual(len(self.widget.tabs), 1) |
---|
70 | self.assertEqual(self.widget.maxIndex, 2) |
---|
71 | self.assertEqual(self.widget.tabName(), "FitPage2") |
---|
72 | |
---|
73 | def testAllowBatch(self): |
---|
74 | '''Assure the perspective allows multiple datasets''' |
---|
75 | self.assertTrue(self.widget.allowBatch()) |
---|
76 | |
---|
77 | def testSetData(self): |
---|
78 | ''' Assure that setting data is correct''' |
---|
79 | with self.assertRaises(AssertionError): |
---|
80 | self.widget.setData(None) |
---|
81 | |
---|
82 | with self.assertRaises(AttributeError): |
---|
83 | self.widget.setData("BOOP") |
---|
84 | |
---|
85 | # Mock the datafromitem() call from FittingWidget |
---|
86 | data = Data1D(x=[1,2], y=[1,2]) |
---|
87 | GuiUtils.dataFromItem = MagicMock(return_value=data) |
---|
88 | |
---|
89 | item = QtGui.QStandardItem("test") |
---|
90 | self.widget.setData([item]) |
---|
91 | |
---|
92 | # First tab should accept data |
---|
93 | self.assertEqual(len(self.widget.tabs), 1) |
---|
94 | |
---|
95 | # Add another set of data |
---|
96 | self.widget.setData([item]) |
---|
97 | |
---|
98 | # Now we should have two tabs |
---|
99 | self.assertEqual(len(self.widget.tabs), 2) |
---|
100 | |
---|
101 | # Add two more items in a list |
---|
102 | self.widget.setData([item, item]) |
---|
103 | |
---|
104 | # Check for 4 tabs |
---|
105 | self.assertEqual(len(self.widget.tabs), 4) |
---|
106 | |
---|
107 | |
---|
108 | if __name__ == "__main__": |
---|
109 | unittest.main() |
---|