Skip to content

Commit

Permalink
[UPD] zpl_printer: adding test for zpl_printer
Browse files Browse the repository at this point in the history
Introducing a test that checks the default behavior of the zpl_printer model. Also doing some minor updates to that to make it more error resilient.
  • Loading branch information
mohs8421 committed Oct 2, 2024
1 parent 16675f1 commit a504014
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 6 deletions.
19 changes: 16 additions & 3 deletions zpl_printer/i18n/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0+e-20240827\n"
"Project-Id-Version: Odoo Server 16.0+e-20240918\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-28 10:41+0000\n"
"PO-Revision-Date: 2024-08-28 10:41+0000\n"
"POT-Creation-Date: 2024-10-02 09:01+0000\n"
"PO-Revision-Date: 2024-10-02 09:01+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
Expand Down Expand Up @@ -52,6 +52,7 @@ msgstr ""

#. module: zpl_printer
#: model:ir.model,name:zpl_printer.model_zpl_printer_zpl_printer
#: model_terms:ir.ui.view,arch_db:zpl_printer.view_zpl_printer_form
msgid "Label Printer"
msgstr "Label-Drucker"

Expand Down Expand Up @@ -81,6 +82,13 @@ msgstr ""
msgid "Name"
msgstr ""

#. module: zpl_printer
#. odoo-python
#: code:addons/zpl_printer/model/zpl_printer.py:0
#, python-format
msgid "No default printer specified"
msgstr "Kein Standard-Label-Drucker festgelegt"

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__resolution
msgid "Printing Resolution (DPI)"
Expand Down Expand Up @@ -124,6 +132,11 @@ msgstr "Die URL muss eindeutig sein."
msgid "Url"
msgstr "URL"

#. module: zpl_printer
#: model_terms:ir.ui.view,arch_db:zpl_printer.view_tree_zpl_printer_tree
msgid "ZPL-Printer"
msgstr "Label-Drucker"

#. module: zpl_printer
#: model:ir.model.fields.selection,name:zpl_printer.selection__ir_actions_report__report_type__qweb-zpl
msgid "qweb-zpl"
Expand Down
139 changes: 139 additions & 0 deletions zpl_printer/i18n/zpl_printer.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * zpl_printer
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0+e-20240918\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-10-02 09:01+0000\n"
"PO-Revision-Date: 2024-10-02 09:01+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: zpl_printer
#: model:ir.model.fields.selection,name:zpl_printer.selection__zpl_printer_zpl_printer__resolution__200
msgid "200"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields.selection,name:zpl_printer.selection__zpl_printer_zpl_printer__resolution__300
msgid "300"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__create_uid
msgid "Created by"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__create_date
msgid "Created on"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__default
msgid "Default"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__display_name
msgid "Display Name"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__id
msgid "ID"
msgstr ""

#. module: zpl_printer
#: model:ir.model,name:zpl_printer.model_zpl_printer_zpl_printer
#: model_terms:ir.ui.view,arch_db:zpl_printer.view_zpl_printer_form
msgid "Label Printer"
msgstr ""

#. module: zpl_printer
#: model:ir.actions.act_window,name:zpl_printer.action_zpl_printer
#: model_terms:ir.ui.view,arch_db:zpl_printer.res_config_settings_view_form
msgid "Label-Printers"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer____last_update
msgid "Last Modified on"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__write_uid
msgid "Last Updated by"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__write_date
msgid "Last Updated on"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__name
msgid "Name"
msgstr ""

#. module: zpl_printer
#. odoo-python
#: code:addons/zpl_printer/model/zpl_printer.py:0
#, python-format
msgid "No default printer specified"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__resolution
msgid "Printing Resolution (DPI)"
msgstr ""

#. module: zpl_printer
#: model:ir.model,name:zpl_printer.model_ir_actions_report
msgid "Report Action"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_ir_actions_report__report_type
msgid "Report Type"
msgstr ""

#. module: zpl_printer
#: model:ir.model.constraint,message:zpl_printer.constraint_zpl_printer_zpl_printer_name_unique
msgid "The name must be unique."
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,help:zpl_printer.field_ir_actions_report__report_type
msgid ""
"The type of the report that will be rendered, each one having its own "
"rendering method. HTML means the report will be opened directly in your "
"browser PDF means the report will be rendered using Wkhtmltopdf and "
"downloaded by the user."
msgstr ""

#. module: zpl_printer
#: model:ir.model.constraint,message:zpl_printer.constraint_zpl_printer_zpl_printer_url_unique
msgid "The url must be unique."
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields,field_description:zpl_printer.field_zpl_printer_zpl_printer__url
msgid "Url"
msgstr ""

#. module: zpl_printer
#: model_terms:ir.ui.view,arch_db:zpl_printer.view_tree_zpl_printer_tree
msgid "ZPL-Printer"
msgstr ""

#. module: zpl_printer
#: model:ir.model.fields.selection,name:zpl_printer.selection__ir_actions_report__report_type__qweb-zpl
msgid "qweb-zpl"
msgstr "ZPL"
16 changes: 13 additions & 3 deletions zpl_printer/model/zpl_printer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from odoo import fields, models
from odoo import _, fields, models


class ZplPrinter(models.Model):
Expand Down Expand Up @@ -26,16 +26,26 @@ class ZplPrinter(models.Model):

def write(self, vals):
"""There may only be one default."""
if vals["default"]:
if vals.get("default", False):
for previous_default in self.search(
[("default", "=", True), ("id", "!=", self.id)]
):
previous_default.default = False
return super().write(vals)

Check warning on line 34 in zpl_printer/model/zpl_printer.py

View check run for this annotation

Codecov / codecov/patch

zpl_printer/model/zpl_printer.py#L33-L34

Added lines #L33 - L34 were not covered by tests

def get_default_printer(self):
return self.search([("default", "=", True)])
printer = self.search([("default", "=", True)])

Check warning on line 37 in zpl_printer/model/zpl_printer.py

View check run for this annotation

Codecov / codecov/patch

zpl_printer/model/zpl_printer.py#L37

Added line #L37 was not covered by tests
if len(printer) >= 1:
return printer[0]
printer = self.search([])

Check warning on line 40 in zpl_printer/model/zpl_printer.py

View check run for this annotation

Codecov / codecov/patch

zpl_printer/model/zpl_printer.py#L39-L40

Added lines #L39 - L40 were not covered by tests
if len(printer) == 0:
raise ValueError(_("No default printer specified"))
return printer[0]

Check warning on line 43 in zpl_printer/model/zpl_printer.py

View check run for this annotation

Codecov / codecov/patch

zpl_printer/model/zpl_printer.py#L42-L43

Added lines #L42 - L43 were not covered by tests

def get_label_printer_data(self, report_name, active_ids):
"""This function is called from the client to determine which printer will be used.
So this implementation just provides the default printer, but other modules will allow
reacting more specific to this, for example to print a label for a certain product
always on a different printer."""
default_printer = self.get_default_printer()
return {"url": default_printer.url, "resolution": default_printer.resolution}

Check warning on line 51 in zpl_printer/model/zpl_printer.py

View check run for this annotation

Codecov / codecov/patch

zpl_printer/model/zpl_printer.py#L50-L51

Added lines #L50 - L51 were not covered by tests
1 change: 1 addition & 0 deletions zpl_printer/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import zpl_printer
45 changes: 45 additions & 0 deletions zpl_printer/tests/zpl_printer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from odoo.tests import common, tagged

_DEFAULT_PRINTER_URL = "https://default_printer.my_company_network.internal"


@tagged("zpl")
class TestZplPrinter(common.TransactionCase):
def setUp(self):
super(TestZplPrinter, self).setUp()
self.env["zpl_printer.zpl_printer"].create(

Check warning on line 10 in zpl_printer/tests/zpl_printer.py

View check run for this annotation

Codecov / codecov/patch

zpl_printer/tests/zpl_printer.py#L9-L10

Added lines #L9 - L10 were not covered by tests
[
{
"name": "default",
"url": _DEFAULT_PRINTER_URL,
"resolution": "200",
"default": True,
},
{
"name": "other_printer",
"url": "https://other_printer.my_company_network.internal",
"resolution": "300",
},
]
)

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(

Check warning on line 28 in zpl_printer/tests/zpl_printer.py

View check run for this annotation

Codecov / codecov/patch

zpl_printer/tests/zpl_printer.py#L28

Added line #L28 was not covered by tests
[("name", "=", "other_printer")]
)
printer.default = True
default_after_change = self.env["zpl_printer.zpl_printer"].search(

Check warning on line 32 in zpl_printer/tests/zpl_printer.py

View check run for this annotation

Codecov / codecov/patch

zpl_printer/tests/zpl_printer.py#L31-L32

Added lines #L31 - L32 were not covered by tests
[("name", "=", "default")]
)
try:
self.assertFalse(default_after_change.default)

Check warning on line 36 in zpl_printer/tests/zpl_printer.py

View check run for this annotation

Codecov / codecov/patch

zpl_printer/tests/zpl_printer.py#L35-L36

Added lines #L35 - L36 were not covered by tests
finally:
default_after_change.default = True

Check warning on line 38 in zpl_printer/tests/zpl_printer.py

View check run for this annotation

Codecov / codecov/patch

zpl_printer/tests/zpl_printer.py#L38

Added line #L38 was not covered by tests

def test_get_label_printer_data(self):
"""Unless otherwise specified through this method, the default should be returned"""
result = self.env["zpl_printer.zpl_printer"].get_label_printer_data(

Check warning on line 42 in zpl_printer/tests/zpl_printer.py

View check run for this annotation

Codecov / codecov/patch

zpl_printer/tests/zpl_printer.py#L42

Added line #L42 was not covered by tests
"unspecific_report_name", [1]
)
self.assertEqual(result, {"url": _DEFAULT_PRINTER_URL, "resolution": "200"})

Check warning on line 45 in zpl_printer/tests/zpl_printer.py

View check run for this annotation

Codecov / codecov/patch

zpl_printer/tests/zpl_printer.py#L45

Added line #L45 was not covered by tests

0 comments on commit a504014

Please sign in to comment.