Skip to content

Commit

Permalink
move metadata tests which require custom cassandra config to integrat…
Browse files Browse the repository at this point in the history
…ion.sh
  • Loading branch information
Zariel committed Aug 16, 2020
1 parent 12459bd commit dbd82f3
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 203 deletions.
198 changes: 0 additions & 198 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2232,204 +2232,6 @@ func TestMaterializedViewMetadata(t *testing.T) {
}
}

func TestAggregateMetadata(t *testing.T) {
session := createSession(t)
defer session.Close()
createAggregate(t, session)

aggregates, err := getAggregatesMetadata(session, "gocql_test")
if err != nil {
t.Fatalf("failed to query aggregate metadata with err: %v", err)
}
if aggregates == nil {
t.Fatal("failed to query aggregate metadata, nil returned")
}
if len(aggregates) != 1 {
t.Fatal("expected only a single aggregate")
}
aggregate := aggregates[0]

expectedAggregrate := AggregateMetadata{
Keyspace: "gocql_test",
Name: "average",
ArgumentTypes: []TypeInfo{NativeType{typ: TypeInt}},
InitCond: "(0, 0)",
ReturnType: NativeType{typ: TypeDouble},
StateType: TupleTypeInfo{
NativeType: NativeType{typ: TypeTuple},

Elems: []TypeInfo{
NativeType{typ: TypeInt},
NativeType{typ: TypeBigInt},
},
},
stateFunc: "avgstate",
finalFunc: "avgfinal",
}

// In this case cassandra is returning a blob
if flagCassVersion.Before(3, 0, 0) {
expectedAggregrate.InitCond = string([]byte{0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0})
}

if !reflect.DeepEqual(aggregate, expectedAggregrate) {
t.Fatalf("aggregate is %+v, but expected %+v", aggregate, expectedAggregrate)
}
}

func TestFunctionMetadata(t *testing.T) {
session := createSession(t)
defer session.Close()
createFunctions(t, session)

functions, err := getFunctionsMetadata(session, "gocql_test")
if err != nil {
t.Fatalf("failed to query function metadata with err: %v", err)
}
if functions == nil {
t.Fatal("failed to query function metadata, nil returned")
}
if len(functions) != 2 {
t.Fatal("expected two functions")
}
avgState := functions[1]
avgFinal := functions[0]

avgStateBody := "if (val !=null) {state.setInt(0, state.getInt(0)+1); state.setLong(1, state.getLong(1)+val.intValue());}return state;"
expectedAvgState := FunctionMetadata{
Keyspace: "gocql_test",
Name: "avgstate",
ArgumentTypes: []TypeInfo{
TupleTypeInfo{
NativeType: NativeType{typ: TypeTuple},

Elems: []TypeInfo{
NativeType{typ: TypeInt},
NativeType{typ: TypeBigInt},
},
},
NativeType{typ: TypeInt},
},
ArgumentNames: []string{"state", "val"},
ReturnType: TupleTypeInfo{
NativeType: NativeType{typ: TypeTuple},

Elems: []TypeInfo{
NativeType{typ: TypeInt},
NativeType{typ: TypeBigInt},
},
},
CalledOnNullInput: true,
Language: "java",
Body: avgStateBody,
}
if !reflect.DeepEqual(avgState, expectedAvgState) {
t.Fatalf("function is %+v, but expected %+v", avgState, expectedAvgState)
}

finalStateBody := "double r = 0; if (state.getInt(0) == 0) return null; r = state.getLong(1); r/= state.getInt(0); return Double.valueOf(r);"
expectedAvgFinal := FunctionMetadata{
Keyspace: "gocql_test",
Name: "avgfinal",
ArgumentTypes: []TypeInfo{
TupleTypeInfo{
NativeType: NativeType{typ: TypeTuple},

Elems: []TypeInfo{
NativeType{typ: TypeInt},
NativeType{typ: TypeBigInt},
},
},
},
ArgumentNames: []string{"state"},
ReturnType: NativeType{typ: TypeDouble},
CalledOnNullInput: true,
Language: "java",
Body: finalStateBody,
}
if !reflect.DeepEqual(avgFinal, expectedAvgFinal) {
t.Fatalf("function is %+v, but expected %+v", avgFinal, expectedAvgFinal)
}
}

// Integration test of querying and composition the keyspace metadata
func TestKeyspaceMetadata(t *testing.T) {
session := createSession(t)
defer session.Close()

if err := createTable(session, "CREATE TABLE gocql_test.test_metadata (first_id int, second_id int, third_id int, PRIMARY KEY (first_id, second_id))"); err != nil {
t.Fatalf("failed to create table with error '%v'", err)
}
createAggregate(t, session)
createViews(t, session)
createMaterializedViews(t, session)

if err := session.Query("CREATE INDEX index_metadata ON test_metadata ( third_id )").Exec(); err != nil {
t.Fatalf("failed to create index with err: %v", err)
}

keyspaceMetadata, err := session.KeyspaceMetadata("gocql_test")
if err != nil {
t.Fatalf("failed to query keyspace metadata with err: %v", err)
}
if keyspaceMetadata == nil {
t.Fatal("expected the keyspace metadata to not be nil, but it was nil")
}
if keyspaceMetadata.Name != session.cfg.Keyspace {
t.Fatalf("Expected the keyspace name to be %s but was %s", session.cfg.Keyspace, keyspaceMetadata.Name)
}
if len(keyspaceMetadata.Tables) == 0 {
t.Errorf("Expected tables but there were none")
}

tableMetadata, found := keyspaceMetadata.Tables["test_metadata"]
if !found {
t.Fatalf("failed to find the test_metadata table metadata")
}

if len(tableMetadata.PartitionKey) != 1 {
t.Errorf("expected partition key length of 1, but was %d", len(tableMetadata.PartitionKey))
}
for i, column := range tableMetadata.PartitionKey {
if column == nil {
t.Errorf("partition key column metadata at index %d was nil", i)
}
}
if tableMetadata.PartitionKey[0].Name != "first_id" {
t.Errorf("Expected the first partition key column to be 'first_id' but was '%s'", tableMetadata.PartitionKey[0].Name)
}
if len(tableMetadata.ClusteringColumns) != 1 {
t.Fatalf("expected clustering columns length of 1, but was %d", len(tableMetadata.ClusteringColumns))
}
for i, column := range tableMetadata.ClusteringColumns {
if column == nil {
t.Fatalf("clustering column metadata at index %d was nil", i)
}
}
if tableMetadata.ClusteringColumns[0].Name != "second_id" {
t.Errorf("Expected the first clustering column to be 'second_id' but was '%s'", tableMetadata.ClusteringColumns[0].Name)
}
thirdColumn, found := tableMetadata.Columns["third_id"]
if !found {
t.Fatalf("Expected a column definition for 'third_id'")
}
if !session.useSystemSchema && thirdColumn.Index.Name != "index_metadata" {
// TODO(zariel): scan index info from system_schema
t.Errorf("Expected column index named 'index_metadata' but was '%s'", thirdColumn.Index.Name)
}

aggregate, found := keyspaceMetadata.Aggregates["average"]
if !found {
t.Fatal("failed to find the aggreate in metadata")
}
if aggregate.FinalFunc.Name != "avgfinal" {
t.Fatalf("expected final function %s, but got %s", "avgFinal", aggregate.FinalFunc.Name)
}
if aggregate.StateFunc.Name != "avgstate" {
t.Fatalf("expected state function %s, but got %s", "avgstate", aggregate.StateFunc.Name)
}
}

// Integration test of the routing key calculation
func TestRoutingKey(t *testing.T) {
session := createSession(t)
Expand Down
Loading

0 comments on commit dbd82f3

Please sign in to comment.