OCA reporting engine fork for dev and update.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
6.6 KiB

8 years ago
8 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 ACSONE SA/NV
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).).
  4. from base64 import b64decode
  5. import mock
  6. import os
  7. import pkg_resources
  8. import tempfile
  9. from py3o.formats import Formats
  10. from odoo.tests.common import TransactionCase
  11. from odoo.exceptions import ValidationError
  12. from ..models.py3o_report import TemplateNotFound
  13. from base64 import b64encode
  14. class TestReportPy3o(TransactionCase):
  15. def test_no_local_fusion_without_fusion_server(self):
  16. report = self.env.ref("report_py3o.res_users_report_py3o")
  17. self.assertTrue(report.py3o_is_local_fusion)
  18. with self.assertRaises(ValidationError) as e:
  19. report.py3o_is_local_fusion = False
  20. self.assertEqual(
  21. e.exception.name,
  22. "Can not use not native format in local fusion. "
  23. "Please specify a Fusion Server")
  24. def test_no_native_format_without_fusion_server(self):
  25. report = self.env.ref("report_py3o.res_users_report_py3o")
  26. formats = Formats()
  27. is_native = formats.get_format(report.py3o_filetype).native
  28. self.assertTrue(is_native)
  29. new_format = None
  30. for name in formats.get_known_format_names():
  31. format = formats.get_format(name)
  32. if not format.native:
  33. new_format = name
  34. break
  35. self.assertTrue(new_format)
  36. with self.assertRaises(ValidationError) as e:
  37. report.py3o_filetype = new_format
  38. self.assertEqual(
  39. e.exception.name,
  40. "Can not use not native format in local fusion. "
  41. "Please specify a Fusion Server")
  42. def test_required_py3_filetype(self):
  43. report = self.env.ref("report_py3o.res_users_report_py3o")
  44. self.assertEqual(report.report_type, "py3o")
  45. with self.assertRaises(ValidationError) as e:
  46. report.py3o_filetype = False
  47. self.assertEqual(
  48. e.exception.name,
  49. "Field 'Output Format' is required for Py3O report")
  50. def test_reports(self):
  51. py3o_report = self.env['py3o.report']
  52. report = self.env.ref("report_py3o.res_users_report_py3o")
  53. with mock.patch.object(
  54. py3o_report.__class__, '_create_single_report') as patched_pdf:
  55. result = tempfile.mktemp('.txt')
  56. with open(result, 'w') as fp:
  57. fp.write('dummy')
  58. patched_pdf.return_value = result
  59. # test the call the the create method inside our custom parser
  60. report.render_report(self.env.user.ids,
  61. report.report_name,
  62. {})
  63. self.assertEqual(1, patched_pdf.call_count)
  64. # generated files no more exists
  65. self.assertFalse(os.path.exists(result))
  66. res = report.render_report(
  67. self.env.user.ids, report.report_name, {})
  68. self.assertTrue(res)
  69. py3o_server = self.env['py3o.server'].create({"url": "http://dummy"})
  70. # check the call to the fusion server
  71. report.write({"py3o_filetype": "pdf",
  72. "py3o_server_id": py3o_server.id})
  73. with mock.patch('requests.post') as patched_post:
  74. magick_response = mock.MagicMock()
  75. magick_response.status_code = 200
  76. patched_post.return_value = magick_response
  77. magick_response.iter_content.return_value = "test result"
  78. res = report.render_report(
  79. self.env.user.ids, report.report_name, {})
  80. self.assertEqual(('test result', 'pdf'), res)
  81. def test_report_post_process(self):
  82. """
  83. By default the post_process method is in charge to save the
  84. generated report into an ir.attachment if requested.
  85. """
  86. report = self.env.ref("report_py3o.res_users_report_py3o")
  87. report.attachment = "object.name + '.txt'"
  88. py3o_server = self.env['py3o.server'].create({"url": "http://dummy"})
  89. # check the call to the fusion server
  90. report.write({"py3o_filetype": "pdf",
  91. "py3o_server_id": py3o_server.id})
  92. ir_attachment = self.env['ir.attachment']
  93. attachements = ir_attachment.search([(1, '=', 1)])
  94. with mock.patch('requests.post') as patched_post:
  95. magick_response = mock.MagicMock()
  96. magick_response.status_code = 200
  97. patched_post.return_value = magick_response
  98. magick_response.iter_content.return_value = "test result"
  99. res = report.render_report(
  100. self.env.user.ids, report.report_name, {})
  101. self.assertEqual(('test result', 'pdf'), res)
  102. attachements = ir_attachment.search([(1, '=', 1)]) - attachements
  103. self.assertEqual(1, len(attachements.ids))
  104. self.assertEqual(self.env.user.name + '.txt', attachements.name)
  105. self.assertEqual(self.env.user._name, attachements.res_model)
  106. self.assertEqual(self.env.user.id, attachements.res_id)
  107. self.assertEqual('test result', b64decode(attachements.datas))
  108. def test_report_template_configs(self):
  109. report = self.env.ref("report_py3o.res_users_report_py3o")
  110. # the demo template is specified with a relative path in in the module
  111. # path
  112. tmpl_name = report.py3o_template_fallback
  113. flbk_filename = pkg_resources.resource_filename(
  114. "odoo.addons.%s" % report.module,
  115. tmpl_name)
  116. self.assertTrue(os.path.exists(flbk_filename))
  117. res = report.render_report(
  118. self.env.user.ids, report.report_name, {})
  119. self.assertTrue(res)
  120. # The generation fails if the tempalte is not found
  121. report.module = False
  122. with self.assertRaises(TemplateNotFound), self.env.cr.savepoint():
  123. report.render_report(
  124. self.env.user.ids, report.report_name, {})
  125. # the template can also be provided as an abspaath
  126. report.py3o_template_fallback = flbk_filename
  127. res = report.render_report(
  128. self.env.user.ids, report.report_name, {})
  129. self.assertTrue(res)
  130. # the tempalte can also be provided as a binay field
  131. report.py3o_template_fallback = False
  132. with open(flbk_filename) as tmpl_file:
  133. tmpl_data = b64encode(tmpl_file.read())
  134. py3o_template = self.env['py3o.template'].create({
  135. 'name': 'test_template',
  136. 'py3o_template_data': tmpl_data,
  137. 'filetype': 'odt'})
  138. report.py3o_template_id = py3o_template
  139. report.py3o_template_fallback = flbk_filename
  140. res = report.render_report(
  141. self.env.user.ids, report.report_name, {})
  142. self.assertTrue(res)