forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PopulateRandTable populates the caller's table with random data. This helper function aims to make it easier for engineers to develop randomized tests that leverage randgen / sqlsmith. Informs cockroachdb#72345 Release note: None
- Loading branch information
Showing
4 changed files
with
179 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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 randgen | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/testutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/skip" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/cockroachdb/cockroach/pkg/util/randutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func PopulateRandTableTest(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
skip.UnderRace(t, "takes >1 min under race") | ||
rng, _ := randutil.NewPseudoRand() | ||
dir, dirCleanupFn := testutils.TempDir(t) | ||
defer dirCleanupFn() | ||
params := base.TestClusterArgs{ | ||
ServerArgs: base.TestServerArgs{ | ||
UseDatabase: "rand", | ||
ExternalIODir: dir, | ||
}, | ||
} | ||
ctx := context.Background() | ||
tc := testcluster.StartTestCluster(t, 1, params) | ||
defer tc.Stopper().Stop(ctx) | ||
sqlDB := sqlutils.MakeSQLRunner(tc.Conns[0]) | ||
sqlDB.Exec(t, "CREATE DATABASE rand") | ||
|
||
stmts := RandCreateTables(rng, "table", 5, | ||
PartialIndexMutator, | ||
ForeignKeyMutator, | ||
) | ||
|
||
var sb strings.Builder | ||
for _, stmt := range stmts { | ||
sb.WriteString(tree.SerializeForDisplay(stmt)) | ||
sb.WriteString(";\n") | ||
} | ||
sqlDB.Exec(t, sb.String()) | ||
|
||
tables := sqlDB.Query(t, `SELECT name FROM crdb_internal. | ||
tables WHERE database_name = 'rand' AND schema_name = 'public'`) | ||
|
||
for tables.Next() { | ||
numRows := rng.Intn(100) | ||
var tableName string | ||
if err := tables.Scan(&tableName); err != nil { | ||
t.Fatal(err) | ||
} | ||
err := PopulateRandTable(rng, tc.Conns[0], tableName, numRows) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
res := sqlDB.QueryStr(t, fmt.Sprintf("SELECT count(*) FROM %s", tableName)) | ||
require.Equal(t, fmt.Sprint(numRows), res[0][0]) | ||
} | ||
} |