def test_function() -> None: assert_round_trip(function_term, P.function("in_subnet").on("foo.bla.bar", 1, "2", True)) assert_round_trip(function_term, P.function("in_subnet").on("foo.bla.bar", "in_subnet")) assert_round_trip( function_term, P.function("in_subnet").on("foo.bla.bar", "in_subnet", "1000")) assert_round_trip(function_term, P.function("has_key").on("foo.bla.bar", "a", "b", "c")) assert_round_trip(function_term, P.function("has_key").on("foo.bla.bar", "a"))
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)
def simple_reference() -> None: # only kind Query.by("ec2") # equality Query.by(P.of_kind("ec2") & (P("simple") == "hallo")) Query.by(P.of_kind("ec2") & (P("simple") != "hallo")) # regex Query.by(P.of_kind("ec2") & P("simple").matches("^some.regex[a-d]+$")) Query.by(P.of_kind("ec2") & P("simple").not_matches("^some.regex[a-d]+$")) # comparator Query.by(P.of_kind("ec2") & (P("num") > 23)) Query.by(P.of_kind("ec2") & (P("num") >= 23)) Query.by(P.of_kind("ec2") & (P("num") == 23)) Query.by(P.of_kind("ec2") & (P("num") <= 23)) Query.by(P.of_kind("ec2") & (P("num") < 23)) # in set Query.by(P.of_kind("ec2") & P("num").is_in([1, 2, 5])) Query.by(P.of_kind("ec2") & P("num").is_not_in([1, 2, 5])) # array: all above operators are available Query.by(P.of_kind("ec2") & (P.array("some.array").for_all() > 12.23)) Query.by(P.of_kind("ec2") & (P.array("some.array").for_any().is_in([1, 2, 3]))) Query.by(P.of_kind("ec2") & (P.array("some.array").for_none() == 5)) # call a function Query.by(P.function("in_subnet").on("ip", "1.2.3.4/16")) # refine with multiple predicates (all predicates have to match) Query.by(P.of_kind("ec2") & P("a").ge(1), P("b") == 2, P("c").matches("aaa"))
def test_simple_query() -> None: a = (Query.by("ec2", P("cpu") > 4, (P("mem") < 23) | (P("mem") < 59)).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/32"))) assert (str(a) == '((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/32"))')
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)