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.7 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. def setUp(self):
  67. super(TestImport, self).setUp()
  68. bank = self.env['res.partner.bank'].create({
  69. 'acc_number': 'NL77ABNA0574908765',
  70. 'partner_id': self.env.ref('base.main_partner').id,
  71. 'company_id': self.env.ref('base.main_company').id,
  72. 'bank_id': self.env.ref('base.res_bank_1').id,
  73. })
  74. self.env['account.journal'].create({
  75. 'name': 'Bank Journal - (test camt)',
  76. 'code': 'TBNKCAMT',
  77. 'type': 'bank',
  78. 'bank_account_id': bank.id,
  79. })
  80. def test_statement_import(self):
  81. """Test correct creation of single statement."""
  82. testfile = get_module_resource(
  83. 'account_bank_statement_import_camt_oca',
  84. 'test_files',
  85. 'test-camt053',
  86. )
  87. with open(testfile, 'rb') as datafile:
  88. action = self.env['account.bank.statement.import'].create({
  89. 'data_file': base64.b64encode(datafile.read())
  90. }).import_file()
  91. for statement in self.env['account.bank.statement'].browse(
  92. action['context']['statement_ids']
  93. ):
  94. self.assertTrue(any(
  95. all(
  96. line[key] == self.transactions[0][key]
  97. for key in ['amount', 'date', 'ref']
  98. ) and
  99. line.bank_account_id.acc_number ==
  100. self.transactions[0]['account_number']
  101. for line in statement.line_ids
  102. ))
  103. def test_zip_import(self):
  104. """Test import of multiple statements from zip file."""
  105. testfile = get_module_resource(
  106. 'account_bank_statement_import_camt_oca',
  107. 'test_files',
  108. 'test-camt053.zip',
  109. )
  110. with open(testfile, 'rb') as datafile:
  111. action = self.env['account.bank.statement.import'].create({
  112. 'data_file': base64.b64encode(datafile.read()),
  113. }).import_file()
  114. for statement in self.env['account.bank.statement'].browse(
  115. action['context']['statement_ids']
  116. ):
  117. self.assertTrue(statement.line_ids)