Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
trisdoan committed Nov 19, 2024
1 parent 4fd9a30 commit 78df10c
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 52 deletions.
109 changes: 77 additions & 32 deletions base_report_to_printer/tests/test_ir_actions_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2016 SYLEAM
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging
from unittest import mock

from odoo.tests.common import TransactionCase
Expand Down Expand Up @@ -102,29 +103,51 @@ def test_behaviour_user_values(self):
report = self.Model.search([], limit=1)
self.env.user.printing_action = "client"
self.env.user.printing_printer_id = self.new_printer()
self.assertEqual(
report.behaviour(),
{
"action": "client",
"printer": self.env.user.printing_printer_id,
"tray": False,
},
)
with (
mock.patch(
"odoo.addons.base_report_to_printer.models."
"ir_actions_report.IrActionsReport."
"behaviour"
) as behaviour,
self.assertLogs(level=logging.WARNING) as logs,
):
behaviour.assert_called_once()
self.assertEqual(
report.behaviour(),
{
"action": "client",
"printer": self.env.user.printing_printer_id,
"tray": False,
},
)
self.assertEqual(len(logs.records), 1)
self.assertEqual(logs.records[0].levelno, logging.WARNING)

def test_behaviour_report_values(self):
"""It should return the action and printer from report"""
report = self.Model.search([], limit=1)
self.env.user.printing_action = "client"
report.property_printing_action_id = self.new_action()
report.printing_printer_id = self.new_printer()
self.assertEqual(
report.behaviour(),
{
"action": report.property_printing_action_id.action_type,
"printer": report.printing_printer_id,
"tray": False,
},
)
with (
mock.patch(
"odoo.addons.base_report_to_printer.models."
"ir_actions_report.IrActionsReport."
"behaviour"
) as behaviour,
self.assertLogs(level=logging.WARNING) as logs,
):
behaviour.assert_called_once()
self.assertEqual(
report.behaviour(),
{
"action": report.property_printing_action_id.action_type,
"printer": report.printing_printer_id,
"tray": False,
},
)
self.assertEqual(len(logs.records), 1)
self.assertEqual(logs.records[0].levelno, logging.WARNING)

def test_behaviour_user_action(self):
"""It should return the action and printer from user action"""
Expand Down Expand Up @@ -188,14 +211,25 @@ def test_behaviour_printing_action_with_printer(self):
printing_action = self.new_printing_action()
printing_action.user_id = self.env.user
printing_action.printer_id = self.new_printer()
self.assertEqual(
report.behaviour(),
{
"action": printing_action.action,
"printer": printing_action.printer_id,
"tray": False,
},
)
with (
mock.patch(
"odoo.addons.base_report_to_printer.models."
"ir_actions_report.IrActionsReport."
"behaviour"
) as behaviour,
self.assertLogs(level=logging.WARNING) as logs,
):
behaviour.assert_called_once()
self.assertEqual(
report.behaviour(),
{
"action": printing_action.action,
"printer": printing_action.printer_id,
"tray": False,
},
)
self.assertEqual(len(logs.records), 1)
self.assertEqual(logs.records[0].levelno, logging.WARNING)

def test_behaviour_printing_action_user_defaults(self):
"""It should return the action and printer from user with printing
Expand Down Expand Up @@ -305,11 +339,22 @@ def test_print_in_new_thread(self):
printing_action.user_id = self.env.user
printing_action.printer_id = self.new_printer()
printing_action.printer_id.multi_thread = True
self.assertEqual(
report.behaviour(),
{
"action": printing_action.action,
"printer": printing_action.printer_id,
"tray": False,
},
)
with (
mock.patch(
"odoo.addons.base_report_to_printer.models."
"ir_actions_report.IrActionsReport."
"behaviour"
) as behaviour,
self.assertLogs(level=logging.WARNING) as logs,
):
behaviour.assert_called_once()
self.assertEqual(
report.behaviour(),
{
"action": printing_action.action,
"printer": printing_action.printer_id,
"tray": False,
},
)
self.assertEqual(len(logs.records), 1)
self.assertEqual(logs.records[0].levelno, logging.WARNING)
61 changes: 41 additions & 20 deletions base_report_to_printer/tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2017 Tecnativa - Jairo Llopis
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging
from unittest import mock

from odoo import exceptions
Expand Down Expand Up @@ -128,11 +129,14 @@ def test_render_qweb_pdf_not_printable(self):

def test_render_qweb_pdf_printable(self):
"""It should print the report, only if it is printable"""
with mock.patch(
"odoo.addons.base_report_to_printer.models."
"printing_printer.PrintingPrinter."
"print_document"
) as print_document:
with (
mock.patch(
"odoo.addons.base_report_to_printer.models."
"printing_printer.PrintingPrinter."
"print_document"
) as print_document,
self.assertLogs(level=logging.WARNING) as logs,
):
self.report.property_printing_action_id.action_type = "server"
self.report.printing_printer_id = self.new_printer()
document = self.report._render_qweb_pdf(
Expand All @@ -145,14 +149,19 @@ def test_render_qweb_pdf_printable(self):
doc_format="qweb-pdf",
tray=False,
)
self.assertEqual(len(logs.records), 1)
self.assertEqual(logs.records[0].levelno, logging.WARNING)

def test_render_qweb_text_printable(self):
"""It should print the report, only if it is printable"""
with mock.patch(
"odoo.addons.base_report_to_printer.models."
"printing_printer.PrintingPrinter."
"print_document"
) as print_document:
with (
mock.patch(
"odoo.addons.base_report_to_printer.models."
"printing_printer.PrintingPrinter."
"print_document"
) as print_document,
self.assertLogs(level=logging.WARNING) as logs,
):
self.report_text.property_printing_action_id.action_type = "server"
self.report_text.printing_printer_id = self.new_printer()
document = self.report_text._render_qweb_text(
Expand All @@ -165,29 +174,41 @@ def test_render_qweb_text_printable(self):
doc_format="qweb-text",
tray=False,
)
self.assertEqual(len(logs.records), 1)
self.assertEqual(logs.records[0].levelno, logging.WARNING)

def test_print_document_not_printable(self):
"""It should print the report, regardless of the defined behaviour"""
self.report.printing_printer_id = self.new_printer()
with mock.patch(
"odoo.addons.base_report_to_printer.models."
"printing_printer.PrintingPrinter."
"print_document"
) as print_document:
with (
mock.patch(
"odoo.addons.base_report_to_printer.models."
"printing_printer.PrintingPrinter."
"print_document"
) as print_document,
self.assertLogs(level=logging.WARNING) as logs,
):
self.report.print_document(self.partners.ids)
print_document.assert_called_once()
self.assertEqual(len(logs.records), 2)
self.assertEqual(logs.records[0].levelno, logging.WARNING)

def test_print_document_printable(self):
"""It should print the report, regardless of the defined behaviour"""
self.report.property_printing_action_id.action_type = "server"
self.report.printing_printer_id = self.new_printer()
with mock.patch(
"odoo.addons.base_report_to_printer.models."
"printing_printer.PrintingPrinter."
"print_document"
) as print_document:
with (
mock.patch(
"odoo.addons.base_report_to_printer.models."
"printing_printer.PrintingPrinter."
"print_document"
) as print_document,
self.assertLogs(level=logging.WARNING) as logs,
):
self.report.print_document(self.partners.ids)
print_document.assert_called_once()
self.assertEqual(len(logs.records), 2)
self.assertEqual(logs.records[0].levelno, logging.WARNING)

def test_print_document_no_printer(self):
"""It should raise an error"""
Expand Down

0 comments on commit 78df10c

Please sign in to comment.