test_edi.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.addons.account_edi.tests.common import AccountEdiTestCommon
  4. from odoo.addons.base.tests.test_ir_cron import CronMixinCase
  5. from odoo.tests import tagged
  6. @tagged('post_install', '-at_install')
  7. class TestAccountEdi(AccountEdiTestCommon, CronMixinCase):
  8. @classmethod
  9. def setUpClass(cls, chart_template_ref=None, edi_format_ref=None):
  10. super().setUpClass(chart_template_ref=chart_template_ref, edi_format_ref=edi_format_ref)
  11. cls.env['account.edi.document'].search([]).unlink()
  12. cls.env['account.edi.format'].search([]).unlink()
  13. cls.test_edi_format = cls.env['account.edi.format'].sudo().create({
  14. 'name': 'test_edi_format',
  15. 'code': 'test_edi_format',
  16. })
  17. cls.company_data['default_journal_sale'].edi_format_ids |= cls.test_edi_format
  18. def test_export_edi(self):
  19. invoice = self.init_invoice('out_invoice', products=self.product_a)
  20. self.assertEqual(len(invoice.edi_document_ids), 0)
  21. with self.with_custom_method('_get_move_applicability', lambda edi_format, inv: {'post': edi_format._test_edi_post_invoice}), \
  22. self.with_custom_method('_test_edi_post_invoice', lambda edi_format, inv: {inv: {'success': True}}):
  23. invoice.action_post()
  24. self.assertEqual(len(invoice.edi_document_ids), 1)
  25. def test_prepare_jobs_no_batching(self):
  26. invoice1 = self.init_invoice('out_invoice', products=self.product_a)
  27. invoice2 = self.init_invoice('out_invoice', products=self.product_a)
  28. with self.with_custom_method('_get_move_applicability', lambda edi_format, inv: {'post': edi_format._test_edi_post_invoice}), \
  29. self.with_custom_method('_test_edi_post_invoice', lambda edi_format, inv: {inv: {'success': True}}), \
  30. self.with_custom_method('_needs_web_services', lambda edi_format: True):
  31. (invoice1 + invoice2).action_post()
  32. jobs = (invoice1 + invoice2).edi_document_ids._prepare_jobs()
  33. self.assertEqual(len(jobs), 2)
  34. def test_prepare_jobs_batching(self):
  35. invoice1 = self.init_invoice('out_invoice', products=self.product_a)
  36. invoice2 = self.init_invoice('out_invoice', products=self.product_a)
  37. with self.with_custom_method('_get_move_applicability',
  38. lambda edi_format, inv: {
  39. 'post': edi_format._test_edi_post_invoice,
  40. 'post_batching': lambda inv: (inv.partner_id,),
  41. }), \
  42. self.with_custom_method('_test_edi_post_invoice', lambda edi_format, inv: {inv: {'success': True}}), \
  43. self.with_custom_method('_needs_web_services', lambda edi_format: True):
  44. (invoice1 + invoice2).action_post()
  45. jobs = (invoice1 + invoice2).edi_document_ids._prepare_jobs()
  46. self.assertEqual(len(jobs), 1)
  47. def test_warning_is_retried(self):
  48. invoice = self.init_invoice('out_invoice', products=self.product_a)
  49. with self.with_custom_method('_get_move_applicability', lambda edi_format, inv: {'post': edi_format._test_edi_post_invoice}), \
  50. self.with_custom_method('_needs_web_services', lambda edi_format: True):
  51. with self.with_custom_method('_test_edi_post_invoice',
  52. lambda edi_format, inv: {inv: {'error': "turlututu", 'blocking_level': 'warning'}}):
  53. invoice.action_post()
  54. self.assertRecordValues(invoice.edi_document_ids, [{'state': 'to_send'}])
  55. with self.with_custom_method('_test_edi_post_invoice', lambda edi_format, inv: {inv: {'success': True}}):
  56. invoice.action_process_edi_web_services(with_commit=False)
  57. self.assertRecordValues(invoice.edi_document_ids, [{'state': 'sent'}])
  58. def test_edi_flow(self):
  59. invoice = self.init_invoice('out_invoice', products=self.product_a)
  60. with self.with_custom_method('_get_move_applicability', lambda edi_format, inv: {
  61. 'post': edi_format._test_edi_post_invoice,
  62. 'cancel': edi_format._test_edi_cancel_invoice,
  63. }), \
  64. self.with_custom_method('_needs_web_services', lambda edi_format: True), \
  65. self.with_custom_method('_test_edi_post_invoice', lambda edi_format, inv: {inv: {'success': True}}), \
  66. self.with_custom_method('_test_edi_cancel_invoice', lambda edi_format, inv: {inv: {'success': True}}):
  67. invoice.action_post()
  68. self.assertRecordValues(invoice.edi_document_ids, [{'state': 'to_send'}])
  69. invoice.action_process_edi_web_services(with_commit=False)
  70. self.assertRecordValues(invoice.edi_document_ids, [{'state': 'sent'}])
  71. invoice.button_cancel_posted_moves()
  72. self.assertRecordValues(invoice.edi_document_ids, [{'state': 'to_cancel'}])
  73. invoice.button_abandon_cancel_posted_posted_moves()
  74. self.assertRecordValues(invoice.edi_document_ids, [{'state': 'sent'}])
  75. invoice.button_cancel_posted_moves()
  76. self.assertRecordValues(invoice.edi_document_ids, [{'state': 'to_cancel'}])
  77. invoice.action_process_edi_web_services(with_commit=False)
  78. self.assertRecordValues(invoice.edi_document_ids, [{'state': 'cancelled'}])
  79. def test_edi_flow_two_steps(self):
  80. def step1(edi_format, invoice):
  81. return {invoice: {'error': "step1 done", 'blocking_level': 'info'}}
  82. def step2(edi_format, invoice):
  83. return {invoice: {'success': True}}
  84. def get_move_applicability(edi_format, invoice):
  85. if "step1" in (invoice.edi_document_ids.error or ''):
  86. return {'post': edi_format._test_edi_post_invoice_step2}
  87. else:
  88. return {'post': edi_format._test_edi_post_invoice_step1}
  89. invoice = self.init_invoice('out_invoice', products=self.product_a)
  90. with self.with_custom_method('_get_move_applicability', get_move_applicability), \
  91. self.with_custom_method('_needs_web_services', lambda edi_format: True), \
  92. self.with_custom_method('_test_edi_post_invoice_step1', step1), \
  93. self.with_custom_method('_test_edi_post_invoice_step2', step2):
  94. invoice.action_post()
  95. self.assertRecordValues(invoice.edi_document_ids, [{'state': 'to_send'}])
  96. invoice.action_process_edi_web_services(with_commit=False)
  97. self.assertRecordValues(invoice.edi_document_ids, [{'state': 'to_send'}])
  98. invoice.action_process_edi_web_services(with_commit=False)
  99. self.assertRecordValues(invoice.edi_document_ids, [{'state': 'sent'}])
  100. def test_cron_triggers(self):
  101. invoice = self.init_invoice('out_invoice', products=self.product_a)
  102. with self.with_custom_method('_get_move_applicability', lambda edi_format, inv: {'post': edi_format._test_edi_post_invoice}), \
  103. self.with_custom_method('_needs_web_services', lambda edi_format: True), \
  104. self.with_custom_method('_test_edi_post_invoice', lambda edi_format, inv: {inv: {'success': True}}), \
  105. self.capture_triggers('account_edi.ir_cron_edi_network') as capt:
  106. invoice.action_post()
  107. capt.records.ensure_one()
  108. def test_cron_self_trigger(self):
  109. # Process single job by CRON call (and thus, disable the auto-commit).
  110. edi_cron = self.env.ref('account_edi.ir_cron_edi_network')
  111. edi_cron.code = 'model._cron_process_documents_web_services(job_count=1)'
  112. invoice1 = self.init_invoice('out_invoice', products=self.product_a)
  113. invoice2 = self.init_invoice('out_invoice', products=self.product_a)
  114. with self.with_custom_method('_get_move_applicability', lambda edi_format, inv: {'post': edi_format._test_edi_post_invoice}), \
  115. self.with_custom_method('_needs_web_services', lambda edi_format: True), \
  116. self.with_custom_method('_test_edi_post_invoice', lambda edi_format, inv: {inv: {'success': True}}), \
  117. self.capture_triggers('account_edi.ir_cron_edi_network') as capt:
  118. (invoice1 + invoice2).action_post()
  119. self.env.ref('account_edi.ir_cron_edi_network').method_direct_trigger()
  120. self.assertEqual(
  121. len(capt.records), 2,
  122. "Not all records have been processed in this run, the cron should re-trigger itself to process some"
  123. " more later",
  124. )
  125. def test_invoice_ready_to_be_sent(self):
  126. invoice = self.init_invoice('out_invoice', products=self.product_a)
  127. with self.with_custom_method('_get_move_applicability', lambda edi_format, inv: {'post': edi_format._test_edi_post_invoice}), \
  128. self.with_custom_method('_needs_web_services', lambda edi_format: True), \
  129. self.with_custom_method('_test_edi_post_invoice', lambda edi_format, inv: {inv: {'success': True}}):
  130. invoice.action_post()
  131. self.assertFalse(invoice._is_ready_to_be_sent())
  132. invoice.action_process_edi_web_services(with_commit=False)
  133. self.assertTrue(invoice._is_ready_to_be_sent())