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.

107 lines
4.0 KiB

  1. # Copyright (C) 2019 Akretion (<http://www.akretion.com>)
  2. # @author: Florian da Costa
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo.tests.common import TransactionCase
  5. import base64
  6. from io import BytesIO
  7. import logging
  8. _logger = logging.getLogger(__name__)
  9. try:
  10. import openpyxl
  11. except ImportError:
  12. _logger.debug('Can not import openpyxl')
  13. class TestExportSqlQueryExcel(TransactionCase):
  14. def setUp(self):
  15. super().setUp()
  16. self.wizard_obj = self.env['sql.file.wizard']
  17. def get_workbook_from_query(self, wizard):
  18. wizard.export_sql()
  19. decoded_data = base64.b64decode(wizard.binary_file)
  20. xlsx_file = BytesIO(decoded_data)
  21. return openpyxl.load_workbook(xlsx_file)
  22. def test_excel_file_generation(self):
  23. test_query = "SELECT 'testcol1' as firstcol, 2 as second_col"
  24. query_vals = {
  25. 'name': 'Test Query Excel',
  26. 'query': test_query,
  27. 'file_format': 'excel'
  28. }
  29. query = self.env['sql.export'].create(query_vals)
  30. query.button_validate_sql_expression()
  31. wizard = self.wizard_obj.create({
  32. 'sql_export_id': query.id,
  33. })
  34. workbook = self.get_workbook_from_query(wizard)
  35. ws = workbook.active
  36. # Check values, header should be here by default
  37. self.assertEqual(ws.cell(row=1, column=1).value, 'firstcol')
  38. self.assertEqual(ws.cell(row=2, column=1).value, 'testcol1')
  39. self.assertEqual(ws.cell(row=2, column=2).value, 2)
  40. query.write({'header': False})
  41. wb2 = self.get_workbook_from_query(wizard)
  42. ws2 = wb2.active
  43. # Check values, the header should not be present
  44. self.assertEqual(ws2.cell(row=1, column=1).value, 'testcol1')
  45. self.assertEqual(ws2.cell(row=1, column=2).value, 2)
  46. def test_excel_file_insert(self):
  47. # Create excel file with 2 sheets. Create a header in second sheet
  48. # where data will be inserted
  49. wb = openpyxl.Workbook()
  50. ws = wb.active
  51. ws.cell(row=1, column=1, value="My Test Value")
  52. ws2 = wb.create_sheet("data")
  53. ws2.cell(row=1, column=1, value='Partner Id')
  54. ws2.cell(row=1, column=2, value='Partner Name')
  55. output = BytesIO()
  56. wb.save(output)
  57. data = output.getvalue()
  58. # Create attachment with the created xlsx file which will be used as
  59. # template in the sql query
  60. attachmnent_vals = {
  61. 'name': 'template xlsx sql export Res Partner',
  62. 'datas': base64.b64encode(data),
  63. }
  64. attachment = self.env['ir.attachment'].create(attachmnent_vals)
  65. # Create the query and configure it to insert the data in the second
  66. # sheet of the xlsx template file and start inserting data at the
  67. # second row, ignoring header (because the template excel file
  68. # already contains a header)
  69. test_query = "SELECT id, name FROM res_partner"
  70. query_vals = {
  71. 'name': 'Test Query Excel',
  72. 'query': test_query,
  73. 'file_format': 'excel',
  74. 'attachment_id': attachment.id,
  75. 'sheet_position': 2,
  76. 'header': False,
  77. 'row_position': 2,
  78. }
  79. query = self.env['sql.export'].create(query_vals)
  80. query.button_validate_sql_expression()
  81. wizard = self.wizard_obj.create({
  82. 'sql_export_id': query.id,
  83. })
  84. # Check the generated excel file. The first sheet should still contain
  85. # the same data and the second sheet should have kept the header and
  86. # inserted data from the query
  87. wb2 = self.get_workbook_from_query(wizard)
  88. sheets = wb2.worksheets
  89. ws1 = sheets[0]
  90. # Check values, header should be here by default
  91. self.assertEqual(ws1.cell(row=1, column=1).value, 'My Test Value')
  92. ws2 = sheets[1]
  93. self.assertEqual(ws2.cell(row=1, column=1).value, 'Partner Id')
  94. self.assertTrue(ws2.cell(row=2, column=1).value)