[FIX] base_report_to_printer: Stateless tests (#123)

If the test was run in a database with the `account` module installed, they were failing for 2 reasons:

- It was searching for the first report it could find, with the first 5 records to report. This made it load the `account.report_agedpartnerbalance` report, which caused a fake `TypeError: 'NoneType' object has no attribute '__getitem__'` error.
- It was running tests without loading the full module graph, thus not getting the default value for the new required `invoice_warn` field.

Now tests are run in `post_install` mode to load full module graph, and they use stateless data.
This commit is contained in:
Jairo Llopis
2018-01-19 17:22:37 +00:00
committed by Carlos Roca
parent bb0b8c0edb
commit cf9fb7986b
9 changed files with 53 additions and 54 deletions

View File

@@ -105,6 +105,7 @@ Contributors
* Guewen Baconnier <guewen.baconnier@camptocamp.com> * Guewen Baconnier <guewen.baconnier@camptocamp.com>
* Dave Lasley <dave@laslabs.com> * Dave Lasley <dave@laslabs.com>
* Sylvain Garancher <sylvain.garancher@syleam.fr> * Sylvain Garancher <sylvain.garancher@syleam.fr>
* Jairo Llopis <jairo.llopis@tecnativa.com>
Maintainer Maintainer
---------- ----------

View File

@@ -8,8 +8,8 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Odoo Server 11.0\n" "Project-Id-Version: Odoo Server 11.0\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-12-19 03:38+0000\n" "POT-Creation-Date: 2018-01-19 17:25+0000\n"
"PO-Revision-Date: 2017-12-19 03:38+0000\n" "PO-Revision-Date: 2018-01-19 17:25+0000\n"
"Last-Translator: OCA Transbot <transbot@odoo-community.org>, 2017\n" "Last-Translator: OCA Transbot <transbot@odoo-community.org>, 2017\n"
"Language-Team: French (https://www.transifex.com/oca/teams/23907/fr/)\n" "Language-Team: French (https://www.transifex.com/oca/teams/23907/fr/)\n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
@@ -414,7 +414,7 @@ msgid "No data has been received"
msgstr "" msgstr ""
#. module: base_report_to_printer #. module: base_report_to_printer
#: code:addons/base_report_to_printer/models/ir_actions_report.py:113 #: code:addons/base_report_to_printer/models/ir_actions_report.py:112
#, python-format #, python-format
msgid "No printer configured to print this report." msgid "No printer configured to print this report."
msgstr "Pas d'imprimante configurée pour imprimer ce rapport." msgstr "Pas d'imprimante configurée pour imprimer ce rapport."

View File

@@ -8,8 +8,8 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Odoo Server 11.0\n" "Project-Id-Version: Odoo Server 11.0\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-12-19 03:38+0000\n" "POT-Creation-Date: 2018-01-19 17:25+0000\n"
"PO-Revision-Date: 2017-12-19 03:38+0000\n" "PO-Revision-Date: 2018-01-19 17:25+0000\n"
"Last-Translator: OCA Transbot <transbot@odoo-community.org>, 2017\n" "Last-Translator: OCA Transbot <transbot@odoo-community.org>, 2017\n"
"Language-Team: Croatian (https://www.transifex.com/oca/teams/23907/hr/)\n" "Language-Team: Croatian (https://www.transifex.com/oca/teams/23907/hr/)\n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
@@ -414,7 +414,7 @@ msgid "No data has been received"
msgstr "Nema zaprimljenih podataka" msgstr "Nema zaprimljenih podataka"
#. module: base_report_to_printer #. module: base_report_to_printer
#: code:addons/base_report_to_printer/models/ir_actions_report.py:113 #: code:addons/base_report_to_printer/models/ir_actions_report.py:112
#, python-format #, python-format
msgid "No printer configured to print this report." msgid "No printer configured to print this report."
msgstr "Nijedan pisač nije postavljen za ispis ovog izvještaja." msgstr "Nijedan pisač nije postavljen za ispis ovog izvještaja."

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2007 Ferran Pegueroles <ferran@pegueroles.com> # Copyright (c) 2007 Ferran Pegueroles <ferran@pegueroles.com>
# Copyright (c) 2009 Albert Cervera i Areny <albert@nan-tic.com> # Copyright (c) 2009 Albert Cervera i Areny <albert@nan-tic.com>
# Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>) # Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>)
@@ -130,7 +129,6 @@ class IrActionsReport(models.Model):
return True return True
return False return False
@api.model
def render_qweb_pdf(self, docids, data=None): def render_qweb_pdf(self, docids, data=None):
""" Generate a PDF and returns it. """ Generate a PDF and returns it.

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2007 Ferran Pegueroles <ferran@pegueroles.com> # Copyright (c) 2007 Ferran Pegueroles <ferran@pegueroles.com>
# Copyright (c) 2009 Albert Cervera i Areny <albert@nan-tic.com> # Copyright (c) 2009 Albert Cervera i Areny <albert@nan-tic.com>
# Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>) # Copyright (C) 2011 Agile Business Group sagl (<http://www.agilebg.com>)

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2016 LasLabs Inc. # Copyright 2016 LasLabs Inc.
# Copyright 2016 SYLEAM # Copyright 2016 SYLEAM
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2016 LasLabs Inc. # Copyright 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

View File

@@ -1,18 +1,16 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2016 LasLabs Inc. # Copyright 2016 LasLabs Inc.
# Copyright 2017 Tecnativa - Jairo Llopis
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import mock import mock
from odoo.tests.common import HttpCase from odoo.tests import common
from odoo import exceptions from odoo import exceptions
class StopTest(Exception): @common.at_install(False)
pass @common.post_install(True)
class TestReport(common.HttpCase):
class TestReport(HttpCase):
def setUp(self): def setUp(self):
super(TestReport, self).setUp() super(TestReport, self).setUp()
self.Model = self.env['ir.actions.report'] self.Model = self.env['ir.actions.report']
@@ -22,6 +20,32 @@ class TestReport(HttpCase):
'model': 'ir.actions.report', 'model': 'ir.actions.report',
'report_name': 'Test Report', 'report_name': 'Test Report',
} }
self.report_imd = self.env["ir.model.data"].create({
"name": "test",
"module": "base_report_to_printer",
"model": "ir.ui.view",
})
self.report_view = self.env["ir.ui.view"].create({
"name": "Test",
"type": "qweb",
"xml_id": "base_report_to_printer.test",
"model_data_id": self.report_imd.id,
"arch": """<t t-name="base_report_to_printer.test">
<div>Test</div>
</t>""",
})
self.report_imd.res_id = self.report_view.id
self.report = self.Model.create({
"name": "Test",
"report_type": "qweb-pdf",
"model": "res.partner",
"report_name": "base_report_to_printer.test",
})
self.partners = self.env["res.partner"]
for n in range(5):
self.partners += self.env["res.partner"].create({
"name": "Test %d" % n,
})
def new_record(self): def new_record(self):
return self.Model.create(self.report_vals) return self.Model.create(self.report_vals)
@@ -69,11 +93,7 @@ class TestReport(HttpCase):
with mock.patch('odoo.addons.base_report_to_printer.models.' with mock.patch('odoo.addons.base_report_to_printer.models.'
'printing_printer.PrintingPrinter.' 'printing_printer.PrintingPrinter.'
'print_document') as print_document: 'print_document') as print_document:
report = self.env['ir.actions.report'].search([ self.report.render_qweb_pdf(self.partners.ids)
('report_type', '=', 'qweb-pdf'),
], limit=1)
records = self.env[report.model].search([], limit=5)
report.render_qweb_pdf(records.ids)
print_document.assert_not_called() print_document.assert_not_called()
def test_render_qweb_pdf_printable(self): def test_render_qweb_pdf_printable(self):
@@ -82,52 +102,33 @@ class TestReport(HttpCase):
with mock.patch('odoo.addons.base_report_to_printer.models.' with mock.patch('odoo.addons.base_report_to_printer.models.'
'printing_printer.PrintingPrinter.' 'printing_printer.PrintingPrinter.'
'print_document') as print_document: 'print_document') as print_document:
report = self.env['ir.actions.report'].search([ self.report.property_printing_action_id.action_type = 'server'
('report_type', '=', 'qweb-pdf'), self.report.printing_printer_id = self.new_printer()
], limit=1) document = self.report.render_qweb_pdf(self.partners.ids)
report.property_printing_action_id.action_type = 'server'
report.printing_printer_id = self.new_printer()
records = self.env[report.model].search([], limit=5)
document, doc_format = report.render_qweb_pdf(records.ids)
print_document.assert_called_once_with( print_document.assert_called_once_with(
report, document, self.report, document[0],
action='server', doc_format='qweb-pdf', tray=False) action='server', doc_format='qweb-pdf', tray=False)
def test_print_document_not_printable(self): def test_print_document_not_printable(self):
""" It should print the report, regardless of the defined behaviour """ """ It should print the report, regardless of the defined behaviour """
report = self.env['ir.actions.report'].search([ self.report.printing_printer_id = self.new_printer()
('report_type', '=', 'qweb-pdf'),
], limit=1)
report.printing_printer_id = self.new_printer()
records = self.env[report.model].search([], limit=5)
with mock.patch('odoo.addons.base_report_to_printer.models.' with mock.patch('odoo.addons.base_report_to_printer.models.'
'printing_printer.PrintingPrinter.' 'printing_printer.PrintingPrinter.'
'print_document') as print_document: 'print_document') as print_document:
report.print_document(records.ids) self.report.print_document(self.partners.ids)
print_document.assert_called_once() print_document.assert_called_once()
def test_print_document_printable(self): def test_print_document_printable(self):
""" It should print the report, regardless of the defined behaviour """ """ It should print the report, regardless of the defined behaviour """
report = self.env['ir.actions.report'].search([ self.report.property_printing_action_id.action_type = 'server'
('report_type', '=', 'qweb-pdf'), self.report.printing_printer_id = self.new_printer()
], limit=1)
report.property_printing_action_id.action_type = 'server'
report.printing_printer_id = self.new_printer()
records = self.env[report.model].search([], limit=5)
with mock.patch('odoo.addons.base_report_to_printer.models.' with mock.patch('odoo.addons.base_report_to_printer.models.'
'printing_printer.PrintingPrinter.' 'printing_printer.PrintingPrinter.'
'print_document') as print_document: 'print_document') as print_document:
report.print_document(records.ids) self.report.print_document(self.partners.ids)
print_document.assert_called_once() print_document.assert_called_once()
def test_print_document_no_printer(self): def test_print_document_no_printer(self):
""" It should raise an error """ """ It should raise an error """
report = self.env['ir.actions.report'].search([
('report_type', '=', 'qweb-pdf'),
], limit=1)
records = self.env[report.model].search([], limit=5)
with self.assertRaises(exceptions.UserError): with self.assertRaises(exceptions.UserError):
report.print_document(records.ids) self.report.print_document(self.partners.ids)

View File

@@ -2,10 +2,12 @@
# Copyright 2016 LasLabs Inc. # Copyright 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests.common import TransactionCase from odoo.tests import common
class TestResUsers(TransactionCase): @common.at_install(False)
@common.post_install(True)
class TestResUsers(common.TransactionCase):
def setUp(self): def setUp(self):
super(TestResUsers, self).setUp() super(TestResUsers, self).setUp()