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.

130 lines
5.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from psycopg2 import ProgrammingError
  5. from odoo.modules.registry import Registry
  6. from odoo.tools import config, mute_logger
  7. from odoo.tests.common import TransactionCase, at_install, post_install
  8. # Use post_install to get all models loaded more info: odoo/odoo#13458
  9. @at_install(False)
  10. @post_install(True)
  11. class TestDatabaseCleanup(TransactionCase):
  12. def setUp(self):
  13. super(TestDatabaseCleanup, self).setUp()
  14. self.module = None
  15. self.model = None
  16. # Create one property for tests
  17. self.env['ir.property'].create({
  18. 'fields_id': self.env.ref('base.field_res_partner_name').id,
  19. 'type': 'char',
  20. 'value_text': 'My default partner name',
  21. })
  22. def test_database_cleanup(self):
  23. # delete some index and check if our module recreated it
  24. self.env.cr.execute('drop index res_partner_name_index')
  25. create_indexes = self.env['cleanup.create_indexes.wizard'].create({})
  26. create_indexes.purge_all()
  27. self.env.cr.execute(
  28. 'select indexname from pg_indexes '
  29. "where indexname='res_partner_name_index' and "
  30. "tablename='res_partner'"
  31. )
  32. self.assertEqual(self.env.cr.rowcount, 1)
  33. # duplicate a property
  34. duplicate_property = self.env['ir.property'].search([], limit=1).copy()
  35. purge_property = self.env['cleanup.purge.wizard.property'].create({})
  36. purge_property.purge_all()
  37. self.assertFalse(duplicate_property.exists())
  38. # create an orphaned column
  39. self.env.cr.execute(
  40. 'alter table res_partner add column database_cleanup_test int')
  41. # We need use a model that is not blocked (Avoid use res.users)
  42. partner_model = self.env['ir.model'].search([
  43. ('model', '=', 'res.partner')], limit=1)
  44. purge_columns = self.env['cleanup.purge.wizard.column'].create({
  45. 'purge_line_ids': [(0, 0, {
  46. 'model_id': partner_model.id, 'name': 'database_cleanup_test'}
  47. )]})
  48. purge_columns.purge_all()
  49. # must be removed by the wizard
  50. with self.assertRaises(ProgrammingError):
  51. with self.env.registry.cursor() as cr:
  52. with mute_logger('odoo.sql_db'):
  53. cr.execute('select database_cleanup_test from res_partner')
  54. # create a data entry pointing nowhere
  55. self.env.cr.execute('select max(id) + 1 from res_users')
  56. self.env['ir.model.data'].create({
  57. 'module': 'database_cleanup',
  58. 'name': 'test_no_data_entry',
  59. 'model': 'res.users',
  60. 'res_id': self.env.cr.fetchone()[0],
  61. })
  62. purge_data = self.env['cleanup.purge.wizard.data'].create({})
  63. purge_data.purge_all()
  64. # must be removed by the wizard
  65. with self.assertRaises(ValueError):
  66. self.env.ref('database_cleanup.test_no_data_entry')
  67. # create a nonexistent model
  68. self.model = self.env['ir.model'].create({
  69. 'name': 'Database cleanup test model',
  70. 'model': 'x_database.cleanup.test.model',
  71. })
  72. self.env.cr.execute(
  73. 'insert into ir_attachment (name, res_model, res_id, type) values '
  74. "('test attachment', 'database.cleanup.test.model', 42, 'binary')")
  75. self.env.registry.models.pop('x_database.cleanup.test.model')
  76. purge_models = self.env['cleanup.purge.wizard.model'].create({})
  77. purge_models.purge_all()
  78. # must be removed by the wizard
  79. self.assertFalse(self.env['ir.model'].search([
  80. ('model', '=', 'x_database.cleanup.test.model'),
  81. ]))
  82. # create a nonexistent module
  83. self.module = self.env['ir.module.module'].create({
  84. 'name': 'database_cleanup_test',
  85. 'state': 'to upgrade',
  86. })
  87. purge_modules = self.env['cleanup.purge.wizard.module'].create({})
  88. # this reloads our registry, and we don't want to run tests twice
  89. # we also need the original registry for further tests, so save a
  90. # reference to it
  91. original_registry = Registry.registries[self.env.cr.dbname]
  92. config.options['test_enable'] = False
  93. purge_modules.purge_all()
  94. config.options['test_enable'] = True
  95. # must be removed by the wizard
  96. self.assertFalse(self.env['ir.module.module'].search([
  97. ('name', '=', 'database_cleanup_test'),
  98. ]))
  99. # reset afterwards
  100. Registry.registries[self.env.cr.dbname] = original_registry
  101. # create an orphaned table
  102. self.env.cr.execute('create table database_cleanup_test (test int)')
  103. purge_tables = self.env['cleanup.purge.wizard.table'].create({})
  104. purge_tables.purge_all()
  105. with self.assertRaises(ProgrammingError):
  106. with self.env.registry.cursor() as cr:
  107. with mute_logger('odoo.sql_db'):
  108. cr.execute('select * from database_cleanup_test')
  109. def tearDown(self):
  110. super(TestDatabaseCleanup, self).tearDown()
  111. with self.registry.cursor() as cr2:
  112. # Release blocked tables with pending deletes
  113. self.env.cr.rollback()
  114. if self.module:
  115. cr2.execute(
  116. "DELETE FROM ir_module_module WHERE id=%s",
  117. (self.module.id,))
  118. if self.model:
  119. cr2.execute(
  120. "DELETE FROM ir_model WHERE id=%s",
  121. (self.model.id,))
  122. cr2.commit()