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.

128 lines
5.1 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. import mock
  5. import os
  6. import pkg_resources
  7. import tempfile
  8. from py3o.formats import Formats
  9. from odoo.tests.common import TransactionCase
  10. from odoo.exceptions import ValidationError
  11. from ..models.py3o_report import TemplateNotFound
  12. from base64 import b64encode
  13. class TestReportPy3o(TransactionCase):
  14. def test_no_local_fusion_without_fusion_server(self):
  15. report = self.env.ref("report_py3o.res_users_report_py3o")
  16. self.assertTrue(report.py3o_is_local_fusion)
  17. with self.assertRaises(ValidationError) as e:
  18. report.py3o_is_local_fusion = False
  19. self.assertEqual(
  20. e.exception.name,
  21. "Can not use not native format in local fusion. "
  22. "Please specify a Fusion Server")
  23. def test_no_native_format_without_fusion_server(self):
  24. report = self.env.ref("report_py3o.res_users_report_py3o")
  25. formats = Formats()
  26. is_native = formats.get_format(report.py3o_filetype).native
  27. self.assertTrue(is_native)
  28. new_format = None
  29. for name in formats.get_known_format_names():
  30. format = formats.get_format(name)
  31. if not format.native:
  32. new_format = name
  33. break
  34. self.assertTrue(new_format)
  35. with self.assertRaises(ValidationError) as e:
  36. report.py3o_filetype = new_format
  37. self.assertEqual(
  38. e.exception.name,
  39. "Can not use not native format in local fusion. "
  40. "Please specify a Fusion Server")
  41. def test_required_py3_filetype(self):
  42. report = self.env.ref("report_py3o.res_users_report_py3o")
  43. self.assertEqual(report.report_type, "py3o")
  44. with self.assertRaises(ValidationError) as e:
  45. report.py3o_filetype = False
  46. self.assertEqual(
  47. e.exception.name,
  48. "Field 'Output Format' is required for Py3O report")
  49. def test_reports(self):
  50. py3o_report = self.env['py3o.report']
  51. report = self.env.ref("report_py3o.res_users_report_py3o")
  52. with mock.patch.object(
  53. py3o_report.__class__, '_create_single_report') as patched_pdf:
  54. result = tempfile.mktemp('.txt')
  55. with open(result, 'w') as fp:
  56. fp.write('dummy')
  57. patched_pdf.return_value = result
  58. # test the call the the create method inside our custom parser
  59. report.render_report(self.env.user.ids,
  60. report.report_name,
  61. {})
  62. self.assertEqual(1, patched_pdf.call_count)
  63. # generated files no more exists
  64. self.assertFalse(os.path.exists(result))
  65. res = report.render_report(
  66. self.env.user.ids, report.report_name, {})
  67. self.assertTrue(res)
  68. py3o_server = self.env['py3o.server'].create({"url": "http://dummy"})
  69. # check the call to the fusion server
  70. report.write({"py3o_filetype": "pdf",
  71. "py3o_server_id": py3o_server.id})
  72. with mock.patch('requests.post') as patched_post:
  73. magick_response = mock.MagicMock()
  74. magick_response.status_code = 200
  75. patched_post.return_value = magick_response
  76. magick_response.iter_content.return_value = "test result"
  77. res = report.render_report(
  78. self.env.user.ids, report.report_name, {})
  79. self.assertEqual(('test result', 'pdf'), res)
  80. def test_report_template_configs(self):
  81. report = self.env.ref("report_py3o.res_users_report_py3o")
  82. # the demo template is specified with a relative path in in the module
  83. # path
  84. tmpl_name = report.py3o_template_fallback
  85. flbk_filename = pkg_resources.resource_filename(
  86. "odoo.addons.%s" % report.module,
  87. tmpl_name)
  88. self.assertTrue(os.path.exists(flbk_filename))
  89. res = report.render_report(
  90. self.env.user.ids, report.report_name, {})
  91. self.assertTrue(res)
  92. # The generation fails if the tempalte is not found
  93. report.module = False
  94. with self.assertRaises(TemplateNotFound), self.env.cr.savepoint():
  95. report.render_report(
  96. self.env.user.ids, report.report_name, {})
  97. # the template can also be provided as an abspaath
  98. report.py3o_template_fallback = flbk_filename
  99. res = report.render_report(
  100. self.env.user.ids, report.report_name, {})
  101. self.assertTrue(res)
  102. # the tempalte can also be provided as a binay field
  103. report.py3o_template_fallback = False
  104. with open(flbk_filename) as tmpl_file:
  105. tmpl_data = b64encode(tmpl_file.read())
  106. py3o_template = self.env['py3o.template'].create({
  107. 'name': 'test_template',
  108. 'py3o_template_data': tmpl_data,
  109. 'filetype': 'odt'})
  110. report.py3o_template_id = py3o_template
  111. report.py3o_template_fallback = flbk_filename
  112. res = report.render_report(
  113. self.env.user.ids, report.report_name, {})
  114. self.assertTrue(res)