[IMP] uninstall: moving tests to the openerp test framework (i.e. under openerp/tests/).

bzr revid: vmt@openerp.com-20120604104404-zibru6794ma1996x
This commit is contained in:
Vo Minh Thu 2012-06-04 12:44:04 +02:00
parent 71211720a0
commit fa4c2a40c0
3 changed files with 123 additions and 36 deletions

View File

@ -11,6 +11,7 @@ See the :ref:`test-framework` section in the :ref:`features` list.
import test_expression
import test_ir_sequence
import test_orm
import test_uninstall
fast_suite = [
test_ir_sequence,

View File

@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
# Run with one of these commands:
# > OPENERP_ADDONS_PATH='../../addons/trunk' OPENERP_PORT=8069 \
# OPENERP_DATABASE=yy PYTHONPATH=. python tests/test_ir_sequence.py
# > OPENERP_ADDONS_PATH='../../addons/trunk' OPENERP_PORT=8069 \
# OPENERP_DATABASE=yy nosetests tests/test_ir_sequence.py
# > OPENERP_ADDONS_PATH='../../../addons/trunk' OPENERP_PORT=8069 \
# OPENERP_DATABASE=yy PYTHONPATH=../:. unit2 test_ir_sequence
# This assume an existing database.
import psycopg2
import unittest2
import openerp
from openerp import SUPERUSER_ID
import common
DB = common.DB
ADMIN_USER_ID = common.ADMIN_USER_ID
def registry(model):
return openerp.modules.registry.RegistryManager.get(DB)[model]
def cursor():
return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
def get_module(module_name):
registry = openerp.modules.registry.RegistryManager.get(DB)
return registry.get(module_name)
def reload_registry():
openerp.modules.registry.RegistryManager.new(
DB, update_module=True)
def search_registry(model_name, domain):
cr = cursor()
model = registry(model_name)
record_ids = model.search(cr, SUPERUSER_ID, domain, {})
cr.close()
return record_ids
def install_module(module_name):
ir_module_module = registry('ir.module.module')
cr = cursor()
module_ids = ir_module_module.search(cr, SUPERUSER_ID,
[('name', '=', module_name)], {})
assert len(module_ids) == 1
ir_module_module.button_install(cr, SUPERUSER_ID, module_ids, {})
cr.commit()
cr.close()
reload_registry()
def uninstall_module(module_name):
ir_module_module = registry('ir.module.module')
cr = cursor()
module_ids = ir_module_module.search(cr, SUPERUSER_ID,
[('name', '=', module_name)], {})
assert len(module_ids) == 1
ir_module_module.button_uninstall(cr, SUPERUSER_ID, module_ids, {})
cr.commit()
cr.close()
reload_registry()
class test_uninstall(unittest2.TestCase):
""" Test install/uninstall of a test module. """
def test_00_nothing(self):
""" Test the assumption the test module is not installed yet. """
assert not get_module('test_uninstall.model')
assert not search_registry('ir.model.data',
[('module', '=', 'test_uninstall')])
assert not search_registry('ir.model.fields',
[('model', '=', 'test_uninstall.model')])
if __name__ == '__main__':
unittest2.main()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

77
t.py
View File

@ -1,3 +1,5 @@
import sys
import openerp
from openerp import SUPERUSER_ID
from openerp.osv import fields
@ -6,73 +8,77 @@ conf = openerp.tools.config
# TODO Exception handling (especially on cursors).
def get_registry(database_name):
registry = openerp.modules.registry.RegistryManager.get(database_name)
return registry
DB = None
def reload_registry(database_name):
def registry(model_name):
return openerp.modules.registry.RegistryManager.get(DB)[model_name]
def cursor():
return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
def get_module(module_name):
registry = openerp.modules.registry.RegistryManager.get(DB)
return registry.get(module_name)
def reload_registry():
openerp.modules.registry.RegistryManager.new(
database_name, update_module=True)
DB, update_module=True)
def search_registry(database_name, model_name, domain):
registry = get_registry(database_name)
cr = registry.db.cursor()
model = registry.get(model_name)
record_ids = model.search(cr, SUPERUSER_ID,
domain, {})
def search_registry(model_name, domain):
cr = cursor()
model = registry(model_name)
record_ids = model.search(cr, SUPERUSER_ID, domain, {})
cr.close()
return record_ids
def install_module(database_name, module_name):
registry = get_registry(database_name)
ir_module_module = registry.get('ir.module.module')
cr = registry.db.cursor()
def install_module(module_name):
ir_module_module = registry('ir.module.module')
cr = cursor()
module_ids = ir_module_module.search(cr, SUPERUSER_ID,
[('name', '=', module_name)], {})
assert len(module_ids) == 1
ir_module_module.button_install(cr, SUPERUSER_ID, module_ids, {})
cr.commit()
cr.close()
reload_registry(database_name)
reload_registry()
def uninstall_module(database_name, module_name):
registry = get_registry(database_name)
ir_module_module = registry.get('ir.module.module')
cr = registry.db.cursor()
def uninstall_module(module_name):
ir_module_module = registry('ir.module.module')
cr = cursor()
module_ids = ir_module_module.search(cr, SUPERUSER_ID,
[('name', '=', module_name)], {})
assert len(module_ids) == 1
ir_module_module.button_uninstall(cr, SUPERUSER_ID, module_ids, {})
cr.commit()
cr.close()
reload_registry(database_name)
reload_registry()
if __name__ == '__main__':
assert len(sys.argv) == 2
DB = sys.argv[1]
openerp.netsvc.init_logger()
conf['addons_path'] = './openerp/tests/addons,../../addons/trunk,../../web/trunk/addons'
install_module('xx', 'test_uninstall')
registry = get_registry('xx')
assert registry.get('test_uninstall.model')
install_module('test_uninstall')
assert get_module('test_uninstall.model')
assert search_registry('xx', 'ir.model.data',
assert search_registry('ir.model.data',
[('module', '=', 'test_uninstall')])
assert search_registry('xx', 'ir.model.fields',
assert search_registry('ir.model.fields',
[('model', '=', 'test_uninstall.model')])
uninstall_module('xx', 'test_uninstall')
registry = get_registry('xx')
assert not registry.get('test_uninstall.model')
uninstall_module('test_uninstall')
assert not get_module('test_uninstall.model')
assert not search_registry('xx', 'ir.model.data',
assert not search_registry('ir.model.data',
[('module', '=', 'test_uninstall')])
assert not search_registry('xx', 'ir.model.fields',
assert not search_registry('ir.model.fields',
[('model', '=', 'test_uninstall.model')])
ir_model_constraint = registry.get('ir.model.constraint')
cr = registry.db.cursor()
ir_model_constraint = registry('ir.model.constraint')
cr = cursor()
ids = ir_model_constraint.search(cr, SUPERUSER_ID, [], {})
#print ir_model_constraint.browse(cr, SUPERUSER_ID, ids, {})
cr.close()
@ -95,9 +101,8 @@ MY_MODULE = {
'depends': ['base'],
}
def create_virtual_module(database_name, module_name, info):
registry = get_registry(database_name)
cr = registry.db.cursor()
def create_virtual_module(module_name, info):
cr = cursor()
cr.execute("""SELECT 1 FROM ir_module_module WHERE name=%s""", (module_name,))
if cr.fetchone(): return