10
10
import unittest
11
11
import tempfile
12
12
13
- from pyPdf import PdfFileReader
14
- import trytond .tests .test_tryton
13
+ from PyPDF2 import PdfFileReader
15
14
from trytond .transaction import Transaction
16
- from trytond .tests .test_tryton import POOL , USER , with_transaction
15
+ from trytond .tests .test_tryton import activate_module , with_transaction , USER
17
16
from trytond .pool import Pool
18
17
19
18
from openlabs_report_webkit import ReportWebkit
@@ -40,17 +39,17 @@ class ReportTestCase(unittest.TestCase):
40
39
def setUp (self ):
41
40
register ()
42
41
43
- trytond .tests .test_tryton .install_module ('report_webkit' )
44
-
45
- self .Currency = POOL .get ('currency.currency' )
46
- self .Company = POOL .get ('company.company' )
47
- self .Party = POOL .get ('party.party' )
48
- self .User = POOL .get ('res.user' )
42
+ activate_module ('report_webkit' )
49
43
50
44
def setup_defaults (self ):
51
45
"""
52
46
Setup the defaults
53
47
"""
48
+ self .Currency = Pool ().get ('currency.currency' )
49
+ self .Company = Pool ().get ('company.company' )
50
+ self .Party = Pool ().get ('party.party' )
51
+ self .User = Pool ().get ('res.user' )
52
+
54
53
with Transaction ().set_context (company = None ):
55
54
self .usd , = self .Currency .create ([{
56
55
'name' : 'US Dollar' ,
@@ -80,8 +79,8 @@ def test_0010_render_report_xhtml(self):
80
79
'''
81
80
Render the report without PDF conversion
82
81
'''
83
- UserReport = POOL .get ('res.user' , type = 'report' )
84
- IRReport = POOL .get ('ir.action.report' )
82
+ UserReport = Pool () .get ('res.user' , type = 'report' )
83
+ IRReport = Pool () .get ('ir.action.report' )
85
84
86
85
self .setup_defaults ()
87
86
@@ -90,24 +89,24 @@ def test_0010_render_report_xhtml(self):
90
89
'name' : 'HTML Report' ,
91
90
'model' : 'res.user' ,
92
91
'report_name' : 'res.user' ,
93
- 'report_content' : buffer (
94
- '<h1>Hello, {{records[0].name}}!</h1>'
92
+ 'report_content' : memoryview (
93
+ '<h1>Hello, {{records[0].name}}!</h1>' . encode ()
95
94
),
96
95
'extension' : 'html' ,
97
96
}])
98
97
val = UserReport .execute ([USER ], {})
99
- self .assertEqual (val [0 ], u 'html' )
98
+ self .assertEqual (val [0 ], 'html' )
100
99
self .assertEqual (
101
- str ( val [1 ]) , '<h1>Hello, Administrator!</h1>'
100
+ val [1 ], '<h1>Hello, Administrator!</h1>' . encode ()
102
101
)
103
102
104
103
@with_transaction ()
105
104
def test_0020_render_unicode (self ):
106
105
'''
107
106
Render the report without PDF conversion but having unicode template
108
107
'''
109
- UserReport = POOL .get ('res.user' , type = 'report' )
110
- IRReport = POOL .get ('ir.action.report' )
108
+ UserReport = Pool () .get ('res.user' , type = 'report' )
109
+ IRReport = Pool () .get ('ir.action.report' )
111
110
112
111
self .setup_defaults ()
113
112
@@ -116,25 +115,25 @@ def test_0020_render_unicode(self):
116
115
'name' : 'HTML Report' ,
117
116
'model' : 'res.user' ,
118
117
'report_name' : 'res.user' ,
119
- 'report_content' : buffer (
120
- "<h1>Héllø, {{data['name']}}!</h1>"
118
+ 'report_content' : memoryview (
119
+ "<h1>Héllø, {{data['name']}}!</h1>" . encode ()
121
120
),
122
121
'extension' : 'html' ,
123
122
}])
124
123
125
- val = UserReport .execute ([USER ], {'name' : u 'Cédric' })
126
- self .assertEqual (val [0 ], u 'html' )
124
+ val = UserReport .execute ([USER ], {'name' : 'Cédric' })
125
+ self .assertEqual (val [0 ], 'html' )
127
126
self .assertEqual (
128
- str ( val [1 ]) , '<h1>Héllø, Cédric!</h1>'
127
+ val [1 ], '<h1>Héllø, Cédric!</h1>' . encode ()
129
128
)
130
129
131
130
@with_transaction ()
132
131
def test_0025_render_escaping (self ):
133
132
'''
134
133
Ensure that escaping works
135
134
'''
136
- UserReport = POOL .get ('res.user' , type = 'report' )
137
- IRReport = POOL .get ('ir.action.report' )
135
+ UserReport = Pool () .get ('res.user' , type = 'report' )
136
+ IRReport = Pool () .get ('ir.action.report' )
138
137
139
138
self .setup_defaults ()
140
139
@@ -143,25 +142,26 @@ def test_0025_render_escaping(self):
143
142
'name' : 'HTML Report' ,
144
143
'model' : 'res.user' ,
145
144
'report_name' : 'res.user' ,
146
- 'report_content' : buffer (
147
- "<h1>Héllø, {{data['name']}}!</h1>"
145
+ 'report_content' : memoryview (
146
+ "<h1>Héllø, {{data['name']}}!</h1>" . encode ()
148
147
),
149
148
'extension' : 'html' ,
150
149
}])
151
150
152
- val = UserReport .execute ([USER ], {'name' : u '<script></script>' })
153
- self .assertEqual (val [0 ], u 'html' )
151
+ val = UserReport .execute ([USER ], {'name' : '<script></script>' })
152
+ self .assertEqual (val [0 ], 'html' )
154
153
self .assertEqual (
155
- str (val [1 ]), '<h1>Héllø, <script></script>!</h1>'
154
+ val [1 ],
155
+ '<h1>Héllø, <script></script>!</h1>' .encode ()
156
156
)
157
157
158
158
@with_transaction ()
159
159
def test_0030_render_pdf (self ):
160
160
'''
161
161
Render the report in PDF
162
162
'''
163
- UserReport = POOL .get ('res.user' , type = 'report' )
164
- IRReport = POOL .get ('ir.action.report' )
163
+ UserReport = Pool () .get ('res.user' , type = 'report' )
164
+ IRReport = Pool () .get ('ir.action.report' )
165
165
166
166
self .setup_defaults ()
167
167
@@ -170,23 +170,23 @@ def test_0030_render_pdf(self):
170
170
'name' : 'HTML Report' ,
171
171
'model' : 'res.user' ,
172
172
'report_name' : 'res.user' ,
173
- 'report_content' : buffer (
174
- "<h1>Héllø, {{data['name']}}!</h1>"
173
+ 'report_content' : memoryview (
174
+ "<h1>Héllø, {{data['name']}}!</h1>" . encode ()
175
175
),
176
176
'extension' : 'pdf' ,
177
177
}])
178
178
179
179
# Set Pool.test as False as we need the report to be generated
180
180
# as PDF
181
181
Pool .test = False
182
- val = UserReport .execute ([USER ], {'name' : u 'Cédric' })
183
- self .assertEqual (val [0 ], u 'pdf' )
182
+ val = UserReport .execute ([USER ], {'name' : 'Cédric' })
183
+ self .assertEqual (val [0 ], 'pdf' )
184
184
185
185
# Revert Pool.test back to True for other tests to run normally
186
186
Pool .test = True
187
187
188
188
with tempfile .TemporaryFile () as file :
189
- file .write (str ( val [1 ]) )
189
+ file .write (val [1 ])
190
190
pdf = PdfFileReader (file )
191
191
192
192
# Probably the only thing you can check from a shitty PDF
0 commit comments