mandatory is a true property (no more MandatoryError) + tests

This commit is contained in:
2013-04-16 22:44:16 +02:00
parent 6097f3af84
commit 656b751995
7 changed files with 293 additions and 92 deletions

View File

@ -106,15 +106,3 @@ def test_cfgimpl_get_home_by_path():
assert config.getpaths(include_groups=False) == ['gc.name', 'gc.dummy', 'gc.float', 'bool', 'objspace', 'wantref', 'str', 'wantframework', 'int', 'boolop']
assert config.getpaths(include_groups=True) == ['gc', 'gc.name', 'gc.dummy', 'gc.float', 'bool', 'objspace', 'wantref', 'str', 'wantframework', 'int', 'boolop']
def test_mandatory_warnings():
descr = make_description()
config = Config(descr)
assert(MandatoryError, "config.str = ''")
setting = config.cfgimpl_get_settings()
setting.read_write()
assert list(mandatory_warnings(config)) == []
setting.disable_property('mandatory')
config.str = ''
assert list(mandatory_warnings(config)) == ['str']
setting.enable_property('mandatory')
assert list(mandatory_warnings(config)) == ['str']

214
test/test_mandatory.py Normal file
View File

@ -0,0 +1,214 @@
import autopath
#from py.test import raises
from tiramisu.config import Config, mandatory_warnings
from tiramisu.option import StrOption, OptionDescription
from tiramisu.error import PropertiesOptionError
def make_description():
stroption = StrOption('str', 'Test string option', default="abc",
properties=('mandatory', ))
stroption1 = StrOption('str1', 'Test string option',
properties=('mandatory', ))
stroption2 = StrOption('str2', 'Test string option',
properties=('mandatory', ))
stroption3 = StrOption('str3', 'Test string option', multi=True,
properties=('mandatory', ))
descr = OptionDescription('tiram', '', [stroption, stroption1, stroption2, stroption3])
return descr
def test_mandatory_ro():
descr = make_description()
config = Config(descr)
setting = config.cfgimpl_get_settings()
setting.read_only()
prop = []
try:
config.str1
except PropertiesOptionError, err:
prop = err.proptype
assert 'mandatory' in prop
setting.read_write()
config.str1 = 'yes'
setting.read_only()
assert config.str1 == 'yes'
def test_mandatory_rw():
descr = make_description()
config = Config(descr)
setting = config.cfgimpl_get_settings()
setting.read_write()
#not mandatory in rw
config.str2
config.str2 = 'yes'
assert config.str2 == 'yes'
def test_mandatory_default():
descr = make_description()
config = Config(descr)
setting = config.cfgimpl_get_settings()
setting.read_only()
#not mandatory in rw
config.str
setting.read_write()
config.str = 'yes'
setting.read_only()
config.str
setting.read_write()
config.str = None
setting.read_only()
prop = []
try:
config.str
except PropertiesOptionError, err:
prop = err.proptype
assert 'mandatory' in prop
#valeur vide : None, '', u'', ...
def test_mandatory_none():
descr = make_description()
config = Config(descr)
config.str1 = None
setting = config.cfgimpl_get_settings()
assert config.cfgimpl_get_values().getowner(descr.str1) == 'user'
setting.read_only()
prop = []
try:
config.str1
except PropertiesOptionError, err:
prop = err.proptype
assert 'mandatory' in prop
def test_mandatory_empty():
descr = make_description()
config = Config(descr)
config.str1 = ''
setting = config.cfgimpl_get_settings()
assert config.cfgimpl_get_values().getowner(descr.str1) == 'user'
setting.read_only()
prop = []
try:
config.str1
except PropertiesOptionError, err:
prop = err.proptype
assert 'mandatory' in prop
def test_mandatory_multi_none():
descr = make_description()
config = Config(descr)
setting = config.cfgimpl_get_settings()
config.str3 = [None]
setting.read_only()
assert config.cfgimpl_get_values().getowner(descr.str3) == 'user'
prop = []
try:
config.str3
except PropertiesOptionError, err:
prop = err.proptype
assert 'mandatory' in prop
setting.read_write()
config.str3 = ['yes', None]
setting.read_only()
assert config.cfgimpl_get_values().getowner(descr.str3) == 'user'
prop = []
try:
config.str3
except PropertiesOptionError, err:
prop = err.proptype
assert 'mandatory' in prop
def test_mandatory_multi_empty():
descr = make_description()
config = Config(descr)
setting = config.cfgimpl_get_settings()
config.str3 = ['']
setting.read_only()
assert config.cfgimpl_get_values().getowner(descr.str3) == 'user'
prop = []
try:
config.str3
except PropertiesOptionError, err:
prop = err.proptype
assert 'mandatory' in prop
setting.read_write()
config.str3 = ['yes', '']
setting.read_only()
assert config.cfgimpl_get_values().getowner(descr.str3) == 'user'
prop = []
try:
config.str3
except PropertiesOptionError, err:
prop = err.proptype
assert 'mandatory' in prop
def test_mandatory_disabled():
descr = make_description()
config = Config(descr)
setting = config.cfgimpl_get_settings()
config.str1
setting.read_only()
prop = []
try:
config.str1
except PropertiesOptionError, err:
prop = err.proptype
assert prop == ['mandatory']
setting.add_property('disabled', descr.str1)
prop = []
try:
config.str1
except PropertiesOptionError, err:
prop = err.proptype
assert prop == ['disabled', 'mandatory']
def test_mandatory_warnings_ro():
descr = make_description()
config = Config(descr)
config.str = ''
setting = config.cfgimpl_get_settings()
setting.read_only()
proc = []
try:
config.str
except PropertiesOptionError, err:
proc = err.proptype
assert proc == ['mandatory']
assert list(mandatory_warnings(config)) == ['str', 'str1', 'str2', 'str3']
setting.read_write()
config.str = 'a'
setting.read_only()
assert list(mandatory_warnings(config)) == ['str1', 'str2', 'str3']
def test_mandatory_warnings_rw():
descr = make_description()
config = Config(descr)
config.str = ''
setting = config.cfgimpl_get_settings()
setting.read_write()
config.str
assert list(mandatory_warnings(config)) == ['str', 'str1', 'str2', 'str3']
config.str = 'a'
assert list(mandatory_warnings(config)) == ['str1', 'str2', 'str3']
def test_mandatory_warnings_disabled():
descr = make_description()
config = Config(descr)
config.str = ''
setting = config.cfgimpl_get_settings()
setting.read_write()
config.str
assert list(mandatory_warnings(config)) == ['str', 'str1', 'str2', 'str3']
setting.add_property('disabled', descr.str)
assert list(mandatory_warnings(config)) == ['str1', 'str2', 'str3']

View File

@ -4,7 +4,7 @@ import autopath
from py.test import raises
from tiramisu.config import *
from tiramisu.option import *
from tiramisu.error import MandatoryError
from tiramisu.error import PropertiesOptionError
def make_description():
gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
@ -50,27 +50,6 @@ def test_set_defaut_value_from_option_object():
b = BoolOption("boolean", "", default=False)
assert b.getdefault() == False
def test_mandatory():
dummy1 = BoolOption('dummy1', 'doc dummy', properties=('mandatory', ))
dummy2 = BoolOption('dummy2', 'doc dummy', properties=('mandatory', ))
group = OptionDescription('group', '', [dummy1, dummy2])
config = Config(group)
setting = config.cfgimpl_get_settings()
setting.read_only()
# config.setoption('dummy1', True)
raises(MandatoryError, 'config.dummy1')
setting.read_write()
config.dummy1 = True
setting.read_only()
assert config.dummy1 == True
raises(MandatoryError, 'config.dummy2 == None')
# raises(MandatoryError, "config.override({'dummy2':None})")
setting.read_write()
config.set(dummy2=True)
config.dummy2 = False
setting.read_only()
assert config.dummy2 == False
def test_force_default_on_freeze():
"a frozen option wich is forced returns his default"
dummy1 = BoolOption('dummy1', 'doc dummy', default=False, properties=('force_default_on_freeze',))