2938
2938
class TestStackGet(TestStackWithTransport):
2941
super(TestStackGet, self).setUp()
2942
self.conf = self.get_stack(self)
2944
2940
def test_get_for_empty_stack(self):
2945
self.assertEquals(None, self.conf.get('foo'))
2941
conf = self.get_stack(self)
2942
self.assertEquals(None, conf.get('foo'))
2947
2944
def test_get_hook(self):
2948
self.conf.store._load_from_string('foo=bar')
2945
conf = self.get_stack(self)
2946
conf.store._load_from_string('foo=bar')
2950
2948
def hook(*args):
2951
2949
calls.append(args)
2952
2950
config.ConfigHooks.install_named_hook('get', hook, None)
2953
2951
self.assertLength(0, calls)
2954
value = self.conf.get('foo')
2952
value = conf.get('foo')
2955
2953
self.assertEquals('bar', value)
2956
2954
self.assertLength(1, calls)
2957
self.assertEquals((self.conf, 'foo', 'bar'), calls[0])
2960
class TestStackGetWithConverter(TestStackGet):
2963
super(TestStackGetWithConverter, self).setUp()
2964
self.overrideAttr(config, 'option_registry', config.OptionRegistry())
2965
self.registry = config.option_registry
2967
def register_bool_option(self, name, default, invalid=None):
2968
b = config.Option(name, default=default, help='A boolean.',
2969
from_unicode=config.bool_from_store,
2971
self.registry.register(b)
2973
def test_get_with_bool_not_defined_default_true(self):
2974
self.register_bool_option('foo', True)
2975
self.assertEquals(True, self.conf.get('foo'))
2977
def test_get_with_bool_not_defined_default_false(self):
2978
self.register_bool_option('foo', False)
2979
self.assertEquals(False, self.conf.get('foo'))
2981
def test_get_with_bool_converter_not_default(self):
2982
self.register_bool_option('foo', False)
2983
self.conf.store._load_from_string('foo=yes')
2984
self.assertEquals(True, self.conf.get('foo'))
2986
def test_get_with_bool_converter_invalid_string(self):
2987
self.register_bool_option('foo', False)
2988
self.conf.store._load_from_string('foo=not-a-boolean')
2989
self.assertEquals(False, self.conf.get('foo'))
2991
def test_get_with_bool_converter_invalid_list(self):
2992
self.register_bool_option('foo', False)
2993
self.conf.store._load_from_string('foo=not,a,boolean')
2994
self.assertEquals(False, self.conf.get('foo'))
2996
def test_get_invalid_warns(self):
2997
self.register_bool_option('foo', False, invalid='warning')
2998
self.conf.store._load_from_string('foo=not-a-boolean')
3001
warnings.append(args[0] % args[1:])
3002
self.overrideAttr(trace, 'warning', warning)
3003
self.assertEquals(False, self.conf.get('foo'))
3004
self.assertLength(1, warnings)
3005
self.assertEquals('Value "not-a-boolean" is not valid for "foo"',
3008
def test_get_invalid_errors(self):
3009
self.register_bool_option('foo', False, invalid='error')
3010
self.conf.store._load_from_string('foo=not-a-boolean')
3011
self.assertRaises(errors.ConfigOptionValueError, self.conf.get, 'foo')
3013
def register_integer_option(self, name, default):
3014
i = config.Option(name, default=default, help='An integer.',
3015
from_unicode=config.int_from_store)
3016
self.registry.register(i)
3018
def test_get_with_integer_not_defined_returns_default(self):
3019
self.register_integer_option('foo', 42)
3020
self.assertEquals(42, self.conf.get('foo'))
3022
def test_get_with_integer_converter_not_default(self):
3023
self.register_integer_option('foo', 42)
3024
self.conf.store._load_from_string('foo=16')
3025
self.assertEquals(16, self.conf.get('foo'))
3027
def test_get_with_integer_converter_invalid_string(self):
3028
# We don't set a default value
3029
self.register_integer_option('foo', None)
3030
self.conf.store._load_from_string('foo=forty-two')
3031
# No default value, so we should get None
3032
self.assertEquals(None, self.conf.get('foo'))
3034
def test_get_with_integer_converter_invalid_list(self):
3035
# We don't set a default value
3036
self.register_integer_option('foo', None)
3037
self.conf.store._load_from_string('foo=a,list')
3038
# No default value, so we should get None
3039
self.assertEquals(None, self.conf.get('foo'))
3041
def register_list_option(self, name, default):
3042
l = config.Option(name, default=default, help='A list.',
3043
from_unicode=config.list_from_store)
3044
self.registry.register(l)
3046
def test_get_with_list_not_defined_returns_default(self):
3047
self.register_list_option('foo', [])
3048
self.assertEquals([], self.conf.get('foo'))
3050
def test_get_with_list_converter_nothing(self):
3051
self.register_list_option('foo', [1])
3052
self.conf.store._load_from_string('foo=')
3053
self.assertEquals([], self.conf.get('foo'))
3055
def test_get_with_list_converter_no_item(self):
3056
self.register_list_option('foo', [1])
3057
self.conf.store._load_from_string('foo=,')
3058
self.assertEquals([], self.conf.get('foo'))
3060
def test_get_with_list_converter_one_boolean(self):
3061
self.register_list_option('foo', [1])
3062
self.conf.store._load_from_string('foo=True')
3063
# We get a list of one string
3064
self.assertEquals(['True'], self.conf.get('foo'))
3066
def test_get_with_list_converter_one_integer(self):
3067
self.register_list_option('foo', [1])
3068
self.conf.store._load_from_string('foo=2')
3069
# We get a list of one string
3070
self.assertEquals(['2'], self.conf.get('foo'))
3072
def test_get_with_list_converter_one_string(self):
3073
self.register_list_option('foo', ['foo'])
3074
self.conf.store._load_from_string('foo=bar')
3075
# We get a list of one string
3076
self.assertEquals(['bar'], self.conf.get('foo'))
3078
def test_get_with_list_converter_many_items(self):
3079
self.register_list_option('foo', [1])
3080
self.conf.store._load_from_string('foo=m,o,r,e')
3081
self.assertEquals(['m', 'o', 'r', 'e'], self.conf.get('foo'))
2955
self.assertEquals((conf, 'foo', 'bar'), calls[0])
3084
2958
class TestStackSet(TestStackWithTransport):