diff --git a/base_report_to_printer/tests/test_printing_job.py b/base_report_to_printer/tests/test_printing_job.py index 2e2a69d24f0..35ba786dc79 100644 --- a/base_report_to_printer/tests/test_printing_job.py +++ b/base_report_to_printer/tests/test_printing_job.py @@ -1,6 +1,7 @@ # Copyright 2016 LasLabs Inc. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import logging from unittest import mock from odoo import fields @@ -42,15 +43,21 @@ def new_job(self, printer, vals=None): values["printer_id"] = printer.id return self.env["printing.job"].create(values) - @mock.patch(f"{model}.cups") - def test_cancel_job_error(self, cups): + def test_cancel_job_error(self): """It should catch any exception from CUPS and update status""" - cups.Connection.side_effect = Exception - printer = self.new_printer() - job = self.new_job(printer, {"job_id_cups": 2}) - job.action_cancel() - cups.Connection.side_effect = None - self.assertEqual(cups.Connection().cancelJob.call_count, 0) + with ( + mock.patch(f"{model}.cups") as cups, + self.assertLogs(level=logging.WARNING) as logs, + ): + cups.Connection.side_effect = Exception + printer = self.new_printer() + job = self.new_job(printer, {"job_id_cups": 2}) + job.action_cancel() + cups.Connection.side_effect = None + self.assertEqual(cups.Connection().cancelJob.call_count, 0) + + self.assertEqual(len(logs.records), 3) + self.assertEqual(logs.records[0].levelno, logging.WARNING) @mock.patch(f"{model}.cups") def test_cancel_job(self, cups): diff --git a/base_report_to_printer/tests/test_printing_printer.py b/base_report_to_printer/tests/test_printing_printer.py index 62453d5a335..ddd3d7ee97c 100644 --- a/base_report_to_printer/tests/test_printing_printer.py +++ b/base_report_to_printer/tests/test_printing_printer.py @@ -1,6 +1,7 @@ # Copyright 2016 LasLabs Inc. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import logging import tempfile from unittest import mock @@ -91,37 +92,52 @@ def test_print_report(self, cups): printer.system_name, file_name, file_name, options={} ) - @mock.patch(f"{server_model}.cups") - def test_print_report_error(self, cups): + def test_print_report_error(self): """It should print a report through CUPS""" - cups.Connection.side_effect = Exception - fd, file_name = tempfile.mkstemp() - with mock.patch(f"{model}.mkstemp") as mkstemp: - mkstemp.return_value = fd, file_name - printer = self.new_record() - with self.assertRaises(UserError): - printer.print_document( - self.report, b"content to print", doc_format="pdf" - ) - - @mock.patch(f"{server_model}.cups") - def test_print_file(self, cups): + with ( + mock.patch(f"{model}.cups") as cups, + self.assertLogs(level=logging.WARNING) as logs, + ): + cups.Connection.side_effect = Exception + fd, file_name = tempfile.mkstemp() + with mock.patch(f"{model}.mkstemp") as mkstemp: + mkstemp.return_value = fd, file_name + printer = self.new_record() + with self.assertRaises(UserError): + printer.print_document( + self.report, b"content to print", doc_format="pdf" + ) + self.assertEqual(len(logs.records), 1) + self.assertEqual(logs.records[0].levelno, logging.WARNING) + + def test_print_file(self): """It should print a file through CUPS""" - file_name = "file_name" - printer = self.new_record() - printer.print_file(file_name, "pdf") - cups.Connection().printFile.assert_called_once_with( - printer.system_name, file_name, file_name, options={} - ) + with ( + mock.patch(f"{server_model}.cups") as cups, + self.assertLogs(level=logging.WARNING) as logs, + ): + file_name = "file_name" + printer = self.new_record() + printer.print_file(file_name, "pdf") + cups.Connection().printFile.assert_called_once_with( + printer.system_name, file_name, file_name, options={} + ) + self.assertEqual(len(logs.records), 1) + self.assertEqual(logs.records[0].levelno, logging.WARNING) - @mock.patch(f"{server_model}.cups") - def test_print_file_error(self, cups): + def test_print_file_error(self): """It should print a file through CUPS""" - cups.Connection.side_effect = Exception - file_name = "file_name" - printer = self.new_record() - with self.assertRaises(UserError): - printer.print_file(file_name) + with ( + mock.patch(f"{server_model}.cups") as cups, + self.assertLogs(level=logging.WARNING) as logs, + ): + cups.Connection.side_effect = Exception + file_name = "file_name" + printer = self.new_record() + with self.assertRaises(UserError): + printer.print_file(file_name) + self.assertEqual(len(logs.records), 1) + self.assertEqual(logs.records[0].levelno, logging.WARNING) def test_set_default(self): """It should set a single record as default""" diff --git a/base_report_to_printer/tests/test_printing_printer_wizard.py b/base_report_to_printer/tests/test_printing_printer_wizard.py index 7a4190b6d38..f378d9b58dc 100644 --- a/base_report_to_printer/tests/test_printing_printer_wizard.py +++ b/base_report_to_printer/tests/test_printing_printer_wizard.py @@ -1,6 +1,7 @@ # Copyright 2016 LasLabs Inc. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import logging from unittest import mock from odoo.exceptions import UserError @@ -52,12 +53,17 @@ def test_action_ok_gets_printers(self, cups): self.Model.action_ok() cups.Connection().getPrinters.assert_called_once_with() - @mock.patch(f"{model}.cups") - def test_action_ok_raises_warning_on_error(self, cups): + def test_action_ok_raises_warning_on_error(self): """It should raise Warning on any error""" - cups.Connection.side_effect = StopTest - with self.assertRaises(UserError): - self.Model.action_ok() + with ( + mock.patch(f"{model}.cups") as cups, + self.assertLogs(level=logging.WARNING) as logs, + ): + cups.Connection.side_effect = StopTest + with self.assertRaises(UserError): + self.Model.action_ok() + self.assertEqual(len(logs.records), 1) + self.assertEqual(logs.records[0].levelno, logging.WARNING) @mock.patch(f"{model}.cups") def test_action_ok_creates_new_printer(self, cups): diff --git a/base_report_to_printer/tests/test_printing_server.py b/base_report_to_printer/tests/test_printing_server.py index 3b359f8781e..f9bafcd8805 100644 --- a/base_report_to_printer/tests/test_printing_server.py +++ b/base_report_to_printer/tests/test_printing_server.py @@ -1,6 +1,7 @@ # Copyright 2016 LasLabs Inc. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import logging from unittest import mock from odoo import fields @@ -43,13 +44,18 @@ def new_job(self, printer, vals=None): values["printer_id"] = printer.id return self.env["printing.job"].create(values) - @mock.patch(f"{model}.cups") - def test_update_printers_error(self, cups): + def test_update_printers_error(self): """It should catch any exception from CUPS and update status""" - cups.Connection.side_effect = Exception - rec_id = self.new_printer() - self.Model.update_printers() - self.assertEqual("server-error", rec_id.status) + with ( + mock.patch(f"{model}.cups") as cups, + self.assertLogs(level=logging.WARNING) as logs, + ): + cups.Connection.side_effect = Exception + rec_id = self.new_printer() + self.Model.update_printers() + self.assertEqual("server-error", rec_id.status) + self.assertEqual(len(logs.records), 1) + self.assertEqual(logs.records[0].levelno, logging.WARNING) @mock.patch(f"{model}.cups") def test_update_printers_inits_cups(self, cups): @@ -147,15 +153,20 @@ def test_update_jobs_button(self, cups): ], ) - @mock.patch(f"{model}.cups") - def test_update_jobs_error(self, cups): + def test_update_jobs_error(self): """It should catch any exception from CUPS and update status""" - cups.Connection.side_effect = Exception - self.new_printer() - self.server.update_jobs() - cups.Connection.assert_called_with( - host=self.server.address, port=self.server.port - ) + with ( + mock.patch(f"{model}.cups") as cups, + self.assertLogs(level=logging.WARNING) as logs, + ): + cups.Connection.side_effect = Exception + self.new_printer() + self.server.update_jobs() + cups.Connection.assert_called_with( + host=self.server.address, port=self.server.port + ) + self.assertEqual(len(logs.records), 2) + self.assertEqual(logs.records[0].levelno, logging.WARNING) @mock.patch(f"{model}.cups") def test_update_jobs_uncompleted(self, cups):