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.

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