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.

150 lines
5.2 KiB

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