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.

107 lines
3.8 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 openerp.tests.common import TransactionCase
  11. from openerp.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. class TestImport(TransactionCase):
  42. """Run test to import camt import."""
  43. transactions = [
  44. {
  45. 'account_number': 'NL46ABNA0499998748',
  46. 'amount': -754.25,
  47. 'date': '2014-01-05',
  48. 'ref': '435005714488-ABNO33052620',
  49. },
  50. ]
  51. def setUp(self):
  52. super(TestImport, self).setUp()
  53. bank = self.env['res.partner.bank'].create({
  54. 'acc_number': 'NL77ABNA0574908765',
  55. 'partner_id': self.env.ref('base.main_partner').id,
  56. 'company_id': self.env.ref('base.main_company').id,
  57. 'bank_id': self.env.ref('base.res_bank_1').id,
  58. })
  59. self.env['account.journal'].create({
  60. 'name': 'Bank Journal - (test camt)',
  61. 'code': 'TBNKCAMT',
  62. 'type': 'bank',
  63. 'bank_account_id': bank.id,
  64. })
  65. def test_statement_import(self):
  66. """Test correct creation of single statement."""
  67. action = {}
  68. with file_open(DATA_DIR + 'test-camt053') as testfile:
  69. action = self.env['account.bank.statement.import'].create({
  70. 'data_file': base64.b64encode(testfile.read()),
  71. }).import_file()
  72. for statement in self.env['account.bank.statement'].browse(
  73. action['context']['statement_ids']
  74. ):
  75. self.assertTrue(any(
  76. all(
  77. line[key] == self.transactions[0][key]
  78. for key in ['amount', 'date', 'ref']
  79. ) and
  80. line.bank_account_id.acc_number ==
  81. self.transactions[0]['account_number']
  82. for line in statement.line_ids
  83. ))
  84. def test_zip_import(self):
  85. """Test import of multiple statements from zip file."""
  86. with file_open(
  87. 'account_bank_statement_import_camt/test_files/test-camt053.zip'
  88. ) as testfile:
  89. action = self.env['account.bank.statement.import'].create({
  90. 'data_file': base64.b64encode(testfile.read()),
  91. }).import_file()
  92. for statement in self.env['account.bank.statement'].browse(
  93. action['context']['statement_ids']
  94. ):
  95. self.assertTrue(statement.line_ids)