diff --git a/sources/dynamodb/schema_test.go b/sources/dynamodb/schema_test.go index 9919d3d5a..d920c1125 100644 --- a/sources/dynamodb/schema_test.go +++ b/sources/dynamodb/schema_test.go @@ -26,10 +26,12 @@ import ( "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface" "github.com/aws/aws-sdk-go/service/dynamodbstreams" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "go.uber.org/zap" "github.com/GoogleCloudPlatform/spanner-migration-tool/internal" "github.com/GoogleCloudPlatform/spanner-migration-tool/logger" + "github.com/GoogleCloudPlatform/spanner-migration-tool/mocks" "github.com/GoogleCloudPlatform/spanner-migration-tool/schema" "github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common" "github.com/GoogleCloudPlatform/spanner-migration-tool/spanner/ddl" @@ -186,10 +188,17 @@ func TestProcessSchema(t *testing.T) { scanOutputs: scanOutputs, } sampleSize := int64(10000) + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) conv := internal.MakeConv() processSchema := common.ProcessSchemaImpl{} - err := processSchema.ProcessSchema(conv, InfoSchemaImpl{client, nil, sampleSize}, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) + err := processSchema.ProcessSchema(conv, InfoSchemaImpl{client, nil, sampleSize}, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) assert.Nil(t, err) expectedSchema := map[string]ddl.CreateTable{ @@ -289,8 +298,9 @@ func TestProcessSchema_FullDataTypes(t *testing.T) { sampleSize := int64(10000) conv := internal.MakeConv() + mockAccessor := new(mocks.MockExpressionVerificationAccessor) processSchema := common.ProcessSchemaImpl{} - err := processSchema.ProcessSchema(conv, InfoSchemaImpl{client, nil, sampleSize}, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) + err := processSchema.ProcessSchema(conv, InfoSchemaImpl{client, nil, sampleSize}, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) assert.Nil(t, err) expectedSchema := map[string]ddl.CreateTable{ diff --git a/sources/dynamodb/toddl_test.go b/sources/dynamodb/toddl_test.go index 87dd94ee1..b574329de 100644 --- a/sources/dynamodb/toddl_test.go +++ b/sources/dynamodb/toddl_test.go @@ -15,15 +15,18 @@ package dynamodb import ( + "context" "testing" "github.com/GoogleCloudPlatform/spanner-migration-tool/common/constants" "github.com/GoogleCloudPlatform/spanner-migration-tool/internal" + "github.com/GoogleCloudPlatform/spanner-migration-tool/mocks" "github.com/GoogleCloudPlatform/spanner-migration-tool/proto/migration" "github.com/GoogleCloudPlatform/spanner-migration-tool/schema" "github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common" "github.com/GoogleCloudPlatform/spanner-migration-tool/spanner/ddl" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" ) func TestToSpannerType(t *testing.T) { @@ -58,7 +61,14 @@ func TestToSpannerType(t *testing.T) { } conv.SrcSchema[name] = srcSchema conv.Audit = audit - schemaToSpanner := common.SchemaToSpannerImpl{} + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) + schemaToSpanner := common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor} assert.Nil(t, schemaToSpanner.SchemaToSpannerDDL(conv, ToDdlImpl{})) actual := conv.SpSchema[name] dropComments(&actual) // Don't test comment. @@ -119,7 +129,14 @@ func TestToSpannerPostgreSQLDialectType(t *testing.T) { } conv.SrcSchema["t1"] = srcSchema conv.Audit = audit - schemaToSpanner := common.SchemaToSpannerImpl{} + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) + schemaToSpanner := common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor} assert.Nil(t, schemaToSpanner.SchemaToSpannerDDL(conv, ToDdlImpl{})) actual := conv.SpSchema["t1"] dropComments(&actual) // Don't test comment. diff --git a/sources/mysql/infoschema_test.go b/sources/mysql/infoschema_test.go index cde949d7e..4a4e95634 100644 --- a/sources/mysql/infoschema_test.go +++ b/sources/mysql/infoschema_test.go @@ -15,6 +15,7 @@ package mysql import ( + "context" "database/sql" "database/sql/driver" "regexp" @@ -22,9 +23,11 @@ import ( "github.com/DATA-DOG/go-sqlmock" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "github.com/GoogleCloudPlatform/spanner-migration-tool/common/constants" "github.com/GoogleCloudPlatform/spanner-migration-tool/internal" + "github.com/GoogleCloudPlatform/spanner-migration-tool/mocks" "github.com/GoogleCloudPlatform/spanner-migration-tool/profiles" "github.com/GoogleCloudPlatform/spanner-migration-tool/schema" "github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common" @@ -473,7 +476,15 @@ func TestProcessData_MultiCol(t *testing.T) { conv := internal.MakeConv() isi := InfoSchemaImpl{"test", db, "migration-project-id", profiles.SourceProfile{}, profiles.TargetProfile{}} processSchema := common.ProcessSchemaImpl{} - err := processSchema.ProcessSchema(conv, isi, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) + + err := processSchema.ProcessSchema(conv, isi, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) assert.Nil(t, err) expectedSchema := map[string]ddl.CreateTable{ "test": { @@ -490,7 +501,7 @@ func TestProcessData_MultiCol(t *testing.T) { } internal.AssertSpSchema(conv, t, expectedSchema, stripSchemaComments(conv.SpSchema)) columnLevelIssues := map[string][]internal.SchemaIssue{ - "c49": { + "c5": { 2, }, } @@ -576,8 +587,15 @@ func TestProcessSchema_Sharded(t *testing.T) { db := mkMockDB(t, ms) conv := internal.MakeConv() isi := InfoSchemaImpl{"test", db, "migration-project-id", profiles.SourceProfile{}, profiles.TargetProfile{}} + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) processSchema := common.ProcessSchemaImpl{} - err := processSchema.ProcessSchema(conv, isi, 1, internal.AdditionalSchemaAttributes{IsSharded: true}, &common.SchemaToSpannerImpl{}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) + err := processSchema.ProcessSchema(conv, isi, 1, internal.AdditionalSchemaAttributes{IsSharded: true}, &common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) assert.Nil(t, err) expectedSchema := map[string]ddl.CreateTable{ "test": { diff --git a/sources/mysql/toddl_test.go b/sources/mysql/toddl_test.go index d33945864..d8c2e327b 100644 --- a/sources/mysql/toddl_test.go +++ b/sources/mysql/toddl_test.go @@ -15,14 +15,17 @@ package mysql import ( + "context" "testing" "github.com/GoogleCloudPlatform/spanner-migration-tool/common/constants" "github.com/GoogleCloudPlatform/spanner-migration-tool/internal" + "github.com/GoogleCloudPlatform/spanner-migration-tool/mocks" "github.com/GoogleCloudPlatform/spanner-migration-tool/schema" "github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common" "github.com/GoogleCloudPlatform/spanner-migration-tool/spanner/ddl" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" ) func TestToSpannerTypeInternal(t *testing.T) { @@ -177,7 +180,14 @@ func TestToSpannerType(t *testing.T) { PrimaryKeys: []schema.Key{{ColId: "c14"}}, } conv.UsedNames = map[string]bool{"ref_table": true, "ref_table2": true} - schemaToSpanner := common.SchemaToSpannerImpl{} + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) + schemaToSpanner := common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor} assert.Nil(t, schemaToSpanner.SchemaToSpannerDDL(conv, ToDdlImpl{})) actual := conv.SpSchema[tableId] dropComments(&actual) // Don't test comment. @@ -271,7 +281,14 @@ func TestToSpannerPostgreSQLDialectType(t *testing.T) { PrimaryKeys: []schema.Key{{ColId: "c14"}}, } conv.UsedNames = map[string]bool{"ref_table": true, "ref_table2": true} - schemaToSpanner := common.SchemaToSpannerImpl{} + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) + schemaToSpanner := common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor} assert.Nil(t, schemaToSpanner.SchemaToSpannerDDL(conv, ToDdlImpl{})) actual := conv.SpSchema[tableId] dropComments(&actual) // Don't test comment. diff --git a/sources/oracle/infoschema_test.go b/sources/oracle/infoschema_test.go index 25237e3d6..df1271312 100644 --- a/sources/oracle/infoschema_test.go +++ b/sources/oracle/infoschema_test.go @@ -15,6 +15,7 @@ package oracle import ( + "context" "database/sql" "database/sql/driver" "fmt" @@ -22,8 +23,10 @@ import ( "github.com/DATA-DOG/go-sqlmock" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "github.com/GoogleCloudPlatform/spanner-migration-tool/internal" + "github.com/GoogleCloudPlatform/spanner-migration-tool/mocks" "github.com/GoogleCloudPlatform/spanner-migration-tool/profiles" "github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common" "github.com/GoogleCloudPlatform/spanner-migration-tool/spanner/ddl" @@ -162,7 +165,16 @@ func TestProcessSchemaOracle(t *testing.T) { db := mkMockDB(t, ms) conv := internal.MakeConv() processSchema := common.ProcessSchemaImpl{} - err := processSchema.ProcessSchema(conv, InfoSchemaImpl{"test", db, "migration-project-id", profiles.SourceProfile{}, profiles.TargetProfile{}}, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) + + err := processSchema.ProcessSchema(conv, InfoSchemaImpl{"test", db, "migration-project-id", profiles.SourceProfile{}, profiles.TargetProfile{}}, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) assert.Nil(t, err) expectedSchema := map[string]ddl.CreateTable{ "USER": { diff --git a/sources/oracle/toddl_test.go b/sources/oracle/toddl_test.go index 51ac927ed..28e083f3b 100644 --- a/sources/oracle/toddl_test.go +++ b/sources/oracle/toddl_test.go @@ -15,15 +15,18 @@ package oracle import ( + "context" "testing" "github.com/GoogleCloudPlatform/spanner-migration-tool/common/constants" "github.com/GoogleCloudPlatform/spanner-migration-tool/internal" "github.com/GoogleCloudPlatform/spanner-migration-tool/logger" + "github.com/GoogleCloudPlatform/spanner-migration-tool/mocks" "github.com/GoogleCloudPlatform/spanner-migration-tool/schema" "github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common" "github.com/GoogleCloudPlatform/spanner-migration-tool/spanner/ddl" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "go.uber.org/zap" ) @@ -167,7 +170,14 @@ func TestToSpannerType(t *testing.T) { PrimaryKeys: []schema.Key{{ColId: "c12"}}, } conv.UsedNames = map[string]bool{"ref_table": true, "ref_table2": true} - schemaToSpanner := common.SchemaToSpannerImpl{} + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) + schemaToSpanner := common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor} assert.Nil(t, schemaToSpanner.SchemaToSpannerDDL(conv, ToDdlImpl{})) actual := conv.SpSchema[tableId] dropComments(&actual) // Don't test comment. @@ -260,7 +270,14 @@ func TestToSpannerPostgreSQLDialectType(t *testing.T) { PrimaryKeys: []schema.Key{{ColId: "c15"}}, } conv.UsedNames = map[string]bool{"ref_table": true, "ref_table2": true} - schemaToSpanner := common.SchemaToSpannerImpl{} + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) + schemaToSpanner := common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor} assert.Nil(t, schemaToSpanner.SchemaToSpannerDDL(conv, ToDdlImpl{})) actual := conv.SpSchema[tableId] dropComments(&actual) // Don't test comment. diff --git a/sources/postgres/infoschema_test.go b/sources/postgres/infoschema_test.go index a9d3803d3..61246a37d 100644 --- a/sources/postgres/infoschema_test.go +++ b/sources/postgres/infoschema_test.go @@ -15,6 +15,7 @@ package postgres import ( + "context" "database/sql" "database/sql/driver" "math/big" @@ -26,11 +27,13 @@ import ( "github.com/GoogleCloudPlatform/spanner-migration-tool/common/constants" "github.com/GoogleCloudPlatform/spanner-migration-tool/internal" "github.com/GoogleCloudPlatform/spanner-migration-tool/logger" + "github.com/GoogleCloudPlatform/spanner-migration-tool/mocks" "github.com/GoogleCloudPlatform/spanner-migration-tool/profiles" "github.com/GoogleCloudPlatform/spanner-migration-tool/schema" "github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common" "github.com/GoogleCloudPlatform/spanner-migration-tool/spanner/ddl" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "go.uber.org/zap" ) @@ -230,8 +233,15 @@ func TestProcessSchema(t *testing.T) { } db := mkMockDB(t, ms) conv := internal.MakeConv() + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) processSchema := common.ProcessSchemaImpl{} - err := processSchema.ProcessSchema(conv, InfoSchemaImpl{db, "migration-project-id", profiles.SourceProfile{}, profiles.TargetProfile{}, newFalsePtr()}, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) + err := processSchema.ProcessSchema(conv, InfoSchemaImpl{db, "migration-project-id", profiles.SourceProfile{}, profiles.TargetProfile{}, newFalsePtr()}, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) assert.Nil(t, err) expectedSchema := map[string]ddl.CreateTable{ "user": ddl.CreateTable{ @@ -515,8 +525,15 @@ func TestConvertSqlRow_MultiCol(t *testing.T) { } db := mkMockDB(t, ms) conv := internal.MakeConv() + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) processSchema := common.ProcessSchemaImpl{} - err := processSchema.ProcessSchema(conv, InfoSchemaImpl{db, "migration-project-id", profiles.SourceProfile{}, profiles.TargetProfile{}, newFalsePtr()}, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) + err := processSchema.ProcessSchema(conv, InfoSchemaImpl{db, "migration-project-id", profiles.SourceProfile{}, profiles.TargetProfile{}, newFalsePtr()}, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) assert.Nil(t, err) conv.SetDataMode() var rows []spannerData diff --git a/sources/postgres/toddl_test.go b/sources/postgres/toddl_test.go index 44adb7973..d4f60c5ca 100644 --- a/sources/postgres/toddl_test.go +++ b/sources/postgres/toddl_test.go @@ -15,15 +15,18 @@ package postgres import ( + "context" "sort" "testing" "github.com/GoogleCloudPlatform/spanner-migration-tool/common/constants" "github.com/GoogleCloudPlatform/spanner-migration-tool/internal" + "github.com/GoogleCloudPlatform/spanner-migration-tool/mocks" "github.com/GoogleCloudPlatform/spanner-migration-tool/schema" "github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common" "github.com/GoogleCloudPlatform/spanner-migration-tool/spanner/ddl" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" ) func TestToSpannerTypeInternal(t *testing.T) { @@ -168,7 +171,14 @@ func TestToSpannerType(t *testing.T) { PrimaryKeys: []schema.Key{{ColId: "c10"}}, } conv.UsedNames = map[string]bool{"ref_table": true, "ref_table2": true} - schemaToSpanner := common.SchemaToSpannerImpl{} + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) + schemaToSpanner := common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor} assert.Nil(t, schemaToSpanner.SchemaToSpannerDDL(conv, ToDdlImpl{})) actual := conv.SpSchema[tableId] dropComments(&actual) // Don't test comment. diff --git a/sources/sqlserver/infoschema_test.go b/sources/sqlserver/infoschema_test.go index 626cf6672..887309948 100644 --- a/sources/sqlserver/infoschema_test.go +++ b/sources/sqlserver/infoschema_test.go @@ -15,6 +15,7 @@ package sqlserver import ( + "context" "database/sql" "database/sql/driver" "testing" @@ -22,9 +23,11 @@ import ( "github.com/DATA-DOG/go-sqlmock" "github.com/GoogleCloudPlatform/spanner-migration-tool/internal" "github.com/GoogleCloudPlatform/spanner-migration-tool/logger" + "github.com/GoogleCloudPlatform/spanner-migration-tool/mocks" "github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common" "github.com/GoogleCloudPlatform/spanner-migration-tool/spanner/ddl" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "go.uber.org/zap" ) @@ -248,8 +251,16 @@ func TestProcessSchema(t *testing.T) { } db := mkMockDB(t, ms) conv := internal.MakeConv() + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + processSchema := common.ProcessSchemaImpl{} - err := processSchema.ProcessSchema(conv, InfoSchemaImpl{"test", db}, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) + err := processSchema.ProcessSchema(conv, InfoSchemaImpl{"test", db}, 1, internal.AdditionalSchemaAttributes{}, &common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor}, &common.UtilsOrderImpl{}, &common.InfoSchemaImpl{}) assert.Nil(t, err) expectedSchema := map[string]ddl.CreateTable{ "user": { diff --git a/sources/sqlserver/toddl_test.go b/sources/sqlserver/toddl_test.go index 6cd024089..96ac25363 100644 --- a/sources/sqlserver/toddl_test.go +++ b/sources/sqlserver/toddl_test.go @@ -15,14 +15,17 @@ package sqlserver import ( + "context" "testing" "github.com/GoogleCloudPlatform/spanner-migration-tool/common/constants" "github.com/GoogleCloudPlatform/spanner-migration-tool/internal" + "github.com/GoogleCloudPlatform/spanner-migration-tool/mocks" "github.com/GoogleCloudPlatform/spanner-migration-tool/schema" "github.com/GoogleCloudPlatform/spanner-migration-tool/sources/common" "github.com/GoogleCloudPlatform/spanner-migration-tool/spanner/ddl" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" ) func TestToSpannerTypeInternal(t *testing.T) { @@ -173,7 +176,14 @@ func TestToSpannerType(t *testing.T) { PrimaryKeys: []schema.Key{{ColId: "c19"}}, } conv.UsedNames = map[string]bool{"ref_table": true, "ref_table2": true} - schemaToSpanner := common.SchemaToSpannerImpl{} + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) + schemaToSpanner := common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor} assert.Nil(t, schemaToSpanner.SchemaToSpannerDDL(conv, ToDdlImpl{})) actual := conv.SpSchema[tableId] dropComments(&actual) // Don't test comment. @@ -277,7 +287,14 @@ func TestToSpannerPostgreSQLDialectType(t *testing.T) { PrimaryKeys: []schema.Key{{ColId: "c19"}}, } conv.UsedNames = map[string]bool{"ref_table": true, "ref_table2": true} - schemaToSpanner := common.SchemaToSpannerImpl{} + mockAccessor := new(mocks.MockExpressionVerificationAccessor) + ctx := context.Background() + mockAccessor.On("VerifyExpressions", ctx, mock.Anything).Return(internal.VerifyExpressionsOutput{ + ExpressionVerificationOutputList: []internal.ExpressionVerificationOutput{ + {Result: true, Err: nil, ExpressionDetail: internal.ExpressionDetail{Expression: "(col1 > 0)", Type: "CHECK", Metadata: map[string]string{"tableId": "t1", "colId": "c1", "checkConstraintName": "check1"}, ExpressionId: "expr1"}}, + }, + }) + schemaToSpanner := common.SchemaToSpannerImpl{ExpressionVerificationAccessor: mockAccessor} assert.Nil(t, schemaToSpanner.SchemaToSpannerDDL(conv, ToDdlImpl{})) actual := conv.SpSchema[tableId] dropComments(&actual) // Don't test comment.