diff --git a/lib/cdc/util/optional_schema_test.go b/lib/cdc/util/optional_schema_test.go index c3347abc1..8e5d16c6e 100644 --- a/lib/cdc/util/optional_schema_test.go +++ b/lib/cdc/util/optional_schema_test.go @@ -2,7 +2,6 @@ package util import ( "encoding/json" - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -12,91 +11,111 @@ import ( ) func TestGetOptionalSchema(t *testing.T) { - type _tc struct { - body string - expected map[string]typing.KindDetails - } - - tcs := []_tc{ - { - body: MySQLInsert, - expected: map[string]typing.KindDetails{ - "id": typing.Integer, - "first_name": typing.String, - "last_name": typing.String, - "email": typing.String, - }, - }, - { - body: MySQLUpdate, - expected: map[string]typing.KindDetails{ - "id": typing.Integer, - "first_name": typing.String, - "last_name": typing.String, - "email": typing.String, - }, - }, + { + // MySQL { - body: MySQLDelete, - expected: map[string]typing.KindDetails{ - "id": typing.Integer, - "first_name": typing.String, - "last_name": typing.String, - "email": typing.String, - }, - }, - { - body: PostgresDelete, - expected: map[string]typing.KindDetails{ - "id": typing.Integer, - "first_name": typing.String, - "last_name": typing.String, - "email": typing.String, - }, - }, + // Insert + var schemaEventPayload SchemaEventPayload + assert.NoError(t, json.Unmarshal([]byte(MySQLInsert), &schemaEventPayload)) + + optionalSchema, err := schemaEventPayload.GetOptionalSchema() + assert.NoError(t, err) + assert.Equal( + t, + optionalSchema, + map[string]typing.KindDetails{ + "id": typing.Integer, + "first_name": typing.String, + "last_name": typing.String, + "email": typing.String, + }, + ) + } { - body: PostgresUpdate, - expected: map[string]typing.KindDetails{ - "id": typing.Integer, - "first_name": typing.String, - "last_name": typing.String, - "email": typing.String, - "boolean_test": typing.Boolean, - "bool_test": typing.Boolean, - "bit_test": typing.Boolean, - "numeric_test": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(decimal.PrecisionNotSpecified, decimal.DefaultScale)), - "numeric_5": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(5, 0)), - "numeric_5_0": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(5, 0)), - "numeric_5_2": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(5, 2)), - "numeric_5_6": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(5, 6)), - "numeric_39_0": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(39, 0)), - "numeric_39_2": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(39, 2)), - "numeric_39_6": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(39, 6)), - }, - }, - } + // Update + var schemaEventPayload SchemaEventPayload + assert.NoError(t, json.Unmarshal([]byte(MySQLUpdate), &schemaEventPayload)) - for idx, tc := range tcs { - var schemaEventPayload SchemaEventPayload - err := json.Unmarshal([]byte(tc.body), &schemaEventPayload) - assert.NoError(t, err, idx) + optionalSchema, err := schemaEventPayload.GetOptionalSchema() + assert.NoError(t, err) + assert.Equal( + t, + optionalSchema, + map[string]typing.KindDetails{ + "id": typing.Integer, + "first_name": typing.String, + "last_name": typing.String, + "email": typing.String, + }, + ) + } + { + // Delete + var schemaEventPayload SchemaEventPayload + assert.NoError(t, json.Unmarshal([]byte(MySQLDelete), &schemaEventPayload)) - actualData, err := schemaEventPayload.GetOptionalSchema() - assert.NoError(t, err) + optionalSchema, err := schemaEventPayload.GetOptionalSchema() + assert.NoError(t, err) + assert.Equal( + t, + optionalSchema, + map[string]typing.KindDetails{ + "id": typing.Integer, + "first_name": typing.String, + "last_name": typing.String, + "email": typing.String, + }, + ) + } + } + { + // Postgres + { + // Delete + var schemaEventPayload SchemaEventPayload + assert.NoError(t, json.Unmarshal([]byte(PostgresDelete), &schemaEventPayload)) - for actualKey, actualVal := range actualData { - testMsg := fmt.Sprintf("key: %s, actualKind: %s, index: %d", actualKey, actualVal.Kind, idx) + optionalSchema, err := schemaEventPayload.GetOptionalSchema() + assert.NoError(t, err) + assert.Equal( + t, + optionalSchema, + map[string]typing.KindDetails{ + "id": typing.Integer, + "first_name": typing.String, + "last_name": typing.String, + "email": typing.String, + }, + ) + } + { + // Update + var schemaEventPayload SchemaEventPayload + assert.NoError(t, json.Unmarshal([]byte(PostgresUpdate), &schemaEventPayload)) - expectedValue, isOk := tc.expected[actualKey] - assert.True(t, isOk, testMsg) - assert.Equal(t, expectedValue.Kind, actualVal.Kind, testMsg) - if expectedValue.ExtendedDecimalDetails != nil || actualVal.ExtendedDecimalDetails != nil { - assert.NotNil(t, actualVal.ExtendedDecimalDetails, testMsg) - assert.Equal(t, expectedValue.ExtendedDecimalDetails.Scale(), actualVal.ExtendedDecimalDetails.Scale(), testMsg) - assert.Equal(t, expectedValue.ExtendedDecimalDetails.Precision(), actualVal.ExtendedDecimalDetails.Precision(), testMsg) - } else { - assert.Nil(t, actualVal.ExtendedDecimalDetails, testMsg) - } + optionalSchema, err := schemaEventPayload.GetOptionalSchema() + assert.NoError(t, err) + assert.Equal( + t, + optionalSchema, + map[string]typing.KindDetails{ + "id": typing.Integer, + "first_name": typing.String, + "last_name": typing.String, + "email": typing.String, + "boolean_test": typing.Boolean, + "bool_test": typing.Boolean, + "bit_test": typing.Boolean, + "numeric_test": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(decimal.PrecisionNotSpecified, decimal.DefaultScale)), + "numeric_5": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(5, 0)), + "numeric_5_0": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(5, 0)), + "numeric_5_2": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(5, 2)), + "numeric_5_6": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(5, 6)), + "numeric_39_0": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(39, 0)), + "numeric_39_2": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(39, 2)), + "numeric_39_6": typing.NewDecimalDetailsFromTemplate(typing.EDecimal, decimal.NewDetails(39, 6)), + }, + ) } } }