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.

201 lines
7.5 KiB

  1. # Copyright 2019 Tecnativa - Vicent Cubells
  2. # Copyright 2019 Brainbean Apps (https://brainbeanapps.com)
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  4. from odoo import fields
  5. from odoo.exceptions import UserError
  6. from odoo.tests import common
  7. from base64 import b64encode
  8. from os import path
  9. class TestAccountBankStatementImportPayPal(common.TransactionCase):
  10. def setUp(self):
  11. super().setUp()
  12. self.now = fields.Datetime.now()
  13. self.currency_eur = self.env.ref('base.EUR')
  14. self.currency_usd = self.env.ref('base.USD')
  15. self.paypal_statement_map_en = self.env.ref(
  16. 'account_bank_statement_import_paypal.paypal_statement_map_en'
  17. )
  18. self.paypal_statement_map_es = self.env.ref(
  19. 'account_bank_statement_import_paypal.paypal_statement_map_es'
  20. )
  21. self.paypal_activity_map_en = self.env.ref(
  22. 'account_bank_statement_import_paypal.paypal_activity_map_en'
  23. )
  24. self.AccountJournal = self.env['account.journal']
  25. self.AccountBankStatement = self.env['account.bank.statement']
  26. self.AccountBankStatementImport = self.env[
  27. 'account.bank.statement.import'
  28. ]
  29. self.AccountBankStatementImportPayPalMapping = self.env[
  30. 'account.bank.statement.import.paypal.mapping'
  31. ]
  32. self.AccountBankStatementImportPayPalMappingWizard = self.env[
  33. 'account.bank.statement.import.paypal.mapping.wizard'
  34. ]
  35. def _data_file(self, filename):
  36. with open(path.join(path.dirname(__file__), filename)) as file:
  37. return b64encode(file.read().encode('utf-8'))
  38. def test_import_statement_en_usd(self):
  39. journal = self.AccountJournal.create({
  40. 'name': 'PayPal',
  41. 'type': 'bank',
  42. 'code': 'PP',
  43. 'currency_id': self.currency_usd.id,
  44. })
  45. wizard = self.AccountBankStatementImport.with_context({
  46. 'journal_id': journal.id,
  47. }).create({
  48. 'filename': 'fixtures/statement_en.csv',
  49. 'data_file': self._data_file('fixtures/statement_en.csv'),
  50. 'paypal_mapping_id': self.paypal_statement_map_en.id,
  51. })
  52. wizard.with_context({
  53. 'journal_id': journal.id,
  54. 'account_bank_statement_import_paypal_test': True,
  55. }).import_file()
  56. statement = self.AccountBankStatement.search([
  57. ('journal_id', '=', journal.id),
  58. ])
  59. self.assertEqual(len(statement), 1)
  60. self.assertEqual(len(statement.line_ids), 18)
  61. def test_import_statement_en_eur(self):
  62. journal = self.AccountJournal.create({
  63. 'name': 'PayPal',
  64. 'type': 'bank',
  65. 'code': 'PP',
  66. 'currency_id': self.currency_eur.id,
  67. })
  68. wizard = self.AccountBankStatementImport.with_context({
  69. 'journal_id': journal.id,
  70. }).create({
  71. 'filename': 'fixtures/statement_en.csv',
  72. 'data_file': self._data_file('fixtures/statement_en.csv'),
  73. 'paypal_mapping_id': self.paypal_statement_map_en.id,
  74. })
  75. wizard.with_context({
  76. 'journal_id': journal.id,
  77. 'account_bank_statement_import_paypal_test': True,
  78. }).import_file()
  79. statement = self.AccountBankStatement.search([
  80. ('journal_id', '=', journal.id),
  81. ])
  82. self.assertEqual(len(statement), 1)
  83. self.assertEqual(len(statement.line_ids), 8)
  84. def test_import_statement_es(self):
  85. journal = self.AccountJournal.create({
  86. 'name': 'PayPal',
  87. 'type': 'bank',
  88. 'code': 'PP',
  89. 'currency_id': self.currency_eur.id,
  90. })
  91. wizard = self.AccountBankStatementImport.with_context({
  92. 'journal_id': journal.id,
  93. }).create({
  94. 'filename': 'fixtures/statement_es.csv',
  95. 'data_file': self._data_file('fixtures/statement_es.csv'),
  96. 'paypal_mapping_id': self.paypal_statement_map_es.id,
  97. })
  98. wizard.with_context({
  99. 'journal_id': journal.id,
  100. 'account_bank_statement_import_paypal_test': True,
  101. }).import_file()
  102. statement = self.AccountBankStatement.search([
  103. ('journal_id', '=', journal.id),
  104. ])
  105. self.assertEqual(len(statement), 1)
  106. self.assertEqual(len(statement.line_ids), 8)
  107. def test_import_activity_en(self):
  108. journal = self.AccountJournal.create({
  109. 'name': 'PayPal',
  110. 'type': 'bank',
  111. 'code': 'PP',
  112. 'currency_id': self.currency_usd.id,
  113. })
  114. wizard = self.AccountBankStatementImport.with_context({
  115. 'journal_id': journal.id,
  116. }).create({
  117. 'filename': 'fixtures/activity_en.csv',
  118. 'data_file': self._data_file('fixtures/activity_en.csv'),
  119. 'paypal_mapping_id': self.paypal_activity_map_en.id,
  120. })
  121. wizard.with_context({
  122. 'journal_id': journal.id,
  123. 'account_bank_statement_import_paypal_test': True,
  124. }).import_file()
  125. statement = self.AccountBankStatement.search([
  126. ('journal_id', '=', journal.id),
  127. ])
  128. self.assertEqual(len(statement), 1)
  129. self.assertEqual(len(statement.line_ids), 71)
  130. def test_import_empty_activity(self):
  131. journal = self.AccountJournal.create({
  132. 'name': 'PayPal',
  133. 'type': 'bank',
  134. 'code': 'PP',
  135. 'currency_id': self.currency_usd.id,
  136. })
  137. wizard = self.AccountBankStatementImport.with_context({
  138. 'journal_id': journal.id,
  139. }).create({
  140. 'filename': 'fixtures/empty_activity.csv',
  141. 'data_file': self._data_file('fixtures/empty_activity.csv'),
  142. 'paypal_mapping_id': self.paypal_activity_map_en.id,
  143. })
  144. with self.assertRaises(UserError):
  145. wizard.with_context({
  146. 'journal_id': journal.id,
  147. 'account_bank_statement_import_paypal_test': True,
  148. }).import_file()
  149. statement = self.AccountBankStatement.search([
  150. ('journal_id', '=', journal.id),
  151. ])
  152. self.assertEqual(len(statement), 0)
  153. def test_import_activity_mapping_en(self):
  154. with common.Form(
  155. self.AccountBankStatementImportPayPalMappingWizard) as form:
  156. form.filename = 'fixtures/activity_en.csv'
  157. form.data_file = self._data_file(
  158. 'fixtures/activity_en.csv'
  159. )
  160. self.assertEqual(
  161. len(
  162. self.AccountBankStatementImportPayPalMappingWizard
  163. .with_context(
  164. header=form.header,
  165. ).statement_columns()
  166. ),
  167. 22
  168. )
  169. wizard = form.save()
  170. wizard.import_mapping()
  171. def test_import_statement_mapping_en(self):
  172. with common.Form(
  173. self.AccountBankStatementImportPayPalMappingWizard) as form:
  174. form.filename = 'fixtures/statement_en.csv'
  175. form.data_file = self._data_file(
  176. 'fixtures/statement_en.csv'
  177. )
  178. self.assertEqual(
  179. len(
  180. self.AccountBankStatementImportPayPalMappingWizard
  181. .with_context(
  182. header=form.header,
  183. ).statement_columns()
  184. ),
  185. 18
  186. )
  187. wizard = form.save()
  188. wizard.import_mapping()