Files
reporting-engine/report_xlsx/tests/test_report.py
josep-tecnativa 554eeae329 [IMP] report_xlsx: test performance improvement
- Switch to setUpClass for avoiding repeat the same setup for each test.
- Include context keys for avoiding mail operations overhead.
2023-10-24 09:43:54 +02:00

79 lines
2.7 KiB
Python

# Copyright 2017 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import logging
from odoo.tests import common
_logger = logging.getLogger(__name__)
try:
from xlrd import open_workbook
except ImportError:
_logger.debug("Can not import xlrd`.")
class TestReport(common.TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# Remove this variable in v16 and put instead:
# from odoo.addons.base.tests.common import DISABLED_MAIL_CONTEXT
DISABLED_MAIL_CONTEXT = {
"tracking_disable": True,
"mail_create_nolog": True,
"mail_create_nosubscribe": True,
"mail_notrack": True,
"no_reset_password": True,
}
cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT))
report_object = cls.env["ir.actions.report"]
cls.xlsx_report = cls.env["report.report_xlsx.abstract"].with_context(
active_model="res.partner"
)
cls.report_name = "report_xlsx.partner_xlsx"
cls.report = report_object._get_report_from_name(cls.report_name)
cls.docs = cls.env["res.company"].search([], limit=1).partner_id
def test_report(self):
report = self.report
self.assertEqual(report.report_type, "xlsx")
rep = report._render(self.docs.ids, {})
wb = open_workbook(file_contents=rep[0])
sheet = wb.sheet_by_index(0)
self.assertEqual(sheet.cell(0, 0).value, self.docs.name)
def test_id_retrieval(self):
# Typical call from WebUI with wizard
objs = self.xlsx_report._get_objs_for_report(
False, {"context": {"active_ids": self.docs.ids}}
)
self.assertEqual(objs, self.docs)
# Typical call from within code not to report_action
objs = self.xlsx_report.with_context(
active_ids=self.docs.ids
)._get_objs_for_report(False, False)
self.assertEqual(objs, self.docs)
# Typical call from WebUI
objs = self.xlsx_report._get_objs_for_report(
self.docs.ids, {"data": [self.report_name, self.report.report_type]}
)
self.assertEqual(objs, self.docs)
# Typical call from render
objs = self.xlsx_report._get_objs_for_report(self.docs.ids, {})
self.assertEqual(objs, self.docs)
def test_currency_format(self):
usd = self.env.ref("base.USD")
self.assertEqual(
self.xlsx_report._report_xlsx_currency_format(usd), "$#,##0.00"
)
eur = self.env.ref("base.EUR")
self.assertEqual(
self.xlsx_report._report_xlsx_currency_format(eur), "#,##0.00 €"
)