From 1f92ff001366b629dd7e7ecfa43073c24f1db792 Mon Sep 17 00:00:00 2001 From: Marcel Savegnago Date: Mon, 28 Oct 2024 09:02:46 -0300 Subject: [PATCH] [RFC] l10n_br_cte_spec: multi-schema refactor --- l10n_br_cte_spec/models/spec_mixin.py | 11 +++----- l10n_br_cte_spec/tests/test_cte_import.py | 33 +++++++++-------------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/l10n_br_cte_spec/models/spec_mixin.py b/l10n_br_cte_spec/models/spec_mixin.py index 52d5afaccfb3..bfd449713557 100644 --- a/l10n_br_cte_spec/models/spec_mixin.py +++ b/l10n_br_cte_spec/models/spec_mixin.py @@ -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", diff --git a/l10n_br_cte_spec/tests/test_cte_import.py b/l10n_br_cte_spec/tests/test_cte_import.py index fc39651fc2a8..864b982a200c 100644 --- a/l10n_br_cte_spec/tests/test_cte_import.py +++ b/l10n_br_cte_spec/tests/test_cte_import.py @@ -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 @@ -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"): @@ -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 @@ -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", @@ -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.infNFe, create=False) + ) self.assertEqual(cte.cte40_infCte.cte40_emit.cte40_CNPJ, "78408960000182")