[FIX] account_bank_statement_import_transfer_move: Resilient test

Don't depend at all of any external statement import, in data nor in code.

For that, we mock the corresponding methods for getting the expected data.
This commit is contained in:
Pedro M. Baeza
2020-08-06 14:05:28 +02:00
parent a9b51da260
commit c25d4d4d60

View File

@@ -1,9 +1,8 @@
# Copyright 2020 Camptocamp SA
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import base64
import unittest
from unittest.mock import patch
from odoo.modules.module import get_module_resource
from odoo.tests.common import SavepointCase
@@ -36,43 +35,67 @@ class TestGenerateBankStatement(SavepointCase):
}
)
def _load_statement(self):
module = self.env["ir.module.module"].search(
def _parse_file(self, data_file):
"""Fake method for returning valuable data. Extracted from CAMT demo"""
return (
None,
"NL77ABNA0574908765",
[
("name", "=", "account_bank_statement_import_camt_oca"),
("state", "=", "installed"),
]
{
"balance_end_real": 15121.12,
"balance_start": 15568.27,
"date": "2014-01-05",
"name": "1234Test/1",
"transactions": [
{
"account_number": "NL46ABNA0499998748",
"amount": -754.25,
"date": "2014-01-05",
"name": "Insurance policy 857239PERIOD 01.01.2014 - "
"31.12.2014",
"note": "MKB Insurance 859239PERIOD 01.01.2014 - "
"31.12.2014",
"partner_name": "INSURANCE COMPANY TESTX",
"ref": "435005714488-ABNO33052620",
},
],
}
],
)
if not (module):
raise unittest.SkipTest(
"account_bank_statement_import_camt_oca module is not installed"
)
testfile = get_module_resource(
"account_bank_statement_import_camt_oca", "test_files", "test-camt053"
)
with open(testfile, "rb") as datafile:
camt_file = base64.b64encode(datafile.read())
def _get_bank_statements_available_import_formats(self):
"""Fake method for returning a fake importer for not having errors."""
return ["test"]
self.env["account.bank.statement.import"].create(
{"attachment_ids": [(0, 0, {"name": "test file", "datas": camt_file})]}
).import_file()
bank_st_record = self.env["account.bank.statement"].search(
[("name", "=", "1234Test/1")], limit=1
)
statement_lines = bank_st_record.line_ids
return statement_lines
def _load_statement(self):
module = "odoo.addons.account_bank_statement_import"
with patch(
module
+ ".account_journal.AccountJournal"
+ "._get_bank_statements_available_import_formats",
self._get_bank_statements_available_import_formats,
):
with patch(
module
+ ".account_bank_statement_import"
+ ".AccountBankStatementImport._parse_file",
self._parse_file,
):
self.env["account.bank.statement.import"].create(
{"attachment_ids": [(0, 0, {"name": "test file", "datas": b""})]}
).import_file()
bank_st_record = self.env["account.bank.statement"].search(
[("name", "=", "1234Test/1")], limit=1
)
statement_lines = bank_st_record.line_ids
return statement_lines
def test_statement_import(self):
self.journal.transfer_line = True
lines = self._load_statement()
self.assertEqual(len(lines), 5)
self.assertEqual(len(lines), 2)
self.assertAlmostEqual(sum(lines.mapped("amount")), 0)
self.journal.transfer_line = False
lines = self._load_statement()
self.assertEqual(len(lines), 4)
self.assertAlmostEqual(sum(lines.mapped("amount")), -12.99)
self.assertEqual(len(lines), 1)
self.assertAlmostEqual(sum(lines.mapped("amount")), -754.25)