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