Skip to content

Commit

Permalink
test: check field types and decimal lengths
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Feb 10, 2024
1 parent ea62cee commit 5878c08
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 deletions go/test/endtoend/utils/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ import (

"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/mysql/collations"

"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/sqlparser"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/test/endtoend/cluster"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/mysqlctl"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/sqlparser"
)

const mysqlShutdownTimeout = 1 * time.Minute
Expand Down Expand Up @@ -160,7 +159,9 @@ func prepareMySQLWithSchema(params mysql.ConnParams, sql string) error {
return nil
}

func compareVitessAndMySQLResults(t *testing.T, query string, vtConn *mysql.Conn, vtQr, mysqlQr *sqltypes.Result, compareColumns bool) error {
func compareVitessAndMySQLResults(t *testing.T, query string, vtConn *mysql.Conn, vtQr, mysqlQr *sqltypes.Result, compareColumnNames bool) error {
t.Helper()

if vtQr == nil && mysqlQr == nil {
return nil
}
Expand All @@ -173,28 +174,29 @@ func compareVitessAndMySQLResults(t *testing.T, query string, vtConn *mysql.Conn
return errors.New("MySQL result is 'nil' while Vitess' is not.\n")
}

var errStr string
if compareColumns {
vtColCount := len(vtQr.Fields)
myColCount := len(mysqlQr.Fields)
if vtColCount > 0 && myColCount > 0 {
if vtColCount != myColCount {
t.Errorf("column count does not match: %d vs %d", vtColCount, myColCount)
errStr += fmt.Sprintf("column count does not match: %d vs %d\n", vtColCount, myColCount)
}

var vtCols []string
var myCols []string
for i, vtField := range vtQr.Fields {
vtCols = append(vtCols, vtField.Name)
myCols = append(myCols, mysqlQr.Fields[i].Name)
}
if !assert.Equal(t, myCols, vtCols, "column names do not match - the expected values are what mysql produced") {
errStr += "column names do not match - the expected values are what mysql produced\n"
errStr += fmt.Sprintf("Not equal: \nexpected: %v\nactual: %v\n", myCols, vtCols)
}
vtColCount := len(vtQr.Fields)
myColCount := len(mysqlQr.Fields)

if vtColCount != myColCount {
t.Errorf("column count does not match: %d vs %d", vtColCount, myColCount)
}

if vtColCount > 0 {
var vtCols []string
var myCols []string
for i, vtField := range vtQr.Fields {
myField := mysqlQr.Fields[i]
checkFields(t, myField.Name, vtField, myField)

vtCols = append(vtCols, vtField.Name)
myCols = append(myCols, myField.Name)
}

if compareColumnNames && !assert.Equal(t, myCols, vtCols, "column names do not match - the expected values are what mysql produced") {
t.Errorf("column names do not match - the expected values are what mysql produced\nNot equal: \nexpected: %v\nactual: %v\n", myCols, vtCols)
}
}

stmt, err := sqlparser.NewTestParser().Parse(query)
if err != nil {
t.Error(err)
Expand All @@ -209,7 +211,7 @@ func compareVitessAndMySQLResults(t *testing.T, query string, vtConn *mysql.Conn
return nil
}

errStr += "Query (" + query + ") results mismatched.\nVitess Results:\n"
errStr := "Query (" + query + ") results mismatched.\nVitess Results:\n"
for _, row := range vtQr.Rows {
errStr += fmt.Sprintf("%s\n", row)
}
Expand All @@ -229,6 +231,20 @@ func compareVitessAndMySQLResults(t *testing.T, query string, vtConn *mysql.Conn
return errors.New(errStr)
}

func checkFields(t *testing.T, columnName string, vtField, myField *querypb.Field) {
t.Helper()
if vtField.Type != myField.Type {
t.Errorf("for column %s field types do not match\nNot equal: \nMySQL: %v\nVitess: %v\n", columnName, myField.Type.String(), vtField.Type.String())
}

// ensure that Decimal types are properly sized
if vtField.Type == sqltypes.Decimal {
if vtField.Decimals != myField.Decimals {
t.Errorf("for column %s field decimals count do not match\nNot equal: \nMySQL: %v\nVitess: %v\n", columnName, myField.Decimals, vtField.Decimals)
}
}
}

func compareVitessAndMySQLErrors(t *testing.T, vtErr, mysqlErr error) {
if vtErr != nil && mysqlErr != nil || vtErr == nil && mysqlErr == nil {
return
Expand Down

0 comments on commit 5878c08

Please sign in to comment.