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.

129 lines
5.5 KiB

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