Skip to content

Commit

Permalink
sql/builtins: remove dependency to sql/randgen
Browse files Browse the repository at this point in the history
Previously, a test in the `builtins` package depended on `randgen`.
This was inadequate because it will introduce a dependency cycle later
when we introduce the dependency from `tabledesc` to `builtins`, as
required to implement some sequence reference releated issues (see
pr cockroachdb#82172 for details).

Release note: None
  • Loading branch information
Xiang-Gu committed Jul 1, 2022
1 parent d324df1 commit 542bc3a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
2 changes: 2 additions & 0 deletions pkg/sql/sem/builtins/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ go_test(
"generator_builtins_test.go",
"geo_builtins_test.go",
"help_test.go",
"helpers_test.go",
"main_test.go",
"math_builtins_test.go",
"show_create_all_tables_builtin_test.go",
Expand Down Expand Up @@ -182,5 +183,6 @@ disallowed_imports_test(
"//pkg/sql/catalog/schemadesc",
"//pkg/sql/catalog/dbdesc",
"//pkg/sql/catalog/typedesc",
"//pkg/sql/randgen",
],
)
20 changes: 11 additions & 9 deletions pkg/sql/sem/builtins/geo_builtins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package builtins
package builtins_test

import (
"math/rand"
Expand All @@ -18,6 +18,7 @@ import (
"unicode"

"github.com/cockroachdb/cockroach/pkg/sql/randgen"
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
Expand All @@ -27,10 +28,9 @@ import (

func TestGeoBuiltinsInfo(t *testing.T) {
defer leaktest.AfterTest(t)()

for k, builtin := range geoBuiltins {
t.Run(k, func(t *testing.T) {
for i, overload := range builtin.overloads {
testForBuiltin := func(builtinName string, builtinOverloads []tree.Overload) {
t.Run(builtinName, func(t *testing.T) {
for i, overload := range builtinOverloads {
t.Run(strconv.Itoa(i+1), func(t *testing.T) {
infoFirstLine := strings.Trim(strings.Split(overload.Info, "\n\n")[0], "\t\n ")
require.True(t, infoFirstLine[len(infoFirstLine)-1] == '.', "first line of info must end with a `.` character")
Expand All @@ -39,6 +39,7 @@ func TestGeoBuiltinsInfo(t *testing.T) {
}
})
}
builtins.IterateGeoBuiltinOverloads(testForBuiltin)
}

// TestGeoBuiltinsPointEmptyArgs tests POINT EMPTY arguments do not cause panics.
Expand All @@ -51,9 +52,9 @@ func TestGeoBuiltinsPointEmptyArgs(t *testing.T) {
require.NoError(t, err)

rng := rand.New(rand.NewSource(0))
for k, builtin := range geoBuiltins {
t.Run(k, func(t *testing.T) {
for i, overload := range builtin.overloads {
testForBuiltin := func(builtinName string, builtinOverloads []tree.Overload) {
t.Run(builtinName, func(t *testing.T) {
for i, overload := range builtinOverloads {
t.Run("overload_"+strconv.Itoa(i+1), func(t *testing.T) {
for overloadIdx := 0; overloadIdx < overload.Types.Length(); overloadIdx++ {
switch overload.Types.GetAt(overloadIdx).Family() {
Expand All @@ -75,7 +76,7 @@ func TestGeoBuiltinsPointEmptyArgs(t *testing.T) {
}
}
var call strings.Builder
call.WriteString(k)
call.WriteString(builtinName)
call.WriteByte('(')
for i, arg := range datums {
if i > 0 {
Expand Down Expand Up @@ -103,4 +104,5 @@ func TestGeoBuiltinsPointEmptyArgs(t *testing.T) {
}
})
}
builtins.IterateGeoBuiltinOverloads(testForBuiltin)
}
19 changes: 19 additions & 0 deletions pkg/sql/sem/builtins/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package builtins

import "github.com/cockroachdb/cockroach/pkg/sql/sem/tree"

func IterateGeoBuiltinOverloads(f func(builtinName string, ol []tree.Overload)) {
for k, builtin := range geoBuiltins {
f(k, builtin.overloads)
}
}

0 comments on commit 542bc3a

Please sign in to comment.