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.

98 lines
3.6 KiB

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