Skip to content

Commit

Permalink
[IMP] l10n_br_cnab_structure: improve unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniospneto committed Nov 5, 2022
1 parent 00c941f commit f1a0243
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
File renamed without changes.
39 changes: 36 additions & 3 deletions l10n_br_cnab_structure/tests/test_cnab_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@
# @author Antônio S. Pereira Neto <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


import base64

from odoo import fields
from odoo.tests.common import tagged

from odoo.addons.account.tests.common import AccountTestInvoicingCommon


# Help function
def replace_chars(string, index, replacement):
return string[:index] + replacement + string[index + len(replacement) :]


@tagged("post_install", "-at_install")
class TestCNABStructure(AccountTestInvoicingCommon):
@classmethod
def setUpClass(cls, chart_template_ref=None):
super().setUpClass(chart_template_ref=chart_template_ref)
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.company = cls.company_data["company"]
cls.company.update({"cnpj_cpf": "82688625000152"})
cls.env.user.company_id = cls.company.id
cls.res_partner_bank_model = cls.env["res.partner.bank"]
cls.payment_mode_model = cls.env["account.payment.mode"]
Expand All @@ -23,6 +32,9 @@ def setUpClass(cls, chart_template_ref=None):
cls.attachment_model = cls.env["ir.attachment"]
cls.res_partner_pix_model = cls.env["res.partner.pix"]
cls.bank_341 = cls.env.ref("l10n_br_base.res_bank_341")
cls.import_wizard_obj = cls.env["cnab.import.wizard"]
cls.cnab_log_obj = cls.env["l10n_br_cnab.return.log"]
cls.partner_a.update({"cnpj_cpf": "45823449000198"})
cls.res_partner_pix_model.create(
{
"partner_id": cls.partner_a.id,
Expand Down Expand Up @@ -107,7 +119,7 @@ def setUpClass(cls, chart_template_ref=None):
]
cls.payment_order_model.search(cls.domain).unlink()

def test_file_generete(self):
def test_file_generete_and_return(self):
# Open invoice
self.invoice.action_post()
# Add to payment order using the wizard
Expand All @@ -119,5 +131,26 @@ def test_file_generete(self):
# Open payment order
payment_order.draft2open()
action = payment_order.open2generated()
attachment = self.attachment_model.browse(action["res_id"])
self.assertIsNotNone(attachment)
delivery_cnab_file = self.attachment_model.browse(action["res_id"])
self.assertIsNotNone(delivery_cnab_file)

cnab_data = base64.b64decode(delivery_cnab_file.datas).decode()
lines = cnab_data.splitlines()
lines[2] = replace_chars(lines[2], 154, "10112022")
lines[2] = replace_chars(lines[2], 172, "300")
return_data = "\r\n".join(lines).encode()

self.wizard = self.import_wizard_obj.create(
{
"journal_id": self.bank_journal_itau.id,
"return_file": base64.b64encode(return_data),
"filename": "TEST.RET",
"type": "outbound",
"cnab_structure_id": self.env.ref(
"l10n_br_cnab_structure.cnab_itau_240"
).id,
}
)
action = self.wizard.import_cnab()
cnab_log = self.cnab_log_obj.browse(action["res_id"])
self.assertIsNotNone(cnab_log)
3 changes: 2 additions & 1 deletion l10n_br_cnab_structure/wizard/cnab_import_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def _get_lines_from_file(self, file):
file = base64.b64decode(self.return_file)
string = StringIO(file.decode("utf-8"))
lines = string.readlines()
lines = [line.replace("\r\n", "") for line in lines]
# Remove special character at the end of the line.
lines = [line.replace("\r", "").replace("\n", "") for line in lines]
return lines

def _check_bank(self, line):
Expand Down

0 comments on commit f1a0243

Please sign in to comment.