diff --git a/trytond/trytond/server_context.py b/trytond/trytond/server_context.py index 9eeb9076587..108fe09c93d 100644 --- a/trytond/trytond/server_context.py +++ b/trytond/trytond/server_context.py @@ -1,3 +1,5 @@ +import string +import random import logging from threading import local @@ -7,6 +9,21 @@ ] +def generate_context(key=None, value=None, length=20): + def generate_random_string(): + return ''.join(random.SystemRandom().choice( + string.ascii_uppercase + string.digits) for _ in range(length)) + if not key: + key = generate_random_string() + if not value: + value = generate_random_string() + return {key: value} + + +TEST_CONTEXT = generate_context(value=True) +TEST_CONTEXT_KEY = list(TEST_CONTEXT.keys())[0] + + class _AttributeManager(object): def __init__(self, **kwargs): self.kwargs = kwargs diff --git a/trytond/trytond/tests/test_tryton.py b/trytond/trytond/tests/test_tryton.py index 7f3a914897b..d8d1b338259 100644 --- a/trytond/trytond/tests/test_tryton.py +++ b/trytond/trytond/tests/test_tryton.py @@ -41,6 +41,7 @@ from trytond.tools import file_open, find_dir, is_instance_method from trytond.transaction import Transaction, TransactionError from trytond.wizard import StateAction, StateView +from trytond.server_context import ServerContext, TEST_CONTEXT __all__ = [ 'CONTEXT', @@ -114,7 +115,8 @@ def activate_module(modules, lang='en', cache_name=None): type='wizard') instance_id, _, _ = ActivateUpgrade.create() transaction.commit() - ActivateUpgrade(instance_id).transition_upgrade() + with ServerContext().set_context(**TEST_CONTEXT): + ActivateUpgrade(instance_id).transition_upgrade() ActivateUpgrade.delete(instance_id) transaction.commit() backup_db_cache(name) diff --git a/trytond/trytond/tests/tools.py b/trytond/trytond/tests/tools.py index ad9f690575e..6f0a737c8ac 100644 --- a/trytond/trytond/tests/tools.py +++ b/trytond/trytond/tests/tools.py @@ -2,6 +2,7 @@ # this repository contains the full copyright notices and license terms. from proteus import Model, Wizard from proteus import config as pconfig +from trytond.server_context import ServerContext, TEST_CONTEXT from .test_tryton import backup_db_cache, drop_create, restore_db_cache @@ -27,7 +28,8 @@ def activate_modules(modules, *, setup_function=None, cache_file_name=None): ]) assert len(records) == len(modules) Module.click(records, 'activate') - Wizard('ir.module.activate_upgrade').execute('upgrade') + with ServerContext().set_context(**TEST_CONTEXT): + Wizard('ir.module.activate_upgrade').execute('upgrade') if callable(setup_function): setup_function(cfg)