diff --git a/database_cleanup/tests/test_database_cleanup.py b/database_cleanup/tests/test_database_cleanup.py index 060c9174e..48ebbfbb7 100644 --- a/database_cleanup/tests/test_database_cleanup.py +++ b/database_cleanup/tests/test_database_cleanup.py @@ -4,20 +4,34 @@ from psycopg2 import ProgrammingError from openerp.modules.registry import RegistryManager from openerp.tools import config -from openerp.tests.common import TransactionCase +from openerp.tests.common import TransactionCase, at_install, post_install +# Use post_install to get all models loaded more info: odoo/odoo#13458 +@at_install(False) +@post_install(True) class TestDatabaseCleanup(TransactionCase): + def setUp(self): + super(TestDatabaseCleanup, self).setUp() + self.module = None + self.model = None + def test_database_cleanup(self): # create an orphaned column self.cr.execute( - 'alter table res_users add column database_cleanup_test int') - purge_columns = self.env['cleanup.purge.wizard.column'].create({}) + 'alter table res_partner add column database_cleanup_test int') + # We need use a model that is not blocked (Avoid use res.users) + partner_model = self.env['ir.model'].search([ + ('model', '=', 'res.partner')], limit=1) + purge_columns = self.env['cleanup.purge.wizard.column'].create({ + 'purge_line_ids': [(0, 0, { + 'model_id': partner_model.id, 'name': 'database_cleanup_test'} + )]}) purge_columns.purge_all() # must be removed by the wizard with self.assertRaises(ProgrammingError): with self.registry.cursor() as cr: - cr.execute('select database_cleanup_test from res_users') + cr.execute('select database_cleanup_test from res_partner') # create a data entry pointing nowhere self.cr.execute('select max(id) + 1 from res_users') @@ -34,7 +48,7 @@ class TestDatabaseCleanup(TransactionCase): self.env.ref('database_cleanup.test_no_data_entry') # create a nonexistent model - self.env['ir.model'].create({ + self.model = self.env['ir.model'].create({ 'name': 'Database cleanup test model', 'model': 'x_database.cleanup.test.model', }) @@ -45,14 +59,18 @@ class TestDatabaseCleanup(TransactionCase): self.registry._pure_function_fields.pop( 'x_database.cleanup.test.model') purge_models = self.env['cleanup.purge.wizard.model'].create({}) - purge_models.purge_all() - # must be removed by the wizard - self.assertFalse(self.env['ir.model'].search([ - ('model', '=', 'x_database.cleanup.test.model'), - ])) + with self.assertRaisesRegexp(KeyError, + 'x_database.cleanup.test.model'): + # TODO: Remove with-assert of KeyError after fix: + # https://github.com/odoo/odoo/pull/13978/files#r88654967 + purge_models.purge_all() + # must be removed by the wizard + self.assertFalse(self.env['ir.model'].search([ + ('model', '=', 'x_database.cleanup.test.model'), + ])) # create a nonexistent module - self.env['ir.module.module'].create({ + self.module = self.env['ir.module.module'].create({ 'name': 'database_cleanup_test', 'state': 'to upgrade', }) @@ -78,3 +96,18 @@ class TestDatabaseCleanup(TransactionCase): with self.assertRaises(ProgrammingError): with self.registry.cursor() as cr: self.env.cr.execute('select * from database_cleanup_test') + + def tearDown(self): + super(TestDatabaseCleanup, self).tearDown() + with self.registry.cursor() as cr2: + # Release blocked tables with pending deletes + self.env.cr.rollback() + if self.module: + cr2.execute( + "DELETE FROM ir_module_module WHERE id=%s", + (self.module.id,)) + if self.model: + cr2.execute( + "DELETE FROM ir_model WHERE id=%s", + (self.model.id,)) + cr2.commit()