Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
trisdoan committed Oct 28, 2024
1 parent 2f8495e commit 3b27729
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 56 deletions.
44 changes: 28 additions & 16 deletions base_report_to_printer/tests/test_printing_job.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -42,22 +43,33 @@ 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)

@mock.patch(f"{model}.cups")
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), 1)
self.assertEqual(logs.records[0].levelno, logging.WARNING)

def test_cancel_job(self, cups):
"""It should catch any exception from CUPS and update status"""
printer = self.new_printer()
job = self.new_job(printer)
job.cancel()
cups.Connection().cancelJob.assert_called_once_with(
job.job_id_cups, purge_job=False
)
with (
mock.patch(f"{model}.cups") as cups,
self.assertLogs(level=logging.WARNING) as logs,
):
printer = self.new_printer()
job = self.new_job(printer)
job.cancel()
cups.Connection().cancelJob.assert_called_once_with(
job.job_id_cups, purge_job=False
)
self.assertEqual(len(logs.records), 1)
self.assertEqual(logs.records[0].levelno, logging.WARNING)
62 changes: 39 additions & 23 deletions base_report_to_printer/tests/test_printing_printer.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
"""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"
)
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)

@mock.patch(f"{server_model}.cups")
def test_print_file(self, cups):
"""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):
"""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"""
Expand Down
14 changes: 10 additions & 4 deletions base_report_to_printer/tests/test_printing_printer_wizard.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
"""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):
Expand Down
37 changes: 24 additions & 13 deletions base_report_to_printer/tests/test_printing_server.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
"""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):
Expand Down Expand Up @@ -147,15 +153,20 @@ def test_update_jobs_button(self, cups):
],
)

@mock.patch(f"{model}.cups")
def test_update_jobs_error(self, cups):
"""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), 1)
self.assertEqual(logs.records[0].levelno, logging.WARNING)

@mock.patch(f"{model}.cups")
def test_update_jobs_uncompleted(self, cups):
Expand Down Expand Up @@ -205,4 +216,4 @@ def test_update_jobs(self, cups):
new_job = self.env["printing.job"].search([("job_id_cups", "=", 4)])
self.assertEqual(completed_job.job_state, "completed")
self.assertEqual(purged_job.active, False)
self.assertEqual(new_job.job_state, "processing")
self.assertEquatest_update_jobs_errorl(new_job.job_state, "processing")

0 comments on commit 3b27729

Please sign in to comment.