1 | import sys |
---|
2 | import unittest |
---|
3 | import numpy as np |
---|
4 | |
---|
5 | from unittest.mock import MagicMock |
---|
6 | |
---|
7 | from PyQt5 import QtGui, QtCore, QtWidgets |
---|
8 | from PyQt5.QtTest import QTest |
---|
9 | |
---|
10 | # set up import paths |
---|
11 | import path_prepare |
---|
12 | |
---|
13 | import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary |
---|
14 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
15 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
16 | |
---|
17 | # Local |
---|
18 | from sas.qtgui.Perspectives.Fitting.ConstraintWidget import ConstraintWidget |
---|
19 | from sas.qtgui.Perspectives.Fitting.Constraint import Constraint |
---|
20 | from sas.qtgui.Perspectives.Fitting.FittingPerspective import FittingWindow |
---|
21 | |
---|
22 | if not QtWidgets.QApplication.instance(): |
---|
23 | app = QtWidgets.QApplication(sys.argv) |
---|
24 | #app = QtWidgets.QApplication(sys.argv) |
---|
25 | |
---|
26 | class ConstraintWidgetTest(unittest.TestCase): |
---|
27 | '''Test the ConstraintWidget dialog''' |
---|
28 | def setUp(self): |
---|
29 | '''Create ConstraintWidget dialog''' |
---|
30 | class dummy_manager(object): |
---|
31 | def communicator(self): |
---|
32 | return GuiUtils.Communicate() |
---|
33 | def communicate(self): |
---|
34 | return GuiUtils.Communicate() |
---|
35 | |
---|
36 | '''Create the perspective''' |
---|
37 | self.perspective = FittingWindow(dummy_manager()) |
---|
38 | |
---|
39 | self.widget = ConstraintWidget(parent=self.perspective) |
---|
40 | |
---|
41 | # Example constraint object |
---|
42 | self.constraint1 = Constraint(parent=None, param="test", value="7.0", min="0.0", max="inf", func="M1:sld") |
---|
43 | self.constraint2 = Constraint(parent=None, param="poop", value="7.0", min="0.0", max="inf", func="7.0") |
---|
44 | |
---|
45 | def tearDown(self): |
---|
46 | '''Destroy the GUI''' |
---|
47 | self.widget.close() |
---|
48 | self.widget = None |
---|
49 | |
---|
50 | def testDefaults(self): |
---|
51 | '''Test the GUI in its default state''' |
---|
52 | self.assertIsInstance(self.widget, QtWidgets.QWidget) |
---|
53 | # Default title |
---|
54 | self.assertEqual(self.widget.windowTitle(), "Constrained and Simultaneous Fit") |
---|
55 | # Dicts |
---|
56 | self.assertIsInstance(self.widget.available_constraints, dict) |
---|
57 | self.assertIsInstance(self.widget.available_tabs, dict) |
---|
58 | # TableWidgets |
---|
59 | self.assertEqual(self.widget.tblTabList.columnCount(), 4) |
---|
60 | self.assertEqual(self.widget.tblConstraints.columnCount(), 1) |
---|
61 | # Data accept |
---|
62 | self.assertFalse(self.widget.acceptsData()) |
---|
63 | # By default, the constraint table is disabled |
---|
64 | self.assertFalse(self.widget.tblConstraints.isEnabled()) |
---|
65 | |
---|
66 | def testOnFitTypeChange(self): |
---|
67 | ''' test the single/batch fit switch ''' |
---|
68 | self.widget.initializeFitList = MagicMock() |
---|
69 | # Assure current type is Single |
---|
70 | self.assertEqual(self.widget.currentType, "FitPage") |
---|
71 | # click on "batch" |
---|
72 | QTest.mouseClick(self.widget.btnBatch, QtCore.Qt.LeftButton) |
---|
73 | app.processEvents() |
---|
74 | # See what the current type is now |
---|
75 | self.assertEqual(self.widget.currentType, "BatchPage") |
---|
76 | # See if the list is getting initialized |
---|
77 | self.assertTrue(self.widget.initializeFitList.called) |
---|
78 | # Go back to single fit |
---|
79 | QTest.mouseClick(self.widget.btnSingle, QtCore.Qt.LeftButton) |
---|
80 | app.processEvents() |
---|
81 | # See what the current type is now |
---|
82 | self.assertEqual(self.widget.currentType, "FitPage") |
---|
83 | |
---|
84 | def testGetTabsForFit(self): |
---|
85 | ''' Test the fitting tab list ''' |
---|
86 | self.assertEqual(self.widget.getTabsForFit(),[]) |
---|
87 | # Add some tabs |
---|
88 | pass |
---|
89 | |
---|
90 | def testIsTabImportable(self): |
---|
91 | ''' tab checks for consistency ''' |
---|
92 | test_tab = QtCore.QObject() |
---|
93 | test_tab.data = self.constraint1 |
---|
94 | ObjectLibrary.getObject = MagicMock(return_value=test_tab) |
---|
95 | |
---|
96 | self.assertFalse(self.widget.isTabImportable(None)) |
---|
97 | self.assertFalse(self.widget.isTabImportable("BatchTab1")) |
---|
98 | self.assertFalse(self.widget.isTabImportable("BatchTab")) |
---|
99 | |
---|
100 | def testOnTabCellEdit(self): |
---|
101 | ''' test what happens on monicker edit ''' |
---|
102 | # Mock the datafromitem() call from FittingWidget |
---|
103 | data = Data1D(x=[1,2], y=[1,2]) |
---|
104 | GuiUtils.dataFromItem = MagicMock(return_value=data) |
---|
105 | item = QtGui.QStandardItem("test") |
---|
106 | self.perspective.addFit([item]) |
---|
107 | |
---|
108 | def testUpdateFitLine(self): |
---|
109 | ''' See if the fit table row can be updated ''' |
---|
110 | pass |
---|
111 | |
---|
112 | def testUpdateFitList(self): |
---|
113 | ''' see if the fit table can be updated ''' |
---|
114 | pass |
---|
115 | |
---|
116 | def testUpdateConstraintList(self): |
---|
117 | ''' see if the constraint table can be updated ''' |
---|
118 | pass |
---|
119 | |
---|
120 | |
---|