Esempio n. 1
0
 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)
Esempio n. 2
0
 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)
Esempio n. 3
0
    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)
Esempio n. 4
0
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)
Esempio n. 5
0
 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)
Esempio n. 6
0
    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)
Esempio n. 7
0
 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
Esempio n. 8
0
    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)
Esempio n. 9
0
 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'}
Esempio n. 10
0
    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}
Esempio n. 11
0
    def test_extra_arguments_are_excluded(self):
        arg = Arg({
            'foo': Arg(),
        })

        in_data = {'foo': 42, 'bar': 24}

        assert arg.validated('', in_data) == {'foo': 42}
Esempio n. 12
0
    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
Esempio n. 13
0
def test_convert_and_use_params():
    arg = Arg(float, use=lambda val: val + 1)
    assert arg.validated(41) == 42.0
Esempio n. 14
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)
Esempio n. 15
0
def test_validated_with_bad_type():
    arg = Arg(type_=int)
    assert arg.validated(42) == 42
    with pytest.raises(ValidationError):
        arg.validated('nonint')
Esempio n. 16
0
 def test_validated_null_noop(self):
     arg = Arg()
     assert arg.validated('foo', {}) == {}
     assert arg.validated('foo', None) is None
Esempio n. 17
0
def test_convert_and_use_params():
    arg = Arg(int, use=lambda x: x + 1)
    assert arg.validated('41') == 42
Esempio n. 18
0
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)
Esempio n. 19
0
def test_use_param():
    arg = Arg(use=lambda x: x.upper())
    assert arg.validated('foo') == 'FOO'
Esempio n. 20
0
def test_multiple_with_use_arg():
    arg = Arg(multiple=True, use=lambda x: x.upper())
    assert arg.validated(['foo', 'bar']) == ['FOO', 'BAR']
Esempio n. 21
0
def test_validated():
    arg = Arg(validate=lambda x: x == 42)
    assert arg.validated(42) == 42
    with pytest.raises(ValidationError):
        arg.validated(32)
Esempio n. 22
0
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)
Esempio n. 23
0
 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)
Esempio n. 24
0
 def test_validated_long_type(self):
     arg = Arg(type_=long_type)
     assert arg.validated('foo', 42) == 42
Esempio n. 25
0
 def test_validated_text_type(self):
     arg = Arg(type_=text_type)
     assert arg.validated('foo', 42) == '42'
Esempio n. 26
0
def test_validate_can_be_none():
    arg = Arg(validate=None)
    assert arg.validated(41) == 41
Esempio n. 27
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)
Esempio n. 28
0
def test_multiple_with_type_arg():
    arg = Arg(int, multiple=True)
    assert arg.validated(['1', 2, 3.0]) == [1, 2, 3]
Esempio n. 29
0
def test_multiple_with_type_arg():
    arg = Arg(int, multiple=True)
    assert arg.validated(['1', 2, 3.0]) == [1, 2, 3]
Esempio n. 30
0
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)
Esempio n. 31
0
 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)
Esempio n. 32
0
def test_validated():
    arg = Arg(validate=lambda x: x == 42)
    assert arg.validated(42) == 42
    with pytest.raises(ValidationError):
        arg.validated(32)
Esempio n. 33
0
def test_conversion_to_str():
    arg = Arg(str)
    assert arg.validated(42) == '42'
Esempio n. 34
0
def test_validated_with_conversion():
    arg = Arg(validate=lambda x: x == 42, type_=int)
    assert arg.validated('42') == 42
Esempio n. 35
0
def test_use_param():
    arg = Arg(use=lambda x: x.upper())
    assert arg.validated('foo') == 'FOO'
Esempio n. 36
0
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)
Esempio n. 37
0
 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)
Esempio n. 38
0
def test_conversion_to_str():
    arg = Arg(str)
    assert arg.validated(42) == '42'
Esempio n. 39
0
def test_validated_with_conversion():
    arg = Arg(validate=lambda x: x == 42, type_=int)
    assert arg.validated('42') == 42
Esempio n. 40
0
def test_convert_and_use_params():
    arg = Arg(float, use=lambda val: val + 1)
    assert arg.validated(41) == 42.0
Esempio n. 41
0
def test_validated_with_bad_type():
    arg = Arg(type_=int)
    assert arg.validated(42) == 42
    with pytest.raises(ValidationError):
        arg.validated('nonint')
Esempio n. 42
0
def test_validate_can_be_none():
    arg = Arg(validate=None)
    assert arg.validated(41) == 41
Esempio n. 43
0
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)
Esempio n. 44
0
def test_multiple_with_use_arg():
    arg = Arg(multiple=True, use=lambda x: x.upper())
    assert arg.validated(['foo', 'bar']) == ['FOO', 'BAR']
Esempio n. 45
0
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)