[f1f3e6a] | 1 | import sys |
---|
| 2 | import time |
---|
| 3 | import numpy |
---|
| 4 | import logging |
---|
| 5 | import unittest |
---|
| 6 | from PyQt5 import QtGui, QtWidgets |
---|
| 7 | from PyQt5 import QtCore |
---|
| 8 | from PyQt5.QtTest import QTest |
---|
| 9 | from PyQt5.QtCore import Qt |
---|
[7c487846] | 10 | from unittest.mock import MagicMock |
---|
| 11 | from unittest.mock import patch |
---|
[f1f3e6a] | 12 | |
---|
| 13 | from twisted.internet import threads |
---|
| 14 | |
---|
| 15 | from sas.qtgui.Perspectives.Invariant.InvariantDetails import DetailsDialog |
---|
| 16 | from sas.qtgui.Perspectives.Invariant.InvariantUtils import WIDGETS |
---|
| 17 | |
---|
| 18 | from sas.qtgui.Utilities.GuiUtils import * |
---|
| 19 | |
---|
| 20 | if not QtWidgets.QApplication.instance(): |
---|
| 21 | app = QtWidgets.QApplication(sys.argv) |
---|
| 22 | |
---|
| 23 | BG_COLOR_ERR = 'background-color: rgb(244, 170, 164);' |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | class InvariantDetailsTest(unittest.TestCase): |
---|
| 27 | """Test the Invariant Perspective Window""" |
---|
| 28 | def setUp(self): |
---|
| 29 | """Create the Invariant Details window""" |
---|
| 30 | |
---|
| 31 | self.widget = DetailsDialog(None) |
---|
| 32 | |
---|
| 33 | self.widget._model = QtGui.QStandardItemModel() |
---|
| 34 | |
---|
| 35 | self.widget._model.setItem(WIDGETS.W_INVARIANT, QtGui.QStandardItem(str(10.))) |
---|
| 36 | |
---|
| 37 | self.widget._model.setItem(WIDGETS.W_INVARIANT_ERR, QtGui.QStandardItem(str(0.1))) |
---|
| 38 | |
---|
| 39 | self.widget._model.setItem(WIDGETS.W_ENABLE_LOWQ, QtGui.QStandardItem('true')) |
---|
| 40 | |
---|
| 41 | self.widget._model.setItem(WIDGETS.D_LOW_QSTAR, QtGui.QStandardItem(str(9.))) |
---|
| 42 | |
---|
| 43 | self.widget._model.setItem(WIDGETS.D_LOW_QSTAR_ERR, QtGui.QStandardItem(str(0.03))) |
---|
| 44 | |
---|
| 45 | # # High-Q |
---|
| 46 | self.widget._model.setItem(WIDGETS.W_ENABLE_HIGHQ, |
---|
| 47 | QtGui.QStandardItem('false')) |
---|
| 48 | # self._model.item(WIDGETS.D_HIGH_QSTAR) |
---|
| 49 | # self._model.item(WIDGETS.D_HIGH_QSTAR_ERR) |
---|
| 50 | |
---|
| 51 | def tearDown(self): |
---|
| 52 | """Destroy the Invariant Details window """ |
---|
| 53 | self.widget.close() |
---|
| 54 | self.widget = None |
---|
| 55 | |
---|
| 56 | def testDefaults(self): |
---|
| 57 | """Test the GUI in its default state""" |
---|
| 58 | |
---|
| 59 | self.widget.qstar_total = None |
---|
| 60 | self.widget.qhigh = None |
---|
| 61 | self.widget.qlow = None |
---|
| 62 | |
---|
| 63 | self.widget.progress_low_qstar = 0.0 |
---|
| 64 | self.widget.progress_high_qstar = 0.0 |
---|
| 65 | self.widget.progress_qstar = 100.0 |
---|
| 66 | |
---|
| 67 | self.widget.warning_msg = "No Details on calculations available...\n" |
---|
| 68 | |
---|
| 69 | self.assertIsInstance(self.widget, QtWidgets.QDialog) |
---|
| 70 | |
---|
| 71 | self.assertEqual(self.widget.progressBarLowQ.minimum(), 0) |
---|
| 72 | self.assertEqual(self.widget.progressBarLowQ.maximum(), 100) |
---|
| 73 | |
---|
| 74 | self.assertEqual(self.widget.progressBarData.minimum(), 0) |
---|
| 75 | self.assertEqual(self.widget.progressBarData.maximum(), 100) |
---|
| 76 | |
---|
| 77 | self.assertEqual(self.widget.progressBarHighQ.minimum(), 0) |
---|
| 78 | self.assertEqual(self.widget.progressBarHighQ.maximum(), 100) |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | # Tooltips |
---|
| 82 | self.assertEqual(self.widget.txtQData.toolTip(), "Invariant in the data set's Q range.") |
---|
| 83 | |
---|
| 84 | self.assertEqual(self.widget.txtQDataErr.toolTip(), "Uncertainty on the invariant from data's range.") |
---|
| 85 | |
---|
| 86 | self.assertEqual(self.widget.txtQLowQ.toolTip(), "Extrapolated invariant from low-Q range.") |
---|
| 87 | |
---|
| 88 | self.assertEqual(self.widget.txtQLowQErr.toolTip(), "Uncertainty on the invariant from low-Q range.") |
---|
| 89 | |
---|
| 90 | self.assertEqual(self.widget.txtQHighQ.toolTip(), "Extrapolated invariant from high-Q range.") |
---|
| 91 | |
---|
| 92 | self.assertEqual(self.widget.txtQHighQErr.toolTip(), "Uncertainty on the invariant from high-Q range.") |
---|
| 93 | |
---|
| 94 | def testOnOK(self): |
---|
| 95 | """ Test closing dialog""" |
---|
| 96 | okButton = self.widget.cmdOK |
---|
| 97 | QTest.mouseClick(okButton, Qt.LeftButton) |
---|
| 98 | |
---|
| 99 | def testShowDialog(self): |
---|
| 100 | """ """ |
---|
| 101 | self.widget.showDialog() |
---|
| 102 | # Low Q true |
---|
| 103 | self.assertEqual(self.widget.qlow , 9.0) |
---|
| 104 | |
---|
| 105 | self.assertEqual(self.widget.txtQLowQ.text(), '9.0') |
---|
| 106 | |
---|
| 107 | self.assertEqual(self.widget.progress_low_qstar, 90.0) |
---|
| 108 | |
---|
| 109 | self.assertEqual(self.widget.qstar_total, 10.0) |
---|
| 110 | |
---|
| 111 | self.assertEqual(self.widget.txtQData.text(), '10.0') |
---|
| 112 | |
---|
| 113 | self.assertEqual(self.widget.txtQDataErr.text(), '0.1') |
---|
| 114 | |
---|
| 115 | # High Q false |
---|
| 116 | self.assertEqual(self.widget.txtQHighQ.text(), '') |
---|
| 117 | self.assertEqual(self.widget.txtQHighQErr.text(), '') |
---|
| 118 | self.assertEqual(self.widget.progress_high_qstar, 0.0) # = 0 |
---|
| 119 | self.widget.qhigh # = 0 |
---|
| 120 | |
---|
| 121 | # Progressbars |
---|
| 122 | self.assertEqual(self.widget.progressBarLowQ.value(), self.widget.progress_low_qstar) |
---|
| 123 | |
---|
| 124 | self.assertEqual(self.widget.progressBarLowQ.value(), self.widget.progress_low_qstar) |
---|
| 125 | |
---|
| 126 | self.assertEqual(self.widget.progressBarHighQ.value(), self.widget.progress_high_qstar) |
---|
| 127 | |
---|
| 128 | # TODO to complete |
---|
| 129 | def testCheckValues(self): |
---|
| 130 | """ """ |
---|
| 131 | self.widget.qstart_total = None |
---|
| 132 | self.assertEqual(self.widget.checkValues(), "Invariant not calculated.\n") |
---|
| 133 | |
---|
| 134 | self.widget.qstar_total = 0 |
---|
| 135 | self.assertEqual(self.widget.checkValues(), "Invariant is zero. \n " \ |
---|
| 136 | "The calculations are likely to be unreliable!\n") |
---|
| 137 | |
---|
| 138 | self.widget.qstar_total = 10 |
---|
| 139 | self.widget.progress_qstar = 'error' |
---|
[7c487846] | 140 | self.assertIn("Error occurred when computing invariant from data.", self.widget.checkValues()) |
---|
[f1f3e6a] | 141 | |
---|
| 142 | self.widget.progress_qstar = 0 |
---|
| 143 | |
---|
| 144 | self.widget.progress_low_qstar = 10 |
---|
| 145 | self.assertEqual(self.widget.checkValues(), |
---|
| 146 | 'Extrapolated contribution at Low Q is higher than 5% of the invariant.\nThe sum of all extrapolated contributions is higher than 5% of the invariant.\n') |
---|
| 147 | |
---|
| 148 | self.widget.progress_low_qstar = -1 |
---|
| 149 | self.assertEqual(self.widget.checkValues(), "Extrapolated contribution at Low Q < 0.\n") |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | if __name__ == "__main__": |
---|
| 153 | unittest.main() |
---|