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.

202 lines
6.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Author: Julien Coux
  3. # Copyright 2016 Camptocamp SA
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp.tests.common import TransactionCase
  6. class AbstractTest(TransactionCase):
  7. """Common technical tests for all reports."""
  8. def setUp(cls):
  9. super(AbstractTest, cls).setUp()
  10. cls.model = cls._getReportModel()
  11. cls.qweb_report_name = cls._getQwebReportName()
  12. cls.xlsx_report_name = cls._getXlsxReportName()
  13. cls.xlsx_action_name = cls._getXlsxReportActionName()
  14. cls.report_title = cls._getReportTitle()
  15. cls.base_filters = cls._getBaseFilters()
  16. cls.additional_filters = cls._getAdditionalFiltersToBeTested()
  17. cls.report = cls.model.create(cls.base_filters)
  18. def test_01_generation_report_qweb(self):
  19. """Check if report PDF/HTML is correctly generated"""
  20. # Check if returned report action is correct
  21. report_action = self.report.print_report()
  22. self.assertDictContainsSubset(
  23. {
  24. 'type': 'ir.actions.report.xml',
  25. 'report_name': self.qweb_report_name,
  26. 'report_type': 'qweb-pdf',
  27. },
  28. report_action
  29. )
  30. # Check if report template is correct
  31. report_html = self.env['report'].get_html(
  32. self.report, self.qweb_report_name
  33. )
  34. self.assertRegexpMatches(report_html, self.report_title)
  35. self.assertRegexpMatches(report_html, self.report.account_ids[0].name)
  36. def test_02_generation_report_xlsx(self):
  37. """Check if report XLSX is correctly generated"""
  38. # Check if returned report action is correct
  39. report_action = self.report.print_report(xlsx_report=True)
  40. self.assertDictContainsSubset(
  41. {
  42. 'type': 'ir.actions.report.xml',
  43. 'report_name': self.xlsx_report_name,
  44. 'report_type': 'xlsx',
  45. },
  46. report_action
  47. )
  48. # Check if report template is correct
  49. report_xlsx = self.env.ref(self.xlsx_action_name).render_report(
  50. self.report.ids,
  51. self.xlsx_report_name,
  52. {'report_type': 'xlsx'}
  53. )
  54. self.assertGreaterEqual(len(report_xlsx[0]), 1)
  55. self.assertEqual(report_xlsx[1], 'xlsx')
  56. def test_03_compute_data(self):
  57. """Check that the SQL queries work with all filters options"""
  58. for filters in [{}] + self.additional_filters:
  59. current_filter = self.base_filters.copy()
  60. current_filter.update(filters)
  61. report = self.model.create(current_filter)
  62. report.compute_data_for_report()
  63. self.assertGreaterEqual(len(report.account_ids), 1)
  64. # Same filters with only one account
  65. current_filter = self.base_filters.copy()
  66. current_filter.update(filters)
  67. current_filter.update({
  68. 'filter_account_ids':
  69. [(6, 0, report.account_ids[0].account_id.ids)],
  70. })
  71. report2 = self.model.create(current_filter)
  72. report2.compute_data_for_report()
  73. self.assertEqual(len(report2.account_ids), 1)
  74. self.assertEqual(report2.account_ids.name,
  75. report.account_ids[0].name)
  76. if self._partner_test_is_possible(filters):
  77. # Same filters with only one partner
  78. report_partner_ids = report.account_ids.mapped('partner_ids')
  79. partner_ids = report_partner_ids.mapped('partner_id')
  80. current_filter = self.base_filters.copy()
  81. current_filter.update(filters)
  82. current_filter.update({
  83. 'filter_partner_ids': [(6, 0, partner_ids[0].ids)],
  84. })
  85. report3 = self.model.create(current_filter)
  86. report3.compute_data_for_report()
  87. self.assertGreaterEqual(len(report3.account_ids), 1)
  88. report_partner_ids3 = report3.account_ids.mapped('partner_ids')
  89. partner_ids3 = report_partner_ids3.mapped('partner_id')
  90. self.assertEqual(len(partner_ids3), 1)
  91. self.assertEqual(
  92. partner_ids3.name,
  93. partner_ids[0].name
  94. )
  95. # Same filters with only one partner and one account
  96. report_partner_ids = report3.account_ids.mapped('partner_ids')
  97. report_account_id = report_partner_ids.filtered(
  98. lambda p: p.partner_id
  99. )[0].report_account_id
  100. current_filter = self.base_filters.copy()
  101. current_filter.update(filters)
  102. current_filter.update({
  103. 'filter_account_ids':
  104. [(6, 0, report_account_id.account_id.ids)],
  105. 'filter_partner_ids': [(6, 0, partner_ids[0].ids)],
  106. })
  107. report4 = self.model.create(current_filter)
  108. report4.compute_data_for_report()
  109. self.assertEqual(len(report4.account_ids), 1)
  110. self.assertEqual(report4.account_ids.name,
  111. report_account_id.account_id.name)
  112. report_partner_ids4 = report4.account_ids.mapped('partner_ids')
  113. partner_ids4 = report_partner_ids4.mapped('partner_id')
  114. self.assertEqual(len(partner_ids4), 1)
  115. self.assertEqual(
  116. partner_ids4.name,
  117. partner_ids[0].name
  118. )
  119. def _partner_test_is_possible(self, filters):
  120. """
  121. :return:
  122. a boolean to indicate if partner test is possible
  123. with current filters
  124. """
  125. return True
  126. def _getReportModel(self):
  127. """
  128. :return: the report model name
  129. """
  130. raise NotImplementedError()
  131. def _getQwebReportName(self):
  132. """
  133. :return: the qweb report name
  134. """
  135. raise NotImplementedError()
  136. def _getXlsxReportName(self):
  137. """
  138. :return: the xlsx report name
  139. """
  140. raise NotImplementedError()
  141. def _getXlsxReportActionName(self):
  142. """
  143. :return: the xlsx report action name
  144. """
  145. raise NotImplementedError()
  146. def _getReportTitle(self):
  147. """
  148. :return: the report title displayed into the report
  149. """
  150. raise NotImplementedError()
  151. def _getBaseFilters(self):
  152. """
  153. :return: the minimum required filters to generate report
  154. """
  155. raise NotImplementedError()
  156. def _getAdditionalFiltersToBeTested(self):
  157. """
  158. :return: the additional filters to generate report variants
  159. """
  160. raise NotImplementedError()