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 4bfd4d8
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 103 deletions.
201 changes: 118 additions & 83 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,39 @@ 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 (
self.assertLogs(level=logging.WARNING) as logs,
):
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 (
self.assertLogs(level=logging.WARNING) as logs,
):
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 +199,19 @@ 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 (
self.assertLogs(level=logging.WARNING) as logs,
):
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 All @@ -215,57 +231,71 @@ def test_print_tray_behaviour(self):
"""
It should return the correct tray
"""
report = self.Model.search([], limit=1)
action = self.env["printing.report.xml.action"].create(
{"user_id": self.env.user.id, "report_id": report.id, "action": "server"}
)
printer = self.new_printer()
tray_vals = {"name": "Tray", "system_name": "Tray", "printer_id": printer.id}
user_tray = self.new_tray({"system_name": "User tray"}, tray_vals)
report_tray = self.new_tray({"system_name": "Report tray"}, tray_vals)
action_tray = self.new_tray({"system_name": "Action tray"}, tray_vals)

# No report passed
self.env.user.printer_tray_id = False
options = printer.print_options()
self.assertFalse("InputSlot" in options)

# No tray defined
self.env.user.printer_tray_id = False
report.printer_tray_id = False
action.printer_tray_id = False
options = report.behaviour()
self.assertTrue("tray" in options)

# Only user tray is defined
self.env.user.printer_tray_id = user_tray
report.printer_tray_id = False
action.printer_tray_id = False
self.assertEqual("User tray", report.behaviour()["tray"])

# Only report tray is defined
self.env.user.printer_tray_id = False
report.printer_tray_id = report_tray
action.printer_tray_id = False
self.assertEqual("Report tray", report.behaviour()["tray"])

# Only action tray is defined
self.env.user.printer_tray_id = False
report.printer_tray_id = False
action.printer_tray_id = action_tray
self.assertEqual("Action tray", report.behaviour()["tray"])

# User and report tray defined
self.env.user.printer_tray_id = user_tray
report.printer_tray_id = report_tray
action.printer_tray_id = False
self.assertEqual("Report tray", report.behaviour()["tray"])

# All trays are defined
self.env.user.printer_tray_id = user_tray
report.printer_tray_id = report_tray
action.printer_tray_id = action_tray
self.assertEqual("Action tray", report.behaviour()["tray"])
with (
self.assertLogs(level=logging.WARNING) as logs,
):
report = self.Model.search([], limit=1)
action = self.env["printing.report.xml.action"].create(
{
"user_id": self.env.user.id,
"report_id": report.id,
"action": "server",
}
)
printer = self.new_printer()
tray_vals = {
"name": "Tray",
"system_name": "Tray",
"printer_id": printer.id,
}
user_tray = self.new_tray({"system_name": "User tray"}, tray_vals)
report_tray = self.new_tray({"system_name": "Report tray"}, tray_vals)
action_tray = self.new_tray({"system_name": "Action tray"}, tray_vals)

# No report passed
self.env.user.printer_tray_id = False
options = printer.print_options()
self.assertFalse("InputSlot" in options)

# No tray defined
self.env.user.printer_tray_id = False
report.printer_tray_id = False
action.printer_tray_id = False
options = report.behaviour()
self.assertTrue("tray" in options)

# Only user tray is defined
self.env.user.printer_tray_id = user_tray
report.printer_tray_id = False
action.printer_tray_id = False
self.assertEqual("User tray", report.behaviour()["tray"])

# Only report tray is defined
self.env.user.printer_tray_id = False
report.printer_tray_id = report_tray
action.printer_tray_id = False
self.assertEqual("Report tray", report.behaviour()["tray"])

# Only action tray is defined
self.env.user.printer_tray_id = False
report.printer_tray_id = False
action.printer_tray_id = action_tray
self.assertEqual("Action tray", report.behaviour()["tray"])

# User and report tray defined
self.env.user.printer_tray_id = user_tray
report.printer_tray_id = report_tray
action.printer_tray_id = False
self.assertEqual("Report tray", report.behaviour()["tray"])

# All trays are defined
self.env.user.printer_tray_id = user_tray
report.printer_tray_id = report_tray
action.printer_tray_id = action_tray
self.assertEqual("Action tray", report.behaviour()["tray"])
self.assertEqual(len(logs.records), 6)
for record in logs.records:
self.assertEqual(record.levelno, logging.WARNING)

def test_onchange_printer_tray_id_empty(self):
action = self.Model.new({"printer_tray_id": False})
Expand Down Expand Up @@ -305,11 +335,16 @@ 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 (
self.assertLogs(level=logging.WARNING) as logs,
):
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 4bfd4d8

Please sign in to comment.