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