From 70c256491d4c75620a85e10c94e17933170eb61f Mon Sep 17 00:00:00 2001 From: Aaron Son Date: Fri, 17 Feb 2023 14:17:57 -0800 Subject: [PATCH 1/2] enginetest: Small cleanup for DROP DATABASE and DROP TABLE tests to be a little more hygienic in their use of state across the tests. --- enginetest/enginetests.go | 85 ++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 33 deletions(-) diff --git a/enginetest/enginetests.go b/enginetest/enginetests.go index 3b19601bec..5d86cb3b38 100644 --- a/enginetest/enginetests.go +++ b/enginetest/enginetests.go @@ -1992,55 +1992,64 @@ func TestDropTable(t *testing.T, harness Harness) { require := require.New(t) harness.Setup(setup.MydbData, setup.MytableData, setup.OthertableData, setup.TabletestData, setup.Pk_tablesData) - e := mustNewEngine(t, harness) - defer e.Close() - ctx := NewContext(harness) - db, err := e.Analyzer.Catalog.Database(ctx, "mydb") - require.NoError(err) - _, ok, err := db.GetTableInsensitive(ctx, "mytable") - require.True(ok) + func() { + e := mustNewEngine(t, harness) + defer e.Close() + ctx := NewContext(harness) + db, err := e.Analyzer.Catalog.Database(ctx, "mydb") + require.NoError(err) - TestQueryWithContext(t, ctx, e, harness, "DROP TABLE IF EXISTS mytable, not_exist", []sql.Row{{types.NewOkResult(0)}}, nil, nil) + _, ok, err := db.GetTableInsensitive(ctx, "mytable") + require.True(ok) - _, ok, err = db.GetTableInsensitive(ctx, "mytable") - require.NoError(err) - require.False(ok) + TestQueryWithContext(t, ctx, e, harness, "DROP TABLE IF EXISTS mytable, not_exist", []sql.Row{{types.NewOkResult(0)}}, nil, nil) - _, ok, err = db.GetTableInsensitive(ctx, "othertable") - require.NoError(err) - require.True(ok) + _, ok, err = db.GetTableInsensitive(ctx, "mytable") + require.NoError(err) + require.False(ok) - _, ok, err = db.GetTableInsensitive(ctx, "tabletest") - require.NoError(err) - require.True(ok) + _, ok, err = db.GetTableInsensitive(ctx, "othertable") + require.NoError(err) + require.True(ok) - TestQueryWithContext(t, ctx, e, harness, "DROP TABLE IF EXISTS othertable, tabletest", []sql.Row{{types.NewOkResult(0)}}, nil, nil) + _, ok, err = db.GetTableInsensitive(ctx, "tabletest") + require.NoError(err) + require.True(ok) - _, ok, err = db.GetTableInsensitive(ctx, "othertable") - require.NoError(err) - require.False(ok) + TestQueryWithContext(t, ctx, e, harness, "DROP TABLE IF EXISTS othertable, tabletest", []sql.Row{{types.NewOkResult(0)}}, nil, nil) - _, ok, err = db.GetTableInsensitive(ctx, "tabletest") - require.NoError(err) - require.False(ok) + _, ok, err = db.GetTableInsensitive(ctx, "othertable") + require.NoError(err) + require.False(ok) - _, _, err = e.Query(NewContext(harness), "DROP TABLE not_exist") - require.Error(err) + _, ok, err = db.GetTableInsensitive(ctx, "tabletest") + require.NoError(err) + require.False(ok) - _, _, err = e.Query(NewContext(harness), "DROP TABLE IF EXISTS not_exist") - require.NoError(err) + _, _, err = e.Query(NewContext(harness), "DROP TABLE not_exist") + require.Error(err) + + _, _, err = e.Query(NewContext(harness), "DROP TABLE IF EXISTS not_exist") + require.NoError(err) + }() t.Run("no database selected", func(t *testing.T) { + e := mustNewEngine(t, harness) + defer e.Close() + ctx := NewContext(harness) ctx.SetCurrentDatabase("") + db, err := e.Analyzer.Catalog.Database(ctx, "mydb") + require.NoError(err) + RunQuery(t, e, harness, "CREATE DATABASE otherdb") otherdb, err := e.Analyzer.Catalog.Database(ctx, "otherdb") TestQueryWithContext(t, ctx, e, harness, "DROP TABLE mydb.one_pk", []sql.Row{{types.NewOkResult(0)}}, nil, nil) - _, ok, err = db.GetTableInsensitive(ctx, "mydb.one_pk") + _, ok, err := db.GetTableInsensitive(ctx, "mydb.one_pk") require.NoError(err) require.False(ok) @@ -2085,9 +2094,15 @@ func TestDropTable(t *testing.T, harness Harness) { }) t.Run("cur database selected, drop tables in other db", func(t *testing.T) { + e := mustNewEngine(t, harness) + defer e.Close() + ctx := NewContext(harness) ctx.SetCurrentDatabase("mydb") + db, err := e.Analyzer.Catalog.Database(ctx, "mydb") + require.NoError(err) + RunQuery(t, e, harness, "DROP DATABASE IF EXISTS otherdb") RunQuery(t, e, harness, "CREATE DATABASE otherdb") otherdb, err := e.Analyzer.Catalog.Database(ctx, "otherdb") @@ -2099,7 +2114,7 @@ func TestDropTable(t *testing.T, harness Harness) { _, _, err = e.Query(ctx, "DROP TABLE otherdb.tab1") require.NoError(err) - _, ok, err = db.GetTableInsensitive(ctx, "tab1") + _, ok, err := db.GetTableInsensitive(ctx, "tab1") require.NoError(err) require.True(ok) @@ -3006,10 +3021,11 @@ func TestPkOrdinalsDML(t *testing.T, harness Harness) { func TestDropDatabase(t *testing.T, harness Harness) { harness.Setup(setup.MydbData) - e := mustNewEngine(t, harness) - defer e.Close() - ctx := NewContext(harness) t.Run("DROP DATABASE correctly works", func(t *testing.T) { + e := mustNewEngine(t, harness) + defer e.Close() + ctx := NewContext(harness) + TestQueryWithContext(t, ctx, e, harness, "DROP DATABASE mydb", []sql.Row{{types.OkResult{RowsAffected: 1}}}, nil, nil) _, err := e.Analyzer.Catalog.Database(NewContext(harness), "mydb") @@ -3022,6 +3038,7 @@ func TestDropDatabase(t *testing.T, harness Harness) { t.Run("DROP DATABASE works on newly created databases.", func(t *testing.T) { e := mustNewEngine(t, harness) defer e.Close() + ctx := NewContext(harness) TestQueryWithContext(t, ctx, e, harness, "CREATE DATABASE testdb", []sql.Row{{types.OkResult{RowsAffected: 1}}}, nil, nil) _, err := e.Analyzer.Catalog.Database(NewContext(harness), "testdb") @@ -3034,6 +3051,7 @@ func TestDropDatabase(t *testing.T, harness Harness) { t.Run("DROP DATABASE works on current database and sets current database to empty.", func(t *testing.T) { e := mustNewEngine(t, harness) defer e.Close() + ctx := NewContext(harness) TestQueryWithContext(t, ctx, e, harness, "CREATE DATABASE testdb", []sql.Row{{types.OkResult{RowsAffected: 1}}}, nil, nil) RunQueryWithContext(t, e, harness, ctx, "USE TESTdb") @@ -3048,6 +3066,7 @@ func TestDropDatabase(t *testing.T, harness Harness) { t.Run("DROP SCHEMA works on newly created databases.", func(t *testing.T) { e := mustNewEngine(t, harness) defer e.Close() + ctx := NewContext(harness) TestQueryWithContext(t, ctx, e, harness, "CREATE SCHEMA testdb", []sql.Row{{types.OkResult{RowsAffected: 1}}}, nil, nil) _, err := e.Analyzer.Catalog.Database(NewContext(harness), "testdb") From 72e75b29250802737b8a4da02292c4d2a403bd10 Mon Sep 17 00:00:00 2001 From: reltuk Date: Fri, 17 Feb 2023 22:19:13 +0000 Subject: [PATCH 2/2] [ga-format-pr] Run ./format_repo.sh to fix formatting --- driver/e2e_test.go | 3 ++- driver/helpers_test.go | 3 ++- enginetest/enginetests.go | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/driver/e2e_test.go b/driver/e2e_test.go index 15711c5a4e..d151505bd7 100644 --- a/driver/e2e_test.go +++ b/driver/e2e_test.go @@ -4,9 +4,10 @@ import ( "testing" "time" - "github.com/dolthub/go-mysql-server/sql/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/dolthub/go-mysql-server/sql/types" ) func TestQuery(t *testing.T) { diff --git a/driver/helpers_test.go b/driver/helpers_test.go index f5ed9f09ac..8c534d3bc3 100644 --- a/driver/helpers_test.go +++ b/driver/helpers_test.go @@ -6,8 +6,9 @@ import ( "sync" "testing" - "github.com/dolthub/go-mysql-server/driver" "github.com/stretchr/testify/require" + + "github.com/dolthub/go-mysql-server/driver" ) var driverMu sync.Mutex diff --git a/enginetest/enginetests.go b/enginetest/enginetests.go index 5d86cb3b38..3e2af361f9 100644 --- a/enginetest/enginetests.go +++ b/enginetest/enginetests.go @@ -2035,7 +2035,7 @@ func TestDropTable(t *testing.T, harness Harness) { }() t.Run("no database selected", func(t *testing.T) { - e := mustNewEngine(t, harness) + e := mustNewEngine(t, harness) defer e.Close() ctx := NewContext(harness) @@ -2094,7 +2094,7 @@ func TestDropTable(t *testing.T, harness Harness) { }) t.Run("cur database selected, drop tables in other db", func(t *testing.T) { - e := mustNewEngine(t, harness) + e := mustNewEngine(t, harness) defer e.Close() ctx := NewContext(harness)