Skip to content

Commit

Permalink
edi_exchange_template: imp test cov for tmpl lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
simahawk committed Feb 5, 2025
1 parent 1b54aa7 commit 621e860
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 12 deletions.
1 change: 1 addition & 0 deletions edi_exchange_template_oca/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import test_edi_backend_output
from . import test_nswrapper
from . import test_backend_and_type
110 changes: 110 additions & 0 deletions edi_exchange_template_oca/tests/test_backend_and_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Copyright 2025 Camptocamp SA
# @author: Simone Orsi <simone.orsi@camptocamp.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).


from odoo.tests.common import SavepointCase


class TestExchangeType(SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.backend_type = cls.env.ref("edi_oca.demo_edi_backend_type")
cls.backend = cls.env.ref("edi_oca.demo_edi_backend")
cls.type_out1 = cls.env["edi.exchange.type"].create(
{
"name": "Type output 1",
"direction": "output",
"code": "test_type_out1",
"exchange_file_ext": "txt",
"backend_type_id": cls.backend_type.id,
}
)
cls.type_out2 = cls.env["edi.exchange.type"].create(
{
"name": "Type output 2",
"direction": "output",
"code": "test_type_out2",
"exchange_file_ext": "txt",
"backend_type_id": cls.backend_type.id,
}
)
model = cls.env["edi.exchange.template.output"]
qweb_tmpl = cls.env["ir.ui.view"].create(
{
"type": "qweb",
"key": "edi_exchange.test_output1",
"arch": """
<t t-name="edi_exchange.test_output1">
TEST
</t>
""",
}
)
cls.tmpl_out1 = model.create(
{
"code": "tmpl_test_type_out1",
"name": "Out 1",
"backend_type_id": cls.backend_type.id,
"type_id": cls.type_out1.id,
"template_id": qweb_tmpl.id,
"output_type": "txt",
"allowed_type_ids": [(6, 0, [cls.type_out1.id, cls.type_out2.id])],
}
)
cls.tmpl_out2 = model.create(
{
"code": "tmpl_test_type_out2",
"name": "Out 2",
"backend_type_id": cls.env.ref("edi_oca.demo_edi_backend_type").id,
"type_id": cls.type_out1.id,
"template_id": qweb_tmpl.id,
"output_type": "txt",
"allowed_type_ids": [(6, 0, [cls.type_out2.id])],
}
)
vals = {
# doesn't matter what model we use
"model": cls.env.user._name,
"res_id": cls.env.user.id,
"type_id": cls.type_out2.id,
}
cls.record1 = cls.backend.create_record("test_type_out1", vals)
vals = {
# doesn't matter what model we use
"model": cls.env.user._name,
"res_id": cls.env.user.id,
"type_id": cls.type_out2.id,
}
cls.record2 = cls.backend.create_record("test_type_out2", vals)

# TODO: getting a template via code is deprecated
def test_get_template_by_code(self):
self.assertEqual(
self.backend._get_output_template(self.record2, code=self.tmpl_out1.code),
self.tmpl_out1,
)
self.record1.type_id.code = self.tmpl_out1.code
self.assertEqual(
self.backend._get_output_template(self.record1), self.tmpl_out1
)
self.record2.type_id.code = self.tmpl_out2.code
self.assertEqual(
self.backend._get_output_template(self.record2), self.tmpl_out2
)

def test_get_template_by_fallback(self):
self.assertEqual(
self.backend._get_output_template(self.record2, code=self.tmpl_out1.code),
self.tmpl_out1,
)
self.assertEqual(
self.backend._get_output_template(self.record1), self.tmpl_out1
)
# Here's is shown the limitation of the fallback lookup by backend type:
# if you have more than one template allowed for the same type,
# the first one is returned.
self.assertEqual(
self.backend._get_output_template(self.record2), self.tmpl_out1
)
12 changes: 0 additions & 12 deletions edi_exchange_template_oca/tests/test_edi_backend_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,6 @@ def _setup_records(cls):

# TODO: add more unit tests
class TestEDIBackendOutput(TestEDIBackendOutputBase):
def test_get_template(self):
self.assertEqual(
self.backend._get_output_template(self.record1), self.tmpl_out1
)
self.assertEqual(
self.backend._get_output_template(self.record2), self.tmpl_out2
)
self.assertEqual(
self.backend._get_output_template(self.record2, code=self.tmpl_out1.code),
self.tmpl_out1,
)

def test_generate_file(self):
output = self.backend.exchange_generate(self.record1)
expected = "{0.ref} - {0.name}".format(self.partner)
Expand Down

0 comments on commit 621e860

Please sign in to comment.