wcs/tests/test_prefill.py

65 lines
1.4 KiB
Python

import sys
import shutil
from quixote import cleanup
from wcs.qommon.http_request import HTTPRequest
from wcs import fields
from utilities import create_temporary_pub
def setup_module(module):
cleanup()
global pub
pub = create_temporary_pub()
req = HTTPRequest(None, {})
pub._set_request(req)
user = pub.user_class(name='user')
user.id = 'user'
user.email = 'test@example.net'
req._user = user
def teardown_module(module):
shutil.rmtree(pub.APP_DIR)
def test_prefill_string():
field = fields.Field()
field.prefill = {'type': 'string', 'value': 'test'}
assert field.get_prefill_value() == 'test'
def test_prefill_user():
field = fields.Field()
field.prefill = {'type': 'user', 'value': 'email'}
assert field.get_prefill_value() == 'test@example.net'
def test_prefill_formula():
field = fields.Field()
field.prefill = {'type': 'formula', 'value': 'str(2+5)'}
assert field.get_prefill_value() == '7'
def test_prefill_formula_with_error():
field = fields.Field()
field.prefill = {'type': 'formula', 'value': 'foobar'}
assert field.get_prefill_value() is None
def test_prefill_formula_substition_variable():
pub.substitutions.get_context_variables = lambda: {'test': 'value'}
field = fields.Field()
field.prefill = {'type': 'formula', 'value': 'test'}
assert field.get_prefill_value() == 'value'