[FIX] base_report_to_printer: Fixed tests

This commit is contained in:
Carlos Roca
2021-01-12 10:51:52 +01:00
committed by John Herholz
parent 145d231449
commit c01450311d
10 changed files with 44 additions and 33 deletions

View File

@@ -100,7 +100,7 @@ class IrActionsReport(models.Model):
""" Print a document, do not return the document file """ """ Print a document, do not return the document file """
document, doc_format = self.with_context( document, doc_format = self.with_context(
must_skip_send_to_printer=True must_skip_send_to_printer=True
).render_qweb_pdf(record_ids, data=data) )._render_qweb_pdf(record_ids, data=data)
behaviour = self.behaviour() behaviour = self.behaviour()
printer = behaviour.pop("printer", None) printer = behaviour.pop("printer", None)
@@ -129,13 +129,13 @@ class IrActionsReport(models.Model):
res["id"] = self.id res["id"] = self.id
return res return res
def render_qweb_pdf(self, res_ids=None, data=None): def _render_qweb_pdf(self, res_ids=None, data=None):
"""Generate a PDF and returns it. """Generate a PDF and returns it.
If the action configured on the report is server, it prints the If the action configured on the report is server, it prints the
generated document as well. generated document as well.
""" """
document, doc_format = super().render_qweb_pdf(res_ids=res_ids, data=data) document, doc_format = super()._render_qweb_pdf(res_ids=res_ids, data=data)
behaviour = self.behaviour() behaviour = self.behaviour()
printer = behaviour.pop("printer", None) printer = behaviour.pop("printer", None)

View File

@@ -61,7 +61,7 @@ class PrintingServer(models.Model):
servers = self.search(domain) servers = self.search(domain)
res = True res = True
for server in servers: for server in servers.with_context(active_test=False):
connection = server._open_connection(raise_on_error=raise_on_error) connection = server._open_connection(raise_on_error=raise_on_error)
if not connection: if not connection:
server.printer_ids.write({"status": "server-error"}) server.printer_ids.write({"status": "server-error"})
@@ -71,8 +71,7 @@ class PrintingServer(models.Model):
# Update Printers # Update Printers
printers = connection.getPrinters() printers = connection.getPrinters()
existing_printers = { existing_printers = {
printer.system_name: printer printer.system_name: printer for printer in server.printer_ids
for printer in server.with_context(active_test=False).printer_ids
} }
updated_printers = [] updated_printers = []
for name, printer_info in printers.items(): for name, printer_info in printers.items():

View File

@@ -2,10 +2,12 @@
# 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).
import mock from unittest import mock
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase
model = "odoo.addons.base.models.ir_actions_report.IrActionsReport"
class TestIrActionsReportXml(TransactionCase): class TestIrActionsReportXml(TransactionCase):
def setUp(self): def setUp(self):
@@ -53,14 +55,14 @@ class TestIrActionsReportXml(TransactionCase):
def test_print_action_for_report_name_gets_report(self): def test_print_action_for_report_name_gets_report(self):
""" It should get report by name """ """ It should get report by name """
with mock.patch.object(self.Model, "_get_report_from_name") as mk: with mock.patch("%s._get_report_from_name" % model) as mk:
expect = "test" expect = "test"
self.Model.print_action_for_report_name(expect) self.Model.print_action_for_report_name(expect)
mk.assert_called_once_with(expect) mk.assert_called_once_with(expect)
def test_print_action_for_report_name_returns_if_no_report(self): def test_print_action_for_report_name_returns_if_no_report(self):
""" It should return empty dict when no matching report """ """ It should return empty dict when no matching report """
with mock.patch.object(self.Model, "_get_report_from_name") as mk: with mock.patch("%s._get_report_from_name" % model) as mk:
expect = "test" expect = "test"
mk.return_value = False mk.return_value = False
res = self.Model.print_action_for_report_name(expect) res = self.Model.print_action_for_report_name(expect)
@@ -68,7 +70,7 @@ class TestIrActionsReportXml(TransactionCase):
def test_print_action_for_report_name_returns_if_report(self): def test_print_action_for_report_name_returns_if_report(self):
""" It should return correct serializable result for behaviour """ """ It should return correct serializable result for behaviour """
with mock.patch.object(self.Model, "_get_report_from_name") as mk: with mock.patch("%s._get_report_from_name" % model) as mk:
res = self.Model.print_action_for_report_name("test") res = self.Model.print_action_for_report_name("test")
behaviour = mk().behaviour() behaviour = mk().behaviour()
expect = { expect = {

View File

@@ -1,7 +1,7 @@
# 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).
import mock from unittest import mock
from odoo import fields from odoo import fields
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase

View File

@@ -2,8 +2,7 @@
# 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 tempfile import tempfile
from unittest import mock
import mock
from odoo.exceptions import UserError from odoo.exceptions import UserError
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase

View File

@@ -3,8 +3,7 @@
import errno import errno
import tempfile import tempfile
from unittest import mock
import mock
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase

View File

@@ -1,7 +1,7 @@
# 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).
import mock from unittest import mock
from odoo.exceptions import UserError from odoo.exceptions import UserError
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase
@@ -15,7 +15,7 @@ class StopTest(Exception):
class TestPrintingPrinterWizard(TransactionCase): class TestPrintingPrinterWizard(TransactionCase):
def setUp(self): def setUp(self):
super(TestPrintingPrinterWizard, self).setUp() super().setUp()
self.Model = self.env["printing.printer.update.wizard"] self.Model = self.env["printing.printer.update.wizard"]
self.server = self.env["printing.server"].create({}) self.server = self.env["printing.server"].create({})
self.printer_vals = { self.printer_vals = {

View File

@@ -1,12 +1,13 @@
# 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).
import mock from unittest import mock
from odoo import fields from odoo import fields
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase
model = "odoo.addons.base_report_to_printer.models.printing_server" model = "odoo.addons.base_report_to_printer.models.printing_server"
model_base = "odoo.models.BaseModel"
class TestPrintingServer(TransactionCase): class TestPrintingServer(TransactionCase):
@@ -69,14 +70,14 @@ class TestPrintingServer(TransactionCase):
@mock.patch("%s.cups" % model) @mock.patch("%s.cups" % model)
def test_update_printers_search(self, cups): def test_update_printers_search(self, cups):
""" It should search all when no domain """ """ It should search all when no domain """
with mock.patch.object(self.Model, "search") as search: with mock.patch("%s.search" % model_base) as search:
self.Model.update_printers() self.Model.update_printers()
search.assert_called_once_with([]) search.assert_called_once_with([])
@mock.patch("%s.cups" % model) @mock.patch("%s.cups" % model)
def test_update_printers_search_domain(self, cups): def test_update_printers_search_domain(self, cups):
""" It should use specific domain for search """ """ It should use specific domain for search """
with mock.patch.object(self.Model, "search") as search: with mock.patch("%s.search" % model_base) as search:
expect = [("id", ">", 0)] expect = [("id", ">", 0)]
self.Model.update_printers(expect) self.Model.update_printers(expect)
search.assert_called_once_with(expect) search.assert_called_once_with(expect)
@@ -89,6 +90,19 @@ class TestPrintingServer(TransactionCase):
self.Model.action_update_printers() self.Model.action_update_printers()
self.assertEqual("unavailable", rec_id.status) self.assertEqual("unavailable", rec_id.status)
@mock.patch("%s.cups" % model)
def test_update_archived_printers(self, cups):
""" It should update status even if printer is archived """
rec_id = self.new_printer()
rec_id.toggle_active()
self.server.refresh()
cups.Connection().getPrinters().get.return_value = False
self.Model.action_update_printers()
self.assertEqual(
"unavailable",
rec_id.status,
)
@mock.patch("%s.cups" % model) @mock.patch("%s.cups" % model)
def test_update_jobs_cron(self, cups): def test_update_jobs_cron(self, cups):
""" It should get all jobs from CUPS server """ """ It should get all jobs from CUPS server """

View File

@@ -2,14 +2,12 @@
# Copyright 2017 Tecnativa - Jairo Llopis # 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 from unittest import mock
from odoo import exceptions from odoo import exceptions
from odoo.tests import common from odoo.tests import common
@common.at_install(False)
@common.post_install(True)
class TestReport(common.HttpCase): class TestReport(common.HttpCase):
def setUp(self): def setUp(self):
super(TestReport, self).setUp() super(TestReport, self).setUp()
@@ -20,21 +18,23 @@ class TestReport(common.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( self.report_view = self.env["ir.ui.view"].create(
{ {
"name": "Test", "name": "Test",
"type": "qweb", "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"> "arch": """<t t-name="base_report_to_printer.test">
<div>Test</div> <div>Test</div>
</t>""", </t>""",
} }
) )
self.report_imd.res_id = self.report_view.id self.report_imd = self.env["ir.model.data"].create(
{
"name": "test",
"module": "base_report_to_printer",
"model": "ir.ui.view",
"res_id": self.report_view.id,
}
)
self.report = self.Model.create( self.report = self.Model.create(
{ {
"name": "Test", "name": "Test",
@@ -88,7 +88,7 @@ class TestReport(common.HttpCase):
"printing_printer.PrintingPrinter." "printing_printer.PrintingPrinter."
"print_document" "print_document"
) as print_document: ) as print_document:
self.report.render_qweb_pdf(self.partners.ids) self.report._render_qweb_pdf(self.partners.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):
@@ -100,7 +100,7 @@ class TestReport(common.HttpCase):
) as print_document: ) as print_document:
self.report.property_printing_action_id.action_type = "server" self.report.property_printing_action_id.action_type = "server"
self.report.printing_printer_id = self.new_printer() self.report.printing_printer_id = self.new_printer()
document = self.report.render_qweb_pdf(self.partners.ids) document = self.report._render_qweb_pdf(self.partners.ids)
print_document.assert_called_once_with( print_document.assert_called_once_with(
self.report, self.report,
document[0], document[0],

View File

@@ -4,8 +4,6 @@
from odoo.tests import common from odoo.tests import common
@common.at_install(False)
@common.post_install(True)
class TestResUsers(common.TransactionCase): class TestResUsers(common.TransactionCase):
def setUp(self): def setUp(self):
super(TestResUsers, self).setUp() super(TestResUsers, self).setUp()