示例#1
0
def test_query() -> None:
    query = (Query.by(
        "ec2",
        P("cpu") > 4, (P("mem") < 23) | (P("mem") < 59)).merge_with(
            "cloud", Navigation(1, Navigation.Max,
                                direction=Direction.inbound),
            Query.mk_term("cloud")).traverse_out().filter(
                P("some.int.value") < 1,
                P("some.other") == 23).traverse_out().filter(
                    P("active") == 12,
                    P.function("in_subnet").on(
                        "ip", "1.2.3.4/96")).filter_with(
                            WithClause(WithClauseFilter(
                                "==", 0), Navigation())).group_by([
                                    AggregateVariable(
                                        AggregateVariableName("foo"))
                                ], [AggregateFunction("sum", "cpu")]).add_sort(
                                    Sort("test", "asc")).with_limit(10))
    assert str(query) == (
        'aggregate(foo: sum(cpu)):((is("ec2") and cpu > 4) and (mem < 23 or mem < 59)) '
        '{cloud: all <-default[1:]- is("cloud")} -default-> '
        "(some.int.value < 1 and some.other == 23) -default-> "
        '(active == 12 and in_subnet(ip, "1.2.3.4/96")) '
        "with(empty, -default->) sort test asc limit 10")
    assert_round_trip(query_parser, query)
示例#2
0
def with_clause(ud: UD) -> WithClause:
    d = Drawer(ud)
    op = d.draw(query_operations)
    num = d.draw(integers(min_value=0))
    nav = d.draw(navigation())
    trm = d.optional(term)
    wc = d.optional(with_clause())
    return WithClause(WithClauseFilter(op, num), nav, trm, wc)
示例#3
0
def test_with_clause() -> None:
    predicate_term.parse("foo == bla")
    wc: WithClause = with_clause_parser.parse(
        "with(empty, -delete-> foo == bla and test > 23 with(any, -delete->))")
    assert wc.with_filter == WithClauseFilter("==", 0)
    assert wc.navigation == Navigation(maybe_edge_types=["delete"])
    assert str(wc.term) == '(foo == "bla" and test > 23)'
    assert str(wc.with_clause) == "with(any, -delete->)"
    term = Query.mk_term("foo", P("test") == 23)
    clause_filter = WithClauseFilter(">", 23)
    nav = Navigation()

    def edge(wc: WithClause) -> WithClause:
        wcr = replace(wc, with_clause=edge(
            wc.with_clause)) if wc.with_clause else wc
        return replace(wcr,
                       navigation=replace(wcr.navigation,
                                          maybe_edge_types=[EdgeType.default]))

    assert_round_trip(
        with_clause_parser,
        WithClause(clause_filter, nav, term, WithClause(clause_filter, nav)),
        edge)
    assert_round_trip(with_clause_parser, WithClause(clause_filter, nav), edge)
示例#4
0
def test_part() -> None:
    assert_round_trip(part_parser, Part(P.of_kind("test")))
    assert_round_trip(
        part_parser,
        Part(P.of_kind("test"),
             navigation=Navigation(1, 10, [EdgeType.delete])))
    assert_round_trip(
        part_parser,
        Part(P.of_kind("test"),
             "red",
             navigation=Navigation(1, 10, [EdgeType.delete])))
    with_clause = WithClause(WithClauseFilter("==", 0),
                             Navigation(maybe_edge_types=[EdgeType.delete]))
    assert_round_trip(
        part_parser,
        Part(P.of_kind("test"),
             "green",
             with_clause,
             navigation=Navigation(1, 10, [EdgeType.delete])))
示例#5
0
def test_query() -> None:
    query = (Query.by(
        "ec2",
        P("cpu") > 4, (P("mem") < 23) | (P("mem") < 59),
        preamble={
            "merge_with_ancestors": "cloud"
        }).traverse_out().filter(
            P("some.int.value") < 1,
            P("some.other") == 23).traverse_out().filter(
                P("active") == 12,
                P.function("in_subnet").on("ip", "1.2.3.4/96")).filter_with(
                    WithClause(WithClauseFilter(
                        "==", 0), Navigation())).group_by(
                            [AggregateVariable(AggregateVariableName("foo"))],
                            [AggregateFunction("sum", "cpu")]).add_sort(
                                "test", "asc").with_limit(10))
    assert (
        str(query) == 'aggregate(foo: sum(cpu))(merge_with_ancestors="cloud"):'
        + '((is("ec2") and cpu > 4) and (mem < 23 or mem < 59)) -default-> ' +
        "(some.int.value < 1 and some.other == 23) -default-> " +
        '(active == 12 and in_subnet(ip, "1.2.3.4/96")) ' +
        "with(empty, -default->) sort test asc limit 10")
    assert_round_trip(query_parser, query)
示例#6
0
def with_count_parser() -> Parser:
    yield count_p
    op = yield operation_p
    num = yield integer_p
    return WithClauseFilter(op, num)
示例#7
0
out_p = lexeme(string("-") >> edge_definition_parser << string("->")).map(
    lambda nav: Navigation(nav[0], nav[1], nav[2], Direction.outbound)
)
in_p = lexeme(string("<-") >> edge_definition_parser << string("-")).map(
    lambda nav: Navigation(nav[0], nav[1], nav[2], Direction.inbound)
)
in_out_p = lexeme(string("<-") >> two_directional_edge_definition_parser << string("->")).map(
    lambda nav: Navigation(nav[0], nav[1], nav[2], Direction.any, nav[3])
)
navigation_parser = in_out_p | out_p | in_p

tag_parser = lexeme(string("#") >> literal_p).optional()
with_p = lexeme(string("with"))
count_p = lexeme(string("count"))

len_empty = lexeme(string("empty")).result(WithClauseFilter("==", 0))
len_any = lexeme(string("any")).result(WithClauseFilter(">", 0))


@make_parser
def with_count_parser() -> Parser:
    yield count_p
    op = yield operation_p
    num = yield integer_p
    return WithClauseFilter(op, num)


@make_parser
def with_clause_parser() -> Parser:
    yield with_p
    yield lparen_p