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.

99 lines
3.8 KiB

  1. # Copyright 2019 Tecnativa - Vicent Cubells
  2. # Copyright 2019 Eficent Business and IT Consulting Services, S.L.
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import os
  5. import base64
  6. from odoo.tests import common
  7. class TestTxtFile(common.TransactionCase):
  8. def setUp(self):
  9. super(TestTxtFile, self).setUp()
  10. self.map = self.env['account.bank.statement.import.map'].create({
  11. 'name': 'Txt Map Test',
  12. })
  13. usd = self.env.ref('base.USD')
  14. self.journal = self.env['account.journal'].create({
  15. 'name': 'Txt Bank',
  16. 'type': 'bank',
  17. 'code': 'TXT',
  18. 'currency_id': (
  19. usd.id if self.env.user.company_id.currency_id != usd
  20. else False
  21. ),
  22. })
  23. def _do_import_xlsx(self, file_name):
  24. file_name = os.path.join(os.path.dirname(__file__), file_name)
  25. with open(file_name, 'rb') as fin:
  26. data = fin.read()
  27. return data
  28. def _do_import(self, file_name):
  29. file_name = os.path.join(os.path.dirname(__file__), file_name)
  30. return open(file_name).read()
  31. def test_import_header(self):
  32. file = self._do_import('sample_statement_en.csv')
  33. file = base64.b64encode(file.encode("utf-8"))
  34. wizard = self.env['wizard.txt.map.create'].with_context({
  35. 'journal_id': self.journal.id,
  36. 'active_ids': [self.map.id],
  37. }).create({'data_file': file})
  38. wizard.create_map_lines()
  39. self.assertEqual(len(self.map.map_line_ids.ids), 7)
  40. def test_import_txt_file(self):
  41. # Current statements before to run the wizard
  42. old_statements = self.env['account.bank.statement'].search([])
  43. # This journal is for Txt statements
  44. txt_map = self.env.ref(
  45. 'account_bank_statement_import_txt_xlsx.txt_map'
  46. )
  47. self.journal.statement_import_txt_map_id = txt_map.id
  48. file = self._do_import('sample_statement_en.csv')
  49. file = base64.b64encode(file.encode("utf-8"))
  50. wizard = self.env['account.bank.statement.import'].with_context({
  51. 'journal_id': self.journal.id,
  52. }).create({'data_file': file})
  53. wizard.import_file()
  54. staments_now = self.env['account.bank.statement'].search([])
  55. statement = staments_now - old_statements
  56. self.assertEqual(len(statement.line_ids), 2)
  57. self.assertEqual(len(statement.mapped('line_ids').filtered(
  58. lambda x: x.partner_id)), 1)
  59. self.assertAlmostEqual(
  60. sum(statement.mapped('line_ids.amount')), 1491.50
  61. )
  62. self.assertAlmostEqual(
  63. sum(statement.mapped('line_ids.amount_currency')), 1000.00
  64. )
  65. def test_import_xlsx_file(self):
  66. # Current statements before to run the wizard
  67. old_statements = self.env['account.bank.statement'].search([])
  68. # This journal is for Txt statements
  69. txt_map = self.env.ref(
  70. 'account_bank_statement_import_txt_xlsx.txt_map'
  71. )
  72. self.journal.statement_import_txt_map_id = txt_map.id
  73. file = self._do_import_xlsx('sample_statement_en.xlsx')
  74. file = base64.b64encode(file)
  75. wizard = self.env['account.bank.statement.import'].with_context({
  76. 'journal_id': self.journal.id,
  77. }).create({'data_file': file})
  78. wizard.import_file()
  79. staments_now = self.env['account.bank.statement'].search([])
  80. statement = staments_now - old_statements
  81. self.assertEqual(len(statement.line_ids), 2)
  82. self.assertEqual(len(statement.mapped('line_ids').filtered(
  83. lambda x: x.partner_id)), 1)
  84. self.assertAlmostEqual(
  85. sum(statement.mapped('line_ids.amount')), 1491.50
  86. )
  87. self.assertAlmostEqual(
  88. sum(statement.mapped('line_ids.amount_currency')), 1000.00
  89. )