def test_validated_non_nullable_types(self, arg_type): print(arg_type) arg = Arg(type_=arg_type) with pytest.raises(ValidationError) as excinfo: arg.validated('foo', None) assert 'Expected type "{0}" for foo, got "null"'.format( __type_map__[arg_type]) in str(excinfo)
def test_validated_unknown_type(self): arg = Arg(type_=UUID) assert (arg.validated('foo', '12345678123456781234567812345678') == UUID('12345678-1234-5678-1234-567812345678')) with pytest.raises(ValidationError) as excinfo: arg.validated('foo', None) assert 'Expected type "UUID" for foo, got "null"' in str(excinfo)
def test_nested_validation(self): arg = Arg({'foo': Arg(validate=lambda v: v <= 42)}) assert arg.validated('myarg', {'foo': 42}) == {'foo': 42} with pytest.raises(ValidationError) as excinfo: arg.validated('', {'foo': 43}) assert 'Validator <lambda>(43) is not True' in str(excinfo)
def test_passing_exception_as_error_argument(): arg = Arg(int, validate=lambda n: n == 42, error=AttributeError('an error occurred.')) with pytest.raises(ValidationError) as excinfo: arg.validated(41) assert 'an error occurred' in str(excinfo)
def test_nested_required(self): arg = Arg({ 'foo': Arg(required=True), 'bar': Arg(required=False), }) with pytest.raises(ValidationError) as excinfo: arg.validated('', {}) assert 'Required parameter "foo" not found.' in str(excinfo)
def test_nested_argdict_has_type_dict(self): arg = Arg({'foo': Arg()}) assert arg.type == dict with pytest.raises(ValidationError) as excinfo: arg.validated('myarg', 'notadict') assert 'Expected type "object" for myarg, got "string"' in str(excinfo)
def test_nested_required_unicode_error_message_override(self): arg = Arg({ 'foo': Arg(required=u'We need foo') }) with pytest.raises(RequiredArgMissingError) as excinfo: arg.validated('', {}) assert 'We need foo' in excinfo.value.message assert 'foo' in excinfo.value.arg_name
def test_nested_multiple(self): arg = Arg({ 'foo': Arg(required=True), 'bar': Arg(required=True), }, multiple=True) in_data = [{'foo': 42, 'bar': 24}, {'foo': 12, 'bar': 21}] assert arg.validated('', in_data) == in_data bad_data = [{'foo': 42, 'bar': 24}, {'bar': 21}] with pytest.raises(ValidationError) as excinfo: arg.validated('', bad_data) assert 'Required' in str(excinfo)
def test_nested_use(self): arg = Arg({ 'foo': Arg(use=lambda v: v.upper()), 'bar': Arg(use=lambda v: v.lower()) }) in_data = {'foo': 'hErP', 'bar': 'dErP'} assert arg.validated('', in_data) == {'foo': 'HERP', 'bar': 'derp'}
def test_outer_use(self): arg = Arg({ 'foo': Arg() }, use=json.loads) # has extra args in_data = json.dumps({'foo': 42, 'bar': 24}) assert arg.validated('', in_data) == {'foo': 42}
def test_extra_arguments_are_excluded(self): arg = Arg({ 'foo': Arg(), }) in_data = {'foo': 42, 'bar': 24} assert arg.validated('', in_data) == {'foo': 42}
def test_deep_nesting_validation(self): arg = Arg({ 'foo': Arg({ 'bar': Arg(validate=lambda v: v <= 42) }) }) valid = {'foo': {'bar': 42}} assert arg.validated('myarg', valid) == valid
def test_convert_and_use_params(): arg = Arg(float, use=lambda val: val + 1) assert arg.validated(41) == 42.0
def test_use_is_called_before_validate(): arg = Arg(use=lambda x: x + 1, validate=lambda x: x == 41) with pytest.raises(ValidationError): arg.validated(41)
def test_validated_with_bad_type(): arg = Arg(type_=int) assert arg.validated(42) == 42 with pytest.raises(ValidationError): arg.validated('nonint')
def test_validated_null_noop(self): arg = Arg() assert arg.validated('foo', {}) == {} assert arg.validated('foo', None) is None
def test_convert_and_use_params(): arg = Arg(int, use=lambda x: x + 1) assert arg.validated('41') == 42
def test_use_param(): arg = Arg(use=lambda x: x.upper()) assert arg.validated('foo') == 'FOO'
def test_multiple_with_use_arg(): arg = Arg(multiple=True, use=lambda x: x.upper()) assert arg.validated(['foo', 'bar']) == ['FOO', 'BAR']
def test_validated(): arg = Arg(validate=lambda x: x == 42) assert arg.validated(42) == 42 with pytest.raises(ValidationError): arg.validated(32)
def test_default_valdation_msg(): arg = Arg(validate=lambda x: x == 42) with pytest.raises(ValidationError) as excinfo: arg.validated(1) assert 'Validator <lambda>(1) is not True' in str(excinfo)
def test_validated_with_bad_type(self): arg = Arg(type_=int) assert arg.validated('foo', 42) == 42 with pytest.raises(ValidationError) as excinfo: arg.validated('foo', 'nonint') assert 'Expected type "integer" for foo, got "string"' in str(excinfo)
def test_validated_long_type(self): arg = Arg(type_=long_type) assert arg.validated('foo', 42) == 42
def test_validated_text_type(self): arg = Arg(type_=text_type) assert arg.validated('foo', 42) == '42'
def test_validate_can_be_none(): arg = Arg(validate=None) assert arg.validated(41) == 41
def test_multiple_with_type_arg(): arg = Arg(int, multiple=True) assert arg.validated(['1', 2, 3.0]) == [1, 2, 3]
def test_validated_with_nonascii_input(): arg = Arg(validate=lambda t: False) text = u'øˆ∆´ƒº' with pytest.raises(ValidationError) as excinfo: arg.validated(text) assert text in unicode(excinfo)
def test_validated_with_nonascii_input(self): arg = Arg(validate=lambda t: False) text = u'øˆ∆´ƒº' with pytest.raises(ValidationError) as excinfo: arg.validated('foo', text) assert text in unicode(excinfo)
def test_conversion_to_str(): arg = Arg(str) assert arg.validated(42) == '42'
def test_validated_with_conversion(): arg = Arg(validate=lambda x: x == 42, type_=int) assert arg.validated('42') == 42
def test_custom_error(): arg = Arg(type_=int, error='not an int!') with pytest.raises(ValidationError) as excinfo: arg.validated('badinput') assert 'not an int!' in str(excinfo)
def test_validated_null(self): arg = Arg(type_=dict) assert arg.validated('foo', {}) == {} with pytest.raises(ValidationError) as excinfo: arg.validated('foo', None) assert 'Expected type "object" for foo, got "null"' in str(excinfo)