Beispiel #1
0
def testi_match2():
    pattern = {
        "between_1_and_5": criteria.more(1, eq=True) & criteria.less(5),
        "between_1_and_5_auto": criteria.between(1, 5),
    }
    tree1 = {"between_1_and_5": 2, "between_1_and_5_auto": 3}
    assert match.match(pattern, tree1).is_valid()
Beispiel #2
0
def test_that_check_nodeinput_dump():
    beautiful_input = NodeInput(dummy_node, "dump_test", int)
    beautiful_input.set(666)
    ugly_output = NodeOutput(dummy_node, "subdump_test", int)
    beautiful_input.connect(ugly_output)
    dump_match = match.match(
        {
            "id":
            criterias.is_type(str)
            & criterias.regex(uuid_regex)
            & criterias.equals(str(beautiful_input.id)),
            "type":
            criterias.equals("NodeInput"),
            "name":
            criterias.equals("dump_test"),
            "input_type":
            criterias.equals(str(int)),
            "parent":
            criterias.is_type(str)
            & criterias.regex(uuid_regex)
            & criterias.equals(str(beautiful_input.node.id)),
            "value":
            criterias.equals(666),
            "hidden":
            criterias.equals(False),
            "connected_output":
            criterias.is_type(str)
            & criterias.regex(uuid_regex)
            & criterias.equals(str(ugly_output.id)),
        },
        beautiful_input.dump(),
    )
    print(beautiful_input.dump())
    print(dump_match.to_string())
    assert dump_match.is_valid()
Beispiel #3
0
def test_that_check_nodeoutput_dump():
    cool_output = NodeOutput(dummy_node, "dump_test", int)
    dump_match = match.match(
        {
            "id":
            criterias.is_type(str)
            & criterias.regex(uuid_regex)
            & criterias.equals(str(cool_output.id)),
            "type":
            criterias.equals("NodeOutput"),
            "name":
            criterias.equals("dump_test"),
            "output_type":
            criterias.equals(str(int)),
            "parent":
            criterias.is_type(str)
            & criterias.regex(uuid_regex)
            & criterias.equals(str(cool_output.node.id)),
            "value":
            criterias.equals(None),
        },
        cool_output.dump(),
    )
    print(cool_output.dump())
    print(dump_match.to_string())
    assert dump_match.is_valid()
Beispiel #4
0
def test_json():
    js = ""
    with open("tests/payload.json") as payload:
        js = json.loads(payload.read())
    name_criteria = criteria.is_type(str) & ~criteria.empty()
    status = store.ValueStorage()
    modules_status = store.ListStorage()
    HEALTH_MODELS = {
        "version":
        criteria.regex(r"(\d+).(\d+)"),
        "status":
        criteria.is_in("UP", "DEGRADED", "DOWN") >> status,
        "comment":
        criteria.is_type(str),
        "az": [{
            "name": name_criteria,
            "status": criteria.is_in("UP", "DEGRADED", "DOWN"),
            "comment": criteria.is_type(str),
        }],
        "modules": [
            {
                "name":
                name_criteria,
                "status":
                criteria.is_in("UP", "DEGRADED", "DOWN") >> modules_status,
                "comment":
                criteria.is_type(str),
                "az": [
                    {
                        "name": name_criteria,
                        "status": criteria.is_in("UP", "DEGRADED", "DOWN"),
                        "comment": criteria.is_type(str),
                    },
                ],
            },
        ],
    }
    res = match.match(HEALTH_MODELS, js)
    print("PAYLOAD =")
    print(res.to_string())
    print("Status :", status.value)
    for stat in modules_status.values:
        print(stat.value)
        assert stat.value == "UP"
    assert res.is_valid()
Beispiel #5
0
def _type_check(base: Any, value: Any) -> bool:
    if isinstance(base, PineappleType):
        if not check_pineapple_type(base, value):
            return False
    elif isinstance(base, (list, tuple, dict)):
        if isinstance(base, (list, tuple)):
            base = type_list_to_klotan_list(base)
        elif isinstance(base, dict):
            base = type_dict_to_klotan_dict(base)
        match_result = match.match(base, value)
        if not match_result.is_valid():
            print(f"Invalid type matching {match_result.to_string()}",
                  file=sys.stderr)
            return False
    elif (not type(value)
          == base):  # Don't change this to isinstance (typing incompatibility)
        result = False
        try:
            result = isinstance(value, base.__args__)
        except AttributeError:
            return False
        return result
    return True
Beispiel #6
0
def test_that_match_simple_dict():
    pattern = {"a": criteria.equals(3) | criteria.equals(4)}
    tree = {"a": 3}
    tree2 = {"a": 4}
    assert match.match(pattern, tree).is_valid()
    assert match.match(pattern, tree2).is_valid()
Beispiel #7
0
from klotan import match, criteria

print(match.match(1, 1).to_string())
assert match.match(1, 1)

print(match.match(int, 1).to_string())
assert match.match(int, 1)

print(match.match({1: 1}, {1: 1}).to_string())
assert match.match({1: 1}, {1: 1})
Beispiel #8
0
from klotan import match, criteria

template = {
    1: "bonjour",  # Simple equality matching
    2: criteria.equals("bonjour"),  # This is strictly the same
    2: {  # It will compare every key of the template
        "x": 3,  # You can use any primitive types
        "y":
        criteria.between(5,
                         12)  # This criteria expects a number between 5 and 12
    },
}

value = {1: "bonjour", 2: {"x": 3, "y": 6}}

print(match.match(template, value).to_string())
assert match.match(template, value)

person_template = {
    # The gender can be either M, F or N/A
    "gender":
    criteria.is_in("M", "F", "N/A"),
    # Expects a name starting with a capital letter, followed by 1 to 15 letters
    "name":
    criteria.regex("[A-Z][a-z]{1,15}"),
    # Jeanne Calment still holds the world record
    "age":
    criteria.between(0, 122),
    # Matches all emails
    "email":
    criteria.is_email(),