コード例 #1
0
def test_post_coerce():
    v = Validator({
        'a': {
            'coerce': 'int',
            'post_coerce': 'str'
        },
        'g': {
            'coerce': 'int',
            'post_coerce': 'gender'
        }
    })

    v.coerce_gender = coerce_gender

    assert v['a'](1) == ('1', None)
    assert v['a'](1.2) == ('1', None)
    assert v['a']('1') == ('1', None)
    assert v['a']({}) == (None, 'Input is not coercible to int')

    assert v['g'](1) == ('male', None)
    assert v['g'](1.2) == ('male', None)
    assert v['g']('1') == ('male', None)
    assert v['g'](2) == (None, 'Input is not coercible to gender')
    assert v['g']('2') == (None, 'Input is not coercible to gender')
    assert v['g']({}) == (None, 'Input is not coercible to int')
コード例 #2
0
def test_coerce():
    v = Validator({
        'n': {'coerce': 'number'},
        's': {'coerce': 'str'},
        'b': {'coerce': 'bool'},
        'g': {'coerce': 'gender'}
    })

    v.coerce_gender = coerce_gender

    assert v['n'](1) == (1, None)
    assert v['n'](1.2) == (1.2, None)
    assert v['n']('1') == (1, None)
    assert v['n']('1.2') == (1.2, None)
    assert v['n']('que?') == (None, 'Input is not coercible to number')

    assert v['s'](1) == ('1', None)
    assert v['s'](1.2) == ('1.2', None)
    assert v['s']('1') == ('1', None)
    assert v['s']({}) == ('{}', None)
    assert v['s'](None) == ('None', None)

    assert v['b'](True) == (True, None)
    assert v['b'](1) == (True, None)
    assert v['b']('Yes') == (True, None)
    assert v['b']('1.0') == (True, None)
    assert v['b'](0) == (False, None)
    assert v['b'](False) == (False, None)
    assert v['b']('N') == (False, None)
    assert v['b']({}) == (None, 'Input is not coercible to bool')
    assert v['b'](5) == (None, 'Input is not coercible to bool')
    assert v['b']('qwe') == (None, 'Input is not coercible to bool')

    assert v['g']('female') == ('female', None)
    assert v['g'](1) == ('male', None)
    assert v['g'](False) == ('female', None)
    assert v['g'](2) == (None, 'Input is not coercible to gender')
    assert v['g']({}) == (None, 'Input is not coercible to gender')
    assert v['g'](None) == (None, 'Input is not coercible to gender')