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.

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