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.

88 lines
3.3 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (C) 2018 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import logging
  20. import datetime
  21. from odoo.tests import common
  22. _logger = logging.getLogger(__name__)
  23. class AutoVacuumTestCase(common.TransactionCase):
  24. at_install = False
  25. post_install = True
  26. def setUp(self):
  27. super(AutoVacuumTestCase, self).setUp()
  28. # create test rules
  29. model_model = self.env['ir.model']
  30. model_fields = self.env['ir.model.fields']
  31. model_logs = model_model.search([('model', '=', 'ir.logging')], limit=1)
  32. time_field_domain = [
  33. ('model_id', '=', model_logs.id),
  34. ('ttype', '=', 'datetime'),
  35. ('name', '=', 'create_date')]
  36. time_field_logs = model_fields.search(time_field_domain, limit=1)
  37. self.rules = self.env['muk_autovacuum.rules']
  38. self.rules |= self.rules.create({
  39. 'name': "Delete Logs after 2 Day",
  40. 'state': 'time',
  41. 'model': model_logs.id,
  42. 'time_field': time_field_logs.id,
  43. 'time_type': 'days',
  44. 'time': 2})
  45. self.rules |= self.rules.create({
  46. 'name': "Delete Logs Count > 1",
  47. 'state': 'size',
  48. 'model': model_logs.id,
  49. 'size': 1,
  50. 'size_order': "id desc",
  51. 'size_type': 'fixed'})
  52. self.rules |= self.rules.create({
  53. 'name': "Delete Logs with Domain",
  54. 'state': 'domain',
  55. 'model': model_logs.id,
  56. 'domain': "[]"})
  57. # create test logs
  58. self.logs = self.env['ir.logging']
  59. time = datetime.datetime.utcnow()
  60. for index in range(0, 10):
  61. self.logs |= self.logs.create({
  62. 'create_date': time - datetime.timedelta(days=index),
  63. 'create_uid': self.env.user.id,
  64. 'name': "Test %s" % index,
  65. 'type': 'server',
  66. 'dbname': self.env.cr.dbname,
  67. 'level': "INFO",
  68. 'message': "TEST",
  69. 'path': "PATH",
  70. 'func': "TEST",
  71. 'line': 1})
  72. def tearDown(self):
  73. super(AutoVacuumTestCase, self).tearDown()
  74. self.logs.unlink()
  75. self.rules.unlink()
  76. def test_autovacuum(self):
  77. count_before = self.env['ir.logging'].search([], count=True)
  78. self.env['ir.cron'].search([('model_id', '=', 'ir.autovacuum')]).ir_actions_server_id.run()
  79. count_after = self.env['ir.logging'].search([], count=True)
  80. self.assertTrue(count_before > count_after)