Doctest/Unittest for fields states Readonly and invisible

Hi,

May I know how to test for states of Readonly or Invisible?

Best regards

We do not have any facilities for that in the framework.
You can still instantiate a trytond.model.EvalEnvironment and decode the PYSON of the states and check the result.
Indeed I think it will be a great addition to provide a way to get the evaluated states of a field from an instance.

Hi Ced,

Thank for advice. I will then try to play around with the trytond.model.EvalEnvironment.
The current test method only left the states test that cannot evaluate using proteus.
If can provide a way to evaluate the states, the client test will be perfect

Such as:

field.readonly
True/False

Best regard

Snippet:

def pyson_result(pyson_expr, target):
    encoder = PYSONEncoder()
    if isinstance(pyson_expr, str):
        the_pyson = encoder.encode(eval(pyson_expr, CONTEXT))
    else:
        the_pyson = encoder.encode(pyson_expr)
    if the_pyson is True:
        return True
    elif the_pyson is False:
        return False

    env = EvalEnvironment(target, Pool().get(target.__name__))
    env.update(Transaction().context)
    env['current_date'] = datetime.datetime.today()
    env['time'] = time
    env['context'] = Transaction().context
    env['active_id'] = target.id
    result = PYSONDecoder(env).decode(the_pyson)

    return result
1 Like

May I know where to put this? Doctest or unittest? Or in the model?

@bala4901 It is just a utility method we are using to “know” the value of a Pyson expr for a record. You could add it in your unittest file and call it from the unittests.

FYI we did NOT write it in order to actually test states values, but rather because we want to check them at runtime in some cases, so there may be some tweaking to make it work in a unittest context (though I do not think it will be necessary)

Edit : PYSONEncoder, PYSONDecoder, EvalEnvironment and CONTEXT should be imported from trytond.pyson