Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor_test: clean enviroment for each test. #2025

Merged
merged 8 commits into from
Nov 18, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func (s *testSuite) TestAggregation(c *C) {
tk.MustExec("insert into t2 values(22, 2), (3, 12), (38, 98)")
result = tk.MustQuery("SELECT COALESCE ( + 1, cor0.col0 ) + - CAST( NULL AS DECIMAL ) FROM t2, t1 AS cor0, t2 AS cor1 GROUP BY cor0.col1")
result.Check(testkit.Rows("<nil>", "<nil>"))
tk.MustExec("drop table t, t1, t2")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use defer statement?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any benifit of using defer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean, and you will never forget to clean up table.

}

func (s *testSuite) TestStreamAgg(c *C) {
Expand Down
54 changes: 53 additions & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ func (s *testSuite) TestAdmin(c *C) {
c.Assert(err, IsNil)
r, err = tk.Exec("admin check table admin_test")
c.Assert(err, NotNil)
tk.MustExec("drop table admin_test")
tk.MustExec("drop table admin_test1")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be written in one statement.

}

func (s *testSuite) fillData(tk *testkit.TestKit, table string) {
Expand Down Expand Up @@ -311,6 +313,7 @@ func (s *testSuite) TestSelectOrderBy(c *C) {
r.Check(testkit.Rows("0", "-1", "-2"))
r = tk.MustQuery("select t.d from t order by d;")
r.Check(testkit.Rows("1", "2", "3"))
tk.MustExec("drop table t")
}

func (s *testSuite) TestSelectDistinct(c *C) {
Expand Down Expand Up @@ -401,6 +404,7 @@ func (s *testSuite) TestIssue345(c *C) {
c.Assert(err, NotNil)

tk.MustExec("commit")
tk.MustExec(`drop table t1, t2`)
}

func (s *testSuite) TestUnion(c *C) {
Expand Down Expand Up @@ -456,6 +460,7 @@ func (s *testSuite) TestUnion(c *C) {
r.Check(testkit.Rows("abc", "1"))

tk.MustExec("commit")
tk.MustExec("drop table union_test")
}

func (s *testSuite) TestIn(c *C) {
Expand All @@ -473,6 +478,7 @@ func (s *testSuite) TestIn(c *C) {

queryStr = `select c2 from t where c1 in ('7a')`
tk.MustQuery(queryStr).Check(testkit.Rows("7"))
tk.MustExec(`drop table t`)
}

func (s *testSuite) TestTablePKisHandleScan(c *C) {
Expand Down Expand Up @@ -536,6 +542,7 @@ func (s *testSuite) TestTablePKisHandleScan(c *C) {
result := tk.MustQuery(ca.sql)
result.Check(ca.result)
}
tk.MustExec(`drop table t`)
}

func (s *testSuite) TestJoin(c *C) {
Expand Down Expand Up @@ -628,14 +635,17 @@ func (s *testSuite) TestJoin(c *C) {
_, err = tk.Exec("select * from t right join t1 on 1")
c.Check(plan.ErrCartesianProductUnsupported.Equal(err), IsTrue)
plan.AllowCartesianProduct = true
tk.MustExec("drop table t")
tk.MustExec("drop table t1")
tk.MustExec("drop table t2")
tk.MustExec("drop table t3")

}

func (s *testSuite) TestMultiJoin(c *C) {
defer testleak.AfterTest(c)()
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t35(a35 int primary key, b35 int, x35 int)")
tk.MustExec("create table t40(a40 int primary key, b40 int, x40 int)")
tk.MustExec("create table t14(a14 int primary key, b14 int, x14 int)")
Expand Down Expand Up @@ -723,6 +733,27 @@ AND a62=b18
AND b4=a37
AND b44=a42`)
result.Check(testkit.Rows("7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7"))
tk.MustExec("drop table t15")
tk.MustExec("drop table t14")
tk.MustExec("drop table t40")
tk.MustExec("drop table t35")
tk.MustExec("drop table t42")
tk.MustExec("drop table t7")
tk.MustExec("drop table t64")
tk.MustExec("drop table t19")
tk.MustExec("drop table t9")
tk.MustExec("drop table t8")
tk.MustExec("drop table t57")
tk.MustExec("drop table t37")
tk.MustExec("drop table t44")
tk.MustExec("drop table t38")
tk.MustExec("drop table t18")
tk.MustExec("drop table t62")
tk.MustExec("drop table t4")
tk.MustExec("drop table t48")
tk.MustExec("drop table t31")
tk.MustExec("drop table t16")
tk.MustExec("drop table t12")
}

func (s *testSuite) TestIndexScan(c *C) {
Expand Down Expand Up @@ -769,6 +800,8 @@ func (s *testSuite) TestIndexScan(c *C) {
tk.MustExec("INSERT INTO tab1 VALUES(1,37,20.85,30,10.69)")
result = tk.MustQuery("SELECT pk FROM tab1 WHERE ((col3 <= 6 OR col3 < 29 AND (col0 < 41)) OR col3 > 42) AND col1 >= 96.1 AND col3 = 30 AND col3 > 17 AND (col0 BETWEEN 36 AND 42)")
result.Check(testkit.Rows())
tk.MustExec("drop table t")
tk.MustExec("drop table tab1")
}

func (s *testSuite) TestSubquerySameTable(c *C) {
Expand All @@ -782,6 +815,7 @@ func (s *testSuite) TestSubquerySameTable(c *C) {
result.Check(testkit.Rows("2"))
result = tk.MustQuery("select a from t where not exists(select 1 from t as x where x.a < t.a)")
result.Check(testkit.Rows("1"))
tk.MustExec("drop table t")
}

func (s *testSuite) TestIndexReverseOrder(c *C) {
Expand All @@ -801,6 +835,7 @@ func (s *testSuite) TestIndexReverseOrder(c *C) {
tk.MustExec("insert t values (0, 2), (1, 2), (2, 2), (0, 1), (1, 1), (2, 1), (0, 0), (1, 0), (2, 0)")
result = tk.MustQuery("select b, a from t order by b, a desc")
result.Check(testkit.Rows("0 2", "0 1", "0 0", "1 2", "1 1", "1 0", "2 2", "2 1", "2 0"))
tk.MustExec("drop table t")
}

func (s *testSuite) TestTableReverseOrder(c *C) {
Expand All @@ -814,6 +849,7 @@ func (s *testSuite) TestTableReverseOrder(c *C) {
result.Check(testkit.Rows("9", "8", "7", "6", "5", "4", "3", "2", "1"))
result = tk.MustQuery("select a from t where a <3 or (a >=6 and a < 8) order by a desc")
result.Check(testkit.Rows("7", "6", "2", "1"))
tk.MustExec("drop table t")
}

func (s *testSuite) TestInSubquery(c *C) {
Expand Down Expand Up @@ -844,6 +880,8 @@ func (s *testSuite) TestInSubquery(c *C) {
tk.MustExec("create table t1 (a float)")
tk.MustExec("insert t1 values (281.37)")
tk.MustQuery("select a from t1 where (a in (select a from t1))").Check(testkit.Rows("281.37"))
tk.MustExec("drop table t")
tk.MustExec("drop table t1")
}

func (s *testSuite) TestDefaultNull(c *C) {
Expand All @@ -861,6 +899,7 @@ func (s *testSuite) TestDefaultNull(c *C) {
tk.MustExec("delete from t where a = 1")
tk.MustExec("insert t (a) values (1)")
tk.MustQuery("select * from t").Check(testkit.Rows("1 1 <nil>"))
tk.MustExec("drop table t")
}

func (s *testSuite) TestUnsignedPKColumn(c *C) {
Expand All @@ -875,6 +914,7 @@ func (s *testSuite) TestUnsignedPKColumn(c *C) {
tk.MustExec("update t set c=2 where a=1;")
result = tk.MustQuery("select * from t where b=1;")
result.Check(testkit.Rows("1 1 2"))
tk.MustExec("drop table t")
}

func (s *testSuite) TestBuiltin(c *C) {
Expand Down Expand Up @@ -1019,6 +1059,7 @@ func (s *testSuite) TestBuiltin(c *C) {
{".*", "abcd", 1},
}
patternMatching(c, tk, "regexp", testCases)
tk.MustExec("drop table t")
}

func (s *testSuite) TestToPBExpr(c *C) {
Expand Down Expand Up @@ -1071,6 +1112,7 @@ func (s *testSuite) TestToPBExpr(c *C) {
result.Check(testkit.Rows("1"))
result = tk.MustQuery("select * from t where not(a != 1 and a != 2)")
result.Check(testkit.Rows("1", "2"))
tk.MustExec("drop table t")
}

func (s *testSuite) TestDatumXAPI(c *C) {
Expand All @@ -1097,6 +1139,7 @@ func (s *testSuite) TestDatumXAPI(c *C) {
result.Check(testkit.Rows("11:11:12 11:11:12", "11:11:13 11:11:13"))
result = tk.MustQuery("select * from t where b > '11:11:11.5'")
result.Check(testkit.Rows("11:11:12 11:11:12", "11:11:13 11:11:13"))
tk.MustExec("drop table t")
}

func (s *testSuite) TestJoinPanic(c *C) {
Expand All @@ -1106,6 +1149,7 @@ func (s *testSuite) TestJoinPanic(c *C) {
tk.MustExec("drop table if exists events")
tk.MustExec("create table events (clock int, source int)")
tk.MustQuery("SELECT * FROM events e JOIN (SELECT MAX(clock) AS clock FROM events e2 GROUP BY e2.source) e3 ON e3.clock=e.clock")
tk.MustExec("drop table events")
}

func (s *testSuite) TestSQLMode(c *C) {
Expand Down Expand Up @@ -1140,6 +1184,7 @@ func (s *testSuite) TestSQLMode(c *C) {
c.Check(err, NotNil)
// Restore original global strict mode.
tk.MustExec("set @@global.sql_mode = 'STRICT_TRANS_TABLES'")
tk.MustExec("drop table t")
}

func (s *testSuite) TestNewSubquery(c *C) {
Expand Down Expand Up @@ -1198,6 +1243,9 @@ func (s *testSuite) TestNewSubquery(c *C) {

result = tk.MustQuery("select * from a b where c = (select d from b a where a.c = 2 and b.c = 1)")
result.Check(testkit.Rows("1 2"))
tk.MustExec("drop table t")
tk.MustExec("drop table a")
tk.MustExec("drop table b")
}

func (s *testSuite) TestNewTableDual(c *C) {
Expand Down Expand Up @@ -1273,6 +1321,7 @@ func (s *testSuite) TestRow(c *C) {
result.Check(testkit.Rows("1 1"))
result = tk.MustQuery("select * from t where (c, d) = (select * from t k where (t.c,t.d) = (c,d))")
result.Check(testkit.Rows("1 1", "1 3", "2 1", "2 3"))
tk.MustExec("drop table t")
}

func (s *testSuite) TestColumnName(c *C) {
Expand Down Expand Up @@ -1302,6 +1351,7 @@ func (s *testSuite) TestColumnName(c *C) {
c.Check(len(fields), Equals, 2)
c.Check(fields[0].Column.Name.L, Equals, "d")
c.Check(fields[1].Column.Name.L, Equals, "c")
tk.MustExec("drop table t")
}

func (s *testSuite) TestSelectVar(c *C) {
Expand All @@ -1314,6 +1364,7 @@ func (s *testSuite) TestSelectVar(c *C) {
result.Check(testkit.Rows("<nil> 2", "<nil> 3", "<nil> 2"))
result = tk.MustQuery("select @a, @a := d+1 from t")
result.Check(testkit.Rows("2 2", "2 3", "3 2"))
tk.MustExec("drop table t")

}

Expand Down Expand Up @@ -1359,4 +1410,5 @@ func (s *testSuite) TestHistoryRead(c *C) {
tk.MustQuery("select * from history_read order by a").Check(testkit.Rows("2", "4"))
tk.MustExec("set @@tidb_snapshot = ''")
tk.MustQuery("select * from history_read order by a").Check(testkit.Rows("2 <nil>", "4 <nil>", "8 8", "9 9"))
tk.MustExec("drop table history_read")
}
11 changes: 11 additions & 0 deletions executor/executor_write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (s *testSuite) TestInsert(c *C) {
c.Assert(types.ErrDataTooLong.Equal(err), IsTrue)
_, err = tk.Exec("insert insert_err values (1, '你好,世界')")
c.Assert(err, IsNil)
tk.MustExec("drop table insert_test, insert_test_1, insert_test_2, insert_err")
}

func (s *testSuite) TestInsertAutoInc(c *C) {
Expand Down Expand Up @@ -189,6 +190,7 @@ func (s *testSuite) TestInsertAutoInc(c *C) {
r = tk.MustQuery("select * from insert_autoinc_test;")
rowStr6 = fmt.Sprintf("%v %v", "6", "6")
r.Check(testkit.Rows(rowStr3, rowStr1, rowStr2, rowStr4, rowStr5, rowStr6))
tk.MustExec("drop table insert_autoinc_test")
}

func (s *testSuite) TestInsertIgnore(c *C) {
Expand All @@ -211,6 +213,7 @@ func (s *testSuite) TestInsertIgnore(c *C) {
rowStr = fmt.Sprintf("%v %v", "1", "2")
rowStr1 := fmt.Sprintf("%v %v", "2", "3")
r.Check(testkit.Rows(rowStr, rowStr1))
tk.MustExec("drop table t")
}

func (s *testSuite) TestReplace(c *C) {
Expand Down Expand Up @@ -333,6 +336,8 @@ func (s *testSuite) TestReplace(c *C) {
c.Assert(int64(tk.Se.AffectedRows()), Equals, int64(3))
r = tk.MustQuery("select * from tIssue1012;")
r.Check(testkit.Rows("1 1"))
tk.MustExec("drop table replace_test, replace_test_1, replace_test_2, replace_test_3, replace_test_4, replace_test_5")
tk.MustExec("drop table tIssue989, tIssue1012")
}

func (s *testSuite) TestUpdate(c *C) {
Expand Down Expand Up @@ -461,6 +466,8 @@ func (s *testSuite) TestMultipleTableUpdate(c *C) {

r = tk.MustQuery("select * from t1")
r.Check(testkit.Rows("10", "10"))
tk.MustExec("drop table items, month")
tk.MustExec("drop table t1, t2")
}

func (s *testSuite) TestDelete(c *C) {
Expand Down Expand Up @@ -491,6 +498,7 @@ func (s *testSuite) TestDelete(c *C) {

tk.MustExec(`delete from delete_test ;`)
tk.CheckExecResult(1, 0)
tk.MustExec("drop table delete_test")
}

func (s *testSuite) fillDataMultiTable(tk *testkit.TestKit) {
Expand Down Expand Up @@ -521,6 +529,7 @@ func (s *testSuite) TestMultiTableDelete(c *C) {
// Select data
r := tk.MustQuery("select * from t3")
c.Assert(r.Rows(), HasLen, 3)
tk.MustExec("drop table t1,t2,t3")
}

func (s *testSuite) TestQualifiedDelete(c *C) {
Expand Down Expand Up @@ -739,6 +748,7 @@ func (s *testSuite) TestLoadData(c *C) {
fmt.Sprintf("%v %v %v %v", 40, 0, []byte(""), 0)}, []byte("xxx")},
}
checkCases(cases, ld, c, tk, ctx, selectSQL, deleteSQL)
tk.MustExec("drop table load_data_test")
}

func (s *testSuite) TestLoadDataEscape(c *C) {
Expand All @@ -762,6 +772,7 @@ func (s *testSuite) TestLoadDataEscape(c *C) {
deleteSQL := "delete from load_data_test"
selectSQL := "select * from load_data_test;"
checkCases(cases, ld, c, tk, ctx, selectSQL, deleteSQL)
tk.MustExec("drop table load_data_test")
}

func makeLoadDataInfo(column int, ctx context.Context, c *C) (ld *executor.LoadDataInfo) {
Expand Down
2 changes: 2 additions & 0 deletions executor/explain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,6 @@ func (s *testSuite) TestExplain(c *C) {
result := tk.MustQuery("explain " + ca.sql)
result.Check(testkit.Rows("EXPLAIN " + ca.result))
}
tk.MustExec("drop table t1")
tk.MustExec("drop table t2")
}