Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions trytond/trytond/server_context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import string
import random
import logging
from threading import local

Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est fait exprès d'avoir un Boolean (d'autant plus à true, qui va faire qu'on aura un dict avec {random_key: True} ) ici ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oui, je l'ai fait comme ça pour récupérer la clé plus facilement avec:
if Transaction().context.get('random_key')

mais il est théoriquement possible d'ajouter une valeur random aussi, dans ce cas il faudra faire plutôt:
if Transaction().context.get('random_key') == RANDOM_KEY

en tout cas on ne devrait avoir cette clé dans le contexte que si on est dans un test, c'est pour ça que je ne lui ai pas donnée une valeur random aussi.
@jmousset t'en penses quoi? j'ajoute des valeurs random aussi aux clés générées dans le contexte ou un boolean suffit?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BeatriceSchuster la version actuelle me semble ok. l'utilisateur a le choix de l'usage.

TEST_CONTEXT_KEY = list(TEST_CONTEXT.keys())[0]


class _AttributeManager(object):
def __init__(self, **kwargs):
self.kwargs = kwargs
Expand Down
4 changes: 3 additions & 1 deletion trytond/trytond/tests/test_tryton.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion trytond/trytond/tests/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down