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.

74 lines
3.1 KiB

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