Skip to content

Commit

Permalink
schema: schema config validation available
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Johansson committed Jul 18, 2019
1 parent f638c56 commit 732ab96
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 3 deletions.
52 changes: 49 additions & 3 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ type Value []interface{}
type SchemaConfig struct {
CompactionStrategy *CompactionStrategy
MaxPartitionKeys int
MinPartitionKeys int
MaxClusteringKeys int
MinClusteringKeys int
MaxColumns int
MinColumns int
MaxUDTParts int
MaxTupleParts int
MaxBlobLength int
Expand All @@ -39,6 +42,49 @@ type SchemaConfig struct {
CQLFeature CQLFeature
}

var (
SchemaConfigInvalidPK = errors.New("max number of partition keys must be bigger than min number of partition keys")
SchemaConfigInvalidCK = errors.New("max number of clustering keys must be bigger than min number of clustering keys")
SchemaConfigInvalidCols = errors.New("max number of columns must be bigger than min number of columns")
)

func (sc *SchemaConfig) Valid() error {
if sc.MaxPartitionKeys <= sc.MinPartitionKeys {
return SchemaConfigInvalidPK
}
if sc.MaxClusteringKeys <= sc.MinClusteringKeys {
return SchemaConfigInvalidCK
}
if sc.MaxColumns <= sc.MinClusteringKeys {
return SchemaConfigInvalidCols
}
return nil
}

func (sc *SchemaConfig) GetMaxPartitionKeys() int {
return sc.MaxPartitionKeys
}

func (sc *SchemaConfig) GetMinPartitionKeys() int {
return sc.MinPartitionKeys
}

func (sc *SchemaConfig) GetMaxClusteringKeys() int {
return sc.MaxClusteringKeys
}

func (sc *SchemaConfig) GetMinClusteringKeys() int {
return sc.MinClusteringKeys
}

func (sc *SchemaConfig) GetMaxColumns() int {
return sc.MaxColumns
}

func (sc *SchemaConfig) GetMinColumns() int {
return sc.MinColumns
}

type Keyspace struct {
Name string `json:"name"`
}
Expand Down Expand Up @@ -322,17 +368,17 @@ func GenSchema(sc SchemaConfig) *Schema {
}
builder.Keyspace(keyspace)
var partitionKeys []ColumnDef
numPartitionKeys := rand.Intn(sc.MaxPartitionKeys-1) + 1
numPartitionKeys := rand.Intn(sc.GetMaxPartitionKeys()-sc.GetMinPartitionKeys()) + sc.GetMinPartitionKeys()
for i := 0; i < numPartitionKeys; i++ {
partitionKeys = append(partitionKeys, ColumnDef{Name: genColumnName("pk", i), Type: genPartitionKeyColumnType()})
}
var clusteringKeys []ColumnDef
numClusteringKeys := rand.Intn(sc.MaxClusteringKeys)
numClusteringKeys := rand.Intn(sc.GetMaxClusteringKeys()-sc.GetMinClusteringKeys()) + sc.GetMinClusteringKeys()
for i := 0; i < numClusteringKeys; i++ {
clusteringKeys = append(clusteringKeys, ColumnDef{Name: genColumnName("ck", i), Type: genPrimaryKeyColumnType()})
}
var columns []ColumnDef
numColumns := rand.Intn(sc.MaxColumns)
numColumns := rand.Intn(sc.GetMaxColumns()-sc.GetMinColumns()) + sc.GetMinColumns()
for i := 0; i < numColumns; i++ {
columns = append(columns, ColumnDef{Name: genColumnName("col", i), Type: genColumnType(numColumns, &sc)})
}
Expand Down
76 changes: 76 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,82 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestSchemaConfigValidate(t *testing.T) {

tests := map[string]struct {
config *SchemaConfig
want error
}{
"empty": {
config: &SchemaConfig{},
want: SchemaConfigInvalidPK,
},
"valid": {
config: &SchemaConfig{
MaxPartitionKeys: 3,
MinPartitionKeys: 2,
MaxClusteringKeys: 3,
MinClusteringKeys: 2,
MaxColumns: 3,
MinColumns: 2,
},
want: nil,
},
"min_pk_gt_than_max_pk": {
config: &SchemaConfig{
MaxPartitionKeys: 2,
MinPartitionKeys: 3,
},
want: SchemaConfigInvalidPK,
},
"ck_missing": {
config: &SchemaConfig{
MaxPartitionKeys: 3,
MinPartitionKeys: 2,
},
want: SchemaConfigInvalidCK,
},
"min_ck_gt_than_max_ck": {
config: &SchemaConfig{
MaxPartitionKeys: 3,
MinPartitionKeys: 2,
MaxClusteringKeys: 2,
MinClusteringKeys: 3,
},
want: SchemaConfigInvalidCK,
},
"columns_missing": {
config: &SchemaConfig{
MaxPartitionKeys: 3,
MinPartitionKeys: 2,
MaxClusteringKeys: 3,
MinClusteringKeys: 2,
},
want: SchemaConfigInvalidCols,
},
"min_cols_gt_than_max_cols": {
config: &SchemaConfig{
MaxPartitionKeys: 3,
MinPartitionKeys: 2,
MaxClusteringKeys: 3,
MinClusteringKeys: 2,
MaxColumns: 2,
MinColumns: 3,
},
want: SchemaConfigInvalidCols,
},
}
cmp.AllowUnexported()
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := test.config.Valid()
if got != test.want {
t.Fatalf("expected '%s', got '%s'", test.want, got)
}
})
}
}

func TestGetCreateSchema(t *testing.T) {
ks := Keyspace{Name: "ks1"}
tests := map[string]struct {
Expand Down

0 comments on commit 732ab96

Please sign in to comment.