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.

94 lines
3.4 KiB

  1. # Copyright (C) 2018 Camptocamp
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. # pylint disable=anomalous-backslash-in-string
  4. from odoo import api
  5. from odoo.tests import common
  6. import datetime
  7. import mock
  8. MOCK_PATH = 'odoo.addons.base_cron_oneshot.models.ir_cron'
  9. class OneshotTestCase(common.SavepointCase):
  10. @property
  11. def cron_model(self):
  12. return self.env['ir.cron']
  13. @mock.patch(MOCK_PATH + '.datetime')
  14. def test_defaults(self, mocked_dt):
  15. mocked_dt.now.return_value = datetime.datetime(2018, 8, 31, 10, 30)
  16. cron = self.cron_model.create({
  17. 'oneshot': True,
  18. 'name': 'Foo',
  19. 'model_id': self.env['ir.model']._get('ir.cron').id,
  20. 'state': 'code',
  21. 'code': 'model.some_method()',
  22. 'interval_number': 1,
  23. 'interval_type': 'days',
  24. 'numbercall': 5, # won't have any effect
  25. })
  26. self.assertRegexpMatches(cron.name, 'Oneshot#\d+ Foo')
  27. self.assertEqual(cron.numbercall, 1)
  28. # call postponed by 10mins
  29. self.assertEqual(cron.nextcall, '2018-08-31 10:40:00')
  30. def test_schedule_oneshot_check(self):
  31. with self.assertRaises(AssertionError) as err:
  32. self.cron_model.schedule_oneshot('res.partner')
  33. self.assertEqual(str(err.exception), 'Provide a method or some code!')
  34. @mock.patch(MOCK_PATH + '.datetime')
  35. def test_schedule_oneshot_method(self, mocked_dt):
  36. mocked_dt.now.return_value = datetime.datetime(2018, 8, 31, 16, 30)
  37. cron = self.cron_model.schedule_oneshot(
  38. 'res.partner', method='read', delay=('minutes', 30))
  39. self.assertRegexpMatches(cron.name, 'Oneshot#\d+')
  40. self.assertEqual(cron.numbercall, 1)
  41. self.assertEqual(cron.code, 'model.read()')
  42. self.assertEqual(
  43. cron.model_id, self.env['ir.model']._get('res.partner'))
  44. self.assertEqual(cron.nextcall, '2018-08-31 17:00:00')
  45. def test_schedule_oneshot_code(self):
  46. cron = self.cron_model.schedule_oneshot(
  47. 'res.partner', code='env["res.partner"].search([])')
  48. self.assertRegexpMatches(cron.name, 'Oneshot#\d+')
  49. self.assertEqual(cron.numbercall, 1)
  50. self.assertEqual(cron.state, 'code')
  51. self.assertEqual(cron.code, 'env["res.partner"].search([])')
  52. self.assertEqual(
  53. cron.model_id, self.env['ir.model']._get('res.partner'))
  54. class OneshotProcessTestCase(common.TransactionCase):
  55. def setUp(self):
  56. super().setUp()
  57. deleted = []
  58. @api.multi
  59. def unlink(self):
  60. deleted.extend(self.ids)
  61. # do nothing as the original one will try to read the lock
  62. # for the current record which is NOT committed
  63. # and has no real ID.
  64. return
  65. self.env['ir.cron']._patch_method('unlink', unlink)
  66. self.addCleanup(self.env['ir.cron']._revert_method, 'unlink')
  67. self.deleted = deleted
  68. def test_schedule_oneshot_cleanup(self):
  69. cron1 = self.env['ir.cron'].schedule_oneshot(
  70. 'res.partner', code='env["res.partner"].search([])')
  71. cron2 = self.env['ir.cron'].schedule_oneshot(
  72. 'res.partner', code='env["res.partner"].read([])')
  73. # simulate excuted
  74. cron1.write({'numbercall': 0, 'active': False})
  75. self.env['ir.cron'].cron_oneshot_cleanup()
  76. self.assertIn(cron1.id, self.deleted)
  77. self.assertNotIn(cron2.id, self.deleted)