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.

89 lines
3.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  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.tools.misc import mute_logger
  8. from openerp.tests.common import TransactionCase
  9. class TestDatabaseCleanup(TransactionCase):
  10. def test_database_cleanup(self):
  11. # create an orphaned column
  12. self.cr.execute(
  13. 'alter table res_users add column database_cleanup_test int')
  14. purge_columns = self.env['cleanup.purge.wizard.column'].create({})
  15. purge_columns.purge_all()
  16. # must be removed by the wizard
  17. with self.assertRaises(ProgrammingError):
  18. with mute_logger('openerp.sql_db'):
  19. with self.registry.cursor() as cr:
  20. cr.execute('select database_cleanup_test from res_users')
  21. # create a data entry pointing nowhere
  22. self.cr.execute('select max(id) + 1 from res_users')
  23. self.env['ir.model.data'].create({
  24. 'module': 'database_cleanup',
  25. 'name': 'test_no_data_entry',
  26. 'model': 'res.users',
  27. 'res_id': self.cr.fetchone()[0],
  28. })
  29. purge_data = self.env['cleanup.purge.wizard.data'].create({})
  30. purge_data.purge_all()
  31. # must be removed by the wizard
  32. with self.assertRaises(ValueError):
  33. self.env.ref('database_cleanup.test_no_data_entry')
  34. # create a nonexistent model
  35. self.env['ir.model'].create({
  36. 'name': 'Database cleanup test model',
  37. 'model': 'x_database.cleanup.test.model',
  38. })
  39. self.env.cr.execute(
  40. 'insert into ir_attachment (name, res_model, res_id, type) values '
  41. "('test attachment', 'database.cleanup.test.model', 42, 'binary')")
  42. self.registry.models.pop('x_database.cleanup.test.model')
  43. self.registry._pure_function_fields.pop(
  44. 'x_database.cleanup.test.model')
  45. purge_models = self.env['cleanup.purge.wizard.model'].create({})
  46. purge_models.purge_all()
  47. # must be removed by the wizard
  48. self.assertFalse(self.env['ir.model'].search([
  49. ('model', '=', 'x_database.cleanup.test.model'),
  50. ]))
  51. # create a nonexistent module
  52. self.env['ir.module.module'].create({
  53. 'name': 'database_cleanup_test',
  54. 'state': 'to upgrade',
  55. })
  56. purge_modules = self.env['cleanup.purge.wizard.module'].create({})
  57. # this reloads our registry, and we don't want to run tests twice
  58. # we also need the original registry for further tests, so save a
  59. # reference to it
  60. original_registry = RegistryManager.registries[self.env.cr.dbname]
  61. config.options['test_enable'] = False
  62. purge_modules.purge_all()
  63. config.options['test_enable'] = True
  64. # must be removed by the wizard
  65. self.assertFalse(self.env['ir.module.module'].search([
  66. ('name', '=', 'database_cleanup_test'),
  67. ]))
  68. # reset afterwards
  69. RegistryManager.registries[self.env.cr.dbname] = original_registry
  70. # create an orphaned table
  71. self.env.cr.execute('create table database_cleanup_test (test int)')
  72. purge_tables = self.env['cleanup.purge.wizard.table'].create({})
  73. self.assertIn(
  74. 'database_cleanup_test',
  75. purge_tables.purge_line_ids.mapped('name')
  76. )
  77. purge_tables.purge_line_ids.filtered(
  78. lambda x: x.name == 'database_cleanup_test'
  79. ).purge()
  80. with self.assertRaises(ProgrammingError):
  81. with mute_logger('openerp.sql_db'):
  82. with self.registry.cursor() as cr:
  83. self.env.cr.execute('select * from database_cleanup_test')