Ejemplo n.º 1
0
def test_json_marshalling() -> None:
    roundtrip(StringKind("string"), Kind)
    roundtrip(StringKind("string", 5, 13, "foo.*bla"), Kind)
    roundtrip(StringKind("string", enum={"foo", "bla"}), Kind)
    roundtrip(NumberKind("num", "int32", minimum=2, maximum=34), Kind)
    roundtrip(NumberKind("num", "int64", enum={1, 2}), Kind)
    roundtrip(BooleanKind("b"), Kind)
    roundtrip(DateKind("d"), Kind)
    roundtrip(DateTimeKind("d"), Kind)
    roundtrip(DurationKind("duration"), Kind)
    roundtrip(
        TransformKind("synth", "duration", "datetime", "duration_to_datetime",
                      True), Kind)
    roundtrip(ArrayKind(StringKind("string")), Kind)
    roundtrip(Property("foo", "foo"), Property)
    roundtrip(
        Property("age", "trafo.duration_to_datetime", False,
                 SyntheticProperty(["ctime"])), Property)
    roundtrip(
        ComplexKind(
            "Test",
            ["Base"],
            [
                Property("array", "string[]"),
                Property("s", "float"),
                Property("i", "int32"),
                Property("other", "SomeComposite"),
            ],
        ),
        Kind,
    )
Ejemplo n.º 2
0
def test_update(person_model: Model) -> None:
    with pytest.raises(
            AttributeError) as not_allowed:  # update city with different type
        person_model.update_kinds([
            ComplexKind(
                "Address",
                ["Base"],
                [
                    Property("city", "int32", required=True),
                ],
            )
        ])
    assert (
        str(not_allowed.value) ==
        "Update not possible: following properties would be non unique having the same path but different type: "
        "Address.city (string -> int32)")

    updated = person_model.update_kinds([StringKind("Foo")])
    assert updated["Foo"].fqn == "Foo"
    with pytest.raises(AttributeError) as simple:
        updated.update_kinds([ComplexKind("Foo", [], [])])
    assert str(
        simple.value) == "Update Foo changes an existing property type Foo"
    with pytest.raises(AttributeError) as duplicate:
        updated.update_kinds(
            [ComplexKind("Bla", [], [Property("id", "int32")])])
    assert (
        str(duplicate.value) ==
        "Update not possible: following properties would be non unique having the same path but different type: "
        "Bla.id (string -> int32)")
Ejemplo n.º 3
0
def test_model() -> List[Kind]:
    string_kind = StringKind("some.string", 0, 3, "\\w+")
    int_kind = NumberKind("some.int", "int32", 0, 100)
    bool_kind = BooleanKind("some.bool")
    base = ComplexKind(
        "base",
        [],
        [
            Property("identifier", "string", required=True),
            Property("kind", "string", required=True),
        ],
    )
    foo = ComplexKind(
        "foo",
        ["base"],
        [
            Property("name", "string"),
            Property("some_int", "some.int"),
            Property("some_string", "some.string"),
            Property("now_is", "datetime"),
        ],
    )
    bla = ComplexKind(
        "bla",
        ["base"],
        [
            Property("name", "string"),
            Property("now", "date"),
            Property("f", "int32"),
            Property("g", "int32[]"),
        ],
    )
    return [string_kind, int_kind, bool_kind, base, foo, bla]
Ejemplo n.º 4
0
async def test_model_api(core_client: ApiClient) -> None:

    # GET /model
    assert len((await core_client.model()).kinds) >= len(predefined_kinds)

    # PATCH /model
    update = await core_client.update_model([
        StringKind("only_three", min_length=3, max_length=3),
        ComplexKind("test_cpl", [], [Property("ot", "only_three")]),
    ])
    assert isinstance(update.get("only_three"), StringKind)
Ejemplo n.º 5
0
def test_string() -> None:
    a = StringKind("string", 5, 13, "foo.*bla")
    assert expect_error(a,
                        "foo") == ">foo< does not conform to regex: foo.*bla"
    assert expect_error(
        a, "fooooo") == ">fooooo< does not conform to regex: foo.*bla"
    assert a.check_valid("fooooobla") is None
    assert expect_error(
        a, "fooooooooooobla") == ">fooooooooooobla< is too long! Allowed: 13"
    b = StringKind("string", enum={"foo", "bla", "bar"})
    assert b.check_valid("foo") is None
    assert expect_error(b, "baz").startswith(">baz< should be one of")
Ejemplo n.º 6
0
def person_model() -> Model:
    zip = StringKind("zip")
    base = ComplexKind(
        "Base",
        [],
        [
            Property(
                "id", "string", required=True, description="Some identifier"),
            Property("kind",
                     "string",
                     required=True,
                     description="Kind if this node."),
            Property("list", "string[]", description="A list of strings."),
            Property("tags",
                     "dictionary[string, string]",
                     description="Key/value pairs."),
            Property("mtime",
                     "datetime",
                     description="Modification time of this node."),
        ],
    )
    address = ComplexKind(
        "Address",
        ["Base"],
        [
            Property("zip", "zip", description="The zip code."),
            Property("city",
                     "string",
                     required=True,
                     description="The name of the city.\nAnd another line."),
        ],
    )
    person = ComplexKind(
        "Person",
        ["Base"],
        [
            Property("name", "string", description="The name of the person."),
            Property("address",
                     "Address",
                     description="The address of the person."),
            Property("other_addresses",
                     "dictionary[string, Address]",
                     description="Other addresses."),
            Property("addresses",
                     "Address[]",
                     description="The list of addresses."),
            Property("any", "any", description="Some arbitrary value."),
        ],
    )
    any_foo = ComplexKind(
        "any_foo",
        ["Base"],
        [
            Property("foo", "any", description="Some foo value."),
            Property("test", "string", description="Some test value."),
        ],
    )
    cloud = ComplexKind("cloud", ["Base"], [])
    account = ComplexKind("account", ["Base"], [])
    region = ComplexKind("region", ["Base"], [])
    parent = ComplexKind("parent", ["Base"], [])
    child = ComplexKind("child", ["Base"], [])

    return Model.from_kinds([
        zip, person, address, base, any_foo, cloud, account, region, parent,
        child
    ])