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.

124 lines
5.3 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 openerp.modules.registry import RegistryManager
  6. from openerp.tools import config
  7. from openerp.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. def test_database_cleanup(self):
  17. # delete some index and check if our module recreated it
  18. self.env.cr.execute('drop index res_partner_name_index')
  19. create_indexes = self.env['cleanup.create_indexes.wizard'].create({})
  20. create_indexes.purge_all()
  21. self.env.cr.execute(
  22. 'select indexname from pg_indexes '
  23. "where indexname='res_partner_name_index' and "
  24. "tablename='res_partner'"
  25. )
  26. self.assertEqual(self.env.cr.rowcount, 1)
  27. # duplicate a property
  28. duplicate_property = self.env['ir.property'].search([], limit=1).copy()
  29. purge_property = self.env['cleanup.purge.wizard.property'].create({})
  30. purge_property.purge_all()
  31. self.assertFalse(duplicate_property.exists())
  32. # create an orphaned column
  33. self.cr.execute(
  34. 'alter table res_partner add column database_cleanup_test int')
  35. # We need use a model that is not blocked (Avoid use res.users)
  36. partner_model = self.env['ir.model'].search([
  37. ('model', '=', 'res.partner')], limit=1)
  38. purge_columns = self.env['cleanup.purge.wizard.column'].create({
  39. 'purge_line_ids': [(0, 0, {
  40. 'model_id': partner_model.id, 'name': 'database_cleanup_test'}
  41. )]})
  42. purge_columns.purge_all()
  43. # must be removed by the wizard
  44. with self.assertRaises(ProgrammingError):
  45. with self.registry.cursor() as cr:
  46. cr.execute('select database_cleanup_test from res_partner')
  47. # create a data entry pointing nowhere
  48. self.cr.execute('select max(id) + 1 from res_users')
  49. self.env['ir.model.data'].create({
  50. 'module': 'database_cleanup',
  51. 'name': 'test_no_data_entry',
  52. 'model': 'res.users',
  53. 'res_id': self.cr.fetchone()[0],
  54. })
  55. purge_data = self.env['cleanup.purge.wizard.data'].create({})
  56. purge_data.purge_all()
  57. # must be removed by the wizard
  58. with self.assertRaises(ValueError):
  59. self.env.ref('database_cleanup.test_no_data_entry')
  60. # create a nonexistent model
  61. self.model = self.env['ir.model'].create({
  62. 'name': 'Database cleanup test model',
  63. 'model': 'x_database.cleanup.test.model',
  64. })
  65. self.env.cr.execute(
  66. 'insert into ir_attachment (name, res_model, res_id, type) values '
  67. "('test attachment', 'database.cleanup.test.model', 42, 'binary')")
  68. self.registry.models.pop('x_database.cleanup.test.model')
  69. self.registry._pure_function_fields.pop(
  70. 'x_database.cleanup.test.model')
  71. purge_models = self.env['cleanup.purge.wizard.model'].create({})
  72. purge_models.purge_all()
  73. # must be removed by the wizard
  74. self.assertFalse(self.env['ir.model'].search([
  75. ('model', '=', 'x_database.cleanup.test.model'),
  76. ]))
  77. # create a nonexistent module
  78. self.module = self.env['ir.module.module'].create({
  79. 'name': 'database_cleanup_test',
  80. 'state': 'to upgrade',
  81. })
  82. purge_modules = self.env['cleanup.purge.wizard.module'].create({})
  83. # this reloads our registry, and we don't want to run tests twice
  84. # we also need the original registry for further tests, so save a
  85. # reference to it
  86. original_registry = RegistryManager.registries[self.env.cr.dbname]
  87. config.options['test_enable'] = False
  88. purge_modules.purge_all()
  89. config.options['test_enable'] = True
  90. # must be removed by the wizard
  91. self.assertFalse(self.env['ir.module.module'].search([
  92. ('name', '=', 'database_cleanup_test'),
  93. ]))
  94. # reset afterwards
  95. RegistryManager.registries[self.env.cr.dbname] = original_registry
  96. # create an orphaned table
  97. self.env.cr.execute('create table database_cleanup_test (test int)')
  98. purge_tables = self.env['cleanup.purge.wizard.table'].create({})
  99. purge_tables.purge_all()
  100. with self.assertRaises(ProgrammingError):
  101. with self.registry.cursor() as cr:
  102. self.env.cr.execute('select * from database_cleanup_test')
  103. def tearDown(self):
  104. super(TestDatabaseCleanup, self).tearDown()
  105. with self.registry.cursor() as cr2:
  106. # Release blocked tables with pending deletes
  107. self.env.cr.rollback()
  108. if self.module:
  109. cr2.execute(
  110. "DELETE FROM ir_module_module WHERE id=%s",
  111. (self.module.id,))
  112. if self.model:
  113. cr2.execute(
  114. "DELETE FROM ir_model WHERE id=%s",
  115. (self.model.id,))
  116. cr2.commit()