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.

121 lines
4.9 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 ..models.py3o_report 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. py3o_report = self.env['py3o.report']
  50. report = self.env.ref("report_py3o.res_users_report_py3o")
  51. with mock.patch.object(
  52. py3o_report.__class__, '_create_single_report') as patched_pdf:
  53. # test the call the the create method inside our custom parser
  54. report.render_report(self.env.user.ids,
  55. report.report_name,
  56. {})
  57. self.assertEqual(1, patched_pdf.call_count)
  58. res = report.render_report(
  59. self.env.user.ids, report.report_name, {})
  60. self.assertTrue(res)
  61. py3o_server = self.env['py3o.server'].create({"url": "http://dummy"})
  62. # check the call to the fusion server
  63. report.write({"py3o_filetype": "pdf",
  64. "py3o_server_id": py3o_server.id})
  65. with mock.patch('requests.post') as patched_post:
  66. magick_response = mock.MagicMock()
  67. magick_response.status_code = 200
  68. patched_post.return_value = magick_response
  69. magick_response.iter_content.return_value = "test result"
  70. res = report.render_report(
  71. self.env.user.ids, report.report_name, {})
  72. self.assertEqual(('test result', 'pdf'), res)
  73. def test_report_template_configs(self):
  74. report = self.env.ref("report_py3o.res_users_report_py3o")
  75. # the demo template is specified with a relative path in in the module
  76. # path
  77. tmpl_name = report.py3o_template_fallback
  78. flbk_filename = pkg_resources.resource_filename(
  79. "odoo.addons.%s" % report.module,
  80. tmpl_name)
  81. self.assertTrue(os.path.exists(flbk_filename))
  82. res = report.render_report(
  83. self.env.user.ids, report.report_name, {})
  84. self.assertTrue(res)
  85. # The generation fails if the tempalte is not found
  86. report.module = False
  87. with self.assertRaises(TemplateNotFound), self.env.cr.savepoint():
  88. report.render_report(
  89. self.env.user.ids, report.report_name, {})
  90. # the template can also be provided as an abspaath
  91. report.py3o_template_fallback = flbk_filename
  92. res = report.render_report(
  93. self.env.user.ids, report.report_name, {})
  94. self.assertTrue(res)
  95. # the tempalte can also be provided as a binay field
  96. report.py3o_template_fallback = False
  97. with open(flbk_filename) as tmpl_file:
  98. tmpl_data = b64encode(tmpl_file.read())
  99. py3o_template = self.env['py3o.template'].create({
  100. 'name': 'test_template',
  101. 'py3o_template_data': tmpl_data,
  102. 'filetype': 'odt'})
  103. report.py3o_template_id = py3o_template
  104. report.py3o_template_fallback = flbk_filename
  105. res = report.render_report(
  106. self.env.user.ids, report.report_name, {})
  107. self.assertTrue(res)