Skip to content

Commit

Permalink
[RFC] l10n_br_mdfe_spec: refactor to work multi-schema
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelsavegnago committed Oct 21, 2024
1 parent 7beaac4 commit 5710c79
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
2 changes: 1 addition & 1 deletion l10n_br_mdfe_spec/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from . import spec_models
from . import spec_mixin
from . import v3_0
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
class MdfeSpecMixin(models.AbstractModel):
_description = "Abstract Model"
_name = "spec.mixin.mdfe"
_field_prefix = "mdfe30_"
_schema_name = "mdfe"
_schema_version = "3.0.0"
_odoo_module = "l10n_br_mdfe"
_spec_module = "odoo.addons.l10n_br_mdfe_spec.models.v3_0.mdfe_tipos_basico_v3_00"
_binding_module = "nfelib.mdfe.bindings.v3_0.mdfe_tipos_basico_v3_00"
_spec_tab_name = "mdfe"
_mdfe30_odoo_module = (
"odoo.addons.l10n_br_mdfe_spec.models.v3_0.mdfe_tipos_basico_v3_00"
)
_mdfe30_binding_module = "nfelib.mdfe.bindings.v3_0.mdfe_tipos_basico_v3_00"

brl_currency_id = fields.Many2one(
comodel_name="res.currency",
Expand Down
57 changes: 25 additions & 32 deletions l10n_br_mdfe_spec/tests/test_mdfe_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

import nfelib
import pkg_resources
from nfelib.mdfe.bindings.v3_0.mdfe_v3_00 import Mdfe
from xsdata.formats.dataclass.parsers import XmlParser
from nfelib.mdfe.bindings.v3_0.mdfe_v3_00 import Tmdfe

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

from ..models import spec_models
from ..models import spec_mixin

tz_datetime = re.compile(r".*[-+]0[0-9]:00$")

Expand All @@ -33,15 +32,12 @@ def build_attrs_fake(self, node, create_m2o=False):
fields = self.fields_get()
vals = self.default_get(fields.keys())
for fname, fspec in node.__dataclass_fields__.items():
if fname == "any_element":
if fname == "any_element": # FIXME in spec_driven_model
continue
value = getattr(node, fname)
if value is None:
continue
key = "%s%s" % (
self._field_prefix,
fspec.metadata.get("name", fname),
)
key = f"mdfe30_{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,12 +62,8 @@ def build_attrs_fake(self, node, create_m2o=False):
key = fields[key]["related"][0]
comodel_name = fields[key]["relation"]
else:
clean_type = binding_type.lower() # TODO double check
comodel_name = "%s.%s.%s" % (
self._schema_name,
self._schema_version.replace(".", "")[0:2],
clean_type.split(".")[-1],
)
clean_type = binding_type.lower()
comodel_name = f"mdfe.30.{clean_type.split('.')[-1]}"
comodel = self.env.get(comodel_name)
if comodel is None: # example skip ICMS100 class
continue
Expand Down Expand Up @@ -114,13 +106,12 @@ def match_or_create_m2o_fake(self, comodel, new_value, create_m2o=False):
return comodel.new(new_value).id


# spec_models.CteSpecMixin._update_cache = _update_cache
spec_models.MdfeSpecMixin.build_fake = build_fake
spec_models.MdfeSpecMixin.build_attrs_fake = build_attrs_fake
spec_models.MdfeSpecMixin.match_or_create_m2o_fake = match_or_create_m2o_fake
spec_mixin.MdfeSpecMixin.build_fake = build_fake
spec_mixin.MdfeSpecMixin.build_attrs_fake = build_attrs_fake
spec_mixin.MdfeSpecMixin.match_or_create_m2o_fake = match_or_create_m2o_fake


class MdfeImportTest(TransactionCase):
class MdfeImportTest(SavepointCase):
def test_import_mdfe(self):
res_items = (
"mdfe",
Expand All @@ -129,13 +120,14 @@ def test_import_mdfe(self):
"41190876676436000167580010000500001000437558-mdfe.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(), Mdfe)
inf_mdfe = self.env["mdfe.30.tmdfe_infmdfe"].build_fake(
binding.infMDFe, create=False
mdfe_stream = pkg_resources.resource_stream(nfelib.__name__, resource_path)
binding = Tmdfe.from_xml(mdfe_stream.read().decode())
mdfe = (
self.env["mdfe.30.tmdfe_infmdfe"]
.with_context(tracking_disable=True, edoc_type="in", lang="pt_BR")
.build_fake(binding.infMDFe, create=False)
)
self.assertEqual(inf_mdfe.mdfe30_emit.mdfe30_CNPJ, "76676436000167")
self.assertEqual(mdfe.mdfe30_emit.mdfe30_CNPJ, "76676436000167")

def test_import_mdfe2(self):
res_items = (
Expand All @@ -145,10 +137,11 @@ def test_import_mdfe2(self):
"50170876063965000276580010000011311421039568-mdfe.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(), Mdfe)
inf_mdfe = self.env["mdfe.30.tmdfe_infmdfe"].build_fake(
binding.infMDFe, create=False
mdfe_stream = pkg_resources.resource_stream(nfelib.__name__, resource_path)
binding = Tmdfe.from_xml(mdfe_stream.read().decode())
mdfe = (
self.env["mdfe.30.tmdfe_infmdfe"]
.with_context(tracking_disable=True, edoc_type="in", lang="pt_BR")
.build_fake(binding.infMDFe, create=False)
)
self.assertEqual(inf_mdfe.mdfe30_emit.mdfe30_xNome, "TESTE")
self.assertEqual(mdfe.mdfe30_emit.mdfe30_xNome, "TESTE")

0 comments on commit 5710c79

Please sign in to comment.