Skip to content

Commit

Permalink
sql: fix AST formatting in TestCreateRandomSchema
Browse files Browse the repository at this point in the history
Fixing this test to use FmtParsable. Without it, an expression that
contains an `Infinity` decimal isn't quoted properly.

Release justification: non-production code change

Release note: None
  • Loading branch information
RaduBerinde committed Mar 4, 2021
1 parent 258ba54 commit 2809314
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions pkg/sql/tests/random_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/tests"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
Expand Down Expand Up @@ -46,23 +47,27 @@ func TestCreateRandomSchema(t *testing.T) {
t.Fatal(err)
}

toStr := func(c tree.Statement) string {
return tree.AsStringWithFlags(c, tree.FmtParsable)
}

rng := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
for i := 0; i < 100; i++ {
tab := rowenc.RandCreateTable(rng, "table", i)
createTable := rowenc.RandCreateTable(rng, "table", i)
setDb(t, db, "test")
_, err := db.Exec(tab.String())
_, err := db.Exec(toStr(createTable))
if err != nil {
t.Fatal(tab, err)
t.Fatal(createTable, err)
}

var tabName, tabStmt, secondTabStmt string
if err := db.QueryRow(fmt.Sprintf("SHOW CREATE TABLE %s",
tab.Table.String())).Scan(&tabName, &tabStmt); err != nil {
createTable.Table.String())).Scan(&tabName, &tabStmt); err != nil {
t.Fatal(err)
}

if tabName != tab.Table.String() {
t.Fatalf("found table name %s, expected %s", tabName, tab.Table.String())
if tabName != createTable.Table.String() {
t.Fatalf("found table name %s, expected %s", tabName, createTable.Table.String())
}

// Reparse the show create table statement that's stored in the database.
Expand All @@ -75,7 +80,7 @@ func TestCreateRandomSchema(t *testing.T) {
// Now run the SHOW CREATE TABLE statement we found on a new db and verify
// that both tables are the same.

_, err = db.Exec(parsed.AST.String())
_, err = db.Exec(tree.AsStringWithFlags(parsed.AST, tree.FmtParsable))
if err != nil {
t.Fatal(parsed.AST, err)
}
Expand All @@ -85,21 +90,21 @@ func TestCreateRandomSchema(t *testing.T) {
t.Fatal(err)
}

if tabName != tab.Table.String() {
t.Fatalf("found table name %s, expected %s", tabName, tab.Table.String())
if tabName != createTable.Table.String() {
t.Fatalf("found table name %s, expected %s", tabName, createTable.Table.String())
}
// Reparse the show create table statement that's stored in the database.
secondParsed, err := parser.ParseOne(secondTabStmt)
if err != nil {
t.Fatalf("error parsing show create table: %s", err)
}
if parsed.AST.String() != secondParsed.AST.String() {
if toStr(parsed.AST) != toStr(secondParsed.AST) {
t.Fatalf("for input statement\n%s\nfound first output\n%q\nbut second output\n%q",
tab.String(), parsed.AST.String(), secondParsed.AST.String())
toStr(createTable), toStr(parsed.AST), toStr(secondParsed.AST))
}
if tabStmt != secondTabStmt {
t.Fatalf("for input statement\n%s\nfound first output\n%q\nbut second output\n%q",
tab.String(), tabStmt, secondTabStmt)
toStr(createTable), tabStmt, secondTabStmt)
}
}
}

0 comments on commit 2809314

Please sign in to comment.