def testPrefix(self): self.assertQueryParses( "-5 + 5 eq - (10)", ast.Equivalence( ast.Sum(ast.Product(ast.Literal(-1), ast.Literal(5)), ast.Literal(5)), ast.Product(ast.Literal(-1), ast.Literal(10))))
def testInfix(self): self.assertQueryMatches( "x == 'foo' and y.z contains 'bar'", ast.Intersection( ast.Equivalence(ast.Var("x"), ast.Literal("foo")), ast.Membership(ast.Literal("bar"), ast.Resolve(ast.Var("y"), ast.Literal("z")))))
def testFormatters(self): """Creating a query with raw AST should generate the source.""" q = query.Query( ast.Complement( ast.Equivalence(ast.Map(ast.Var("Process"), ast.Var("pid")), ast.Literal(10)))) self.assertEqual(q.source, "Process.pid != 10")
def testSelectWhereOrder(self): self.assertQueryMatches( "SELECT * FROM pslist() WHERE pid == 1 ORDER BY command DESC", ast.Apply( ast.Var("reverse"), ast.Sort( ast.Filter(ast.Apply(ast.Var("pslist")), ast.Equivalence(ast.Var("pid"), ast.Literal(1))), ast.Var("command"))))
def testParams(self): self.assertQueryMatches("? == 1 and ? == 2", ast.Intersection( ast.Equivalence(ast.Literal(1), ast.Literal(1)), ast.Equivalence(ast.Literal(2), ast.Literal(2))), params=[1, 2]) self.assertQueryMatches("{1} == 1 and {0} == 2", ast.Intersection( ast.Equivalence(ast.Literal(2), ast.Literal(1)), ast.Equivalence(ast.Literal(1), ast.Literal(2))), params=[1, 2]) # Test with a = as well as ==. self.assertQueryMatches("{bar} = 1 and {foo} = 2", ast.Intersection( ast.Equivalence(ast.Literal("foo"), ast.Literal(1)), ast.Equivalence(ast.Literal(1), ast.Literal(2))), params=dict(bar="foo", foo=1))
def testBuiltins(self): self.assertQueryMatches( "filter(pslist(), proc.pid == 1)", ast.Filter( ast.Apply(ast.Var("pslist")), ast.Equivalence( ast.Resolve(ast.Var("proc"), ast.Literal("pid")), ast.Literal(1)))) self.assertQueryMatches( "map(pslist(), [proc.pid, proc['command']])", ast.Map( ast.Apply(ast.Var("pslist")), ast.Tuple(ast.Resolve(ast.Var("proc"), ast.Literal("pid")), ast.Select(ast.Var("proc"), ast.Literal("command"))))) self.assertQueryMatches( "bind(x: 1, y: 2)", ast.Bind(ast.Pair(ast.Var("x"), ast.Literal(1)), ast.Pair(ast.Var("y"), ast.Literal(2)))) self.assertQueryRaises("bind (x: 1, y: 2)")
def testParens(self): self.assertQueryParses( "5 + (5 eq 10)", # It doesn't have to make sense. ast.Sum(ast.Literal(5), ast.Equivalence(ast.Literal(5), ast.Literal(10))))
def testInfix(self): self.assertQueryParses( "5 + 5 eq 10", ast.Equivalence(ast.Sum(ast.Literal(5), ast.Literal(5)), ast.Literal(10)))
def testSelectAnyWhere(self): self.assertQueryMatches( "SELECT ANY FROM pslist() WHERE pid == 1", ast.Any(ast.Apply(ast.Var("pslist")), ast.Equivalence(ast.Var("pid"), ast.Literal(1))))
def testCreation(self): q = query.Query("foo == bar") self.assertEqual(q.root, ast.Equivalence(ast.Var("foo"), ast.Var("bar")))
def testBasic(self): self.assertQueryMatches(("==", ("var", "foo"), "bar"), ast.Equivalence(ast.Var("foo"), ast.Literal("bar")))
def ComplementEquivalence(*args, **kwargs): """Change x != y to not(x == y).""" return ast.Complement( ast.Equivalence(*args, **kwargs), **kwargs)