Beispiel #1
0
 def test_transform_value(self) -> None:
     opt_vs = vs.Optional(valuespec=vs.Transform(
         valuespec=vs.Dictionary(elements=[
             ("a", vs.TextInput()),
             ("b", vs.Age()),
         ]),
         forth=lambda p:
         {k: v + 10 if k == "b" else v
          for k, v in p.items()},
     ))
     assert opt_vs.transform_value(None) is None
     assert opt_vs.transform_value({
         "a": "text",
         "b": 10
     }) == {
         "a": "text",
         "b": 20
     }
Beispiel #2
0
def test_age_value_to_json_conversion(value, result):
    assert vs.Age().value_to_html(value) == result
    json_value = vs.Age().value_to_json(value)
    assert vs.Age().value_from_json(json_value) == value
Beispiel #3
0
    # mypy is wrong about the nullcontext object type :-(
    with pytest.raises(MKUserError) if not expected_valid else nullcontext(
    ):  # type: ignore[attr-defined]
        vs.HostAddress(
            allow_host_name=allow_host_name,
            allow_ipv4_address=allow_ipv4_address,
            allow_ipv6_address=allow_ipv6_address,
            allow_empty=False,
        ).validate_value(value, "varprefix")


@pytest.mark.parametrize(
    "choices,default_value,expected_default",
    [
        (
            [("single_age", "Age", vs.Age(default_value=30))],
            None,
            ("single_age", 30),
        ),
        (
            [("age_1", "Age", vs.Age()), ("age_2", "Age", vs.Age())],
            "age_1",
            ("age_1", 0),
        ),
        (
            [("list_choice", "ListChoice", vs.ListChoice())],
            None,
            ("list_choice", []),
        ),
        ([("value", "Title")], None, "value"),
        ([("value", "Title", None)], None, "value"),