Skip to content

Commit

Permalink
add(generators) add test for GenSchema with different seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Jul 7, 2023
1 parent ae1d81c commit e64e8e7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/generators/statement_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func genTable(sc typedef.SchemaConfig, tableName string, r *rand.Rand) *typedef.
table.Indexes = indexes

var mvs []typedef.MaterializedView
if sc.CQLFeature > typedef.CQL_FEATURE_BASIC && len(clusteringKeys) > 0 {
if sc.CQLFeature > typedef.CQL_FEATURE_BASIC && len(clusteringKeys) > 0 && columns.ValidColumnsForPrimaryKey().Len() != 0 {
mvs = CreateMaterializedViews(columns, table.Name, partitionKeys, clusteringKeys, r)
}

Expand Down
77 changes: 77 additions & 0 deletions pkg/generators/statement_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
package generators_test

import (
"encoding/json"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"golang.org/x/exp/rand"

"github.com/scylladb/gemini/pkg/generators"
"github.com/scylladb/gemini/pkg/replication"
"github.com/scylladb/gemini/pkg/tableopts"
"github.com/scylladb/gemini/pkg/typedef"
)
Expand Down Expand Up @@ -180,6 +185,49 @@ func TestGetCreateSchema(t *testing.T) {
}
}

func TestGenSchema(t *testing.T) {
seeds := [10]uint64{
uint64(10 + rand.Intn(10)),
uint64(100 + rand.Intn(100)),
uint64(1000 + rand.Intn(1000)),
uint64(10000 + rand.Intn(10000)),
uint64(100000 + rand.Intn(100000)),
uint64(1000000 + rand.Intn(1000000)),
uint64(10000000 + rand.Intn(10000000)),
uint64(100000000 + rand.Intn(100000000)),
uint64(1000000000 + rand.Intn(1000000000)),
uint64(time.Now().Nanosecond()),
}

for idx := range seeds {
testSchema := generators.GenSchema(testSchemaConfig, seeds[idx])
testSchema.Config = typedef.SchemaConfig{}
transformAndDiff(t, testSchema)
}
}

func transformAndDiff(t *testing.T, testSchema *typedef.Schema) {
t.Helper()
opts := cmp.Options{
cmp.AllowUnexported(typedef.Table{}, typedef.MaterializedView{}),
cmpopts.IgnoreUnexported(typedef.Table{}, typedef.MaterializedView{}),
cmpopts.EquateEmpty(),
}

testSchemaMarshaled, err := json.MarshalIndent(testSchema, " ", " ")
if err != nil {
t.Fatalf("unable to marshal schema example json, error=%s\n", err)
}
testSchemaTransformed := typedef.Schema{}
if err = json.Unmarshal(testSchemaMarshaled, &testSchemaTransformed); err != nil {
t.Fatalf("unable to unmarshal json, error=%s\n", err)
}

if diff := cmp.Diff(*testSchema, testSchemaTransformed, opts); diff != "" {
t.Fatalf("schema not the same after marshal/unmarshal, diff=%s", diff)
}
}

func createColumns(cnt int, prefix string) typedef.Columns {
var cols typedef.Columns
for i := 0; i < cnt; i++ {
Expand All @@ -190,3 +238,32 @@ func createColumns(cnt int, prefix string) typedef.Columns {
}
return cols
}

var testSchemaConfig = typedef.SchemaConfig{
ReplicationStrategy: replication.NewSimpleStrategy(),
OracleReplicationStrategy: replication.NewSimpleStrategy(),
TableOptions: tableopts.CreateTableOptions([]string{
"compression = {'sstable_compression':'LZ4Compressor'}",
"read_repair_chance = 1.0", "comment = 'Important records'", "cdc = {'enabled':'true','preimage':'true'}",
"compaction = {'class':'LeveledCompactionStrategy','enabled':true,'sstable_size_in_mb':160,'tombstone_compaction_interval':86400,'tombstone_threshold':0.2}",
},
nil),
MaxTables: 10,
MaxPartitionKeys: 10,
MinPartitionKeys: 1,
MaxClusteringKeys: 10,
MinClusteringKeys: 1,
MaxColumns: 25,
MinColumns: 1,
MaxUDTParts: 20,
MaxTupleParts: 20,
MaxBlobLength: 1e4,
MaxStringLength: 1000,
MinBlobLength: 0,
MinStringLength: 0,
UseCounters: false,
UseLWT: false,
CQLFeature: typedef.CQL_FEATURE_NORMAL,
AsyncObjectStabilizationAttempts: 10,
AsyncObjectStabilizationDelay: 100000,
}

0 comments on commit e64e8e7

Please sign in to comment.