From c6f615c48302567c6c2129a0e43ad11be8e3f34c Mon Sep 17 00:00:00 2001 From: Florent Poinsard Date: Tue, 7 Jan 2025 10:09:41 -0600 Subject: [PATCH] Simplify tests and regen code Signed-off-by: Florent Poinsard --- go/test/endtoend/utils/cmp.go | 4 ++-- .../vtgate/plan_tests/plan_e2e_test.go | 2 +- .../endtoend/vtgate/queries/misc/main_test.go | 2 +- .../endtoend/vtgate/queries/misc/misc_test.go | 23 +++++++++++-------- go/vt/sqlparser/ast_test.go | 8 +++++++ go/vt/vtgate/engine/cached_size.go | 2 +- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/go/test/endtoend/utils/cmp.go b/go/test/endtoend/utils/cmp.go index 3a05c75c33b..b2e1eca03e9 100644 --- a/go/test/endtoend/utils/cmp.go +++ b/go/test/endtoend/utils/cmp.go @@ -215,8 +215,8 @@ func (mcmp *MySQLCompare) Exec(query string) *sqltypes.Result { return vtQr } -// ExecVitessAndMySQL executes Vitess and MySQL with the queries provided. -func (mcmp *MySQLCompare) ExecVitessAndMySQL(vtQ, mQ string) *sqltypes.Result { +// ExecVitessAndMySQLDifferentQueries executes Vitess and MySQL with the queries provided. +func (mcmp *MySQLCompare) ExecVitessAndMySQLDifferentQueries(vtQ, mQ string) *sqltypes.Result { mcmp.t.Helper() vtQr, err := mcmp.VtConn.ExecuteFetch(vtQ, 1000, true) require.NoError(mcmp.t, err, "[Vitess Error] for query: "+vtQ) diff --git a/go/test/endtoend/vtgate/plan_tests/plan_e2e_test.go b/go/test/endtoend/vtgate/plan_tests/plan_e2e_test.go index ffe712c141c..0068616c3b8 100644 --- a/go/test/endtoend/vtgate/plan_tests/plan_e2e_test.go +++ b/go/test/endtoend/vtgate/plan_tests/plan_e2e_test.go @@ -49,7 +49,7 @@ func TestE2ECases(t *testing.T) { require.NoError(mcmp.AsT(), err) sqlparser.RemoveKeyspaceIgnoreSysSchema(stmt) - mcmp.ExecVitessAndMySQL(test.Query, sqlparser.String(stmt)) + mcmp.ExecVitessAndMySQLDifferentQueries(test.Query, sqlparser.String(stmt)) pd := utils.ExecTrace(mcmp.AsT(), mcmp.VtConn, test.Query) verifyTestExpectations(mcmp.AsT(), pd, test) if mcmp.VtConn.IsClosed() { diff --git a/go/test/endtoend/vtgate/queries/misc/main_test.go b/go/test/endtoend/vtgate/queries/misc/main_test.go index ee9be542634..536dfa7500a 100644 --- a/go/test/endtoend/vtgate/queries/misc/main_test.go +++ b/go/test/endtoend/vtgate/queries/misc/main_test.go @@ -95,7 +95,7 @@ func TestMain(m *testing.M) { vtParams = clusterInstance.GetVTParams(keyspaceName) // create mysql instance and connection parameters - conn, closer, err := utils.NewMySQL(clusterInstance, keyspaceName, schemaSQL) + conn, closer, err := utils.NewMySQL(clusterInstance, keyspaceName, schemaSQL, uschemaSQL) if err != nil { fmt.Println(err) return 1 diff --git a/go/test/endtoend/vtgate/queries/misc/misc_test.go b/go/test/endtoend/vtgate/queries/misc/misc_test.go index 6034ff4a7f5..7ab0fe7ef54 100644 --- a/go/test/endtoend/vtgate/queries/misc/misc_test.go +++ b/go/test/endtoend/vtgate/queries/misc/misc_test.go @@ -25,6 +25,7 @@ import ( "time" "vitess.io/vitess/go/mysql" + "vitess.io/vitess/go/vt/sqlparser" _ "github.com/go-sql-driver/mysql" "github.com/stretchr/testify/assert" @@ -189,7 +190,6 @@ func TestSetAndGetLastInsertID(t *testing.T) { } func TestSetAndGetLastInsertIDWithInsertUnsharded(t *testing.T) { - // in this test we can't use the mcmp, so we need to assert the returned values manually mcmp, closer := start(t) defer closer() @@ -206,13 +206,16 @@ func TestSetAndGetLastInsertIDWithInsertUnsharded(t *testing.T) { runTests := func(mcmp *utils.MySQLCompare) { for _, test := range tests { + lastInsertID := getVal() query := fmt.Sprintf(test, lastInsertID) - utils.Exec(mcmp.AsT(), mcmp.VtConn, query) - result := utils.Exec(mcmp.AsT(), mcmp.VtConn, "select last_insert_id()") - uintVal, err := result.Rows[0][0].ToCastUint64() + + stmt, err := sqlparser.NewTestParser().Parse(query) require.NoError(mcmp.AsT(), err) - require.EqualValues(mcmp.AsT(), lastInsertID, uintVal, query) + sqlparser.RemoveKeyspaceIgnoreSysSchema(stmt) + + mcmp.ExecVitessAndMySQLDifferentQueries(query, sqlparser.String(stmt)) + mcmp.Exec("select last_insert_id()") } } @@ -230,11 +233,11 @@ func TestSetAndGetLastInsertIDWithInsertUnsharded(t *testing.T) { } // Now test to set the last insert id to 0, see that it has changed correctly even if the value is 0 - utils.Exec(t, mcmp.VtConn, "insert into uks.unsharded(id1, id2) values (last_insert_id(0),12)") - result := utils.Exec(t, mcmp.VtConn, "select last_insert_id()") - uintVal, err := result.Rows[0][0].ToCastUint64() - require.NoError(t, err) - require.Zero(t, uintVal) + mcmp.ExecVitessAndMySQLDifferentQueries( + "insert into uks.unsharded(id1, id2) values (last_insert_id(0),12)", + "insert into unsharded(id1, id2) values (last_insert_id(0),12)", + ) + mcmp.Exec("select last_insert_id()") } func TestSetAndGetLastInsertIDWithInsert(t *testing.T) { diff --git a/go/vt/sqlparser/ast_test.go b/go/vt/sqlparser/ast_test.go index f01b47cbd7b..c1484df7cc4 100644 --- a/go/vt/sqlparser/ast_test.go +++ b/go/vt/sqlparser/ast_test.go @@ -917,3 +917,11 @@ func TestCloneComments(t *testing.T) { assert.Equal(t, "b", val) } } + +func TestRemoveKeyspace(t *testing.T) { + stmt, err := NewTestParser().Parse("select 1 from uks.unsharded") + require.NoError(t, err) + RemoveKeyspaceIgnoreSysSchema(stmt) + + require.Equal(t, "select 1 from unsharded", String(stmt)) +} diff --git a/go/vt/vtgate/engine/cached_size.go b/go/vt/vtgate/engine/cached_size.go index e59832cdab5..50d3a4b6bbf 100644 --- a/go/vt/vtgate/engine/cached_size.go +++ b/go/vt/vtgate/engine/cached_size.go @@ -465,7 +465,7 @@ func (cached *Insert) CachedSize(alloc bool) int64 { } size := int64(0) if alloc { - size += int64(240) + size += int64(224) } // field InsertCommon vitess.io/vitess/go/vt/vtgate/engine.InsertCommon size += cached.InsertCommon.CachedSize(false)