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.

95 lines
3.5 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. def setUp(self):
  25. super(AutoVacuumTestCase, self).setUp()
  26. self.logs = self.env['ir.logging']
  27. self.rules = self.env['muk_autovacuum.rules']
  28. self.model_model = self.env['ir.model']
  29. self.model_fields = self.env['ir.model.fields']
  30. self.model_logs = self.model_model.search([('model', '=', 'ir.logging')], limit=1)
  31. time_field_domain = [
  32. ('model_id', '=', self.model_logs.id),
  33. ('ttype', '=', 'datetime'),
  34. ('name', '=', 'create_date')]
  35. self.time_field_logs = self.model_fields.search(time_field_domain, limit=1)
  36. def test_autovacuum_time(self):
  37. self.create_logs()
  38. self.rules.create({
  39. 'name': "Delete Logs after 1 Minute",
  40. 'state': 'time',
  41. 'model': self.model_logs.id,
  42. 'time_field': self.time_field_logs.id,
  43. 'time_type': 'minutes',
  44. 'time': 1})
  45. self.run_autovacuum()
  46. def test_autovacuum_size(self):
  47. self.create_logs()
  48. self.rules.create({
  49. 'name': "Delete Logs Count > 1",
  50. 'state': 'size',
  51. 'model': self.model_logs.id,
  52. 'size': 1,
  53. 'size_order': "id desc",
  54. 'size_type': 'fixed'})
  55. self.run_autovacuum()
  56. def test_autovacuum_domain(self):
  57. self.create_logs()
  58. self.rules.create({
  59. 'name': "Delete Logs with Domain",
  60. 'state': 'domain',
  61. 'model': self.model_logs.id,
  62. 'domain': "[]"})
  63. self.run_autovacuum()
  64. def create_logs(self):
  65. ids = []
  66. time = datetime.datetime.utcnow()
  67. for index in range(0, 10):
  68. log = self.logs.create({
  69. 'create_date': time - datetime.timedelta(days=index),
  70. 'create_uid': self.env.user.id,
  71. 'name': "Test %s" % index,
  72. 'type': 'server',
  73. 'dbname': self.env.cr.dbname,
  74. 'level': "INFO",
  75. 'message': "TEST",
  76. 'path': "PATH",
  77. 'func': "TEST",
  78. 'line': 1})
  79. ids.append(log.id)
  80. return ids
  81. def run_autovacuum(self):
  82. count_before = self.env['ir.logging'].search([], count=True)
  83. self.env['ir.cron'].search([('model_id', '=', 'ir.autovacuum')]).ir_actions_server_id.run()
  84. count_after = self.env['ir.logging'].search([], count=True)
  85. self.assertTrue(count_before > count_after)