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 ad38882
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 60 deletions.
38 changes: 24 additions & 14 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,31 @@ 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):
"""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 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)
70 changes: 43 additions & 27 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):
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"""
Expand Down
16 changes: 11 additions & 5 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):
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):
Expand Down
39 changes: 25 additions & 14 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):
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):
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):
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):
Expand Down

0 comments on commit ad38882

Please sign in to comment.