From de0828ac6434d6be686ce655e96be63871a4139a Mon Sep 17 00:00:00 2001 From: Luc De Meyer Date: Sun, 12 Aug 2018 15:17:52 +0200 Subject: [PATCH] add unit test --- report_xlsx_helper/tests/__init__.py | 3 + .../tests/test_partner_report_xlsx.py | 100 ++++++++++++++++++ .../tests/test_report_xlsx_helper.py | 22 ++++ 3 files changed, 125 insertions(+) create mode 100644 report_xlsx_helper/tests/__init__.py create mode 100644 report_xlsx_helper/tests/test_partner_report_xlsx.py create mode 100644 report_xlsx_helper/tests/test_report_xlsx_helper.py diff --git a/report_xlsx_helper/tests/__init__.py b/report_xlsx_helper/tests/__init__.py new file mode 100644 index 00000000..798b9f69 --- /dev/null +++ b/report_xlsx_helper/tests/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- +from . import test_partner_report_xlsx +from . import test_report_xlsx_helper diff --git a/report_xlsx_helper/tests/test_partner_report_xlsx.py b/report_xlsx_helper/tests/test_partner_report_xlsx.py new file mode 100644 index 00000000..4623e4d9 --- /dev/null +++ b/report_xlsx_helper/tests/test_partner_report_xlsx.py @@ -0,0 +1,100 @@ +# -*- coding: utf-8 -*- +# Copyright 2009-2018 Noviat. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.addons.report_xlsx_helper.report.abstract_report_xlsx \ + import AbstractReportXlsx +from odoo.report import report_sxw + + +class TestPartnerReportXlsx(AbstractReportXlsx): + + def _get_ws_params(self, wb, data, partners): + + partner_template = { + 'name': { + 'header': { + 'value': 'Name', + }, + 'data': { + 'value': self._render("partner.name"), + }, + 'width': 20, + }, + 'number_of_contacts': { + 'header': { + 'value': '# Contacts', + }, + 'data': { + 'value': self._render("len(partner.child_ids)"), + }, + 'width': 10, + }, + 'is_customer': { + 'header': { + 'value': 'Customer', + }, + 'data': { + 'value': self._render("partner.customer"), + }, + 'width': 10, + }, + 'is_customer_formula': { + 'header': { + 'value': 'Customer Y/N ?', + }, + 'data': { + 'type': 'formula', + 'value': self._render("customer_formula"), + }, + 'width': 10, + }, + } + + ws_params = { + 'ws_name': 'Partners', + 'generate_ws_method': '_partner_report', + 'title': 'Partners', + 'wanted_list': [k for k in partner_template], + 'col_specs': partner_template, + } + + return [ws_params] + + def _partner_report(self, workbook, ws, ws_params, data, partners): + + ws.set_portrait() + ws.fit_to_pages(1, 0) + ws.set_header(self.xls_headers['standard']) + ws.set_footer(self.xls_footers['standard']) + + self._set_column_width(ws, ws_params) + + row_pos = 0 + row_pos = self._write_ws_title(ws, row_pos, ws_params) + row_pos = self._write_line( + ws, row_pos, ws_params, col_specs_section='header', + default_format=self.format_theader_yellow_left) + ws.freeze_panes(row_pos, 0) + + wl = ws_params['wanted_list'] + + for partner in partners: + is_customer_pos = 'is_customer' in wl and \ + wl.index('is_customer') + is_customer_cell = self._rowcol_to_cell( + row_pos, is_customer_pos) + customer_formula = 'IF(%s=TRUE;"Y"; "N")' % is_customer_cell + row_pos = self._write_line( + ws, row_pos, ws_params, col_specs_section='data', + render_space={ + 'partner': partner, + 'customer_formula': customer_formula, + }, + default_format=self.format_tcell_left) + + +TestPartnerReportXlsx( + 'report.test.partner.xlsx', + 'res.partner', + parser=report_sxw.rml_parse) diff --git a/report_xlsx_helper/tests/test_report_xlsx_helper.py b/report_xlsx_helper/tests/test_report_xlsx_helper.py new file mode 100644 index 00000000..e5b140a7 --- /dev/null +++ b/report_xlsx_helper/tests/test_report_xlsx_helper.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Copyright 2009-2018 Noviat. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.tests.common import TransactionCase + + +class TestReportXlsxHelper(TransactionCase): + + def setUp(self): + super(TestReportXlsxHelper, self).setUp() + ctx = {'xlsx_export': True} + self.report = self.env['ir.actions.report.xml'].with_context(ctx) + self.report_name = 'test.partner.xlsx' + p1 = self.env.ref('base.res_partner_1') + p2 = self.env.ref('base.res_partner_2') + self.partners = p1 + p2 + + def test_report_xlsx_helper(self): + report_xls = self.report.render_report( + self.partners.ids, self.report_name, {}) + self.assertEqual(report_xls[1], 'xlsx')