1 | import sys |
---|
2 | import unittest |
---|
3 | from unittest.mock import MagicMock |
---|
4 | |
---|
5 | from PyQt4 import QtGui |
---|
6 | from PyQt4 import QtCore |
---|
7 | |
---|
8 | # set up import paths |
---|
9 | import sas.qtgui.path_prepare |
---|
10 | |
---|
11 | # Local |
---|
12 | from sas.qtgui.Plotting.SlicerModel import SlicerModel |
---|
13 | |
---|
14 | if not QtGui.QApplication.instance(): |
---|
15 | app = QtGui.QApplication(sys.argv) |
---|
16 | |
---|
17 | class SlicerModelTest(unittest.TestCase): |
---|
18 | '''Test the SlicerModel''' |
---|
19 | def setUp(self): |
---|
20 | '''Create the SlicerModel''' |
---|
21 | class SModel(SlicerModel): |
---|
22 | params = {"a":1, "b":2} |
---|
23 | def __init__(self): |
---|
24 | SlicerModel.__init__(self) |
---|
25 | def getParams(self): |
---|
26 | return self.params |
---|
27 | def setParams(self, par): |
---|
28 | self.params = par |
---|
29 | self.model = SModel() |
---|
30 | |
---|
31 | def tearDown(self): |
---|
32 | '''Destroy the model''' |
---|
33 | self.model = None |
---|
34 | |
---|
35 | def testBaseClass(self): |
---|
36 | '''Assure that SlicerModel contains pure virtuals''' |
---|
37 | model = SlicerModel() |
---|
38 | with self.assertRaises(NotImplementedError): |
---|
39 | model.setParams() |
---|
40 | with self.assertRaises(NotImplementedError): |
---|
41 | model.setModelFromParams() |
---|
42 | |
---|
43 | def testDefaults(self): |
---|
44 | '''Test the GUI in its default state''' |
---|
45 | self.assertIsInstance(self.model.model(), QtGui.QStandardItemModel) |
---|
46 | |
---|
47 | def testSetModelFromParams(self): |
---|
48 | '''Test the model update''' |
---|
49 | # Add a row to params |
---|
50 | new_dict = self.model.getParams() |
---|
51 | new_dict["c"] = 3 |
---|
52 | self.model.setParams(new_dict) |
---|
53 | |
---|
54 | # Call the update |
---|
55 | self.model.setModelFromParams() |
---|
56 | |
---|
57 | # Check the new model. |
---|
58 | self.assertEqual(self.model.model().rowCount(), 3) |
---|
59 | self.assertEqual(self.model.model().columnCount(), 2) |
---|
60 | |
---|
61 | def testSetParamsFromModel(self): |
---|
62 | ''' Test the parameters update''' |
---|
63 | # First - the default model |
---|
64 | self.model.setModelFromParams() |
---|
65 | self.assertEqual(self.model.model().rowCount(), 2) |
---|
66 | self.assertEqual(self.model.model().columnCount(), 2) |
---|
67 | |
---|
68 | # Add a row |
---|
69 | item1 = QtGui.QStandardItem("c") |
---|
70 | item2 = QtGui.QStandardItem(3) |
---|
71 | self.model.model().appendRow([item1, item2]) |
---|
72 | # Check the new model. The update should be automatic |
---|
73 | self.assertEqual(self.model.model().rowCount(), 3) |
---|
74 | self.assertEqual(self.model.model().columnCount(), 2) |
---|
75 | |
---|
76 | |
---|
77 | if __name__ == "__main__": |
---|
78 | unittest.main() |
---|