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

     1  // Copyright 2018 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 tests_test
    12  
    13  import (
    14  	"context"
    15  	gosql "database/sql"
    16  	"fmt"
    17  	"math/rand"
    18  	"testing"
    19  
    20  	"github.com/cockroachdb/cockroach/pkg/sql/parser"
    21  	"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
    22  	"github.com/cockroachdb/cockroach/pkg/sql/tests"
    23  	"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
    24  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    25  	"github.com/cockroachdb/cockroach/pkg/util/timeutil"
    26  )
    27  
    28  func setDb(t *testing.T, db *gosql.DB, name string) {
    29  	if _, err := db.Exec("USE " + name); err != nil {
    30  		t.Fatal(err)
    31  	}
    32  }
    33  
    34  func TestCreateRandomSchema(t *testing.T) {
    35  	defer leaktest.AfterTest(t)()
    36  
    37  	ctx := context.Background()
    38  
    39  	params, _ := tests.CreateTestServerParams()
    40  	s, db, _ := serverutils.StartServer(t, params)
    41  	defer s.Stopper().Stop(ctx)
    42  
    43  	if _, err := db.Exec("CREATE DATABASE test; CREATE DATABASE test2"); err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	rng := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
    48  	for i := 0; i < 100; i++ {
    49  		tab := sqlbase.RandCreateTable(rng, "table", i)
    50  		setDb(t, db, "test")
    51  		_, err := db.Exec(tab.String())
    52  		if err != nil {
    53  			t.Fatal(tab, err)
    54  		}
    55  
    56  		var tabName, tabStmt, secondTabStmt string
    57  		if err := db.QueryRow(fmt.Sprintf("SHOW CREATE TABLE %s",
    58  			tab.Table.String())).Scan(&tabName, &tabStmt); err != nil {
    59  			t.Fatal(err)
    60  		}
    61  
    62  		if tabName != tab.Table.String() {
    63  			t.Fatalf("found table name %s, expected %s", tabName, tab.Table.String())
    64  		}
    65  
    66  		// Reparse the show create table statement that's stored in the database.
    67  		parsed, err := parser.ParseOne(tabStmt)
    68  		if err != nil {
    69  			t.Fatalf("error parsing show create table: %s", err)
    70  		}
    71  
    72  		setDb(t, db, "test2")
    73  		// Now run the SHOW CREATE TABLE statement we found on a new db and verify
    74  		// that both tables are the same.
    75  
    76  		_, err = db.Exec(parsed.AST.String())
    77  		if err != nil {
    78  			t.Fatal(parsed.AST, err)
    79  		}
    80  
    81  		if err := db.QueryRow(fmt.Sprintf("SHOW CREATE TABLE %s",
    82  			tabName)).Scan(&tabName, &secondTabStmt); err != nil {
    83  			t.Fatal(err)
    84  		}
    85  
    86  		if tabName != tab.Table.String() {
    87  			t.Fatalf("found table name %s, expected %s", tabName, tab.Table.String())
    88  		}
    89  		// Reparse the show create table statement that's stored in the database.
    90  		secondParsed, err := parser.ParseOne(secondTabStmt)
    91  		if err != nil {
    92  			t.Fatalf("error parsing show create table: %s", err)
    93  		}
    94  		if parsed.AST.String() != secondParsed.AST.String() {
    95  			t.Fatalf("for input statement\n%s\nfound first output\n%q\nbut second output\n%q",
    96  				tab.String(), parsed.AST.String(), secondParsed.AST.String())
    97  		}
    98  		if tabStmt != secondTabStmt {
    99  			t.Fatalf("for input statement\n%s\nfound first output\n%q\nbut second output\n%q",
   100  				tab.String(), tabStmt, secondTabStmt)
   101  		}
   102  	}
   103  }