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
  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. 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. self.env.registry._fields_by_model.pop(
  76. 'x_database.cleanup.test.model')
  77. purge_models = self.env['cleanup.purge.wizard.model'].create({})
  78. purge_models.purge_all()
  79. # must be removed by the wizard
  80. self.assertFalse(self.env['ir.model'].search([
  81. ('model', '=', 'x_database.cleanup.test.model'),
  82. ]))
  83. # create a nonexistent module
  84. self.module = self.env['ir.module.module'].create({
  85. 'name': 'database_cleanup_test',
  86. 'state': 'to upgrade',
  87. })
  88. purge_modules = self.env['cleanup.purge.wizard.module'].create({})
  89. # this reloads our registry, and we don't want to run tests twice
  90. # we also need the original registry for further tests, so save a
  91. # reference to it
  92. original_registry = Registry.registries[self.env.cr.dbname]
  93. config.options['test_enable'] = False
  94. purge_modules.purge_all()
  95. config.options['test_enable'] = True
  96. # must be removed by the wizard
  97. self.assertFalse(self.env['ir.module.module'].search([
  98. ('name', '=', 'database_cleanup_test'),
  99. ]))
  100. # reset afterwards
  101. Registry.registries[self.env.cr.dbname] = original_registry
  102. # create an orphaned table
  103. self.env.cr.execute('create table database_cleanup_test (test int)')
  104. purge_tables = self.env['cleanup.purge.wizard.table'].create({})
  105. purge_tables.purge_all()
  106. with self.assertRaises(ProgrammingError):
  107. with self.env.registry.cursor() as cr:
  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()