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.

113 lines
4.8 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. with self.assertRaisesRegexp(KeyError,
  58. 'x_database.cleanup.test.model'):
  59. # TODO: Remove with-assert of KeyError after fix:
  60. # https://github.com/odoo/odoo/pull/13978/files#r88654967
  61. purge_models.purge_all()
  62. # must be removed by the wizard
  63. self.assertFalse(self.env['ir.model'].search([
  64. ('model', '=', 'x_database.cleanup.test.model'),
  65. ]))
  66. # create a nonexistent module
  67. self.module = self.env['ir.module.module'].create({
  68. 'name': 'database_cleanup_test',
  69. 'state': 'to upgrade',
  70. })
  71. purge_modules = self.env['cleanup.purge.wizard.module'].create({})
  72. # this reloads our registry, and we don't want to run tests twice
  73. # we also need the original registry for further tests, so save a
  74. # reference to it
  75. original_registry = RegistryManager.registries[self.env.cr.dbname]
  76. config.options['test_enable'] = False
  77. purge_modules.purge_all()
  78. config.options['test_enable'] = True
  79. # must be removed by the wizard
  80. self.assertFalse(self.env['ir.module.module'].search([
  81. ('name', '=', 'database_cleanup_test'),
  82. ]))
  83. # reset afterwards
  84. RegistryManager.registries[self.env.cr.dbname] = original_registry
  85. # create an orphaned table
  86. self.env.cr.execute('create table database_cleanup_test (test int)')
  87. purge_tables = self.env['cleanup.purge.wizard.table'].create({})
  88. purge_tables.purge_all()
  89. with self.assertRaises(ProgrammingError):
  90. with self.registry.cursor() as cr:
  91. self.env.cr.execute('select * from database_cleanup_test')
  92. def tearDown(self):
  93. super(TestDatabaseCleanup, self).tearDown()
  94. with self.registry.cursor() as cr2:
  95. # Release blocked tables with pending deletes
  96. self.env.cr.rollback()
  97. if self.module:
  98. cr2.execute(
  99. "DELETE FROM ir_module_module WHERE id=%s",
  100. (self.module.id,))
  101. if self.model:
  102. cr2.execute(
  103. "DELETE FROM ir_model WHERE id=%s",
  104. (self.model.id,))
  105. cr2.commit()