44from psycopg2 import ProgrammingError
55from openerp .modules .registry import RegistryManager
66from openerp .tools import config
7- from openerp .tests .common import TransactionCase
7+ from openerp .tests .common import TransactionCase , at_install , post_install
88
99
10+ # Use post_install to get all models loaded more info: odoo/odoo#13458
11+ @at_install (False )
12+ @post_install (True )
1013class TestDatabaseCleanup (TransactionCase ):
14+ def setUp (self ):
15+ super (TestDatabaseCleanup , self ).setUp ()
16+ self .module = None
17+ self .model = None
18+
1119 def test_database_cleanup (self ):
1220 # create an orphaned column
1321 self .cr .execute (
14- 'alter table res_users add column database_cleanup_test int' )
15- purge_columns = self .env ['cleanup.purge.wizard.column' ].create ({})
22+ 'alter table res_partner add column database_cleanup_test int' )
23+ # We need use a model that is not blocked (Avoid use res.users)
24+ partner_model = self .env ['ir.model' ].search ([
25+ ('model' , '=' , 'res.partner' )], limit = 1 )
26+ purge_columns = self .env ['cleanup.purge.wizard.column' ].create ({
27+ 'purge_line_ids' : [(0 , 0 , {
28+ 'model_id' : partner_model .id , 'name' : 'database_cleanup_test' }
29+ )]})
1630 purge_columns .purge_all ()
1731 # must be removed by the wizard
1832 with self .assertRaises (ProgrammingError ):
1933 with self .registry .cursor () as cr :
20- cr .execute ('select database_cleanup_test from res_users ' )
34+ cr .execute ('select database_cleanup_test from res_partner ' )
2135
2236 # create a data entry pointing nowhere
2337 self .cr .execute ('select max(id) + 1 from res_users' )
@@ -34,7 +48,7 @@ def test_database_cleanup(self):
3448 self .env .ref ('database_cleanup.test_no_data_entry' )
3549
3650 # create a nonexistent model
37- self .env ['ir.model' ].create ({
51+ self .model = self . env ['ir.model' ].create ({
3852 'name' : 'Database cleanup test model' ,
3953 'model' : 'x_database.cleanup.test.model' ,
4054 })
@@ -45,14 +59,18 @@ def test_database_cleanup(self):
4559 self .registry ._pure_function_fields .pop (
4660 'x_database.cleanup.test.model' )
4761 purge_models = self .env ['cleanup.purge.wizard.model' ].create ({})
48- purge_models .purge_all ()
49- # must be removed by the wizard
50- self .assertFalse (self .env ['ir.model' ].search ([
51- ('model' , '=' , 'x_database.cleanup.test.model' ),
52- ]))
62+ with self .assertRaisesRegexp (KeyError ,
63+ 'x_database.cleanup.test.model' ):
64+ # TODO: Remove with-assert of KeyError after fix:
65+ # https://github.com/odoo/odoo/pull/13978/files#r88654967
66+ purge_models .purge_all ()
67+ # must be removed by the wizard
68+ self .assertFalse (self .env ['ir.model' ].search ([
69+ ('model' , '=' , 'x_database.cleanup.test.model' ),
70+ ]))
5371
5472 # create a nonexistent module
55- self .env ['ir.module.module' ].create ({
73+ self .module = self . env ['ir.module.module' ].create ({
5674 'name' : 'database_cleanup_test' ,
5775 'state' : 'to upgrade' ,
5876 })
@@ -78,3 +96,18 @@ def test_database_cleanup(self):
7896 with self .assertRaises (ProgrammingError ):
7997 with self .registry .cursor () as cr :
8098 self .env .cr .execute ('select * from database_cleanup_test' )
99+
100+ def tearDown (self ):
101+ super (TestDatabaseCleanup , self ).tearDown ()
102+ with self .registry .cursor () as cr2 :
103+ # Release blocked tables with pending deletes
104+ self .env .cr .rollback ()
105+ if self .module :
106+ cr2 .execute (
107+ "DELETE FROM ir_module_module WHERE id=%s" ,
108+ (self .module .id ,))
109+ if self .model :
110+ cr2 .execute (
111+ "DELETE FROM ir_model WHERE id=%s" ,
112+ (self .model .id ,))
113+ cr2 .commit ()
0 commit comments