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.

123 lines
4.2 KiB

  1. # Copyright 2015 Guewen Baconnier (Camptocamp SA)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from datetime import datetime, timedelta
  4. import odoo.tests.common as common
  5. from odoo.exceptions import ValidationError
  6. class TestArchive(common.SavepointCase):
  7. at_install = False
  8. post_install = True
  9. @classmethod
  10. def setUpClass(cls):
  11. super().setUpClass()
  12. cls.env = cls.env(context=dict(
  13. cls.env.context,
  14. tracking_disable=True,
  15. ))
  16. Partner = cls.env['res.partner']
  17. Cron = cls.env['ir.cron'] # has both `active` and `state` fields
  18. cls.Lifespan = cls.env['record.lifespan']
  19. cls.partner_model = cls.env.ref('base.model_res_partner')
  20. cls.cron_model = cls.env.ref('base.model_ir_cron')
  21. cls.partner1 = Partner.create({
  22. 'name': 'test user 1',
  23. })
  24. cls.partner2 = Partner.create({
  25. 'name': 'test user 2',
  26. })
  27. cls.partner3 = Partner.create({
  28. 'name': 'test user 3',
  29. })
  30. cls.cron1 = Cron.create({
  31. 'active': True,
  32. 'model_id': cls.partner_model.id,
  33. 'name': 'Dummy cron 1',
  34. 'state': 'code',
  35. 'code': 'model.browse()',
  36. })
  37. cls.cron2 = cls.cron1.copy({
  38. 'name': 'Dummy cron 2',
  39. 'state': 'multi',
  40. })
  41. cls.cron3 = cls.cron1.copy({
  42. 'name': 'Dummy cron 3',
  43. 'state': 'object_create',
  44. })
  45. old_date = datetime.now() - timedelta(days=365)
  46. cls.env.cr.execute(
  47. 'UPDATE res_partner SET write_date = %s '
  48. 'WHERE id IN %s', (old_date, (cls.partner2.id, cls.partner3.id))
  49. )
  50. cls.env.cr.execute(
  51. 'UPDATE ir_cron SET write_date = %s '
  52. 'WHERE id IN %s', (old_date, (cls.cron2.id, cls.cron3.id)))
  53. def test_get_archive_states(self):
  54. # Valid ir.cron states: code, object_create, object_write, multi
  55. archive_states_valid_variants = [
  56. 'code, multi, object_create',
  57. 'code,multi,object_create',
  58. 'code,multi,object_create,',
  59. ' code , multi, object_create',
  60. ]
  61. xpected = ['code', 'multi', 'object_create']
  62. guineapig = self.Lifespan.create({
  63. 'model_id': self.cron_model.id,
  64. 'months': 12,
  65. })
  66. for variant in archive_states_valid_variants:
  67. guineapig.archive_states = variant
  68. self.assertEqual(guineapig._get_archive_states(), xpected)
  69. def test_states_constraint_valid(self):
  70. # Valid ir.cron states: code, object_create, object_write, multi
  71. self.Lifespan.create({
  72. 'model_id': self.cron_model.id,
  73. 'months': 12,
  74. 'archive_states': 'code',
  75. })
  76. def test_states_constraint_invalid(self):
  77. with self.assertRaises(ValidationError):
  78. # Valid ir.cron states: code, object_create, object_write, multi
  79. self.Lifespan.create({
  80. 'model_id': self.cron_model.id,
  81. 'months': 12,
  82. 'archive_states': 'none, of, these, are, valid, states',
  83. })
  84. def test_lifespan(self):
  85. lifespan = self.Lifespan.create({
  86. 'model_id': self.partner_model.id,
  87. 'months': 3,
  88. })
  89. lifespan.archive_records()
  90. self.assertTrue(self.partner1.active)
  91. self.assertFalse(self.partner2.active)
  92. self.assertFalse(self.partner3.active)
  93. def test_lifespan_states(self):
  94. lifespan = self.Lifespan.create({
  95. 'model_id': self.cron_model.id,
  96. 'months': 3,
  97. 'archive_states': 'code, multi',
  98. })
  99. lifespan.archive_records()
  100. self.assertTrue(self.cron1.active) # state: code, fresh
  101. self.assertFalse(self.cron2.active) # state: multi, fresh
  102. self.assertTrue(self.cron3.active) # state: object_create, outdated
  103. def test_scheduler(self):
  104. self.Lifespan.create({
  105. 'model_id': self.partner_model.id,
  106. 'months': 3,
  107. })
  108. self.Lifespan._scheduler_archive_records()
  109. self.assertTrue(self.partner1.active)
  110. self.assertFalse(self.partner2.active)
  111. self.assertFalse(self.partner3.active)