Skip to content

Commit

Permalink
[UPD] zpl_printer_stock: adding test for zpl_printer
Browse files Browse the repository at this point in the history
Adding another test, which validates the behavior of determining the correct printer, when there is a specification for a product.
Including some minor adaptations to zpl_printer to improve documentation and to support the new test
  • Loading branch information
mohs8421 committed Oct 7, 2024
1 parent a504014 commit ae253b2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
2 changes: 2 additions & 0 deletions zpl_printer/controllers/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class ReportController(ReportController):
def report_download(self, data, context=None, token=None):
"""This function is used by 'zplactionmanager.js' in order to trigger
the download of a zpl report.
The cors header has to be set to * as there has to be a call to the actual printer,
which is probably in the local network of the using client.
:param data: a javascript array JSON.stringified containg report
internal url ([0]) and type [1]
Expand Down
21 changes: 14 additions & 7 deletions zpl_printer/tests/zpl_printer.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
from odoo.tests import common, tagged

_DEFAULT_PRINTER_URL = "https://default_printer.my_company_network.internal"
DEFAULT_PRINTER_URL = "https://default_printer.my_company_network.internal"
OTHER_PRINTER_URL = "https://other_printer.my_company_network.internal"


@tagged("zpl")
class TestZplPrinter(common.TransactionCase):
class TestZplPrinterBase(common.TransactionCase):
def setUp(self):
super(TestZplPrinter, self).setUp()
super(TestZplPrinterBase, self).setUp()
# if the database has a default printer, disable it for this test
printer = self.env["zpl_printer.zpl_printer"].search([("default", "=", True)])
if len(printer) >= 1:
printer.default = False
self.env["zpl_printer.zpl_printer"].create(
[
{
"name": "default",
"url": _DEFAULT_PRINTER_URL,
"url": DEFAULT_PRINTER_URL,
"resolution": "200",
"default": True,
},
{
"name": "other_printer",
"url": "https://other_printer.my_company_network.internal",
"url": OTHER_PRINTER_URL,
"resolution": "300",
},
]
)


@tagged("zpl")
class TestZplPrinter(TestZplPrinterBase):
def test_write(self):
"""Changing the default flag of a printer should remove it from all other printers"""
printer = self.env["zpl_printer.zpl_printer"].search(
Expand All @@ -42,4 +49,4 @@ def test_get_label_printer_data(self):
result = self.env["zpl_printer.zpl_printer"].get_label_printer_data(
"unspecific_report_name", [1]
)
self.assertEqual(result, {"url": _DEFAULT_PRINTER_URL, "resolution": "200"})
self.assertEqual(result, {"url": DEFAULT_PRINTER_URL, "resolution": "200"})
1 change: 1 addition & 0 deletions zpl_printer_stock/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import zpl_printer
55 changes: 55 additions & 0 deletions zpl_printer_stock/tests/zpl_printer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from odoo.tests import common, tagged
from odoo.addons.zpl_printer.tests import TestZplPrinterBase, DEFAULT_PRINTER_URL, OTHER_PRINTER_URL


@tagged("zpl")
class TestZplPrinterStock(TestZplPrinterBase):
_unspecific_lot_id = 0
_other_printer_lot_id = 0

def setUp(self):
super(TestZplPrinterStock, self).setUp()
second_printer = self.env["zpl_printer.zpl_printer"].search([("name", "=", "other_printer")])
(unspecific_product, other_printer_product) = self.env["product.product"].create(
[
{
"name": "Unspecific Product",
"type": "product",
"tracking": "serial",
},
{
"name": "Product with different printer",
"type": "product",
"tracking": "serial",
},
]
)
other_printer_product.product_tmpl_id.zpl_printer_id = second_printer
lots = self.env["stock.lot"].create(
[
{
"name": "UP-00001",
"product_id": unspecific_product.id,
},
{
"name": "OPP-00001",
"product_id": other_printer_product.id,
},
]
)
self._unspecific_lot_id = lots[0].id
self._other_printer_lot_id = lots[1].id

def test_get_label_printer_data_for_anything(self):
"""Unless otherwise specified through this method, the default should be returned"""
result = self.env["zpl_printer.zpl_printer"].get_label_printer_data(
"stock.label_lot_template_view", [self._unspecific_lot_id]
)
self.assertEqual(result, {"url": DEFAULT_PRINTER_URL, "resolution": "200"})

def test_get_label_printer_data_for_product_with_other_printer(self):
"""Unless otherwise specified through this method, the default should be returned"""
result = self.env["zpl_printer.zpl_printer"].get_label_printer_data(
"stock.label_lot_template_view", [self._other_printer_lot_id]
)
self.assertEqual(result, {"url": OTHER_PRINTER_URL, "resolution": "300"})

0 comments on commit ae253b2

Please sign in to comment.