Exemplo n.º 1
0
def test_ordered_dict():
    if not hasattr(collections, 'OrderedDict'):
        # collections.OrderedDict was added in Python2.7; only run if present
        return
    schema = Schema({Number():
                     Number()})  # x, y pairs (for interpolation or something)
    data = collections.OrderedDict([(5.0, 3.7), (24.0, 8.7), (43.0, 1.5),
                                    (62.0, 2.1), (71.5, 6.7), (90.5, 4.1),
                                    (109.0, 3.9)])
    out = schema(data)
    assert isinstance(
        out, collections.OrderedDict), 'Collection is no longer ordered'
    assert data.keys() == out.keys(), 'Order is not consistent'
Exemplo n.º 2
0
def test_number_when_precision_none_n_invalid_scale_yield_decimal_true():
    """ test with Number with no precision and invalid scale"""
    schema = Schema({"number": Number(scale=2, yield_decimal=True)})
    try:
        schema({"number": '12345678901.234'})
    except MultipleInvalid as e:
        assert_equal(
            str(e),
            "Scale must be equal to 2 for dictionary value @ data['number']")
    else:
        assert False, "Did not raise Invalid for String"
Exemplo n.º 3
0
def test_number_validation_with_invalid_precision_invalid_scale():
    """ test with Number with invalid precision and scale"""
    schema = Schema({"number": Number(precision=6, scale=2)})
    try:
        schema({"number": '123456.712'})
    except MultipleInvalid as e:
        assert_equal(
            str(e),
            "Precision must be equal to 6, and Scale must be equal to 2 for dictionary value @ data['number']"
        )
    else:
        assert False, "Did not raise Invalid for String"
Exemplo n.º 4
0
def test_number_validation_with_string():
    """ test with Number with string"""
    schema = Schema({"number": Number(precision=6, scale=2)})
    try:
        schema({"number": 'teststr'})
    except MultipleInvalid as e:
        assert_equal(
            str(e),
            "Value must be a number enclosed with string for dictionary value @ data['number']"
        )
    else:
        assert False, "Did not raise Invalid for String"
Exemplo n.º 5
0
def test_number_validation_with_valid_precision_scale_yield_decimal_false():
    """ test with Number with valid precision, scale and no yield_decimal"""
    schema = Schema(
        {"number": Number(precision=6, scale=2, yield_decimal=False)})
    out_ = schema({"number": '1234.00'})
    assert_equal(out_.get("number"), '1234.00')
Exemplo n.º 6
0
def test_number_when_valid_precision_n_scale_none_yield_decimal_true():
    """ test with Number with no precision and valid scale"""
    schema = Schema({"number": Number(precision=14, yield_decimal=True)})
    out_ = schema({"number": '1234567.8901234'})
    assert_equal(float(out_.get("number")), 1234567.8901234)
Exemplo n.º 7
0
def test_number_when_precision_none_n_valid_scale_case2_yield_decimal_true():
    """ test with Number with no precision and valid scale case 2 with zero in decimal part"""
    schema = Schema({"number": Number(scale=2, yield_decimal=True)})
    out_ = schema({"number": '123456789012.00'})
    assert_equal(float(out_.get("number")), 123456789012.00)
Exemplo n.º 8
0
def test_number_when_precision_scale_none_yield_decimal_true():
    """ test with Number with no precision and scale"""
    schema = Schema({"number": Number(yield_decimal=True)})
    out_ = schema({"number": '12345678901234'})
    assert_equal(out_.get("number"), 12345678901234)
Exemplo n.º 9
0
def test_number_validation_with_valid_precision_scale_yield_decimal_true():
    """ test with Number with valid precision and scale"""
    schema = Schema(
        {"number": Number(precision=6, scale=2, yield_decimal=True)})
    out_ = schema({"number": '1234.00'})
    assert_equal(float(out_.get("number")), 1234.00)