You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.6 KiB

  1. # -*- coding: utf-8 -*-
  2. """Run test to import camt.053 import."""
  3. # © 2013-2016 Therp BV <http://therp.nl>
  4. # Copyright 2017 Open Net Sàrl
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. import base64
  7. import difflib
  8. import pprint
  9. import tempfile
  10. from odoo.tests.common import TransactionCase
  11. from odoo.tools.misc import file_open
  12. DATA_DIR = 'account_bank_statement_import_camt/test_files/'
  13. class TestParser(TransactionCase):
  14. """Tests for the camt parser itself."""
  15. def setUp(self):
  16. super(TestParser, self).setUp()
  17. self.parser = self.env['account.bank.statement.import.camt.parser']
  18. def _do_parse_test(self, inputfile, goldenfile):
  19. with file_open(inputfile) as testfile:
  20. data = testfile.read()
  21. res = self.parser.parse(data)
  22. with tempfile.NamedTemporaryFile(suffix='.pydata') as temp:
  23. pprint.pprint(res, temp)
  24. with file_open(goldenfile) as golden:
  25. temp.seek(0)
  26. diff = list(
  27. difflib.unified_diff(golden.readlines(), temp.readlines(),
  28. golden.name, temp.name))
  29. if len(diff) > 2:
  30. self.fail(
  31. "actual output doesn't match exptected output:\n%s" %
  32. "".join(diff))
  33. def test_parse(self):
  34. self._do_parse_test(
  35. DATA_DIR + 'test-camt053',
  36. DATA_DIR + 'golden-camt053.pydata')
  37. def test_parse_txdtls(self):
  38. self._do_parse_test(
  39. DATA_DIR + 'test-camt053-txdtls',
  40. DATA_DIR + 'golden-camt053-txdtls.pydata')
  41. def test_parse_no_ntry(self):
  42. self._do_parse_test(
  43. DATA_DIR + 'test-camt053-no-ntry',
  44. DATA_DIR + 'golden-camt053-no-ntry.pydata',
  45. )
  46. class TestImport(TransactionCase):
  47. """Run test to import camt import."""
  48. transactions = [
  49. {
  50. 'account_number': 'NL46ABNA0499998748',
  51. 'amount': -754.25,
  52. 'date': '2014-01-05',
  53. 'ref': '435005714488-ABNO33052620',
  54. },
  55. {
  56. 'remote_account': 'NL46ABNA0499998748',
  57. 'transferred_amount': -564.05,
  58. 'value_date': '2014-01-05',
  59. 'ref': 'TESTBANK/NL/20141229/01206408',
  60. },
  61. {
  62. 'remote_account': 'NL46ABNA0499998748',
  63. 'transferred_amount': -100.0,
  64. 'value_date': '2014-01-05',
  65. 'ref': 'TESTBANK/NL/20141229/01206407',
  66. },
  67. {
  68. 'remote_account': 'NL69ABNA0522123643',
  69. 'transferred_amount': 1405.31,
  70. 'value_date': '2014-01-05',
  71. 'ref': '115',
  72. },
  73. ]
  74. def setUp(self):
  75. super(TestImport, self).setUp()
  76. bank = self.env['res.partner.bank'].create({
  77. 'acc_number': 'NL77ABNA0574908765',
  78. 'partner_id': self.env.ref('base.main_partner').id,
  79. 'company_id': self.env.ref('base.main_company').id,
  80. 'bank_id': self.env.ref('base.res_bank_1').id,
  81. })
  82. self.env['account.journal'].create({
  83. 'name': 'Bank Journal - (test camt)',
  84. 'code': 'TBNKCAMT',
  85. 'type': 'bank',
  86. 'bank_account_id': bank.id,
  87. })
  88. def test_statement_import(self):
  89. """Test correct creation of single statement."""
  90. action = {}
  91. with file_open(DATA_DIR + 'test-camt053') as testfile:
  92. action = self.env['account.bank.statement.import'].create({
  93. 'data_file': base64.b64encode(testfile.read()),
  94. }).import_file()
  95. for statement in self.env['account.bank.statement'].browse(
  96. action['context']['statement_ids']
  97. ):
  98. self.assertTrue(any(
  99. all(
  100. line[key] == self.transactions[0][key]
  101. for key in ['amount', 'date', 'ref']
  102. ) and
  103. line.bank_account_id.acc_number ==
  104. self.transactions[0]['account_number']
  105. for line in statement.line_ids
  106. ))
  107. def test_zip_import(self):
  108. """Test import of multiple statements from zip file."""
  109. with file_open(
  110. 'account_bank_statement_import_camt/test_files/test-camt053.zip'
  111. ) as testfile:
  112. action = self.env['account.bank.statement.import'].create({
  113. 'data_file': base64.b64encode(testfile.read()),
  114. }).import_file()
  115. for statement in self.env['account.bank.statement'].browse(
  116. action['context']['statement_ids']
  117. ):
  118. self.assertTrue(statement.line_ids)