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.

204 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.assertTrue(self.report_title.encode('utf8') in report_html)
  35. self.assertTrue(
  36. self.report.account_ids[0].name.encode('utf8') in report_html
  37. )
  38. def test_02_generation_report_xlsx(self):
  39. """Check if report XLSX is correctly generated"""
  40. # Check if returned report action is correct
  41. report_action = self.report.print_report(xlsx_report=True)
  42. self.assertDictContainsSubset(
  43. {
  44. 'type': 'ir.actions.report.xml',
  45. 'report_name': self.xlsx_report_name,
  46. 'report_type': 'xlsx',
  47. },
  48. report_action
  49. )
  50. # Check if report template is correct
  51. report_xlsx = self.env.ref(self.xlsx_action_name).render_report(
  52. self.report.ids,
  53. self.xlsx_report_name,
  54. {'report_type': 'xlsx'}
  55. )
  56. self.assertGreaterEqual(len(report_xlsx[0]), 1)
  57. self.assertEqual(report_xlsx[1], 'xlsx')
  58. def test_03_compute_data(self):
  59. """Check that the SQL queries work with all filters options"""
  60. for filters in [{}] + self.additional_filters:
  61. current_filter = self.base_filters.copy()
  62. current_filter.update(filters)
  63. report = self.model.create(current_filter)
  64. report.compute_data_for_report()
  65. self.assertGreaterEqual(len(report.account_ids), 1)
  66. # Same filters with only one account
  67. current_filter = self.base_filters.copy()
  68. current_filter.update(filters)
  69. current_filter.update({
  70. 'filter_account_ids':
  71. [(6, 0, report.account_ids[0].account_id.ids)],
  72. })
  73. report2 = self.model.create(current_filter)
  74. report2.compute_data_for_report()
  75. self.assertEqual(len(report2.account_ids), 1)
  76. self.assertEqual(report2.account_ids.name,
  77. report.account_ids[0].name)
  78. if self._partner_test_is_possible(filters):
  79. # Same filters with only one partner
  80. report_partner_ids = report.account_ids.mapped('partner_ids')
  81. partner_ids = report_partner_ids.mapped('partner_id')
  82. current_filter = self.base_filters.copy()
  83. current_filter.update(filters)
  84. current_filter.update({
  85. 'filter_partner_ids': [(6, 0, partner_ids[0].ids)],
  86. })
  87. report3 = self.model.create(current_filter)
  88. report3.compute_data_for_report()
  89. self.assertGreaterEqual(len(report3.account_ids), 1)
  90. report_partner_ids3 = report3.account_ids.mapped('partner_ids')
  91. partner_ids3 = report_partner_ids3.mapped('partner_id')
  92. self.assertEqual(len(partner_ids3), 1)
  93. self.assertEqual(
  94. partner_ids3.name,
  95. partner_ids[0].name
  96. )
  97. # Same filters with only one partner and one account
  98. report_partner_ids = report3.account_ids.mapped('partner_ids')
  99. report_account_id = report_partner_ids.filtered(
  100. lambda p: p.partner_id
  101. )[0].report_account_id
  102. current_filter = self.base_filters.copy()
  103. current_filter.update(filters)
  104. current_filter.update({
  105. 'filter_account_ids':
  106. [(6, 0, report_account_id.account_id.ids)],
  107. 'filter_partner_ids': [(6, 0, partner_ids[0].ids)],
  108. })
  109. report4 = self.model.create(current_filter)
  110. report4.compute_data_for_report()
  111. self.assertEqual(len(report4.account_ids), 1)
  112. self.assertEqual(report4.account_ids.name,
  113. report_account_id.account_id.name)
  114. report_partner_ids4 = report4.account_ids.mapped('partner_ids')
  115. partner_ids4 = report_partner_ids4.mapped('partner_id')
  116. self.assertEqual(len(partner_ids4), 1)
  117. self.assertEqual(
  118. partner_ids4.name,
  119. partner_ids[0].name
  120. )
  121. def _partner_test_is_possible(self, filters):
  122. """
  123. :return:
  124. a boolean to indicate if partner test is possible
  125. with current filters
  126. """
  127. return True
  128. def _getReportModel(self):
  129. """
  130. :return: the report model name
  131. """
  132. raise NotImplementedError()
  133. def _getQwebReportName(self):
  134. """
  135. :return: the qweb report name
  136. """
  137. raise NotImplementedError()
  138. def _getXlsxReportName(self):
  139. """
  140. :return: the xlsx report name
  141. """
  142. raise NotImplementedError()
  143. def _getXlsxReportActionName(self):
  144. """
  145. :return: the xlsx report action name
  146. """
  147. raise NotImplementedError()
  148. def _getReportTitle(self):
  149. """
  150. :return: the report title displayed into the report
  151. """
  152. raise NotImplementedError()
  153. def _getBaseFilters(self):
  154. """
  155. :return: the minimum required filters to generate report
  156. """
  157. raise NotImplementedError()
  158. def _getAdditionalFiltersToBeTested(self):
  159. """
  160. :return: the additional filters to generate report variants
  161. """
  162. raise NotImplementedError()