mirror of
https://github.com/OCA/report-print-send.git
synced 2025-02-16 07:11:31 +02:00
[ADD] printing_auto_base
This commit is contained in:
1
printing_auto_base/tests/__init__.py
Normal file
1
printing_auto_base/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import test_printing_auto_base
|
||||
92
printing_auto_base/tests/common.py
Normal file
92
printing_auto_base/tests/common.py
Normal file
@@ -0,0 +1,92 @@
|
||||
# Copyright 2022 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
|
||||
# Copyright 2022 Michael Tietz (MT Software) <mtietz@mt-software.de>
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
|
||||
from odoo.tests import common
|
||||
|
||||
|
||||
def print_document(cls, *args, **kwargs):
|
||||
return
|
||||
|
||||
|
||||
class TestPrintingAutoCommon(common.SavepointCase):
|
||||
@classmethod
|
||||
def _create_printer(cls, name):
|
||||
return cls.env["printing.printer"].create(
|
||||
{
|
||||
"name": name,
|
||||
"system_name": name,
|
||||
"server_id": cls.server.id,
|
||||
}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _create_tray(cls, name, printer):
|
||||
return cls.env["printing.tray"].create(
|
||||
{"name": name, "system_name": name, "printer_id": printer.id}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def setUpReportAndRecord(cls):
|
||||
cls.report = cls.env.ref("base.report_ir_model_overview")
|
||||
cls.record = cls.env.ref("base.model_res_partner")
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
|
||||
cls.server = cls.env["printing.server"].create({})
|
||||
for i in range(1, 4):
|
||||
printer_name = f"printer_{i}"
|
||||
tray_name = f"tray_{i}"
|
||||
printer = cls._create_printer(printer_name)
|
||||
tray = cls._create_tray(tray_name, printer)
|
||||
setattr(cls, printer_name, printer)
|
||||
setattr(cls, tray_name, tray)
|
||||
|
||||
cls.setUpReportAndRecord()
|
||||
cls.data = cls.report._render(cls.record.id)[0]
|
||||
|
||||
@classmethod
|
||||
def _create_printing_auto(cls, vals):
|
||||
return cls.env["printing.auto"].create(vals)
|
||||
|
||||
@classmethod
|
||||
def _create_attachment(cls, record, data, name_suffix):
|
||||
return cls.env["ir.attachment"].create(
|
||||
{
|
||||
"res_model": record._name,
|
||||
"res_id": record.id,
|
||||
"name": f"printing_auto_test_attachment_{name_suffix}.txt",
|
||||
"raw": data,
|
||||
}
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _prepare_printing_auto_report_vals(cls):
|
||||
return {
|
||||
"data_source": "report",
|
||||
"report_id": cls.report.id,
|
||||
"name": "Printing auto report",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def _create_printing_auto_report(cls, vals=None):
|
||||
_vals = cls._prepare_printing_auto_report_vals()
|
||||
_vals.update(vals or {})
|
||||
return cls._create_printing_auto(_vals)
|
||||
|
||||
@classmethod
|
||||
def _prepare_printing_auto_attachment_vals(cls):
|
||||
return {
|
||||
"data_source": "attachment",
|
||||
"attachment_domain": "[('name', 'like', 'printing_auto_test_attachment')]",
|
||||
"name": "Printing auto attachment",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def _create_printing_auto_attachment(cls, vals=None):
|
||||
_vals = cls._prepare_printing_auto_attachment_vals()
|
||||
_vals.update(vals or {})
|
||||
return cls._create_printing_auto(_vals)
|
||||
92
printing_auto_base/tests/test_printing_auto_base.py
Normal file
92
printing_auto_base/tests/test_printing_auto_base.py
Normal file
@@ -0,0 +1,92 @@
|
||||
# Copyright 2022 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
|
||||
# Copyright 2022 Michael Tietz (MT Software) <mtietz@mt-software.de>
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from odoo.exceptions import UserError, ValidationError
|
||||
|
||||
from .common import PrintingPrinter, TestPrintingAutoCommon, print_document
|
||||
|
||||
|
||||
@mock.patch.object(PrintingPrinter, "print_document", print_document)
|
||||
class TestPrintingAutoBase(TestPrintingAutoCommon):
|
||||
def test_check_data_source(self):
|
||||
with self.assertRaises(UserError):
|
||||
self._create_printing_auto_report({"report_id": False})
|
||||
|
||||
with self.assertRaises(UserError):
|
||||
self._create_printing_auto_attachment({"attachment_domain": "[]"})
|
||||
|
||||
with self.assertRaises(UserError):
|
||||
printing_auto = self._create_printing_auto_attachment()
|
||||
printing_auto.attachment_domain = False
|
||||
|
||||
def test_behaviour(self):
|
||||
expected = {"printer": self.printer_1}
|
||||
printing_auto = self._create_printing_auto_report(
|
||||
{"printer_id": self.printer_1.id}
|
||||
)
|
||||
self.assertEqual(expected, printing_auto._get_behaviour())
|
||||
|
||||
printing_auto.printer_tray_id = self.tray_1
|
||||
expected["tray"] = self.tray_1.system_name
|
||||
self.assertEqual(expected, printing_auto._get_behaviour())
|
||||
|
||||
expected = printing_auto.report_id.behaviour()
|
||||
printing_auto.printer_id = False
|
||||
printing_auto.printer_tray_id = False
|
||||
self.assertEqual(expected, printing_auto._get_behaviour())
|
||||
|
||||
expected = self.env["ir.actions.report"]._get_user_default_print_behaviour()
|
||||
printing_auto = self._create_printing_auto_attachment()
|
||||
self.assertEqual(expected, printing_auto._get_behaviour())
|
||||
|
||||
def test_record_change(self):
|
||||
parent = self.env["res.partner"].create({"name": "Parent"})
|
||||
partner = parent.create({"name": "Child", "parent_id": parent.id})
|
||||
printing_auto = self._create_printing_auto_report(
|
||||
{"record_change": "parent_id"}
|
||||
)
|
||||
self.assertEqual(parent, printing_auto._get_record(partner))
|
||||
|
||||
def test_check_condition(self):
|
||||
partner = self.env["res.partner"].create({"name": "Partner"})
|
||||
printing_auto = self._create_printing_auto_report(
|
||||
{"condition": f"[('name', '=', '{partner.name}')]"}
|
||||
)
|
||||
self.assertEqual(partner, printing_auto._check_condition(partner))
|
||||
printing_auto.condition = "[('name', '=', '1')]"
|
||||
self.assertFalse(printing_auto._check_condition(partner))
|
||||
|
||||
def test_get_content(self):
|
||||
printing_auto_report = self._create_printing_auto_report()
|
||||
self.assertEqual([self.data], printing_auto_report._get_content(self.record))
|
||||
|
||||
printing_auto_attachment = self._create_printing_auto_attachment()
|
||||
attachment = self._create_attachment(self.record, self.data, "1")
|
||||
self.assertEqual(
|
||||
[attachment.raw], printing_auto_attachment._get_content(self.record)
|
||||
)
|
||||
attachment.unlink()
|
||||
|
||||
with self.assertRaises(ValidationError):
|
||||
printing_auto_attachment._get_content(self.record)
|
||||
|
||||
def test_do_print(self):
|
||||
printing_auto = self._create_printing_auto_attachment(
|
||||
{"attachment_domain": "[('name', 'like', 'printing_auto_test')]"}
|
||||
)
|
||||
self._create_attachment(self.record, self.data, "1")
|
||||
with self.assertRaises(UserError):
|
||||
printing_auto.do_print(self.record)
|
||||
|
||||
printing_auto.printer_id = self.printer_1
|
||||
for nbr_of_copies in [0, 2, 1]:
|
||||
expected = (self.printer_1, nbr_of_copies)
|
||||
printing_auto.nbr_of_copies = nbr_of_copies
|
||||
self.assertEqual(expected, printing_auto.do_print(self.record))
|
||||
|
||||
printing_auto.condition = "[('name', '=', 'test_printing_auto')]"
|
||||
expected = (self.printer_1, 0)
|
||||
self.assertEqual(expected, printing_auto.do_print(self.record))
|
||||
Reference in New Issue
Block a user