Skip to content

Commit

Permalink
[RFC] l10n_br_cte_spec: multi-schema refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelsavegnago committed Oct 28, 2024
1 parent a3f1781 commit b3a033b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
11 changes: 4 additions & 7 deletions l10n_br_cte_spec/models/spec_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
class CteSpecMixin(models.AbstractModel):
_description = "Abstract Model"
_name = "spec.mixin.cte"
_field_prefix = "cte40_"
_schema_name = "cte"
_schema_version = "4.0.0"
_odoo_module = "l10n_br_cte"
_spec_module = "odoo.addons.l10n_br_cte_spec.models.v4_0.cte_tipos_basico_v4_00"
_binding_module = "nfelib.cte.bindings.v4_0.cte_v4_00"
_spec_tab_name = "cte"
_cte40_odoo_module = (
"odoo.addons.l10n_br_cte_spec.models.v4_0.cte_tipos_basico_v4_00"
)
_cte40_binding_module = "nfelib.cte.bindings.v4_0.cte_v4_00"

brl_currency_id = fields.Many2one(
comodel_name="res.currency",
Expand Down
33 changes: 13 additions & 20 deletions l10n_br_cte_spec/tests/test_cte_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import nfelib
import pkg_resources
from nfelib.cte.bindings.v4_0.cte_v4_00 import Cte
from xsdata.formats.dataclass.parsers import XmlParser

from odoo import api
from odoo.tests import TransactionCase
from odoo.tests import SavepointCase

from ..models import spec_mixin

Expand Down Expand Up @@ -38,11 +37,7 @@ def build_attrs_fake(self, node, create_m2o=False):
value = getattr(node, fname)
if value is None:
continue
key = "%s%s" % (
self._field_prefix,
fname,
)

key = f"cte40_{fspec.metadata.get('name', fname)}"
if (
fspec.type == str or not any(["." in str(i) for i in fspec.type.__args__])
) and not str(fspec.type).startswith("typing.List"):
Expand All @@ -66,15 +61,10 @@ def build_attrs_fake(self, node, create_m2o=False):
if fields.get(key) and fields[key].get("related"):
key = fields[key]["related"][0]
comodel_name = fields[key]["relation"]
comodel = self.env.get(comodel_name)
else:
comodel = None
for name in self.env.keys():
if (
hasattr(self.env[name], "_binding_type")
and self.env[name]._binding_type == binding_type
):
comodel = self.env[name]
clean_type = binding_type.lower()
comodel_name = f"cte.40.{clean_type.split('.')[-1]}"
comodel = self.env.get(comodel_name)
if comodel is None: # example skip ICMS100 class
continue

Expand Down Expand Up @@ -122,7 +112,7 @@ def match_or_create_m2o_fake(self, comodel, new_value, create_m2o=False):
spec_mixin.CteSpecMixin.match_or_create_m2o_fake = match_or_create_m2o_fake


class CTeImportTest(TransactionCase):
class CTeImportTest(SavepointCase):
def test_import_cte(self):
res_items = (
"cte",
Expand All @@ -131,8 +121,11 @@ def test_import_cte(self):
"43120178408960000182570010000000041000000047-cte.xml",
)
resource_path = "/".join(res_items)
nfe_stream = pkg_resources.resource_stream(nfelib.__name__, resource_path)
parser = XmlParser()
binding = parser.from_string(nfe_stream.read().decode(), Cte)
cte = self.env["cte.40.tcte"].build_fake(binding, create=False)
cte_stream = pkg_resources.resource_stream(nfelib.__name__, resource_path)
binding = Cte.from_xml(cte_stream.read().decode())
cte = (
self.env["cte.40.tcte"]
.with_context(tracking_disable=True, edoc_type="in", lang="pt_BR")
.build_fake(binding.infCte, create=False)
)
self.assertEqual(cte.cte40_infCte.cte40_emit.cte40_CNPJ, "78408960000182")

0 comments on commit b3a033b

Please sign in to comment.