[IMP] sql_export_excel: black, isort, prettier

This commit is contained in:
hkapatel
2021-06-21 18:18:03 +05:30
committed by Maksym Yankin
parent 7cce5a0286
commit 81ba189c52
4 changed files with 114 additions and 83 deletions

View File

@@ -2,24 +2,24 @@
# @author: Florian da Costa
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests.common import TransactionCase
import base64
from io import BytesIO
import logging
from io import BytesIO
from odoo.tests.common import TransactionCase
_logger = logging.getLogger(__name__)
try:
import openpyxl
except ImportError:
_logger.debug('Can not import openpyxl')
_logger.debug("Can not import openpyxl")
class TestExportSqlQueryExcel(TransactionCase):
def setUp(self):
super().setUp()
self.wizard_obj = self.env['sql.file.wizard']
self.wizard_obj = self.env["sql.file.wizard"]
def get_workbook_from_query(self, wizard):
wizard.export_sql()
@@ -30,27 +30,29 @@ class TestExportSqlQueryExcel(TransactionCase):
def test_excel_file_generation(self):
test_query = "SELECT 'testcol1' as firstcol, 2 as second_col"
query_vals = {
'name': 'Test Query Excel',
'query': test_query,
'file_format': 'excel'
"name": "Test Query Excel",
"query": test_query,
"file_format": "excel",
}
query = self.env['sql.export'].create(query_vals)
query = self.env["sql.export"].create(query_vals)
query.button_validate_sql_expression()
wizard = self.wizard_obj.create({
'sql_export_id': query.id,
})
wizard = self.wizard_obj.create(
{
"sql_export_id": query.id,
}
)
workbook = self.get_workbook_from_query(wizard)
ws = workbook.active
# Check values, header should be here by default
self.assertEqual(ws.cell(row=1, column=1).value, 'firstcol')
self.assertEqual(ws.cell(row=2, column=1).value, 'testcol1')
self.assertEqual(ws.cell(row=1, column=1).value, "firstcol")
self.assertEqual(ws.cell(row=2, column=1).value, "testcol1")
self.assertEqual(ws.cell(row=2, column=2).value, 2)
query.write({'header': False})
query.write({"header": False})
wb2 = self.get_workbook_from_query(wizard)
ws2 = wb2.active
# Check values, the header should not be present
self.assertEqual(ws2.cell(row=1, column=1).value, 'testcol1')
self.assertEqual(ws2.cell(row=1, column=1).value, "testcol1")
self.assertEqual(ws2.cell(row=1, column=2).value, 2)
def test_excel_file_insert(self):
@@ -60,8 +62,8 @@ class TestExportSqlQueryExcel(TransactionCase):
ws = wb.active
ws.cell(row=1, column=1, value="My Test Value")
ws2 = wb.create_sheet("data")
ws2.cell(row=1, column=1, value='Partner Id')
ws2.cell(row=1, column=2, value='Partner Name')
ws2.cell(row=1, column=1, value="Partner Id")
ws2.cell(row=1, column=2, value="Partner Name")
output = BytesIO()
wb.save(output)
data = output.getvalue()
@@ -69,10 +71,10 @@ class TestExportSqlQueryExcel(TransactionCase):
# Create attachment with the created xlsx file which will be used as
# template in the sql query
attachmnent_vals = {
'name': 'template xlsx sql export Res Partner',
'datas': base64.b64encode(data),
"name": "template xlsx sql export Res Partner",
"datas": base64.b64encode(data),
}
attachment = self.env['ir.attachment'].create(attachmnent_vals)
attachment = self.env["ir.attachment"].create(attachmnent_vals)
# Create the query and configure it to insert the data in the second
# sheet of the xlsx template file and start inserting data at the
@@ -80,19 +82,21 @@ class TestExportSqlQueryExcel(TransactionCase):
# already contains a header)
test_query = "SELECT id, name FROM res_partner"
query_vals = {
'name': 'Test Query Excel',
'query': test_query,
'file_format': 'excel',
'attachment_id': attachment.id,
'sheet_position': 2,
'header': False,
'row_position': 2,
"name": "Test Query Excel",
"query": test_query,
"file_format": "excel",
"attachment_id": attachment.id,
"sheet_position": 2,
"header": False,
"row_position": 2,
}
query = self.env['sql.export'].create(query_vals)
query = self.env["sql.export"].create(query_vals)
query.button_validate_sql_expression()
wizard = self.wizard_obj.create({
'sql_export_id': query.id,
})
wizard = self.wizard_obj.create(
{
"sql_export_id": query.id,
}
)
# Check the generated excel file. The first sheet should still contain
# the same data and the second sheet should have kept the header and
@@ -101,7 +105,7 @@ class TestExportSqlQueryExcel(TransactionCase):
sheets = wb2.worksheets
ws1 = sheets[0]
# Check values, header should be here by default
self.assertEqual(ws1.cell(row=1, column=1).value, 'My Test Value')
self.assertEqual(ws1.cell(row=1, column=1).value, "My Test Value")
ws2 = sheets[1]
self.assertEqual(ws2.cell(row=1, column=1).value, 'Partner Id')
self.assertEqual(ws2.cell(row=1, column=1).value, "Partner Id")
self.assertTrue(ws2.cell(row=2, column=1).value)