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.

246 lines
8.4 KiB

  1. # Author: Julien Coux
  2. # Copyright 2016 Camptocamp SA
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo.tests.common import TransactionCase
  5. import logging
  6. _logger = logging.getLogger(__name__)
  7. try:
  8. from xlrd import open_workbook
  9. except ImportError:
  10. _logger.debug('Can not import xlsxwriter`.')
  11. class AbstractTest(TransactionCase):
  12. """Common technical tests for all reports."""
  13. def setUp(cls):
  14. super(AbstractTest, cls).setUp()
  15. cls.model = cls._getReportModel()
  16. cls.qweb_report_name = cls._getQwebReportName()
  17. cls.xlsx_report_name = cls._getXlsxReportName()
  18. cls.xlsx_action_name = cls._getXlsxReportActionName()
  19. cls.report_title = cls._getReportTitle()
  20. cls.base_filters = cls._getBaseFilters()
  21. cls.additional_filters = cls._getAdditionalFiltersToBeTested()
  22. cls.report = cls.model.create(cls.base_filters)
  23. cls.report.compute_data_for_report()
  24. def test_01_generation_report_qweb(self):
  25. """Check if report PDF/HTML is correctly generated"""
  26. # Check if returned report action is correct
  27. report_type = 'qweb-pdf'
  28. report_action = self.report.print_report(report_type)
  29. self.assertDictContainsSubset(
  30. {
  31. 'type': 'ir.actions.report',
  32. 'report_name': self.qweb_report_name,
  33. 'report_type': 'qweb-pdf',
  34. },
  35. report_action
  36. )
  37. # Check if report template is correct
  38. report = self.env['ir.actions.report'].search(
  39. [('report_name', '=', self.qweb_report_name),
  40. ('report_type', '=', report_type)], limit=1)
  41. self.assertEqual(report.report_type, 'qweb-pdf')
  42. rep = report.render(self.report.ids, {})
  43. self.assertTrue(self.report_title.encode('utf8') in rep[0])
  44. self.assertTrue(
  45. self.report.account_ids[0].name.encode('utf8') in rep[0]
  46. )
  47. def test_02_generation_report_html(self):
  48. """Check if report HTML is correctly generated"""
  49. # Check if returned report action is correct
  50. report_type = 'qweb-html'
  51. report_action = self.report.print_report(report_type)
  52. self.assertDictContainsSubset(
  53. {
  54. 'type': 'ir.actions.report',
  55. 'report_name': self.qweb_report_name,
  56. 'report_type': 'qweb-html',
  57. },
  58. report_action
  59. )
  60. # Check if report template is correct
  61. report = self.env['ir.actions.report'].search(
  62. [('report_name', '=', self.qweb_report_name),
  63. ('report_type', '=', report_type)], limit=1)
  64. self.assertEqual(report.report_type, 'qweb-html')
  65. rep = report.render(self.report.ids, {})
  66. self.assertTrue(self.report_title.encode('utf8') in rep[0])
  67. self.assertTrue(
  68. self.report.account_ids[0].name.encode('utf8') in rep[0]
  69. )
  70. def test_03_generation_report_xlsx(self):
  71. """Check if report XLSX is correctly generated"""
  72. report_object = self.env['ir.actions.report']
  73. # Check if returned report action is correct
  74. report_type = 'xlsx'
  75. report_action = self.report.print_report(report_type)
  76. self.assertDictContainsSubset(
  77. {
  78. 'type': 'ir.actions.report',
  79. 'report_name': self.xlsx_report_name,
  80. 'report_type': 'xlsx',
  81. },
  82. report_action
  83. )
  84. report = report_object._get_report_from_name(self.xlsx_report_name)
  85. self.assertEqual(report.report_type, 'xlsx')
  86. report_xlsx = report.render(self.report.ids, {})
  87. wb = open_workbook(file_contents=report_xlsx[0])
  88. sheet = wb.sheet_by_index(0)
  89. class_name = 'report.%s' % report.report_name
  90. self.assertEqual(sheet.cell(0, 0).value,
  91. self.env[class_name]._get_report_name())
  92. def test_04_compute_data(self):
  93. """Check that the SQL queries work with all filters options"""
  94. for filters in [{}] + self.additional_filters:
  95. current_filter = self.base_filters.copy()
  96. current_filter.update(filters)
  97. report = self.model.create(current_filter)
  98. report.compute_data_for_report()
  99. self.assertGreaterEqual(len(report.account_ids), 1)
  100. # Same filters with only one account
  101. current_filter = self.base_filters.copy()
  102. current_filter.update(filters)
  103. current_filter.update({
  104. 'filter_account_ids':
  105. [(6, 0, report.account_ids[0].account_id.ids)],
  106. })
  107. report2 = self.model.create(current_filter)
  108. report2.compute_data_for_report()
  109. self.assertEqual(len(report2.account_ids), 1)
  110. self.assertEqual(report2.account_ids.name,
  111. report.account_ids[0].name)
  112. if self._partner_test_is_possible(filters):
  113. # Same filters with only one partner
  114. report_partner_ids = report.account_ids.mapped('partner_ids')
  115. partner_ids = report_partner_ids.mapped('partner_id')
  116. current_filter = self.base_filters.copy()
  117. current_filter.update(filters)
  118. current_filter.update({
  119. 'filter_partner_ids': [(6, 0, partner_ids[0].ids)],
  120. })
  121. report3 = self.model.create(current_filter)
  122. report3.compute_data_for_report()
  123. self.assertGreaterEqual(len(report3.account_ids), 1)
  124. report_partner_ids3 = report3.account_ids.mapped('partner_ids')
  125. partner_ids3 = report_partner_ids3.mapped('partner_id')
  126. self.assertEqual(len(partner_ids3), 1)
  127. self.assertEqual(
  128. partner_ids3.name,
  129. partner_ids[0].name
  130. )
  131. # Same filters with only one partner and one account
  132. report_partner_ids = report3.account_ids.mapped('partner_ids')
  133. report_account_id = report_partner_ids.filtered(
  134. lambda p: p.partner_id
  135. )[0].report_account_id
  136. current_filter = self.base_filters.copy()
  137. current_filter.update(filters)
  138. current_filter.update({
  139. 'filter_account_ids':
  140. [(6, 0, report_account_id.account_id.ids)],
  141. 'filter_partner_ids': [(6, 0, partner_ids[0].ids)],
  142. })
  143. report4 = self.model.create(current_filter)
  144. report4.compute_data_for_report()
  145. self.assertEqual(len(report4.account_ids), 1)
  146. self.assertEqual(report4.account_ids.name,
  147. report_account_id.account_id.name)
  148. report_partner_ids4 = report4.account_ids.mapped('partner_ids')
  149. partner_ids4 = report_partner_ids4.mapped('partner_id')
  150. self.assertEqual(len(partner_ids4), 1)
  151. self.assertEqual(
  152. partner_ids4.name,
  153. partner_ids[0].name
  154. )
  155. def _partner_test_is_possible(self, filters):
  156. """
  157. :return:
  158. a boolean to indicate if partner test is possible
  159. with current filters
  160. """
  161. return True
  162. def _getReportModel(self):
  163. """
  164. :return: the report model name
  165. """
  166. raise NotImplementedError()
  167. def _getQwebReportName(self):
  168. """
  169. :return: the qweb report name
  170. """
  171. raise NotImplementedError()
  172. def _getXlsxReportName(self):
  173. """
  174. :return: the xlsx report name
  175. """
  176. raise NotImplementedError()
  177. def _getXlsxReportActionName(self):
  178. """
  179. :return: the xlsx report action name
  180. """
  181. raise NotImplementedError()
  182. def _getReportTitle(self):
  183. """
  184. :return: the report title displayed into the report
  185. """
  186. raise NotImplementedError()
  187. def _getBaseFilters(self):
  188. """
  189. :return: the minimum required filters to generate report
  190. """
  191. raise NotImplementedError()
  192. def _getAdditionalFiltersToBeTested(self):
  193. """
  194. :return: the additional filters to generate report variants
  195. """
  196. raise NotImplementedError()