1#!/usr/bin/env python3 2# Copyright (C) 2023 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License a 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16from python.generators.diff_tests.testing import Path, DataPath, Metric 17from python.generators.diff_tests.testing import Csv, Json, TextProto 18from python.generators.diff_tests.testing import DiffTestBlueprint 19from python.generators.diff_tests.testing import TestSuite 20 21 22class PerfettoMacro(TestSuite): 23 24 def test_macro(self): 25 return DiffTestBlueprint( 26 trace=TextProto(''), 27 query=''' 28 CREATE PERFETTO MACRO foo(a Expr,b Expr) RETURNS TableOrSubquery AS 29 SELECT $a - $b; 30 SELECT (foo!(123, 100)) as res; 31 ''', 32 out=Csv(""" 33 "res" 34 23 35 """)) 36 37 def test_nested_macro(self): 38 return DiffTestBlueprint( 39 trace=TextProto(''), 40 query=''' 41 CREATE PERFETTO MACRO foo(a Expr) returns Expr AS $a; 42 CREATE PERFETTO MACRO bar(a Expr) returns Expr AS (SELECT $a); 43 CREATE PERFETTO MACRO baz(a Expr,b Expr) returns TableOrSubquery AS 44 SELECT bar!(foo!(123)) - $b as res; 45 baz!(123, 100); 46 ''', 47 out=Csv(""" 48 "res" 49 23 50 """)) 51 52 def test_replace_macro(self): 53 return DiffTestBlueprint( 54 trace=TextProto(''), 55 query=''' 56 CREATE PERFETTO MACRO foo() RETURNS Expr AS 1; 57 CREATE OR REPLACE PERFETTO MACRO foo() RETURNS Expr AS 2; 58 59 SELECT foo!() as res; 60 ''', 61 out=Csv(""" 62 "res" 63 2 64 """)) 65 66 def test_stringify(self): 67 return DiffTestBlueprint( 68 trace=TextProto(''), 69 query=''' 70 SELECT __intrinsic_stringify!(foo) 71 UNION ALL 72 SELECT __intrinsic_stringify!(foo bar baz) 73 UNION ALL 74 SELECT __intrinsic_stringify!(foo'') 75 UNION ALL 76 SELECT __intrinsic_stringify!(bar()) 77 ''', 78 out=Csv(""" 79 "'foo'" 80 "foo" 81 "foo bar baz" 82 "foo'" 83 "bar()" 84 """)) 85