better error messages

This commit is contained in:
2016-09-11 20:41:36 +02:00
parent c21949c637
commit 408e4cf088
3 changed files with 85 additions and 79 deletions

View File

@ -11,15 +11,18 @@ from tiramisu.error import ValueWarning
from tiramisu.i18n import _
msg_err = _("attention, {0} could be an invalid {1} for option {2}, {3}")
def return_true(value, param=None):
if value == 'val' and param in [None, 'yes']:
return True
return ValueError('error')
return ValueError('test error')
def return_false(value, param=None):
if value == 'val' and param in [None, 'yes']:
return ValueError('error')
return ValueError('test error')
def return_val(value, param=None):
@ -28,7 +31,7 @@ def return_val(value, param=None):
def return_if_val(value):
if value != 'val':
return ValueError('error')
return ValueError('test error')
def test_validator():
@ -96,7 +99,7 @@ def test_validator_warning():
cfg.opt2 = 'val'
assert len(w) == 1
assert w[0].message.opt == opt2
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('opt2', 'error')
assert str(w[0].message) == msg_err.format('val', opt2.display_name, 'opt2', 'test error')
#
with warnings.catch_warnings(record=True) as w:
cfg.opt3.append('val')
@ -106,7 +109,7 @@ def test_validator_warning():
cfg.opt3.append('val1')
assert len(w) == 1
assert w[0].message.opt == opt3
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('opt3', 'error')
assert str(w[0].message) == msg_err.format('val1', opt3.display_name, 'opt3', 'test error')
raises(ValueError, "cfg.opt2 = 1")
#
with warnings.catch_warnings(record=True) as w:
@ -114,9 +117,9 @@ def test_validator_warning():
cfg.opt3.append('val')
assert len(w) == 2
assert w[0].message.opt == opt2
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('opt2', 'error')
assert str(w[0].message) == msg_err.format('val', opt2.display_name, 'opt2', 'test error')
assert w[1].message.opt == opt3
assert str(w[1].message) == _("warning on the value of the option {0}: {1}").format('opt3', 'error')
assert str(w[0].message) == msg_err.format('val', opt2.display_name, 'opt2', 'test error')
def test_validator_warning_disabled():
@ -168,29 +171,29 @@ def test_validator_warning_master_slave():
cfg.ip_admin_eth0.netmask_admin_eth0 = ['val1']
assert len(w) == 1
assert w[0].message.opt == netmask_admin_eth0
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('netmask_admin_eth0', 'error')
assert str(w[0].message) == msg_err.format('val1', netmask_admin_eth0.display_name, 'netmask_admin_eth0', 'test error')
#
with warnings.catch_warnings(record=True) as w:
cfg.ip_admin_eth0.ip_admin_eth0 = ['val']
assert len(w) == 1
assert w[0].message.opt == ip_admin_eth0
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('ip_admin_eth0', 'error')
assert str(w[0].message) == msg_err.format('val', ip_admin_eth0.display_name, 'ip_admin_eth0', 'test error')
#
with warnings.catch_warnings(record=True) as w:
cfg.ip_admin_eth0.ip_admin_eth0 = ['val', 'val1', 'val1']
assert len(w) == 1
assert w[0].message.opt == ip_admin_eth0
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('ip_admin_eth0', 'error')
assert str(w[0].message) == msg_err.format('val', ip_admin_eth0.display_name, 'ip_admin_eth0', 'test error')
#
with warnings.catch_warnings(record=True) as w:
cfg.ip_admin_eth0.ip_admin_eth0 = ['val1', 'val', 'val1']
assert len(w) == 1
assert w[0].message.opt == ip_admin_eth0
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('ip_admin_eth0', 'error')
assert str(w[0].message) == msg_err.format('val', ip_admin_eth0.display_name, 'ip_admin_eth0', 'test error')
#
warnings.resetwarnings()
with warnings.catch_warnings(record=True) as w:
cfg.ip_admin_eth0.ip_admin_eth0 = ['val1', 'val1', 'val']
assert len(w) == 1
assert w[0].message.opt == ip_admin_eth0
assert str(w[0].message) == _("warning on the value of the option {0}: {1}").format('ip_admin_eth0', 'error')
assert str(w[0].message) == msg_err.format('val', ip_admin_eth0.display_name, 'ip_admin_eth0', 'test error')