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.

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