Skip to content

Commit

Permalink
[FIX] l10n_br_mdfe_spec: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelsavegnago committed Oct 21, 2024
1 parent 082cf14 commit fb1f8ee
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions l10n_br_mdfe_spec/tests/test_mdfe_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

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_mixin

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_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 fb1f8ee

Please sign in to comment.