示例#1
0
def test_decimal_field_invalid_type():

    field = Decimal(name='name')
    mapper_session = get_mapper_session(
        data={'name': None, 'email': '*****@*****.**'}, output={})
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)
示例#2
0
def test_decimal_field_invalid_type():

    field = Decimal(name='name')
    mapper_session = get_mapper_session(
        data={'name': None, 'email': '*****@*****.**'}, output={})
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)
示例#3
0
def test_decimal_input_precision():

    field = Decimal(name='name', required=True, precision=4)

    output = {}
    mapper_session = get_mapper_session(
        data={'name': '3.147261', 'email': '*****@*****.**'}, output=output)
    field.marshal(mapper_session)
    assert output == {'name': decimal.Decimal('3.1473')}
示例#4
0
def test_decimal_input_bounds_when_passed_as_string():

    field = Decimal(name='name', required=True, min=0.1)

    output = {}
    mapper_session = get_mapper_session(
        data={'name': "2", 'email': '*****@*****.**'}, output=output)
    field.marshal(mapper_session)
    assert output == {'name': decimal.Decimal(2)}
示例#5
0
def test_decimal_input_precision():

    field = Decimal(name='name', required=True, precision=4)

    output = {}
    mapper_session = get_mapper_session(
        data={'name': '3.147261', 'email': '*****@*****.**'}, output=output)
    field.marshal(mapper_session)
    assert output == {'name': decimal.Decimal('3.1473')}
示例#6
0
def test_decimal_output():
    class Foo(object):
        name = decimal.Decimal('2.52')

    field = Decimal(name='name', required=True)

    output = {}
    mapper_session = get_mapper_session(obj=Foo(), output=output)
    field.serialize(mapper_session)
    assert output == {'name': '2.52000'}
示例#7
0
def test_decimal_output():

    class Foo(object):
        name = decimal.Decimal('2.52')

    field = Decimal(name='name', required=True)

    output = {}
    mapper_session = get_mapper_session(obj=Foo(), output=output)
    field.serialize(mapper_session)
    assert output == {'name': '2.52000'}
示例#8
0
def test_decimal_input():

    field = Decimal(name='name', required=True)

    mapper_session = get_mapper_session(data={'email': '*****@*****.**'},
                                        output={})
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(data={
        'name': 'foo',
        'email': '*****@*****.**'
    },
                                        output={})
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    output = {}
    mapper_session = get_mapper_session(data={
        'name': 2,
        'email': '*****@*****.**'
    },
                                        output=output)
    field.marshal(mapper_session)
    assert output == {'name': decimal.Decimal(2)}
示例#9
0
def test_min_max_decimal():
    field = Decimal(name='age', min=0, max=1.5)
    output = {}

    mapper_session = get_mapper_session(data={'age': -1}, output=output)
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(data={'age': 4}, output=output)
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(data={'age': 0.7}, output=output)
    field.marshal(mapper_session)
    assert output == {'age': decimal.Decimal('0.7')}

    mapper_session = get_mapper_session(data={'age': 1.5}, output=output)
    field.marshal(mapper_session)
    assert output == {'age': decimal.Decimal('1.5')}
示例#10
0
def test_min_max_decimal():
    field = Decimal(name='age', min=0, max=1.5)
    output = {}

    mapper_session = get_mapper_session(data={'age': -1}, output=output)
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(data={'age': 4}, output=output)
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(data={'age': 0.7}, output=output)
    field.marshal(mapper_session)
    assert output == {'age': decimal.Decimal('0.7')}

    mapper_session = get_mapper_session(data={'age': 1.5}, output=output)
    field.marshal(mapper_session)
    assert output == {'age': decimal.Decimal('1.5')}
示例#11
0
def test_is_valid_decimal_pipe():
    """test piping data through is_valid_decimal.
    """

    field = Decimal(name='test')
    session = Session(field, 'test', {})

    with pytest.raises(FieldInvalid):
        is_valid_decimal(session)

    session.data = '2.5'
    assert is_valid_decimal(session) == decimal.Decimal('2.5')
    session.data = 2
    assert is_valid_decimal(session) == decimal.Decimal(2)
    session.data = 2.3
    assert is_valid_decimal(session) == decimal.Decimal(2.3)
示例#12
0
def test_decimal_input():

    field = Decimal(name='name', required=True)

    mapper_session = get_mapper_session(
        data={'email': '*****@*****.**'}, output={})
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(
        data={'name': 'foo', 'email': '*****@*****.**'}, output={})
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    output = {}
    mapper_session = get_mapper_session(
        data={'name': 2, 'email': '*****@*****.**'}, output=output)
    field.marshal(mapper_session)
    assert output == {'name': decimal.Decimal(2)}