test_alert.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. from datetime import datetime, timedelta
  3. from odoo import fields
  4. from odoo.tests import common
  5. from odoo.addons.lunch.tests.common import TestsCommon
  6. class TestAlarm(TestsCommon):
  7. @common.users('cle-lunch-manager')
  8. def test_cron_sync_create(self):
  9. cron_ny = self.alert_ny.cron_id
  10. self.assertTrue(cron_ny.active)
  11. self.assertEqual(cron_ny.name, "Lunch: alert chat notification (New York UTC-5)")
  12. self.assertEqual(
  13. [line for line in cron_ny.code.splitlines() if not line.lstrip().startswith("#")],
  14. ["env['lunch.alert'].browse([%i])._notify_chat()" % self.alert_ny.id])
  15. self.assertEqual(cron_ny.nextcall, datetime(2021, 1, 29, 15, 0)) # New-york is UTC-5
  16. tokyo_cron = self.alert_tokyo.cron_id
  17. self.assertEqual(tokyo_cron.nextcall, datetime(2021, 1, 29, 23, 0)) # Tokyo is UTC+9 but the cron is posponed
  18. @common.users('cle-lunch-manager')
  19. def test_cron_sync_active(self):
  20. cron_ny = self.alert_ny.cron_id
  21. self.alert_ny.active = False
  22. self.assertFalse(cron_ny.active)
  23. self.alert_ny.active = True
  24. self.assertTrue(cron_ny.active)
  25. self.alert_ny.mode = 'alert'
  26. self.assertFalse(cron_ny.active)
  27. self.alert_ny.mode = 'chat'
  28. self.assertTrue(cron_ny.active)
  29. ctx_today = fields.Date.context_today(self.alert_ny, self.fakenow)
  30. self.alert_ny.until = ctx_today - timedelta(days=1)
  31. self.assertFalse(cron_ny.active)
  32. self.alert_ny.until = ctx_today + timedelta(days=2)
  33. self.assertTrue(cron_ny.active)
  34. self.alert_ny.until = False
  35. self.assertTrue(cron_ny.active)
  36. @common.users('cle-lunch-manager')
  37. def test_cron_sync_nextcall(self):
  38. cron_ny = self.alert_ny.cron_id
  39. old_nextcall = cron_ny.nextcall
  40. self.alert_ny.notification_time -= 5
  41. self.assertEqual(cron_ny.nextcall, old_nextcall - timedelta(hours=5) + timedelta(days=1))
  42. # Simulate cron execution
  43. cron_ny.sudo().lastcall = old_nextcall - timedelta(hours=5)
  44. cron_ny.sudo().nextcall += timedelta(days=1)
  45. self.alert_ny.notification_time += 7
  46. self.assertEqual(cron_ny.nextcall, old_nextcall + timedelta(days=1, hours=2))
  47. self.alert_ny.notification_time -= 1
  48. self.assertEqual(cron_ny.nextcall, old_nextcall + timedelta(days=1, hours=1))