|
|
@ -3,64 +3,248 @@ |
|
|
|
# Copyright 2016 Camptocamp SA |
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|
|
|
|
|
|
|
from odoo.tests.common import TransactionCase |
|
|
|
from odoo.tests import common |
|
|
|
from odoo.tools import test_reports |
|
|
|
|
|
|
|
import logging |
|
|
|
_logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
try: |
|
|
|
from xlrd import open_workbook |
|
|
|
except ImportError: |
|
|
|
_logger.debug('Can not import xlsxwriter`.') |
|
|
|
|
|
|
|
|
|
|
|
class AbstractTest(TransactionCase): |
|
|
|
class AbstractTest(common.TransactionCase): |
|
|
|
"""Common technical tests for all reports.""" |
|
|
|
at_install = False |
|
|
|
post_install = True |
|
|
|
|
|
|
|
accounts = {} |
|
|
|
|
|
|
|
def with_context(self, *args, **kwargs): |
|
|
|
context = dict(args[0] if args else self.env.context, **kwargs) |
|
|
|
self.env = self.env(context=context) |
|
|
|
return self |
|
|
|
|
|
|
|
def _chart_template_create(self): |
|
|
|
transfer_account_id = self.env['account.account.template'].create({ |
|
|
|
'code': '000', |
|
|
|
'name': 'Liquidity Transfers', |
|
|
|
'reconcile': True, |
|
|
|
'user_type_id': self.ref( |
|
|
|
"account.data_account_type_current_assets"), |
|
|
|
}) |
|
|
|
self.chart = self.env['account.chart.template'].create({ |
|
|
|
'name': 'Test COA', |
|
|
|
'code_digits': 4, |
|
|
|
'bank_account_code_prefix': 1014, |
|
|
|
'cash_account_code_prefix': 1014, |
|
|
|
'currency_id': self.ref('base.USD'), |
|
|
|
'transfer_account_id': transfer_account_id.id, |
|
|
|
}) |
|
|
|
transfer_account_id.update({ |
|
|
|
'chart_template_id': self.chart.id, |
|
|
|
}) |
|
|
|
self.env['ir.model.data'].create({ |
|
|
|
'res_id': transfer_account_id.id, |
|
|
|
'model': transfer_account_id._name, |
|
|
|
'name': 'Liquidity Transfers', |
|
|
|
}) |
|
|
|
act = self.env['account.account.template'].create({ |
|
|
|
'code': '001', |
|
|
|
'name': 'Expenses', |
|
|
|
'user_type_id': self.ref("account.data_account_type_expenses"), |
|
|
|
'chart_template_id': self.chart.id, |
|
|
|
'reconcile': True, |
|
|
|
}) |
|
|
|
self.env['ir.model.data'].create({ |
|
|
|
'res_id': act.id, |
|
|
|
'model': act._name, |
|
|
|
'name': 'expenses', |
|
|
|
}) |
|
|
|
act = self.env['account.account.template'].create({ |
|
|
|
'code': '002', |
|
|
|
'name': 'Product Sales', |
|
|
|
'user_type_id': self.ref("account.data_account_type_revenue"), |
|
|
|
'chart_template_id': self.chart.id, |
|
|
|
'reconcile': True, |
|
|
|
}) |
|
|
|
self.env['ir.model.data'].create({ |
|
|
|
'res_id': act.id, |
|
|
|
'model': act._name, |
|
|
|
'name': 'sales', |
|
|
|
}) |
|
|
|
act = self.env['account.account.template'].create({ |
|
|
|
'code': '003', |
|
|
|
'name': 'Account Receivable', |
|
|
|
'user_type_id': self.ref("account.data_account_type_receivable"), |
|
|
|
'chart_template_id': self.chart.id, |
|
|
|
'reconcile': True, |
|
|
|
}) |
|
|
|
self.env['ir.model.data'].create({ |
|
|
|
'res_id': act.id, |
|
|
|
'model': act._name, |
|
|
|
'name': 'receivable', |
|
|
|
}) |
|
|
|
act = self.env['account.account.template'].create({ |
|
|
|
'code': '004', |
|
|
|
'name': 'Account Payable', |
|
|
|
'user_type_id': self.ref("account.data_account_type_payable"), |
|
|
|
'chart_template_id': self.chart.id, |
|
|
|
'reconcile': True, |
|
|
|
}) |
|
|
|
self.env['ir.model.data'].create({ |
|
|
|
'res_id': act.id, |
|
|
|
'model': act._name, |
|
|
|
'name': 'payable', |
|
|
|
}) |
|
|
|
|
|
|
|
def setUp(cls): |
|
|
|
super(AbstractTest, cls).setUp() |
|
|
|
def _add_chart_of_accounts(self): |
|
|
|
self.company = self.env['res.company'].create({ |
|
|
|
'name': 'Spanish test company', |
|
|
|
'external_report_layout': 'standard', |
|
|
|
}) |
|
|
|
self.env.ref('base.group_multi_company').write({ |
|
|
|
'users': [(4, self.env.uid)], |
|
|
|
}) |
|
|
|
self.env.user.write({ |
|
|
|
'company_ids': [(4, self.company.id)], |
|
|
|
'company_id': self.company.id, |
|
|
|
}) |
|
|
|
self.with_context( |
|
|
|
company_id=self.company.id, force_company=self.company.id) |
|
|
|
wizard = self.env['wizard.multi.charts.accounts'].create({ |
|
|
|
'company_id': self.company.id, |
|
|
|
'chart_template_id': self.chart.id, |
|
|
|
'code_digits': 4, |
|
|
|
'currency_id': self.ref('base.USD'), |
|
|
|
'transfer_account_id': self.chart.transfer_account_id.id, |
|
|
|
}) |
|
|
|
wizard.onchange_chart_template_id() |
|
|
|
wizard.execute() |
|
|
|
self.revenue = self.env['account.account'].search( |
|
|
|
[('user_type_id', '=', self.ref( |
|
|
|
"account.data_account_type_revenue"))], limit=1) |
|
|
|
self.expense = self.env['account.account'].search( |
|
|
|
[('user_type_id', '=', self.ref( |
|
|
|
"account.data_account_type_expenses"))], limit=1) |
|
|
|
self.receivable = self.env['account.account'].search( |
|
|
|
[('user_type_id', '=', self.ref( |
|
|
|
"account.data_account_type_receivable"))], limit=1) |
|
|
|
self.payable = self.env['account.account'].search( |
|
|
|
[('user_type_id', '=', self.ref( |
|
|
|
"account.data_account_type_payable"))], limit=1) |
|
|
|
return True |
|
|
|
|
|
|
|
cls.model = cls._getReportModel() |
|
|
|
def _journals_create(self): |
|
|
|
self.journal_sale = self.env['account.journal'].create({ |
|
|
|
'company_id': self.company.id, |
|
|
|
'name': 'Test journal for sale', |
|
|
|
'type': 'sale', |
|
|
|
'code': 'TSALE', |
|
|
|
'default_debit_account_id': self.revenue.id, |
|
|
|
'default_credit_account_id': self.revenue.id, |
|
|
|
}) |
|
|
|
self.journal_purchase = self.env['account.journal'].create({ |
|
|
|
'company_id': self.company.id, |
|
|
|
'name': 'Test journal for purchase', |
|
|
|
'type': 'purchase', |
|
|
|
'code': 'TPUR', |
|
|
|
'default_debit_account_id': self.expense.id, |
|
|
|
'default_credit_account_id': self.expense.id, |
|
|
|
}) |
|
|
|
return True |
|
|
|
|
|
|
|
cls.qweb_report_name = cls._getQwebReportName() |
|
|
|
cls.xlsx_report_name = cls._getXlsxReportName() |
|
|
|
cls.xlsx_action_name = cls._getXlsxReportActionName() |
|
|
|
def _invoice_create(self): |
|
|
|
self.partner = self.env['res.partner'].create({ |
|
|
|
'name': 'Test partner', |
|
|
|
'company_id': self.company.id, |
|
|
|
'property_account_receivable_id': self.receivable.id, |
|
|
|
'property_account_payable_id': self.payable.id, |
|
|
|
}) |
|
|
|
|
|
|
|
cls.report_title = cls._getReportTitle() |
|
|
|
# customer invoice |
|
|
|
customer_invoice_lines = [(0, False, { |
|
|
|
'name': 'Test description #1', |
|
|
|
'account_id': self.revenue.id, |
|
|
|
'quantity': 1.0, |
|
|
|
'price_unit': 100.0, |
|
|
|
}), (0, False, { |
|
|
|
'name': 'Test description #2', |
|
|
|
'account_id': self.revenue.id, |
|
|
|
'quantity': 2.0, |
|
|
|
'price_unit': 25.0, |
|
|
|
})] |
|
|
|
self.invoice_out = self.env['account.invoice'].create({ |
|
|
|
'partner_id': self.partner.id, |
|
|
|
'type': 'out_invoice', |
|
|
|
'invoice_line_ids': customer_invoice_lines, |
|
|
|
'account_id': self.partner.property_account_receivable_id.id, |
|
|
|
'journal_id': self.journal_sale.id, |
|
|
|
}) |
|
|
|
self.invoice_out.action_invoice_open() |
|
|
|
|
|
|
|
# vendor bill |
|
|
|
vendor_invoice_lines = [(0, False, { |
|
|
|
'name': 'Test description #1', |
|
|
|
'account_id': self.revenue.id, |
|
|
|
'quantity': 1.0, |
|
|
|
'price_unit': 100.0, |
|
|
|
}), (0, False, { |
|
|
|
'name': 'Test description #2', |
|
|
|
'account_id': self.revenue.id, |
|
|
|
'quantity': 2.0, |
|
|
|
'price_unit': 25.0, |
|
|
|
})] |
|
|
|
self.invoice_in = self.env['account.invoice'].create({ |
|
|
|
'partner_id': self.partner.id, |
|
|
|
'type': 'in_invoice', |
|
|
|
'invoice_line_ids': vendor_invoice_lines, |
|
|
|
'account_id': self.partner.property_account_payable_id.id, |
|
|
|
'journal_id': self.journal_purchase.id, |
|
|
|
}) |
|
|
|
self.invoice_in.action_invoice_open() |
|
|
|
|
|
|
|
cls.base_filters = cls._getBaseFilters() |
|
|
|
cls.additional_filters = cls._getAdditionalFiltersToBeTested() |
|
|
|
def setUp(self): |
|
|
|
super(AbstractTest, self).setUp() |
|
|
|
|
|
|
|
cls.report = cls.model.create(cls.base_filters) |
|
|
|
cls.report.compute_data_for_report() |
|
|
|
self.with_context() |
|
|
|
self._chart_template_create() |
|
|
|
self._add_chart_of_accounts() |
|
|
|
self._journals_create() |
|
|
|
self._invoice_create() |
|
|
|
|
|
|
|
def test_01_generation_report_qweb(self): |
|
|
|
"""Check if report PDF/HTML is correctly generated""" |
|
|
|
self.model = self._getReportModel() |
|
|
|
|
|
|
|
# Check if returned report action is correct |
|
|
|
report_type = 'qweb-pdf' |
|
|
|
report_action = self.report.print_report(report_type) |
|
|
|
self.assertDictContainsSubset( |
|
|
|
{ |
|
|
|
'type': 'ir.actions.report', |
|
|
|
'report_name': self.qweb_report_name, |
|
|
|
'report_type': 'qweb-pdf', |
|
|
|
}, |
|
|
|
report_action |
|
|
|
) |
|
|
|
self.qweb_report_name = self._getQwebReportName() |
|
|
|
self.xlsx_report_name = self._getXlsxReportName() |
|
|
|
self.xlsx_action_name = self._getXlsxReportActionName() |
|
|
|
|
|
|
|
# Check if report template is correct |
|
|
|
report = self.env['ir.actions.report'].search( |
|
|
|
[('report_name', '=', self.qweb_report_name), |
|
|
|
('report_type', '=', report_type)], limit=1) |
|
|
|
self.assertEqual(report.report_type, 'qweb-pdf') |
|
|
|
self.report_title = self._getReportTitle() |
|
|
|
|
|
|
|
rep = report.render(self.report.ids, {}) |
|
|
|
self.base_filters = self._getBaseFilters() |
|
|
|
self.additional_filters = self._getAdditionalFiltersToBeTested() |
|
|
|
|
|
|
|
self.assertTrue(self.report_title.encode('utf8') in rep[0]) |
|
|
|
self.report = self.model.create(self.base_filters) |
|
|
|
self.report.compute_data_for_report() |
|
|
|
|
|
|
|
self.assertTrue( |
|
|
|
self.report.account_ids[0].name.encode('utf8') in rep[0] |
|
|
|
) |
|
|
|
def test_html(self): |
|
|
|
test_reports.try_report(self.env.cr, self.env.uid, |
|
|
|
self.qweb_report_name, |
|
|
|
[self.report.id], |
|
|
|
report_type='qweb-html') |
|
|
|
|
|
|
|
def test_qweb(self): |
|
|
|
test_reports.try_report(self.env.cr, self.env.uid, |
|
|
|
self.qweb_report_name, |
|
|
|
[self.report.id], |
|
|
|
report_type='qweb-pdf') |
|
|
|
|
|
|
|
def test_xlsx(self): |
|
|
|
test_reports.try_report(self.env.cr, self.env.uid, |
|
|
|
self.xlsx_report_name, |
|
|
|
[self.report.id], |
|
|
|
report_type='xlsx') |
|
|
|
|
|
|
|
def test_print(self): |
|
|
|
self.report.print_report('qweb') |
|
|
|
self.report.print_report('xlsx') |
|
|
|
|
|
|
|
def test_02_generation_report_html(self): |
|
|
|
"""Check if report HTML is correctly generated""" |
|
|
@ -90,29 +274,6 @@ class AbstractTest(TransactionCase): |
|
|
|
self.report.account_ids[0].name.encode('utf8') in rep[0] |
|
|
|
) |
|
|
|
|
|
|
|
def test_03_generation_report_xlsx(self): |
|
|
|
"""Check if report XLSX is correctly generated""" |
|
|
|
report_object = self.env['ir.actions.report'] |
|
|
|
# Check if returned report action is correct |
|
|
|
report_type = 'xlsx' |
|
|
|
report_action = self.report.print_report(report_type) |
|
|
|
self.assertDictContainsSubset( |
|
|
|
{ |
|
|
|
'type': 'ir.actions.report', |
|
|
|
'report_name': self.xlsx_report_name, |
|
|
|
'report_type': 'xlsx', |
|
|
|
}, |
|
|
|
report_action |
|
|
|
) |
|
|
|
report = report_object._get_report_from_name(self.xlsx_report_name) |
|
|
|
self.assertEqual(report.report_type, 'xlsx') |
|
|
|
report_xlsx = report.render(self.report.ids, {}) |
|
|
|
wb = open_workbook(file_contents=report_xlsx[0]) |
|
|
|
sheet = wb.sheet_by_index(0) |
|
|
|
class_name = 'report.%s' % report.report_name |
|
|
|
self.assertEqual(sheet.cell(0, 0).value, |
|
|
|
self.env[class_name]._get_report_name()) |
|
|
|
|
|
|
|
def test_04_compute_data(self): |
|
|
|
"""Check that the SQL queries work with all filters options""" |
|
|
|
|
|
|
|