/
test_report.py
128 lines (101 loc) · 3.71 KB
/
test_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
:copyright: (c) 2013 by Openlabs Technologies & Consulting (P) Ltd.
:license: GPLv3, see LICENSE for more details
'''
import unittest
import tempfile
from pyPdf import PdfFileReader
import trytond.tests.test_tryton
from trytond.transaction import Transaction
from trytond.tests.test_tryton import POOL, USER, DB_NAME, CONTEXT
from trytond.pool import Pool
from openlabs_report_webkit import ReportWebkit
def register():
"""
Register this report to a module
"""
class UserReport(ReportWebkit):
__name__ = 'res.user'
Pool.register(
UserReport,
module='res', type_='report')
class ReportTestCase(unittest.TestCase):
"""
Test the webkit reporting system
"""
def setUp(self):
register()
trytond.tests.test_tryton.install_module('res')
def test_0010_render_report_xhtml(self):
'''
Render the report without PDF conversion
'''
UserReport = POOL.get('res.user', type='report')
IRReport = POOL.get('ir.action.report')
with Transaction().start(DB_NAME, USER, context=CONTEXT):
IRReport.create({
'name': 'HTML Report',
'model': 'res.user',
'report_name': 'res.user',
'report_content': buffer('<h1>Hello, ${records[0].name}!</h1>'),
'extension': 'html',
})
val = UserReport.execute([USER], {})
self.assertEqual(val[0], u'html')
self.assertEqual(
str(val[1]), '<h1>Hello, Administrator!</h1>'
)
def test_0020_render_unicode(self):
'''
Render the report without PDF conversion but having unicode template
'''
UserReport = POOL.get('res.user', type='report')
IRReport = POOL.get('ir.action.report')
with Transaction().start(DB_NAME, USER, context=CONTEXT):
IRReport.create({
'name': 'HTML Report',
'model': 'res.user',
'report_name': 'res.user',
'report_content': buffer("<h1>Héllø, ${data['name']}!</h1>"),
'extension': 'html',
})
val = UserReport.execute([USER], {'name': u'Cédric'})
self.assertEqual(val[0], u'html')
self.assertEqual(
str(val[1]), '<h1>Héllø, Cédric!</h1>'
)
def test_0030_render_pdf(self):
'''
Render the report in PDF
'''
UserReport = POOL.get('res.user', type='report')
IRReport = POOL.get('ir.action.report')
with Transaction().start(DB_NAME, USER, context=CONTEXT):
IRReport.create({
'name': 'HTML Report',
'model': 'res.user',
'report_name': 'res.user',
'report_content': buffer("<h1>Héllø, ${data['name']}!</h1>"),
'extension': 'pdf',
})
val = UserReport.execute([USER], {'name': u'Cédric'})
self.assertEqual(val[0], u'pdf')
with tempfile.TemporaryFile() as file:
file.write(str(val[1]))
pdf = PdfFileReader(file)
# Probably the only thing you can check from a shitty PDF
# format. God save Adobe and its god forsaken format.
#
# PDF IS EVIL
self.assertEqual(pdf.getNumPages(), 1)
def suite():
func = unittest.TestLoader().loadTestsFromTestCase
suite = unittest.TestSuite()
for testcase in (ReportTestCase,):
suite.addTests(func(testcase))
return suite
if __name__ == '__main__':
suite = suite()
unittest.TextTestRunner(verbosity=2).run(suite)