source: sasview/src/sas/qtgui/Utilities/UnitTesting/ReportDialogTest.py @ 57be490

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 57be490 was 57be490, checked in by Piotr Rozyczko <rozyczko@…>, 3 years ago

Merged ESS_GUI_reporting

  • Property mode set to 100755
File size: 4.4 KB
Line 
1import os
2import sys
3import unittest
4import logging
5from xhtml2pdf import pisa
6
7from unittest.mock import mock_open, patch
8
9from unittest.mock import MagicMock
10
11from PyQt5 import QtWidgets, QtPrintSupport
12
13# set up import paths
14import path_prepare
15
16import sas.qtgui.Utilities.GuiUtils as GuiUtils
17# Local
18from sas.qtgui.Utilities.ReportDialog import ReportDialog
19
20if not QtWidgets.QApplication.instance():
21    app = QtWidgets.QApplication(sys.argv)
22
23class ReportDialogTest(unittest.TestCase):
24    '''Test the report dialog'''
25    def setUp(self):
26        '''Create the dialog'''
27        class dummy_manager(object):
28            _parent = QtWidgets.QWidget()
29            def communicator(self):
30                return GuiUtils.Communicate()
31            def communicate(self):
32                return GuiUtils.Communicate()
33
34        test_html = "test_html"
35        test_txt = "test_txt"
36        test_images = []
37        self.test_list = [test_html, test_txt, test_images]
38        self.widget = ReportDialog(parent=dummy_manager(), report_list=self.test_list)
39
40    def tearDown(self):
41        '''Destroy the GUI'''
42        #self.widget.close()
43        self.widget = None
44
45    def testDefaults(self):
46        '''Look at the default state of the widget'''
47        self.assertIn(self.test_list[0], self.widget.txtBrowser.toHtml())
48        self.assertTrue(self.widget.txtBrowser.isReadOnly())
49
50    def testOnPrint(self):
51        ''' Printing the report '''
52        document = self.widget.txtBrowser.document()
53        document.print = MagicMock()
54
55        # test rejected dialog
56        QtPrintSupport.QPrintDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Rejected)
57
58        # invoke the method
59        self.widget.onPrint()
60
61        # Assure printing was not done
62        self.assertFalse(document.print.called)
63
64        # test accepted dialog
65        QtPrintSupport.QPrintDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Accepted)
66
67        # invoke the method
68        self.widget.onPrint()
69
70        # Assure printing was done
71        self.assertTrue(document.print.called)
72
73
74    def testOnSave(self):
75        ''' Saving the report to a file '''
76        # PDF save
77        QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=["test.pdf", "(*.pdf)"])
78        os.startfile = MagicMock()
79        os.system = MagicMock()
80
81        # conversion failed
82        self.widget.HTML2PDF = MagicMock(return_value=1)
83
84        # invoke the method
85        self.widget.onSave()
86
87        # Check that the file wasn't saved
88        if os.name == 'nt':  # Windows
89            self.assertFalse(os.startfile.called)
90        elif sys.platform == "darwin":  # Mac
91            self.assertFalse(os.system.called)
92
93        # conversion succeeded
94        self.widget.HTML2PDF = MagicMock(return_value=0)
95
96        # invoke the method
97        self.widget.onSave()
98
99        # Check that the file was saved
100        if os.name == 'nt':  # Windows
101            self.assertTrue(os.startfile.called)
102        elif sys.platform == "darwin":  # Mac
103            self.assertTrue(os.system.called)
104
105        # TXT save
106        QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=["test.txt", "(*.txt)"])
107        self.widget.onTXTSave = MagicMock()
108        # invoke the method
109        self.widget.onSave()
110
111        # Check that the file was saved
112        self.assertTrue(self.widget.onTXTSave)
113
114        # HTML save
115        QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=["test.html", "(*.html)"])
116        self.widget.onHTMLSave = MagicMock()
117        # invoke the method
118        self.widget.onSave()
119
120        # Check that the file was saved
121        self.assertTrue(self.widget.onHTMLSave)
122
123    def testGetPictures(self):
124        ''' Saving MPL charts and returning filenames '''
125        pass
126
127    def testHTML2PDF(self):
128        ''' html to pdf conversion '''
129        class pisa_dummy(object):
130            err = 0
131        pisa.CreatePDF = MagicMock(return_value=pisa_dummy())
132        open = MagicMock(return_value="y")
133
134        data = self.widget.txtBrowser.toHtml()
135        return_value = self.widget.HTML2PDF(data, "b")
136
137        self.assertTrue(pisa.CreatePDF.called)
138        self.assertEqual(return_value, 0)
139
140        # Try to raise somewhere
141        pisa.CreatePDF.side_effect = Exception("Failed")
142
143        logging.error = MagicMock()
144
145        #run the method
146        return_value = self.widget.HTML2PDF(data, "c")
147
148        self.assertTrue(logging.error.called)
149        logging.error.assert_called_with("Error creating pdf: Failed")
150
Note: See TracBrowser for help on using the repository browser.