From 3b83192fa50fbefb74aa8940fd39a8db07027ca2 Mon Sep 17 00:00:00 2001 From: Han Fei Date: Mon, 11 Sep 2017 18:25:37 +0800 Subject: [PATCH] session_test: move out test of affect_rows (#4492) --- new_session_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++++ session_test.go | 61 --------------------------------------------- 2 files changed, 61 insertions(+), 61 deletions(-) diff --git a/new_session_test.go b/new_session_test.go index 3eb31c6923..f5a18c9eb1 100644 --- a/new_session_test.go +++ b/new_session_test.go @@ -23,6 +23,7 @@ import ( . "github.com/pingcap/check" "github.com/pingcap/tidb" "github.com/pingcap/tidb/kv" + "github.com/pingcap/tidb/mysql" "github.com/pingcap/tidb/store/tikv" "github.com/pingcap/tidb/store/tikv/mock-tikv" "github.com/pingcap/tidb/util/testkit" @@ -88,3 +89,63 @@ func (s *testSessionSuite) TestErrorRollback(c *C) { wg.Wait() tk.MustQuery("select c2 from t_rollback where c1 = 0").Check(testkit.Rows(fmt.Sprint(cnt * num))) } + +func (s *testSessionSuite) TestAffectedRows(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 t(id TEXT)") + tk.MustExec(`INSERT INTO t VALUES ("a");`) + c.Assert(int(tk.Se.AffectedRows()), Equals, 1) + tk.MustExec(`INSERT INTO t VALUES ("b");`) + c.Assert(int(tk.Se.AffectedRows()), Equals, 1) + tk.MustExec(`UPDATE t set id = 'c' where id = 'a';`) + c.Assert(int(tk.Se.AffectedRows()), Equals, 1) + tk.MustExec(`UPDATE t set id = 'a' where id = 'a';`) + c.Assert(int(tk.Se.AffectedRows()), Equals, 0) + tk.MustQuery(`SELECT * from t`).Check(testkit.Rows("c", "b")) + c.Assert(int(tk.Se.AffectedRows()), Equals, 0) + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (id int, data int)") + tk.MustExec(`INSERT INTO t VALUES (1, 0), (0, 0), (1, 1);`) + tk.MustExec(`UPDATE t set id = 1 where data = 0;`) + c.Assert(int(tk.Se.AffectedRows()), Equals, 1) + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (id int, c1 timestamp);") + tk.MustExec(`insert t values(1, 0);`) + tk.MustExec(`UPDATE t set id = 1 where id = 1;`) + c.Assert(int(tk.Se.AffectedRows()), Equals, 0) + + // With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, + // 2 if an existing row is updated, and 0 if an existing row is set to its current values. + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (c1 int PRIMARY KEY, c2 int);") + tk.MustExec(`insert t values(1, 1);`) + tk.MustExec(`insert into t values (1, 1) on duplicate key update c2=2;`) + c.Assert(int(tk.Se.AffectedRows()), Equals, 2) + tk.MustExec(`insert into t values (1, 1) on duplicate key update c2=2;`) + c.Assert(int(tk.Se.AffectedRows()), Equals, 0) + tk.MustExec("drop table if exists test") + createSQL := `CREATE TABLE test ( + id VARCHAR(36) PRIMARY KEY NOT NULL, + factor INTEGER NOT NULL DEFAULT 2);` + tk.MustExec(createSQL) + insertSQL := `INSERT INTO test(id) VALUES('id') ON DUPLICATE KEY UPDATE factor=factor+3;` + tk.MustExec(insertSQL) + c.Assert(int(tk.Se.AffectedRows()), Equals, 1) + tk.MustExec(insertSQL) + c.Assert(int(tk.Se.AffectedRows()), Equals, 2) + tk.MustExec(insertSQL) + c.Assert(int(tk.Se.AffectedRows()), Equals, 2) + + tk.Se.SetClientCapability(mysql.ClientFoundRows) + tk.MustExec("drop table if exists t") + tk.MustExec("create table t (id int, data int)") + tk.MustExec(`INSERT INTO t VALUES (1, 0), (0, 0), (1, 1);`) + tk.MustExec(`UPDATE t set id = 1 where data = 0;`) + c.Assert(int(tk.Se.AffectedRows()), Equals, 2) +} diff --git a/session_test.go b/session_test.go index 2d339836a7..414e085aa8 100644 --- a/session_test.go +++ b/session_test.go @@ -174,67 +174,6 @@ func (s *testSessionSuite) TestPrepare(c *C) { mustExecSQL(c, se, dropDBSQL) } -func (s *testSessionSuite) TestAffectedRows(c *C) { - defer testleak.AfterTest(c)() - dbName := "test_affect_rows" - dropDBSQL := fmt.Sprintf("drop database %s;", dbName) - se := newSession(c, s.store, dbName) - mustExecSQL(c, se, s.dropTableSQL) - mustExecSQL(c, se, s.createTableSQL) - mustExecSQL(c, se, `INSERT INTO t VALUES ("a");`) - c.Assert(int(se.AffectedRows()), Equals, 1) - mustExecSQL(c, se, `INSERT INTO t VALUES ("b");`) - c.Assert(int(se.AffectedRows()), Equals, 1) - mustExecSQL(c, se, `UPDATE t set id = 'c' where id = 'a';`) - c.Assert(int(se.AffectedRows()), Equals, 1) - mustExecSQL(c, se, `UPDATE t set id = 'a' where id = 'a';`) - c.Assert(int(se.AffectedRows()), Equals, 0) - mustExecSQL(c, se, `SELECT * from t;`) - c.Assert(int(se.AffectedRows()), Equals, 0) - - mustExecSQL(c, se, s.dropTableSQL) - mustExecSQL(c, se, "create table t (id int, data int)") - mustExecSQL(c, se, `INSERT INTO t VALUES (1, 0), (0, 0), (1, 1);`) - mustExecSQL(c, se, `UPDATE t set id = 1 where data = 0;`) - c.Assert(int(se.AffectedRows()), Equals, 1) - - mustExecSQL(c, se, s.dropTableSQL) - mustExecSQL(c, se, "create table t (id int, c1 timestamp);") - mustExecSQL(c, se, `insert t values(1, 0);`) - mustExecSQL(c, se, `UPDATE t set id = 1 where id = 1;`) - c.Assert(int(se.AffectedRows()), Equals, 0) - - // With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, - // 2 if an existing row is updated, and 0 if an existing row is set to its current values. - mustExecSQL(c, se, s.dropTableSQL) - mustExecSQL(c, se, "create table t (c1 int PRIMARY KEY, c2 int);") - mustExecSQL(c, se, `insert t values(1, 1);`) - mustExecSQL(c, se, `insert into t values (1, 1) on duplicate key update c2=2;`) - c.Assert(int(se.AffectedRows()), Equals, 2) - mustExecSQL(c, se, `insert into t values (1, 1) on duplicate key update c2=2;`) - c.Assert(int(se.AffectedRows()), Equals, 0) - createSQL := `CREATE TABLE IF NOT EXISTS test ( - id VARCHAR(36) PRIMARY KEY NOT NULL, - factor INTEGER NOT NULL DEFAULT 2);` - mustExecSQL(c, se, createSQL) - insertSQL := `INSERT INTO test(id) VALUES('id') ON DUPLICATE KEY UPDATE factor=factor+3;` - mustExecSQL(c, se, insertSQL) - c.Assert(int(se.AffectedRows()), Equals, 1) - mustExecSQL(c, se, insertSQL) - c.Assert(int(se.AffectedRows()), Equals, 2) - mustExecSQL(c, se, insertSQL) - c.Assert(int(se.AffectedRows()), Equals, 2) - - se.SetClientCapability(mysql.ClientFoundRows) - mustExecSQL(c, se, s.dropTableSQL) - mustExecSQL(c, se, "create table t (id int, data int)") - mustExecSQL(c, se, `INSERT INTO t VALUES (1, 0), (0, 0), (1, 1);`) - mustExecSQL(c, se, `UPDATE t set id = 1 where data = 0;`) - c.Assert(int(se.AffectedRows()), Equals, 2) - - sessionExec(c, se, dropDBSQL) -} - func (s *testSessionSuite) TestString(c *C) { defer testleak.AfterTest(c)() se := newSession(c, s.store, s.dbName)