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.GuiUtils as GuiUtils |
---|
15 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
16 | |
---|
17 | from sas.qtgui.Perspectives.Fitting.FittingPerspective import FittingWindow |
---|
18 | |
---|
19 | app = QtGui.QApplication(sys.argv) |
---|
20 | |
---|
21 | class FittingPerspectiveTest(unittest.TestCase): |
---|
22 | '''Test the Fitting Perspective''' |
---|
23 | def setUp(self): |
---|
24 | class dummy_manager(object): |
---|
25 | def communicator(self): |
---|
26 | return GuiUtils.Communicate() |
---|
27 | def communicate(self): |
---|
28 | return GuiUtils.Communicate() |
---|
29 | |
---|
30 | '''Create the perspective''' |
---|
31 | self.widget = FittingWindow(dummy_manager()) |
---|
32 | |
---|
33 | def tearDown(self): |
---|
34 | '''Destroy the perspective''' |
---|
35 | self.widget.close() |
---|
36 | self.widget = None |
---|
37 | |
---|
38 | def testDefaults(self): |
---|
39 | '''Test the GUI in its default state''' |
---|
40 | self.assertIsInstance(self.widget, QtGui.QWidget) |
---|
41 | self.assertIn("Fit panel", self.widget.windowTitle()) |
---|
42 | self.assertEqual(self.widget.optimizer, "DREAM") |
---|
43 | self.assertEqual(len(self.widget.tabs), 1) |
---|
44 | self.assertEqual(self.widget.maxIndex, 1) |
---|
45 | self.assertEqual(self.widget.tabName(), "FitPage1") |
---|
46 | |
---|
47 | def testAddTab(self): |
---|
48 | '''Add a tab and test it''' |
---|
49 | |
---|
50 | # Add an empty tab |
---|
51 | self.widget.addFit(None) |
---|
52 | self.assertEqual(len(self.widget.tabs), 2) |
---|
53 | self.assertEqual(self.widget.tabName(), "FitPage2") |
---|
54 | self.assertEqual(self.widget.maxIndex, 2) |
---|
55 | |
---|
56 | def testCloseTab(self): |
---|
57 | '''Delete a tab and test''' |
---|
58 | # Add an empty tab |
---|
59 | self.widget.addFit(None) |
---|
60 | |
---|
61 | # Remove the original tab |
---|
62 | self.widget.tabCloses(1) |
---|
63 | self.assertEqual(len(self.widget.tabs), 1) |
---|
64 | self.assertEqual(self.widget.maxIndex, 2) |
---|
65 | self.assertEqual(self.widget.tabName(), "FitPage2") |
---|
66 | |
---|
67 | # Attemtp to remove the last tab |
---|
68 | self.widget.tabCloses(1) |
---|
69 | # The tab should still be there |
---|
70 | self.assertEqual(len(self.widget.tabs), 1) |
---|
71 | self.assertEqual(self.widget.maxIndex, 2) |
---|
72 | self.assertEqual(self.widget.tabName(), "FitPage2") |
---|
73 | |
---|
74 | def testAllowBatch(self): |
---|
75 | '''Assure the perspective allows multiple datasets''' |
---|
76 | self.assertTrue(self.widget.allowBatch()) |
---|
77 | |
---|
78 | def testSetData(self): |
---|
79 | ''' Assure that setting data is correct''' |
---|
80 | with self.assertRaises(AssertionError): |
---|
81 | self.widget.setData(None) |
---|
82 | |
---|
83 | with self.assertRaises(AttributeError): |
---|
84 | self.widget.setData("BOOP") |
---|
85 | |
---|
86 | # Mock the datafromitem() call from FittingWidget |
---|
87 | data = Data1D(x=[1,2], y=[1,2]) |
---|
88 | GuiUtils.dataFromItem = MagicMock(return_value=data) |
---|
89 | |
---|
90 | item = QtGui.QStandardItem("test") |
---|
91 | self.widget.setData([item]) |
---|
92 | |
---|
93 | # Look at the data in tab |
---|
94 | self.assertEqual(self.widget._model_item, item) |
---|
95 | |
---|
96 | # First tab should accept data |
---|
97 | self.assertEqual(len(self.widget.tabs), 1) |
---|
98 | |
---|
99 | # Add another set of data |
---|
100 | self.widget.setData([item]) |
---|
101 | |
---|
102 | # Now we should have two tabs |
---|
103 | self.assertEqual(len(self.widget.tabs), 2) |
---|
104 | |
---|
105 | |
---|
106 | if __name__ == "__main__": |
---|
107 | unittest.main() |
---|