Esempio n. 1
0
def test_float():
    field = "field_name"
    default = 10.0
    target = "target_name"
    label = "Field Name"
    min = 10
    max = 20

    # Default values
    s_opt = server_options.Float(field)
    assert s_opt.default == 0
    assert s_opt.label == field
    assert s_opt.target == field
    assert s_opt.min is None
    assert s_opt.max is None

    # Specified values propogate
    s_opt = server_options.Float(field,
                                 default=default,
                                 target=target,
                                 label=label,
                                 min=min,
                                 max=max)
    assert s_opt.default == default
    assert s_opt.target == target
    assert s_opt.label == label
    assert s_opt.min == min
    assert s_opt.max == max
    assert isinstance(s_opt.min, float)
    assert isinstance(s_opt.max, float)
    assert isinstance(s_opt.default, float)

    def check(opt):
        assert opt.validate(15.5) == 15.5
        assert opt.validate(10) == 10.0
        assert opt.validate(20) == 20.0

        with pytest.raises(TypeError):
            opt.validate("1")
        with pytest.raises(ValueError):
            opt.validate(min - 1)
        with pytest.raises(ValueError):
            opt.validate(max + 1)

    # Server-side validation
    check(s_opt)

    # Serialization works
    spec = s_opt.json_spec()

    c_opt = client_options.Field._from_spec(spec)

    assert isinstance(c_opt, client_options.Float)
    assert c_opt.value == default
    assert c_opt.label == label
    assert s_opt.min == min
    assert s_opt.max == max

    # Client-side validation
    check(c_opt)
Esempio n. 2
0
def server_opts():
    return server_options.Options(
        server_options.Integer("integer_field", 1, target="integer_field2"),
        server_options.Float("float_field", 2.5, min=2, max=4),
        server_options.Select("select_field", [("a", 5), "b", ("c", 10)]),
    )