1 | import sys |
---|
2 | import numpy as np |
---|
3 | import unittest |
---|
4 | from unittest.mock import mock_open, patch |
---|
5 | import webbrowser |
---|
6 | import logging |
---|
7 | |
---|
8 | from unittest.mock import MagicMock |
---|
9 | |
---|
10 | from PyQt5 import QtGui, QtWidgets |
---|
11 | |
---|
12 | # set up import paths |
---|
13 | import path_prepare |
---|
14 | |
---|
15 | from sas.qtgui.UnitTesting.TestUtils import QtSignalSpy |
---|
16 | |
---|
17 | from sas.sascalc.fit.AbstractFitEngine import FResult |
---|
18 | from sasmodels.sasview_model import load_standard_models |
---|
19 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
20 | |
---|
21 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
22 | # Local |
---|
23 | from sas.qtgui.Utilities.GridPanel import BatchOutputPanel |
---|
24 | |
---|
25 | if not QtWidgets.QApplication.instance(): |
---|
26 | app = QtWidgets.QApplication(sys.argv) |
---|
27 | |
---|
28 | class BatchOutputPanelTest(unittest.TestCase): |
---|
29 | '''Test the batch output dialog''' |
---|
30 | def setUp(self): |
---|
31 | '''Create the dialog''' |
---|
32 | # dummy perspective |
---|
33 | class dummy_manager(object): |
---|
34 | def communicator(self): |
---|
35 | return GuiUtils.Communicate() |
---|
36 | def communicate(self): |
---|
37 | return GuiUtils.Communicate() |
---|
38 | self.widget = BatchOutputPanel(parent=dummy_manager(), output_data=self.output_for_test()) |
---|
39 | test_table = {"p1":[1,2,3], |
---|
40 | "p2":[4,5,None], |
---|
41 | "":["a",credits,], |
---|
42 | "":[]} |
---|
43 | |
---|
44 | def tearDown(self): |
---|
45 | '''Destroy the GUI''' |
---|
46 | self.widget.close() |
---|
47 | self.widget = None |
---|
48 | |
---|
49 | def output_for_test(self): |
---|
50 | ''' define DATA1D structure for testing''' |
---|
51 | # dummy parameter set |
---|
52 | m = None |
---|
53 | for m in load_standard_models(): |
---|
54 | if m.name == "core_shell_ellipsoid": |
---|
55 | model = m() |
---|
56 | self.assertIsNotNone(m) |
---|
57 | data = Data1D(x=[1,2], y=[3,4], dx=[0.1, 0.1], dy=[0.,0.]) |
---|
58 | param_list = ['sld_shell', 'sld_solvent'] |
---|
59 | output = FResult(model=model, data=data, param_list=param_list) |
---|
60 | output.theory = np.array([0.1,0.2]) |
---|
61 | output.pvec = np.array([0.1, 0.02]) |
---|
62 | output.residuals = np.array([0.01, 0.02]) |
---|
63 | output.fitness = 9000.0 |
---|
64 | output.fitter_id = 200 |
---|
65 | output_data = [[output],[output]] |
---|
66 | return output_data |
---|
67 | |
---|
68 | def testDefaults(self): |
---|
69 | '''Test the GUI in its default state''' |
---|
70 | self.assertIsInstance(self.widget, QtWidgets.QMainWindow) |
---|
71 | # Default title |
---|
72 | self.assertEqual(self.widget.windowTitle(), "Grid Panel") |
---|
73 | |
---|
74 | # non-modal window |
---|
75 | self.assertFalse(self.widget.isModal()) |
---|
76 | |
---|
77 | def testActionLoadData(self): |
---|
78 | '''Test CSV data loader''' |
---|
79 | test_csv = mock_open(read_data = ("""\ |
---|
80 | File generated by SasView\n |
---|
81 | Chi2,Data,scale,background,radius_equat_core,x_core,thick_shell,x_polar_shell,sld_core,sld_shell,sld_solvent,theta,phi,\n |
---|
82 | 1917.8,cyl_400_40.txt,1,0.001,20,3,30,1,-85.088,-97.636,-92.797,0,0,\n |
---|
83 | 2.6169,cyl_400_20.txt,1,0.001,20,3,30,1,914.64,906.09,905.67,0,0,\n |
---|
84 | """)) |
---|
85 | logging.info = MagicMock() |
---|
86 | self.widget.setupTableFromCSV = MagicMock() |
---|
87 | |
---|
88 | # No filename given |
---|
89 | QtWidgets.QFileDialog.getOpenFileName = MagicMock(return_value=[""]) |
---|
90 | self.widget.actionLoadData() |
---|
91 | # Assure parser wasn't called and logging got a message |
---|
92 | self.assertTrue(logging.info.called_once()) |
---|
93 | self.assertIn("No data", logging.info.call_args[0][0]) |
---|
94 | |
---|
95 | # Filename given |
---|
96 | QtWidgets.QFileDialog.getOpenFileName = MagicMock(return_value="test") |
---|
97 | with patch('builtins.open', test_csv): |
---|
98 | self.widget.actionLoadData() |
---|
99 | # Assure parser was called |
---|
100 | self.assertTrue(self.widget.actionLoadData) |
---|
101 | self.widget.setupTableFromCSV.assert_called_once() |
---|
102 | self.assertIn("File generated by SasView", self.widget.setupTableFromCSV.call_args[0][0][0]) |
---|
103 | |
---|
104 | def notestPlotFits(self): |
---|
105 | '''Test plot generation from selected table rows''' |
---|
106 | # mock tested calls |
---|
107 | #GuiUtils.Communicate.plot |
---|
108 | spy_plot_signal = QtSignalSpy(self.widget.communicate, self.widget.communicate().plotFromFilenameSignal) |
---|
109 | # Select row #1 |
---|
110 | self.widget.tblParams.selectRow(0) |
---|
111 | QtWidgets.QApplication.processEvents() |
---|
112 | |
---|
113 | # See that the signal was emitted |
---|
114 | self.assertEqual(spy_plot_signal.count(), 1) |
---|
115 | self.assertIn("ddd", str(spy_plot_signal.called()[0]['args'][0])) |
---|
116 | |
---|
117 | |
---|
118 | def testDataFromTable(self): |
---|
119 | '''Test dictionary generation from data''' |
---|
120 | params = self.widget.dataFromTable(self.widget.tblParams) |
---|
121 | self.assertEqual(len(params), 13) |
---|
122 | self.assertEqual(params['Chi2'][0], '9000') |
---|
123 | self.assertEqual(params['Data'][1], '') |
---|
124 | self.assertEqual(params['sld_solvent'][1], '0.02') |
---|
125 | |
---|
126 | def testActionSendToExcel(self): |
---|
127 | '''Test Excel bindings''' |
---|
128 | pass |
---|
129 | |
---|
130 | def testActionSaveFile(self): |
---|
131 | '''Test file save''' |
---|
132 | self.widget.writeBatchToFile = MagicMock() |
---|
133 | |
---|
134 | # user cancels dialog |
---|
135 | QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=("","BOOP")) |
---|
136 | # write not called |
---|
137 | self.widget.actionSaveFile() |
---|
138 | self.widget.writeBatchToFile.assert_not_called() |
---|
139 | |
---|
140 | # user chooses proper name |
---|
141 | QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=("plop","BOOP")) |
---|
142 | # write called |
---|
143 | self.widget.actionSaveFile() |
---|
144 | self.widget.writeBatchToFile.assert_called_once() |
---|
145 | |
---|
146 | def testSetupTableFromCSV(self): |
---|
147 | '''Test generation of grid table rows from a CSV file''' |
---|
148 | pass |
---|
149 | |
---|
150 | |
---|