github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/function_name_test.go (about)

     1  // Copyright 2016 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package tree_test
    12  
    13  import (
    14  	"testing"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/sql/parser"
    17  	_ "github.com/cockroachdb/cockroach/pkg/sql/sem/builtins"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
    20  	"github.com/cockroachdb/cockroach/pkg/testutils"
    21  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    22  )
    23  
    24  func TestResolveFunction(t *testing.T) {
    25  	defer leaktest.AfterTest(t)()
    26  	testCases := []struct {
    27  		in, out string
    28  		err     string
    29  	}{
    30  		{`count`, `count`, ``},
    31  		{`pg_catalog.pg_typeof`, `pg_typeof`, ``},
    32  
    33  		{`foo`, ``, `unknown function: foo`},
    34  		{`""`, ``, `invalid function name: ""`},
    35  	}
    36  
    37  	searchPath := sessiondata.MakeSearchPath([]string{"pg_catalog"})
    38  	for _, tc := range testCases {
    39  		stmt, err := parser.ParseOne("SELECT " + tc.in + "(1)")
    40  		if err != nil {
    41  			t.Fatalf("%s: %v", tc.in, err)
    42  		}
    43  		f, ok := stmt.AST.(*tree.Select).Select.(*tree.SelectClause).Exprs[0].Expr.(*tree.FuncExpr)
    44  		if !ok {
    45  			t.Fatalf("%s does not parse to a tree.FuncExpr", tc.in)
    46  		}
    47  		q := f.Func
    48  		_, err = q.Resolve(searchPath)
    49  		if tc.err != "" {
    50  			if !testutils.IsError(err, tc.err) {
    51  				t.Fatalf("%s: expected %s, but found %v", tc.in, tc.err, err)
    52  			}
    53  			continue
    54  		}
    55  		if err != nil {
    56  			t.Fatalf("%s: expected success, but found %v", tc.in, err)
    57  		}
    58  		if out := q.String(); tc.out != out {
    59  			t.Errorf("%s: expected %s, but found %s", tc.in, tc.out, out)
    60  		}
    61  	}
    62  }