Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

primarykey package test #128

Open
wants to merge 9 commits into
base: test-coverage-lyazii-v1
Choose a base branch
from
2 changes: 1 addition & 1 deletion webv2/primarykey/hotspot.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func hotspotAutoincrement(insert []ddl.IndexKey, spannerTable ddl.CreateTable) {

for i := 0; i < len(insert); i++ {
for _, c := range spannerTable.ColDefs {
if insert[i].ColId == c.Name {
if insert[i].ColId == c.Id {
spannerColumnId := c.Id
detecthotspotAutoincrement(spannerTable, spannerColumnId)
}
Expand Down
116 changes: 116 additions & 0 deletions webv2/primarykey/hotspot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package primarykey

import (
"reflect"
"testing"

"github.com/cloudspannerecosystem/harbourbridge/internal"
"github.com/cloudspannerecosystem/harbourbridge/schema"
"github.com/cloudspannerecosystem/harbourbridge/spanner/ddl"
"github.com/cloudspannerecosystem/harbourbridge/webv2/session"
)

func TestDetectHotspot(t *testing.T) {
input1 := []ddl.CreateTable{{
Name: "TimeStamp_t1",
Id: "t1",
ColIds: []string{"c1", "c2", "c3"},
ColDefs: map[string]ddl.ColumnDef{
"c1": {Name: "film_id", T: ddl.Type{Name: ddl.String, Len: ddl.MaxLength}, Id: "c1"},
"c2": {Name: "actor_id", T: ddl.Type{Name: ddl.String, Len: ddl.MaxLength}, Id: "c2"},
"c3": {Name: "last_update", T: ddl.Type{Name: ddl.Timestamp, Len: ddl.MaxLength}, Id: "c3"},
},
PrimaryKeys: []ddl.IndexKey{{ColId: "c3", Order: 1, Desc: false}, {ColId: "c2", Order: 2, Desc: false}},
},
{
Name: "AutoIncrement_t2",
Id: "t1",
ColIds: []string{"c1"},
ColDefs: map[string]ddl.ColumnDef{
"c1": {Name: "film_id", T: ddl.Type{Name: ddl.String, Len: ddl.MaxLength}, Id: "c1"},
},
PrimaryKeys: []ddl.IndexKey{{ColId: "c1", Order: 1, Desc: false}},
},
}

sessionState := session.GetSessionState()
sourcetable := schema.Table{
Name: "t1",
Schema: "s",
ColIds: []string{"c1"},
ColDefs: map[string]schema.Column{
"c1": {Name: "film_id", Type: schema.Type{Name: ddl.String}, Id: "c1", Ignored: schema.Ignored{AutoIncrement: true}},
},
PrimaryKeys: []schema.Key{{ColId: "c1", Order: 1, Desc: false}},
}

//sourcetable := sessionState.Conv.SrcSchema[]
sessionState.Conv = &internal.Conv{
SpSchema: ddl.Schema{
"t1": input1[1],
//"t2": input1[1],
},
SrcSchema: map[string]schema.Table{
"t1": sourcetable,
},
SchemaIssues: map[string]map[string][]internal.SchemaIssue{
"t1": {},
//"t2": {},
},
}

for i, input := range input1 {
if i != 1 {
continue
}
isHotSpot(input.PrimaryKeys, input)
expected := []internal.SchemaIssue{internal.HotspotAutoIncrement}
if !reflect.DeepEqual(sessionState.Conv.SchemaIssues[input.Id]["c1"], expected) {
t.Errorf("hotspotTimestamp failed, expected: %v, got: %v", expected, sessionState.Conv.SchemaIssues[input.Id][input.Id])
}

}

}

/*
func TestDetecthotspotAutoincrement(t *testing.T) {
// Define test input
spannerTable := ddl.CreateTable{
Name: "film_actor",
ColIds: []string{"c1", "c2", "c3"},
ColDefs: map[string]ddl.ColumnDef{
"c1": {Name: "film_id", T: ddl.Type{Name: ddl.String, Len: ddl.MaxLength}, Id: "c1"},
"c2": {Name: "actor_id", T: ddl.Type{Name: ddl.String, Len: ddl.MaxLength}, Id: "c2"},
"c3": {Name: "last_update", T: ddl.Type{Name: ddl.String, Len: ddl.MaxLength}, Id: "c3"},
},
PrimaryKeys: []ddl.IndexKey{{ColId: "c1", Order: 1, Desc: true}, {ColId: "c2", Order: 2, Desc: true}},
Id: "t1",
}

spannerColumnId := "c1"

sessionState := session.GetSessionState()
sessionState.Conv.SrcSchema = map[string]ddl.CreateTable{
"t1":{
Name: "film_actor",
ColDefs: map[string]ddl.ColumnDef{
"c1": {Name: "film_id", T: ddl.Type{Name: ddl.String, Len: ddl.MaxLength}, Id: "c1", Ignored: ddl.Ignored{AutoIncrement: true}},
"c2": {Name: "actor_id", T: ddl.Type{Name: ddl.String, Len: ddl.MaxLength}, Id: "c2"},
"c3": {Name: "last_update", T: ddl.Type{Name: ddl.String, Len: ddl.MaxLength}, Id: "c3"},
},
Id: "t1",
},
},

// Call the function being tested
detecthotspotAutoincrement(spannerTable, spannerColumnId)

// Check the output for correctness
schemaIssue := sessionState.Conv.SchemaIssues[spannerTable.Id][spannerColumnId]
expectedSchemaIssue := []internal.SchemaIssue{internal.HotspotAutoIncrement}
if !reflect.DeepEqual(schemaIssue, expectedSchemaIssue) {
t.Errorf("detecthotspotAutoincrement(%v, %v) = %v, expected %v", spannerTable, spannerColumnId, schemaIssue, expectedSchemaIssue)
}
}
*/
6 changes: 3 additions & 3 deletions webv2/primarykey/primarykey_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ func RemoveInterleave(conv *internal.Conv, spannertable ddl.CreateTable) {
var parentPkFirstColumn string
for i := 0; i < len(spannertable.PrimaryKeys); i++ {
if spannertable.PrimaryKeys[i].Order == 1 {
childPkFirstColumn = spannertable.PrimaryKeys[i].ColId
childPkFirstColumn = spannertable.ColDefs[spannertable.PrimaryKeys[i].ColId].Name
}
}
for i := 0; i < len(conv.SpSchema[spannertable.ParentId].PrimaryKeys); i++ {
if conv.SpSchema[spannertable.ParentId].PrimaryKeys[i].Order == 1 {
parentPkFirstColumn = conv.SpSchema[spannertable.ParentId].PrimaryKeys[i].ColId
parentPkFirstColumn = conv.SpSchema[spannertable.ParentId].ColDefs[conv.SpSchema[spannertable.ParentId].PrimaryKeys[i].ColId].Name
}
}
if childPkFirstColumn != parentPkFirstColumn {
spannertable.ParentId = ""
conv.SpSchema[spannertable.Name] = spannertable
conv.SpSchema[spannertable.Id] = spannertable
}
}
}
Expand Down
Loading