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
5.2 KiB

  1. # -*- coding: utf-8 -*-
  2. """Provide common base for bank statement import tests."""
  3. ##############################################################################
  4. #
  5. # Copyright (C) 2015 Therp BV <http://therp.nl>.
  6. #
  7. # All other contributions are (C) by their respective contributors
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU Affero General Public License as
  11. # published by the Free Software Foundation, either version 3 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Affero General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. ##############################################################################
  23. import logging
  24. from openerp.tests.common import TransactionCase
  25. from openerp.modules.module import get_module_resource
  26. _logger = logging.getLogger(__name__)
  27. class TestStatementFile(TransactionCase):
  28. """Check wether statements with transactions correctly imported.
  29. No actual tests are done in this class, implementations are in
  30. subclasses in actual import modules.
  31. """
  32. def _test_transaction(
  33. self, statement_obj, remote_account=False,
  34. transferred_amount=False, value_date=False, ref=False):
  35. """Check wether transaction with attributes passed was created.
  36. Actually this method also tests wether automatic creation of
  37. partner bank accounts is working.
  38. """
  39. transaction_model = self.env['account.bank.statement.line']
  40. partner_bank_model = self.env['res.partner.bank']
  41. domain = [('statement_id', '=', statement_obj.id)]
  42. if remote_account:
  43. bids = partner_bank_model.search(
  44. [('acc_number', '=', remote_account)])
  45. self.assertTrue(
  46. bids,
  47. 'Bank-account %s not found after parse.' % remote_account
  48. )
  49. domain.append(('bank_account_id', '=', bids[0].id))
  50. if transferred_amount:
  51. domain.append(('amount', '=', transferred_amount))
  52. if value_date:
  53. domain.append(('date', '=', value_date))
  54. if ref:
  55. domain.append(('ref', '=', ref))
  56. ids = transaction_model.search(domain)
  57. if not ids:
  58. # We will get assertion error, but to solve we need to see
  59. # what transactions have been added:
  60. self.cr.execute(
  61. "select name, date, amount, ref, bank_account_id"
  62. " from account_bank_statement_line"
  63. " where statement_id=%d" % statement_obj.id)
  64. _logger.error(
  65. "Transaction not found in %s" %
  66. str(self.cr.fetchall())
  67. )
  68. self.assertTrue(
  69. ids,
  70. 'Transaction %s not found after parse.' % str(domain)
  71. )
  72. def _test_statement_import(
  73. self, module_name, file_name, statement_name, local_account=False,
  74. start_balance=False, end_balance=False, transactions=None):
  75. """Test correct creation of single statement."""
  76. import_model = self.env['account.bank.statement.import']
  77. partner_bank_model = self.env['res.partner.bank']
  78. statement_model = self.env['account.bank.statement']
  79. statement_path = get_module_resource(
  80. module_name,
  81. 'test_files',
  82. file_name
  83. )
  84. statement_file = open(
  85. statement_path, 'rb').read().encode('base64')
  86. bank_statement_id = import_model.create(
  87. dict(
  88. data_file=statement_file,
  89. filename=file_name,
  90. )
  91. )
  92. bank_statement_id.import_file()
  93. # Check wether bank account has been created:
  94. if local_account:
  95. bids = partner_bank_model.search(
  96. [('acc_number', '=', local_account)])
  97. self.assertTrue(
  98. bids,
  99. 'Bank account %s not created from statement' % local_account
  100. )
  101. # statement name is account number + '-' + date of last 62F line:
  102. ids = statement_model.search([('name', '=', statement_name)])
  103. self.assertTrue(
  104. ids,
  105. 'Statement %s not found after parse.' % statement_name
  106. )
  107. statement_obj = ids[0]
  108. if start_balance:
  109. self.assertTrue(
  110. abs(statement_obj.balance_start - start_balance) < 0.00001,
  111. 'Start balance %f not equal to expected %f' %
  112. (statement_obj.balance_start, start_balance)
  113. )
  114. if end_balance:
  115. self.assertTrue(
  116. abs(statement_obj.balance_end_real - end_balance) < 0.00001,
  117. 'End balance %f not equal to expected %f' %
  118. (statement_obj.balance_end_real, end_balance)
  119. )
  120. # Maybe we need to test transactions?
  121. if transactions:
  122. for transaction in transactions:
  123. self._test_transaction(statement_obj, **transaction)