1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
from decimal import Decimal
import setoptconf as soc
GOOD_SIMPLE_VALUES = (
(soc.String, None, None),
(soc.String, 'foo', 'foo'),
(soc.String, '1', '1'),
(soc.String, 1, '1'),
(soc.String, 1.23, '1.23'),
(soc.String, Decimal('1.23'), '1.23'),
(soc.Integer, None, None),
(soc.Integer, 123, 123),
(soc.Integer, '123', 123),
(soc.Integer, 123.45, 123),
(soc.Integer, Decimal('123'), 123),
(soc.Integer, Decimal('123.45'), 123),
(soc.Float, None, None),
(soc.Float, 123, 123.0),
(soc.Float, '123', 123.0),
(soc.Float, 123.45, 123.45),
(soc.Float, Decimal('123'), 123.0),
(soc.Float, Decimal('123.45'), 123.45),
(soc.Boolean, None, None),
(soc.Boolean, True, True),
(soc.Boolean, False, False),
(soc.Boolean, 'y', True),
(soc.Boolean, 'yes', True),
(soc.Boolean, 't', True),
(soc.Boolean, 'true', True),
(soc.Boolean, 'on', True),
(soc.Boolean, '1', True),
(soc.Boolean, '', False),
(soc.Boolean, 'n', False),
(soc.Boolean, 'no', False),
(soc.Boolean, 'f', False),
(soc.Boolean, 'false', False),
(soc.Boolean, 'off', False),
(soc.Boolean, '0', False),
(soc.Boolean, 123, True),
(soc.Boolean, 0, False),
(soc.Boolean, 123.45, True),
)
BAD_SIMPLE_VALUES = (
(soc.Integer, 'foo'),
(soc.Integer, '123abc'),
(soc.Float, 'foo'),
(soc.Float, '123abc'),
(soc.Float, '123.45abc'),
(soc.Boolean, 'foo'),
)
def test_simple_sanitization():
for datatype, in_value, out_value in GOOD_SIMPLE_VALUES:
check_good_value(datatype, in_value, out_value)
for datatype, in_value in BAD_SIMPLE_VALUES:
check_bad_value(datatype, in_value)
def check_good_value(datatype, in_value, out_value):
dt = datatype()
assert dt.sanitize(in_value) == out_value
assert dt.is_valid(in_value) is True
def check_bad_value(datatype, in_value):
dt = datatype()
try:
dt.sanitize(in_value)
except soc.DataTypeError:
pass
else:
assert False, 'Invalid %s allowed: %s' % (
datatype.__name__,
in_value,
)
assert dt.is_valid(in_value) is False
GOOD_LIST_VALUES = (
(soc.String, None, None),
(soc.String, [], []),
(soc.String, ['foo', 'bar'], ['foo', 'bar']),
(soc.String, ('foo', 'bar'), ['foo', 'bar']),
(soc.String(), ['foo', 'bar'], ['foo', 'bar']),
(soc.String, 'foo', ['foo']),
(soc.Integer, [123, '456'], [123, 456]),
)
BAD_LIST_VALUES = (
(soc.Integer, ['foo'], soc.DataTypeError),
(soc.Boolean, [True, False, 'y', 4, 'foo'], soc.DataTypeError),
('a', ['foo'], TypeError),
(soc.Configuration, ['foo'], TypeError),
)
def test_list_sanitization():
for subtype, in_value, out_value in GOOD_LIST_VALUES:
check_good_list_value(subtype, in_value, out_value)
for subtype, in_value, exc in BAD_LIST_VALUES:
check_bad_list_value(subtype, in_value, exc)
def check_good_list_value(subtype, in_value, out_value):
dt = soc.List(subtype)
assert dt.sanitize(in_value) == out_value
def check_bad_list_value(subtype, in_value, exc):
try:
dt = soc.List(subtype)
dt.sanitize(in_value)
except exc:
pass
else:
assert False, 'Invalid %s allowed: %s' % (
subtype.__class__.__name__,
in_value,
)
GOOD_CHOICE_VALUES = (
(soc.String, ['foo', 'bar'], None),
(soc.String, ['foo', 'bar'], 'foo'),
(None, ['foo', 'bar'], 'foo'),
(soc.Integer, [1,2,3], 2),
(soc.Integer(), [1,2,3], 2),
)
BAD_CHOICE_VALUES = (
(soc.String, ['foo', 'bar'], 'baz', soc.DataTypeError),
(soc.String, [1, 2, 3], 'baz', soc.DataTypeError),
('a', [1, 2, 3], 4, TypeError),
)
def test_choice_sanitization():
for subtype, choices, value in GOOD_CHOICE_VALUES:
check_good_choice_value(subtype, choices, value)
for subtype, choices, value, exc in BAD_CHOICE_VALUES:
check_bad_choice_value(subtype, choices, value, exc)
def check_good_choice_value(subtype, choices, value):
dt = soc.Choice(choices, subtype)
assert dt.sanitize(value) == value
def check_bad_choice_value(subtype, choices, value, exc):
try:
dt = soc.Choice(choices, subtype)
dt.sanitize(value)
except exc:
pass
else:
assert False, 'Invalid choice allowed: %s' % value
|