github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/steampipeconfig/parse/query_invocation_test.go (about) 1 package parse 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/turbot/steampipe/pkg/utils" 8 9 "github.com/turbot/steampipe/pkg/steampipeconfig/modconfig" 10 ) 11 12 // NOTE: all query arg values must be JSON representations 13 type parseQueryInvocationTest struct { 14 input string 15 expected parseQueryInvocationResult 16 } 17 18 type parseQueryInvocationResult struct { 19 queryName string 20 args *modconfig.QueryArgs 21 } 22 23 var emptyParams = modconfig.NewQueryArgs() 24 var testCasesParseQueryInvocation = map[string]parseQueryInvocationTest{ 25 "no brackets": { 26 input: `query.q1`, 27 expected: parseQueryInvocationResult{"query.q1", emptyParams}, 28 }, 29 "no params": { 30 input: `query.q1()`, 31 expected: parseQueryInvocationResult{"query.q1", emptyParams}, 32 }, 33 "invalid params 1": { 34 input: `query.q1(foo)`, 35 expected: parseQueryInvocationResult{ 36 queryName: `query.q1`, 37 args: &modconfig.QueryArgs{}, 38 }, 39 }, 40 "invalid params 4": { 41 input: `query.q1("foo", "bar"])`, 42 expected: parseQueryInvocationResult{ 43 queryName: `query.q1`, 44 45 args: &modconfig.QueryArgs{}, 46 }, 47 }, 48 49 "single positional param": { 50 input: `query.q1("foo")`, 51 expected: parseQueryInvocationResult{ 52 queryName: `query.q1`, 53 args: &modconfig.QueryArgs{ArgList: []*string{utils.ToStringPointer("foo")}}, 54 }, 55 }, 56 "single positional param extra spaces": { 57 input: `query.q1("foo" ) `, 58 expected: parseQueryInvocationResult{ 59 queryName: `query.q1`, 60 args: &modconfig.QueryArgs{ArgList: []*string{utils.ToStringPointer("foo")}}, 61 }, 62 }, 63 "multiple positional params": { 64 input: `query.q1("foo", "bar", "foo-bar")`, 65 expected: parseQueryInvocationResult{ 66 queryName: `query.q1`, 67 args: &modconfig.QueryArgs{ArgList: []*string{utils.ToStringPointer("foo"), utils.ToStringPointer("bar"), utils.ToStringPointer("foo-bar")}}, 68 }, 69 }, 70 "multiple positional params extra spaces": { 71 input: `query.q1("foo", "bar", "foo-bar" )`, 72 expected: parseQueryInvocationResult{ 73 queryName: `query.q1`, 74 args: &modconfig.QueryArgs{ArgList: []*string{utils.ToStringPointer("foo"), utils.ToStringPointer("bar"), utils.ToStringPointer("foo-bar")}}, 75 }, 76 }, 77 "single named param": { 78 input: `query.q1(p1 => "foo")`, 79 expected: parseQueryInvocationResult{ 80 queryName: `query.q1`, 81 args: &modconfig.QueryArgs{ArgMap: map[string]string{"p1": "foo"}}, 82 }, 83 }, 84 "single named param extra spaces": { 85 input: `query.q1( p1 => "foo" ) `, 86 expected: parseQueryInvocationResult{ 87 queryName: `query.q1`, 88 args: &modconfig.QueryArgs{ArgMap: map[string]string{"p1": "foo"}}, 89 }, 90 }, 91 "multiple named params": { 92 input: `query.q1(p1 => "foo", p2 => "bar")`, 93 expected: parseQueryInvocationResult{ 94 queryName: `query.q1`, 95 args: &modconfig.QueryArgs{ArgMap: map[string]string{"p1": "foo", "p2": "bar"}}, 96 }, 97 }, 98 "multiple named params extra spaces": { 99 input: ` query.q1 ( p1 => "foo" , p2 => "bar" ) `, 100 expected: parseQueryInvocationResult{ 101 queryName: `query.q1`, 102 args: &modconfig.QueryArgs{ArgMap: map[string]string{"p1": "foo", "p2": "bar"}}, 103 }, 104 }, 105 "named param with dot in value": { 106 input: `query.q1(p1 => "foo.bar")`, 107 expected: parseQueryInvocationResult{ 108 queryName: `query.q1`, 109 args: &modconfig.QueryArgs{ArgMap: map[string]string{"p1": "foo.bar"}}, 110 }, 111 }, 112 } 113 114 func TestParseQueryInvocation(t *testing.T) { 115 for name, test := range testCasesParseQueryInvocation { 116 queryName, args, _ := ParseQueryInvocation(test.input) 117 118 if queryName != test.expected.queryName || !test.expected.args.Equals(args) { 119 fmt.Printf("") 120 t.Errorf("Test: '%s'' FAILED : expected:\nquery: %s params: %s\n\ngot:\nquery: %s params: %s", 121 name, 122 test.expected.queryName, 123 test.expected.args, 124 queryName, args) 125 } 126 } 127 }