1 | import os |
---|
2 | import sys |
---|
3 | import unittest |
---|
4 | import logging |
---|
5 | from xhtml2pdf import pisa |
---|
6 | |
---|
7 | from unittest.mock import mock_open, patch |
---|
8 | |
---|
9 | from unittest.mock import MagicMock |
---|
10 | |
---|
11 | from PyQt5 import QtWidgets, QtPrintSupport |
---|
12 | from PyQt5.QtTest import QTest |
---|
13 | |
---|
14 | # set up import paths |
---|
15 | import path_prepare |
---|
16 | |
---|
17 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
18 | # Local |
---|
19 | from sas.qtgui.Utilities.ReportDialog import ReportDialog |
---|
20 | |
---|
21 | if not QtWidgets.QApplication.instance(): |
---|
22 | app = QtWidgets.QApplication(sys.argv) |
---|
23 | |
---|
24 | class ReportDialogTest(unittest.TestCase): |
---|
25 | '''Test the report dialog''' |
---|
26 | def setUp(self): |
---|
27 | '''Create the dialog''' |
---|
28 | class dummy_manager(object): |
---|
29 | _parent = QtWidgets.QWidget() |
---|
30 | def communicator(self): |
---|
31 | return GuiUtils.Communicate() |
---|
32 | def communicate(self): |
---|
33 | return GuiUtils.Communicate() |
---|
34 | |
---|
35 | test_html = "test_html" |
---|
36 | test_txt = "test_txt" |
---|
37 | test_images = [] |
---|
38 | self.test_list = [test_html, test_txt, test_images] |
---|
39 | self.widget = ReportDialog(parent=dummy_manager(), report_list=self.test_list) |
---|
40 | |
---|
41 | def tearDown(self): |
---|
42 | '''Destroy the GUI''' |
---|
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 | self.setUp() |
---|
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 | # This potentially spawns a "file to write to" dialog, if say, a PrintToPDF is the |
---|
68 | # default printer |
---|
69 | |
---|
70 | # invoke the method |
---|
71 | #self.widget.onPrint() |
---|
72 | |
---|
73 | # Assure printing was done |
---|
74 | #self.assertTrue(document.print.called) |
---|
75 | |
---|
76 | |
---|
77 | def testOnSave(self): |
---|
78 | ''' Saving the report to a file ''' |
---|
79 | # PDF save |
---|
80 | QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=["test.pdf", "(*.pdf)"]) |
---|
81 | os.startfile = MagicMock() |
---|
82 | os.system = MagicMock() |
---|
83 | self.setUp() |
---|
84 | |
---|
85 | # conversion failed |
---|
86 | self.widget.HTML2PDF = MagicMock(return_value=1) |
---|
87 | |
---|
88 | # invoke the method |
---|
89 | self.widget.onSave() |
---|
90 | |
---|
91 | # Check that the file wasn't saved |
---|
92 | if os.name == 'nt': # Windows |
---|
93 | self.assertFalse(os.startfile.called) |
---|
94 | elif sys.platform == "darwin": # Mac |
---|
95 | self.assertFalse(os.system.called) |
---|
96 | |
---|
97 | # conversion succeeded |
---|
98 | temp_html2pdf = self.widget.HTML2PDF |
---|
99 | self.widget.HTML2PDF = MagicMock(return_value=0) |
---|
100 | |
---|
101 | # invoke the method |
---|
102 | self.widget.onSave() |
---|
103 | |
---|
104 | # Check that the file was saved |
---|
105 | if os.name == 'nt': # Windows |
---|
106 | self.assertTrue(os.startfile.called) |
---|
107 | elif sys.platform == "darwin": # Mac |
---|
108 | self.assertTrue(os.system.called) |
---|
109 | |
---|
110 | # TXT save |
---|
111 | QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=["test.txt", "(*.txt)"]) |
---|
112 | self.widget.onTXTSave = MagicMock() |
---|
113 | # invoke the method |
---|
114 | self.widget.onSave() |
---|
115 | |
---|
116 | # Check that the file was saved |
---|
117 | self.assertTrue(self.widget.onTXTSave) |
---|
118 | |
---|
119 | # HTML save |
---|
120 | QtWidgets.QFileDialog.getSaveFileName = MagicMock(return_value=["test.html", "(*.html)"]) |
---|
121 | self.widget.onHTMLSave = MagicMock() |
---|
122 | # invoke the method |
---|
123 | self.widget.onSave() |
---|
124 | |
---|
125 | # Check that the file was saved |
---|
126 | self.assertTrue(self.widget.onHTMLSave) |
---|
127 | |
---|
128 | self.widget.HTML2PDF = temp_html2pdf |
---|
129 | |
---|
130 | |
---|
131 | def testGetPictures(self): |
---|
132 | ''' Saving MPL charts and returning filenames ''' |
---|
133 | pass |
---|
134 | |
---|
135 | def testHTML2PDF(self): |
---|
136 | ''' html to pdf conversion ''' |
---|
137 | class pisa_dummy(object): |
---|
138 | err = 0 |
---|
139 | pisa.CreatePDF = MagicMock(return_value=pisa_dummy()) |
---|
140 | open = MagicMock(return_value="y") |
---|
141 | self.setUp() |
---|
142 | |
---|
143 | QTest.qWait(100) |
---|
144 | |
---|
145 | data = self.widget.txtBrowser.toHtml() |
---|
146 | return_value = self.widget.HTML2PDF(data, "b") |
---|
147 | |
---|
148 | self.assertTrue(pisa.CreatePDF.called) |
---|
149 | self.assertEqual(return_value, 0) |
---|
150 | |
---|
151 | # Try to raise somewhere |
---|
152 | pisa.CreatePDF.side_effect = Exception("Failed") |
---|
153 | |
---|
154 | logging.error = MagicMock() |
---|
155 | |
---|
156 | #run the method |
---|
157 | return_value = self.widget.HTML2PDF(data, "c") |
---|
158 | |
---|
159 | self.assertTrue(logging.error.called) |
---|
160 | #logging.error.assert_called_with("Error creating pdf") |
---|
161 | |
---|