From 943fff9d91459988ea2831ff2bc56f51c188e764 Mon Sep 17 00:00:00 2001 From: oldme Date: Thu, 26 Sep 2024 17:04:48 +0800 Subject: [PATCH 1/5] test --- .../mssql/mssql_z_unit_transaction_test.go | 1141 +++++++++++++++++ 1 file changed, 1141 insertions(+) create mode 100644 contrib/drivers/mssql/mssql_z_unit_transaction_test.go diff --git a/contrib/drivers/mssql/mssql_z_unit_transaction_test.go b/contrib/drivers/mssql/mssql_z_unit_transaction_test.go new file mode 100644 index 00000000000..e57adbb8f58 --- /dev/null +++ b/contrib/drivers/mssql/mssql_z_unit_transaction_test.go @@ -0,0 +1,1141 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package mssql_test + +import ( + "context" + "fmt" + "testing" + + "github.com/gogf/gf/v2/database/gdb" + "github.com/gogf/gf/v2/errors/gerror" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gctx" + "github.com/gogf/gf/v2/os/gtime" + "github.com/gogf/gf/v2/test/gtest" +) + +func Test_TX_Query(t *testing.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if _, err = tx.Query("SELECT ?", 1); err != nil { + gtest.Error(err) + } + if _, err = tx.Query("SELECT ?+?", 1, 2); err != nil { + gtest.Error(err) + } + if _, err = tx.Query("SELECT ?+?", g.Slice{1, 2}); err != nil { + gtest.Error(err) + } + if _, err = tx.Query("ERROR"); err == nil { + gtest.Error("FAIL") + } + if err = tx.Commit(); err != nil { + gtest.Error(err) + } +} + +func Test_TX_Exec(t *testing.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if _, err := tx.Exec("SELECT ?", 1); err != nil { + gtest.Error(err) + } + if _, err := tx.Exec("SELECT ?+?", 1, 2); err != nil { + gtest.Error(err) + } + if _, err := tx.Exec("SELECT ?+?", g.Slice{1, 2}); err != nil { + gtest.Error(err) + } + if _, err := tx.Exec("ERROR"); err == nil { + gtest.Error("FAIL") + } + if err := tx.Commit(); err != nil { + gtest.Error(err) + } +} + +func Test_TX_Commit(t *testing.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if err := tx.Commit(); err != nil { + gtest.Error(err) + } +} + +func Test_TX_Rollback(t *testing.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if err := tx.Rollback(); err != nil { + gtest.Error(err) + } +} + +func Test_TX_Prepare(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + t.AssertNil(err) + + st, err := tx.Prepare("SELECT 100") + t.AssertNil(err) + + rows, err := st.Query() + t.AssertNil(err) + + array, err := rows.Columns() + t.AssertNil(err) + t.Assert(array[0], "100") + + rows.Close() + t.AssertNil(err) + + tx.Commit() + t.AssertNil(err) + }) +} + +func Test_TX_Insert(t *testing.T) { + table := createTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + user := tx.Model(table) + if _, err := user.Data(g.Map{ + "id": 1, + "passport": "t1", + "password": "25d55ad283aa400af464c76d713c07ad", + "nickname": "T1", + "create_time": gtime.Now().String(), + }).Insert(); err != nil { + gtest.Error(err) + } + if _, err := tx.Insert(table, g.Map{ + "id": 2, + "passport": "t1", + "password": "25d55ad283aa400af464c76d713c07ad", + "nickname": "T1", + "create_time": gtime.Now().String(), + }); err != nil { + gtest.Error(err) + } + + if n, err := tx.Model(table).Count(); err != nil { + gtest.Error(err) + } else { + t.Assert(n, int64(2)) + } + + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) +} + +func Test_TX_BatchInsert(t *testing.T) { + table := createTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if _, err := tx.Insert(table, g.List{ + { + "id": 2, + "passport": "t", + "password": "25d55ad283aa400af464c76d713c07ad", + "nickname": "T2", + "create_time": gtime.Now().String(), + }, + { + "id": 3, + "passport": "t3", + "password": "25d55ad283aa400af464c76d713c07ad", + "nickname": "T3", + "create_time": gtime.Now().String(), + }, + }, 10); err != nil { + gtest.Error(err) + } + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + if n, err := db.Model(table).Count(); err != nil { + gtest.Error(err) + } else { + t.Assert(n, int64(2)) + } + }) +} + +func Test_TX_BatchReplace(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if _, err := tx.Replace(table, g.List{ + { + "id": 2, + "passport": "USER_2", + "password": "PASS_2", + "nickname": "NAME_2", + "create_time": gtime.Now().String(), + }, + { + "id": 4, + "passport": "USER_4", + "password": "PASS_4", + "nickname": "NAME_4", + "create_time": gtime.Now().String(), + }, + }, 10); err != nil { + gtest.Error(err) + } + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + if n, err := db.Model(table).Count(); err != nil { + gtest.Error(err) + } else { + t.Assert(n, int64(TableSize)) + } + if value, err := db.Model(table).Fields("password").Where("id", 2).Value(); err != nil { + gtest.Error(err) + } else { + t.Assert(value.String(), "PASS_2") + } + }) +} + +func Test_TX_BatchSave(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if _, err := tx.Save(table, g.List{ + { + "id": 4, + "passport": "USER_4", + "password": "PASS_4", + "nickname": "NAME_4", + "create_time": gtime.Now().String(), + }, + }, 10); err != nil { + gtest.Error(err) + } + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + + if n, err := db.Model(table).Count(); err != nil { + gtest.Error(err) + } else { + t.Assert(n, int64(TableSize)) + } + + if value, err := db.Model(table).Fields("password").Where("id", 4).Value(); err != nil { + gtest.Error(err) + } else { + t.Assert(value.String(), "PASS_4") + } + }) +} + +func Test_TX_Replace(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if _, err := tx.Replace(table, g.Map{ + "id": 1, + "passport": "USER_1", + "password": "PASS_1", + "nickname": "NAME_1", + "create_time": gtime.Now().String(), + }); err != nil { + gtest.Error(err) + } + if err := tx.Rollback(); err != nil { + gtest.Error(err) + } + if value, err := db.Model(table).Fields("nickname").Where("id", 1).Value(); err != nil { + gtest.Error(err) + } else { + t.Assert(value.String(), "name_1") + } + }) +} + +func Test_TX_Save(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if _, err := tx.Save(table, g.Map{ + "id": 1, + "passport": "USER_1", + "password": "PASS_1", + "nickname": "NAME_1", + "create_time": gtime.Now().String(), + }); err != nil { + gtest.Error(err) + } + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + if value, err := db.Model(table).Fields("nickname").Where("id", 1).Value(); err != nil { + gtest.Error(err) + } else { + t.Assert(value.String(), "NAME_1") + } + }) +} + +func Test_TX_Update(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if result, err := tx.Update(table, "create_time='2019-10-24 10:00:00'", "id=3"); err != nil { + gtest.Error(err) + } else { + n, _ := result.RowsAffected() + t.Assert(n, 1) + } + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + _, err = tx.Model(table).Fields("create_time").Where("id", 3).Value() + t.AssertNE(err, nil) + + if value, err := db.Model(table).Fields("create_time").Where("id", 3).Value(); err != nil { + gtest.Error(err) + } else { + t.Assert(value.String(), "2019-10-24 10:00:00") + } + }) +} + +func Test_TX_GetAll(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if result, err := tx.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1); err != nil { + gtest.Error(err) + } else { + t.Assert(len(result), 1) + } + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) +} + +func Test_TX_GetOne(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if record, err := tx.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE passport=?", table), "user_2"); err != nil { + gtest.Error(err) + } else { + if record == nil { + gtest.Error("FAIL") + } + t.Assert(record["nickname"].String(), "name_2") + } + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) +} + +func Test_TX_GetValue(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if value, err := tx.GetValue(fmt.Sprintf("SELECT id FROM %s WHERE passport=?", table), "user_3"); err != nil { + gtest.Error(err) + } else { + t.Assert(value.Int(), 3) + } + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) +} + +func Test_TX_GetCount(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if count, err := tx.GetCount("SELECT * FROM " + table); err != nil { + gtest.Error(err) + } else { + t.Assert(count, int64(TableSize)) + } + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) +} + +func Test_TX_GetStruct(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + type User struct { + Id int + Passport string + Password string + NickName string + CreateTime gtime.Time + } + user := new(User) + if err := tx.GetStruct(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3); err != nil { + gtest.Error(err) + } + t.Assert(user.NickName, "name_3") + t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + type User struct { + Id int + Passport string + Password string + NickName string + CreateTime *gtime.Time + } + user := new(User) + if err := tx.GetStruct(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3); err != nil { + gtest.Error(err) + } + t.Assert(user.NickName, "name_3") + t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) +} + +func Test_TX_GetStructs(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + type User struct { + Id int + Passport string + Password string + NickName string + CreateTime gtime.Time + } + var users []User + if err := tx.GetStructs(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>=?", table), 1); err != nil { + gtest.Error(err) + } + t.Assert(len(users), TableSize) + t.Assert(users[0].Id, 1) + t.Assert(users[1].Id, 2) + t.Assert(users[2].Id, 3) + t.Assert(users[0].NickName, "name_1") + t.Assert(users[1].NickName, "name_2") + t.Assert(users[2].NickName, "name_3") + t.Assert(users[2].CreateTime.String(), "2018-10-24 10:00:00") + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + type User struct { + Id int + Passport string + Password string + NickName string + CreateTime *gtime.Time + } + var users []User + if err := tx.GetStructs(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>=?", table), 1); err != nil { + gtest.Error(err) + } + t.Assert(len(users), TableSize) + t.Assert(users[0].Id, 1) + t.Assert(users[1].Id, 2) + t.Assert(users[2].Id, 3) + t.Assert(users[0].NickName, "name_1") + t.Assert(users[1].NickName, "name_2") + t.Assert(users[2].NickName, "name_3") + t.Assert(users[2].CreateTime.String(), "2018-10-24 10:00:00") + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) +} + +func Test_TX_GetScan(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + type User struct { + Id int + Passport string + Password string + NickName string + CreateTime gtime.Time + } + user := new(User) + if err := tx.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3); err != nil { + gtest.Error(err) + } + t.Assert(user.NickName, "name_3") + t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + type User struct { + Id int + Passport string + Password string + NickName string + CreateTime *gtime.Time + } + user := new(User) + if err := tx.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3); err != nil { + gtest.Error(err) + } + t.Assert(user.NickName, "name_3") + t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + type User struct { + Id int + Passport string + Password string + NickName string + CreateTime gtime.Time + } + var users []User + if err := tx.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>=?", table), 1); err != nil { + gtest.Error(err) + } + t.Assert(len(users), TableSize) + t.Assert(users[0].Id, 1) + t.Assert(users[1].Id, 2) + t.Assert(users[2].Id, 3) + t.Assert(users[0].NickName, "name_1") + t.Assert(users[1].NickName, "name_2") + t.Assert(users[2].NickName, "name_3") + t.Assert(users[2].CreateTime.String(), "2018-10-24 10:00:00") + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + type User struct { + Id int + Passport string + Password string + NickName string + CreateTime *gtime.Time + } + var users []User + if err := tx.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>=?", table), 1); err != nil { + gtest.Error(err) + } + t.Assert(len(users), TableSize) + t.Assert(users[0].Id, 1) + t.Assert(users[1].Id, 2) + t.Assert(users[2].Id, 3) + t.Assert(users[0].NickName, "name_1") + t.Assert(users[1].NickName, "name_2") + t.Assert(users[2].NickName, "name_3") + t.Assert(users[2].CreateTime.String(), "2018-10-24 10:00:00") + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + }) +} + +func Test_TX_Delete(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + table := createInitTable() + defer dropTable(table) + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if _, err := tx.Delete(table, 1); err != nil { + gtest.Error(err) + } + if err := tx.Commit(); err != nil { + gtest.Error(err) + } + if n, err := db.Model(table).Count(); err != nil { + gtest.Error(err) + } else { + t.Assert(n, int64(0)) + } + + t.Assert(tx.IsClosed(), true) + }) + + gtest.C(t, func(t *gtest.T) { + table := createInitTable() + defer dropTable(table) + tx, err := db.Begin(ctx) + if err != nil { + gtest.Error(err) + } + if _, err := tx.Delete(table, 1); err != nil { + gtest.Error(err) + } + if n, err := tx.Model(table).Count(); err != nil { + gtest.Error(err) + } else { + t.Assert(n, int64(0)) + } + if err := tx.Rollback(); err != nil { + gtest.Error(err) + } + if n, err := db.Model(table).Count(); err != nil { + gtest.Error(err) + } else { + t.Assert(n, int64(TableSize)) + t.AssertNE(n, int64(0)) + } + + t.Assert(tx.IsClosed(), true) + }) +} + +func Test_Transaction(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + ctx := context.TODO() + err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + if _, err := tx.Ctx(ctx).Replace(table, g.Map{ + "id": 1, + "passport": "USER_1", + "password": "PASS_1", + "nickname": "NAME_1", + "create_time": gtime.Now().String(), + }); err != nil { + t.Error(err) + } + t.Assert(tx.IsClosed(), false) + return gerror.New("error") + }) + t.AssertNE(err, nil) + + if value, err := db.Model(table).Ctx(ctx).Fields("nickname").Where("id", 1).Value(); err != nil { + gtest.Error(err) + } else { + t.Assert(value.String(), "name_1") + } + }) + + gtest.C(t, func(t *gtest.T) { + ctx := context.TODO() + err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + if _, err := tx.Replace(table, g.Map{ + "id": 1, + "passport": "USER_1", + "password": "PASS_1", + "nickname": "NAME_1", + "create_time": gtime.Now().String(), + }); err != nil { + t.Error(err) + } + return nil + }) + t.AssertNil(err) + + if value, err := db.Model(table).Fields("nickname").Where("id", 1).Value(); err != nil { + gtest.Error(err) + } else { + t.Assert(value.String(), "NAME_1") + } + }) +} + +func Test_Transaction_Panic(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + ctx := context.TODO() + err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + if _, err := tx.Replace(table, g.Map{ + "id": 1, + "passport": "USER_1", + "password": "PASS_1", + "nickname": "NAME_1", + "create_time": gtime.Now().String(), + }); err != nil { + t.Error(err) + } + panic("error") + return nil + }) + t.AssertNE(err, nil) + + if value, err := db.Model(table).Fields("nickname").Where("id", 1).Value(); err != nil { + gtest.Error(err) + } else { + t.Assert(value.String(), "name_1") + } + }) +} + +func Test_Transaction_Nested_Begin_Rollback_Commit(t *testing.T) { + table := createTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + t.AssertNil(err) + // tx begin. + err = tx.Begin() + t.AssertNil(err) + // tx rollback. + _, err = tx.Model(table).Data(g.Map{ + "id": 1, + "passport": "user_1", + "password": "pass_1", + "nickname": "name_1", + }).Insert() + err = tx.Rollback() + t.AssertNil(err) + // tx commit. + _, err = tx.Model(table).Data(g.Map{ + "id": 2, + "passport": "user_2", + "password": "pass_2", + "nickname": "name_2", + }).Insert() + err = tx.Commit() + t.AssertNil(err) + // check data. + all, err := db.Model(table).All() + t.AssertNil(err) + t.Assert(len(all), 1) + t.Assert(all[0]["id"], 2) + }) +} + +func Test_Transaction_Nested_TX_Transaction_UseTX(t *testing.T) { + table := createTable() + defer dropTable(table) + + db.SetDebug(true) + defer db.SetDebug(false) + + gtest.C(t, func(t *gtest.T) { + var ( + err error + ctx = context.TODO() + ) + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + // commit + err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + _, err = tx.Model(table).Data(g.Map{ + "id": 1, + "passport": "USER_1", + "password": "PASS_1", + "nickname": "NAME_1", + "create_time": gtime.Now().String(), + }).Insert() + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + // rollback + err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + _, err = tx.Model(table).Data(g.Map{ + "id": 2, + "passport": "USER_2", + "password": "PASS_2", + "nickname": "NAME_2", + "create_time": gtime.Now().String(), + }).Insert() + t.AssertNil(err) + panic("error") + return err + }) + t.AssertNE(err, nil) + return nil + }) + t.AssertNil(err) + + all, err := db.Ctx(ctx).Model(table).All() + t.AssertNil(err) + t.Assert(len(all), 1) + t.Assert(all[0]["id"], 1) + + // another record. + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + // commit + err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + _, err = tx.Model(table).Data(g.Map{ + "id": 3, + "passport": "USER_1", + "password": "PASS_1", + "nickname": "NAME_1", + "create_time": gtime.Now().String(), + }).Insert() + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + // rollback + err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + _, err = tx.Model(table).Data(g.Map{ + "id": 4, + "passport": "USER_2", + "password": "PASS_2", + "nickname": "NAME_2", + "create_time": gtime.Now().String(), + }).Insert() + t.AssertNil(err) + panic("error") + return err + }) + t.AssertNE(err, nil) + return nil + }) + t.AssertNil(err) + + all, err = db.Ctx(ctx).Model(table).All() + t.AssertNil(err) + t.Assert(len(all), 2) + t.Assert(all[0]["id"], 1) + t.Assert(all[1]["id"], 3) + }) +} + +func Test_Transaction_Nested_TX_Transaction_UseDB(t *testing.T) { + table := createTable() + defer dropTable(table) + + // db.SetDebug(true) + // defer db.SetDebug(false) + + gtest.C(t, func(t *gtest.T) { + var ( + err error + ctx = context.TODO() + ) + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + // commit + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + _, err = db.Model(table).Ctx(ctx).Data(g.Map{ + "id": 1, + "passport": "USER_1", + "password": "PASS_1", + "nickname": "NAME_1", + "create_time": gtime.Now().String(), + }).Insert() + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + + // rollback + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + _, err = tx.Model(table).Ctx(ctx).Data(g.Map{ + "id": 2, + "passport": "USER_2", + "password": "PASS_2", + "nickname": "NAME_2", + "create_time": gtime.Now().String(), + }).Insert() + t.AssertNil(err) + // panic makes this transaction rollback. + panic("error") + return err + }) + t.AssertNE(err, nil) + return nil + }) + t.AssertNil(err) + all, err := db.Model(table).All() + t.AssertNil(err) + t.Assert(len(all), 1) + t.Assert(all[0]["id"], 1) + + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + // commit + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + _, err = db.Model(table).Ctx(ctx).Data(g.Map{ + "id": 3, + "passport": "USER_1", + "password": "PASS_1", + "nickname": "NAME_1", + "create_time": gtime.Now().String(), + }).Insert() + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + return err + }) + t.AssertNil(err) + + // rollback + err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { + _, err = tx.Model(table).Ctx(ctx).Data(g.Map{ + "id": 4, + "passport": "USER_2", + "password": "PASS_2", + "nickname": "NAME_2", + "create_time": gtime.Now().String(), + }).Insert() + t.AssertNil(err) + // panic makes this transaction rollback. + panic("error") + return err + }) + t.AssertNE(err, nil) + return nil + }) + t.AssertNil(err) + + all, err = db.Model(table).All() + t.AssertNil(err) + t.Assert(len(all), 2) + t.Assert(all[0]["id"], 1) + t.Assert(all[1]["id"], 3) + }) +} + +func Test_Transaction_Nested_SavePoint_RollbackTo(t *testing.T) { + table := createTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + t.AssertNil(err) + // tx save point. + _, err = tx.Model(table).Data(g.Map{ + "id": 1, + "passport": "user_1", + "password": "pass_1", + "nickname": "name_1", + }).Insert() + err = tx.SavePoint("MyPoint") + t.AssertNil(err) + _, err = tx.Model(table).Data(g.Map{ + "id": 2, + "passport": "user_2", + "password": "pass_2", + "nickname": "name_2", + }).Insert() + // tx rollback to. + err = tx.RollbackTo("MyPoint") + t.AssertNil(err) + // tx commit. + err = tx.Commit() + t.AssertNil(err) + + // check data. + all, err := db.Model(table).All() + t.AssertNil(err) + t.Assert(len(all), 1) + t.Assert(all[0]["id"], 1) + }) +} + +func Test_Transaction_Method(t *testing.T) { + table := createTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + var err error + err = db.Transaction(gctx.New(), func(ctx context.Context, tx gdb.TX) error { + _, err = db.Model(table).Ctx(ctx).Data(g.Map{ + "id": 1, + "passport": "t1", + "password": "25d55ad283aa400af464c76d713c07ad", + "nickname": "T1", + "create_time": gtime.Now().String(), + }).Insert() + t.AssertNil(err) + + _, err = db.Ctx(ctx).Exec(ctx, fmt.Sprintf( + "insert into %s(`passport`,`password`,`nickname`,`create_time`,`id`) "+ + "VALUES('t2','25d55ad283aa400af464c76d713c07ad','T2','2021-08-25 21:53:00',2) ", + table)) + t.AssertNil(err) + return gerror.New("rollback") + }) + t.AssertNE(err, nil) + + count, err := db.Model(table).Count() + t.AssertNil(err) + t.Assert(count, int64(0)) + }) +} From 70e763dedf00b6d93c8b615d281fb3d206e01f33 Mon Sep 17 00:00:00 2001 From: oldme Date: Fri, 27 Sep 2024 11:46:34 +0800 Subject: [PATCH 2/5] up --- .../drivers/mssql/mssql_z_unit_basic_test.go | 1 - .../drivers/mssql/mssql_z_unit_init_test.go | 2 +- .../mssql/mssql_z_unit_transaction_test.go | 822 +++++++----------- 3 files changed, 335 insertions(+), 490 deletions(-) diff --git a/contrib/drivers/mssql/mssql_z_unit_basic_test.go b/contrib/drivers/mssql/mssql_z_unit_basic_test.go index 40d39f988ca..a240fae917f 100644 --- a/contrib/drivers/mssql/mssql_z_unit_basic_test.go +++ b/contrib/drivers/mssql/mssql_z_unit_basic_test.go @@ -231,7 +231,6 @@ func Test_DB_Insert(t *testing.T) { one, err := db.Model(table).Where("id", 3).One() t.AssertNil(err) - fmt.Println(one) t.Assert(one["ID"].Int(), 3) t.Assert(one["PASSPORT"].String(), "user_3") t.Assert(one["PASSWORD"].String(), "25d55ad283aa400af464c76d713c07ad") diff --git a/contrib/drivers/mssql/mssql_z_unit_init_test.go b/contrib/drivers/mssql/mssql_z_unit_init_test.go index a5db58e4d4d..c7d67d079d4 100644 --- a/contrib/drivers/mssql/mssql_z_unit_init_test.go +++ b/contrib/drivers/mssql/mssql_z_unit_init_test.go @@ -126,7 +126,7 @@ func createInitTable(table ...string) (name string) { "passport": fmt.Sprintf(`user_%d`, i), "password": fmt.Sprintf(`pass_%d`, i), "nickname": fmt.Sprintf(`name_%d`, i), - "create_time": gtime.Now(), + "create_time": "2018-10-24 10:00:00", }) } result, err := db.Insert(context.Background(), name, array.Slice()) diff --git a/contrib/drivers/mssql/mssql_z_unit_transaction_test.go b/contrib/drivers/mssql/mssql_z_unit_transaction_test.go index e57adbb8f58..bb52668fbba 100644 --- a/contrib/drivers/mssql/mssql_z_unit_transaction_test.go +++ b/contrib/drivers/mssql/mssql_z_unit_transaction_test.go @@ -94,14 +94,17 @@ func Test_TX_Prepare(t *testing.T) { rows, err := st.Query() t.AssertNil(err) - array, err := rows.Columns() - t.AssertNil(err) - t.Assert(array[0], "100") + var value int + if rows.Next() { + err = rows.Scan(&value) + t.AssertNil(err) + } + t.Assert(value, 100) - rows.Close() + err = rows.Close() t.AssertNil(err) - tx.Commit() + err = tx.Commit() t.AssertNil(err) }) } @@ -185,145 +188,6 @@ func Test_TX_BatchInsert(t *testing.T) { }) } -func Test_TX_BatchReplace(t *testing.T) { - table := createInitTable() - defer dropTable(table) - - gtest.C(t, func(t *gtest.T) { - tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if _, err := tx.Replace(table, g.List{ - { - "id": 2, - "passport": "USER_2", - "password": "PASS_2", - "nickname": "NAME_2", - "create_time": gtime.Now().String(), - }, - { - "id": 4, - "passport": "USER_4", - "password": "PASS_4", - "nickname": "NAME_4", - "create_time": gtime.Now().String(), - }, - }, 10); err != nil { - gtest.Error(err) - } - if err := tx.Commit(); err != nil { - gtest.Error(err) - } - if n, err := db.Model(table).Count(); err != nil { - gtest.Error(err) - } else { - t.Assert(n, int64(TableSize)) - } - if value, err := db.Model(table).Fields("password").Where("id", 2).Value(); err != nil { - gtest.Error(err) - } else { - t.Assert(value.String(), "PASS_2") - } - }) -} - -func Test_TX_BatchSave(t *testing.T) { - table := createInitTable() - defer dropTable(table) - - gtest.C(t, func(t *gtest.T) { - tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if _, err := tx.Save(table, g.List{ - { - "id": 4, - "passport": "USER_4", - "password": "PASS_4", - "nickname": "NAME_4", - "create_time": gtime.Now().String(), - }, - }, 10); err != nil { - gtest.Error(err) - } - if err := tx.Commit(); err != nil { - gtest.Error(err) - } - - if n, err := db.Model(table).Count(); err != nil { - gtest.Error(err) - } else { - t.Assert(n, int64(TableSize)) - } - - if value, err := db.Model(table).Fields("password").Where("id", 4).Value(); err != nil { - gtest.Error(err) - } else { - t.Assert(value.String(), "PASS_4") - } - }) -} - -func Test_TX_Replace(t *testing.T) { - table := createInitTable() - defer dropTable(table) - - gtest.C(t, func(t *gtest.T) { - tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if _, err := tx.Replace(table, g.Map{ - "id": 1, - "passport": "USER_1", - "password": "PASS_1", - "nickname": "NAME_1", - "create_time": gtime.Now().String(), - }); err != nil { - gtest.Error(err) - } - if err := tx.Rollback(); err != nil { - gtest.Error(err) - } - if value, err := db.Model(table).Fields("nickname").Where("id", 1).Value(); err != nil { - gtest.Error(err) - } else { - t.Assert(value.String(), "name_1") - } - }) -} - -func Test_TX_Save(t *testing.T) { - table := createInitTable() - defer dropTable(table) - - gtest.C(t, func(t *gtest.T) { - tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if _, err := tx.Save(table, g.Map{ - "id": 1, - "passport": "USER_1", - "password": "PASS_1", - "nickname": "NAME_1", - "create_time": gtime.Now().String(), - }); err != nil { - gtest.Error(err) - } - if err := tx.Commit(); err != nil { - gtest.Error(err) - } - if value, err := db.Model(table).Fields("nickname").Where("id", 1).Value(); err != nil { - gtest.Error(err) - } else { - t.Assert(value.String(), "NAME_1") - } - }) -} - func Test_TX_Update(t *testing.T) { table := createInitTable() defer dropTable(table) @@ -388,7 +252,7 @@ func Test_TX_GetOne(t *testing.T) { if record == nil { gtest.Error("FAIL") } - t.Assert(record["nickname"].String(), "name_2") + t.Assert(record["NICKNAME"].String(), "name_2") } if err := tx.Commit(); err != nil { gtest.Error(err) @@ -665,7 +529,7 @@ func Test_TX_Delete(t *testing.T) { if err != nil { gtest.Error(err) } - if _, err := tx.Delete(table, 1); err != nil { + if _, err := tx.Delete(table, "1=1"); err != nil { gtest.Error(err) } if err := tx.Commit(); err != nil { @@ -687,7 +551,7 @@ func Test_TX_Delete(t *testing.T) { if err != nil { gtest.Error(err) } - if _, err := tx.Delete(table, 1); err != nil { + if _, err := tx.Delete(table, "1=1"); err != nil { gtest.Error(err) } if n, err := tx.Model(table).Count(); err != nil { @@ -736,29 +600,6 @@ func Test_Transaction(t *testing.T) { t.Assert(value.String(), "name_1") } }) - - gtest.C(t, func(t *gtest.T) { - ctx := context.TODO() - err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - if _, err := tx.Replace(table, g.Map{ - "id": 1, - "passport": "USER_1", - "password": "PASS_1", - "nickname": "NAME_1", - "create_time": gtime.Now().String(), - }); err != nil { - t.Error(err) - } - return nil - }) - t.AssertNil(err) - - if value, err := db.Model(table).Fields("nickname").Where("id", 1).Value(); err != nil { - gtest.Error(err) - } else { - t.Assert(value.String(), "NAME_1") - } - }) } func Test_Transaction_Panic(t *testing.T) { @@ -790,324 +631,329 @@ func Test_Transaction_Panic(t *testing.T) { }) } -func Test_Transaction_Nested_Begin_Rollback_Commit(t *testing.T) { - table := createTable() - defer dropTable(table) - - gtest.C(t, func(t *gtest.T) { - tx, err := db.Begin(ctx) - t.AssertNil(err) - // tx begin. - err = tx.Begin() - t.AssertNil(err) - // tx rollback. - _, err = tx.Model(table).Data(g.Map{ - "id": 1, - "passport": "user_1", - "password": "pass_1", - "nickname": "name_1", - }).Insert() - err = tx.Rollback() - t.AssertNil(err) - // tx commit. - _, err = tx.Model(table).Data(g.Map{ - "id": 2, - "passport": "user_2", - "password": "pass_2", - "nickname": "name_2", - }).Insert() - err = tx.Commit() - t.AssertNil(err) - // check data. - all, err := db.Model(table).All() - t.AssertNil(err) - t.Assert(len(all), 1) - t.Assert(all[0]["id"], 2) - }) -} - -func Test_Transaction_Nested_TX_Transaction_UseTX(t *testing.T) { - table := createTable() - defer dropTable(table) - - db.SetDebug(true) - defer db.SetDebug(false) - - gtest.C(t, func(t *gtest.T) { - var ( - err error - ctx = context.TODO() - ) - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - // commit - err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - _, err = tx.Model(table).Data(g.Map{ - "id": 1, - "passport": "USER_1", - "password": "PASS_1", - "nickname": "NAME_1", - "create_time": gtime.Now().String(), - }).Insert() - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - // rollback - err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - _, err = tx.Model(table).Data(g.Map{ - "id": 2, - "passport": "USER_2", - "password": "PASS_2", - "nickname": "NAME_2", - "create_time": gtime.Now().String(), - }).Insert() - t.AssertNil(err) - panic("error") - return err - }) - t.AssertNE(err, nil) - return nil - }) - t.AssertNil(err) - - all, err := db.Ctx(ctx).Model(table).All() - t.AssertNil(err) - t.Assert(len(all), 1) - t.Assert(all[0]["id"], 1) +// TODO +// MSSQL does not support nested transaction. +// So the following test cases are not supported. +// If the problem is solved in the future, the test cases will be enabled. - // another record. - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - // commit - err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - _, err = tx.Model(table).Data(g.Map{ - "id": 3, - "passport": "USER_1", - "password": "PASS_1", - "nickname": "NAME_1", - "create_time": gtime.Now().String(), - }).Insert() - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - // rollback - err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - _, err = tx.Model(table).Data(g.Map{ - "id": 4, - "passport": "USER_2", - "password": "PASS_2", - "nickname": "NAME_2", - "create_time": gtime.Now().String(), - }).Insert() - t.AssertNil(err) - panic("error") - return err - }) - t.AssertNE(err, nil) - return nil - }) - t.AssertNil(err) - - all, err = db.Ctx(ctx).Model(table).All() - t.AssertNil(err) - t.Assert(len(all), 2) - t.Assert(all[0]["id"], 1) - t.Assert(all[1]["id"], 3) - }) -} - -func Test_Transaction_Nested_TX_Transaction_UseDB(t *testing.T) { - table := createTable() - defer dropTable(table) - - // db.SetDebug(true) - // defer db.SetDebug(false) - - gtest.C(t, func(t *gtest.T) { - var ( - err error - ctx = context.TODO() - ) - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - // commit - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - _, err = db.Model(table).Ctx(ctx).Data(g.Map{ - "id": 1, - "passport": "USER_1", - "password": "PASS_1", - "nickname": "NAME_1", - "create_time": gtime.Now().String(), - }).Insert() - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - - // rollback - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - _, err = tx.Model(table).Ctx(ctx).Data(g.Map{ - "id": 2, - "passport": "USER_2", - "password": "PASS_2", - "nickname": "NAME_2", - "create_time": gtime.Now().String(), - }).Insert() - t.AssertNil(err) - // panic makes this transaction rollback. - panic("error") - return err - }) - t.AssertNE(err, nil) - return nil - }) - t.AssertNil(err) - all, err := db.Model(table).All() - t.AssertNil(err) - t.Assert(len(all), 1) - t.Assert(all[0]["id"], 1) - - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - // commit - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - _, err = db.Model(table).Ctx(ctx).Data(g.Map{ - "id": 3, - "passport": "USER_1", - "password": "PASS_1", - "nickname": "NAME_1", - "create_time": gtime.Now().String(), - }).Insert() - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - return err - }) - t.AssertNil(err) - - // rollback - err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - _, err = tx.Model(table).Ctx(ctx).Data(g.Map{ - "id": 4, - "passport": "USER_2", - "password": "PASS_2", - "nickname": "NAME_2", - "create_time": gtime.Now().String(), - }).Insert() - t.AssertNil(err) - // panic makes this transaction rollback. - panic("error") - return err - }) - t.AssertNE(err, nil) - return nil - }) - t.AssertNil(err) - - all, err = db.Model(table).All() - t.AssertNil(err) - t.Assert(len(all), 2) - t.Assert(all[0]["id"], 1) - t.Assert(all[1]["id"], 3) - }) -} - -func Test_Transaction_Nested_SavePoint_RollbackTo(t *testing.T) { - table := createTable() - defer dropTable(table) - - gtest.C(t, func(t *gtest.T) { - tx, err := db.Begin(ctx) - t.AssertNil(err) - // tx save point. - _, err = tx.Model(table).Data(g.Map{ - "id": 1, - "passport": "user_1", - "password": "pass_1", - "nickname": "name_1", - }).Insert() - err = tx.SavePoint("MyPoint") - t.AssertNil(err) - _, err = tx.Model(table).Data(g.Map{ - "id": 2, - "passport": "user_2", - "password": "pass_2", - "nickname": "name_2", - }).Insert() - // tx rollback to. - err = tx.RollbackTo("MyPoint") - t.AssertNil(err) - // tx commit. - err = tx.Commit() - t.AssertNil(err) - - // check data. - all, err := db.Model(table).All() - t.AssertNil(err) - t.Assert(len(all), 1) - t.Assert(all[0]["id"], 1) - }) -} +// func Test_Transaction_Nested_Begin_Rollback_Commit(t *testing.T) { +// table := createTable() +// defer dropTable(table) +// +// gtest.C(t, func(t *gtest.T) { +// tx, err := db.Begin(ctx) +// t.AssertNil(err) +// // tx begin. +// err = tx.Begin() +// t.AssertNil(err) +// // tx rollback. +// _, err = tx.Model(table).Data(g.Map{ +// "id": 1, +// "passport": "user_1", +// "password": "pass_1", +// "nickname": "name_1", +// }).Insert() +// err = tx.Rollback() +// t.AssertNil(err) +// // tx commit. +// _, err = tx.Model(table).Data(g.Map{ +// "id": 2, +// "passport": "user_2", +// "password": "pass_2", +// "nickname": "name_2", +// }).Insert() +// err = tx.Commit() +// t.AssertNil(err) +// // check data. +// all, err := db.Model(table).All() +// t.AssertNil(err) +// t.Assert(len(all), 1) +// t.Assert(all[0]["id"], 2) +// }) +// } +// +// func Test_Transaction_Nested_TX_Transaction_UseTX(t *testing.T) { +// table := createTable() +// defer dropTable(table) +// +// db.SetDebug(true) +// defer db.SetDebug(false) +// +// gtest.C(t, func(t *gtest.T) { +// var ( +// err error +// ctx = context.TODO() +// ) +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// // commit +// err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// _, err = tx.Model(table).Data(g.Map{ +// "id": 1, +// "passport": "USER_1", +// "password": "PASS_1", +// "nickname": "NAME_1", +// "create_time": gtime.Now().String(), +// }).Insert() +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// // rollback +// err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// _, err = tx.Model(table).Data(g.Map{ +// "id": 2, +// "passport": "USER_2", +// "password": "PASS_2", +// "nickname": "NAME_2", +// "create_time": gtime.Now().String(), +// }).Insert() +// t.AssertNil(err) +// panic("error") +// return err +// }) +// t.AssertNE(err, nil) +// return nil +// }) +// t.AssertNil(err) +// +// all, err := db.Ctx(ctx).Model(table).All() +// t.AssertNil(err) +// t.Assert(len(all), 1) +// t.Assert(all[0]["id"], 1) +// +// // another record. +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// // commit +// err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// _, err = tx.Model(table).Data(g.Map{ +// "id": 3, +// "passport": "USER_1", +// "password": "PASS_1", +// "nickname": "NAME_1", +// "create_time": gtime.Now().String(), +// }).Insert() +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// // rollback +// err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// _, err = tx.Model(table).Data(g.Map{ +// "id": 4, +// "passport": "USER_2", +// "password": "PASS_2", +// "nickname": "NAME_2", +// "create_time": gtime.Now().String(), +// }).Insert() +// t.AssertNil(err) +// panic("error") +// return err +// }) +// t.AssertNE(err, nil) +// return nil +// }) +// t.AssertNil(err) +// +// all, err = db.Ctx(ctx).Model(table).All() +// t.AssertNil(err) +// t.Assert(len(all), 2) +// t.Assert(all[0]["id"], 1) +// t.Assert(all[1]["id"], 3) +// }) +// } +// +// func Test_Transaction_Nested_TX_Transaction_UseDB(t *testing.T) { +// table := createTable() +// defer dropTable(table) +// +// // db.SetDebug(true) +// // defer db.SetDebug(false) +// +// gtest.C(t, func(t *gtest.T) { +// var ( +// err error +// ctx = context.TODO() +// ) +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// // commit +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// _, err = db.Model(table).Ctx(ctx).Data(g.Map{ +// "id": 1, +// "passport": "USER_1", +// "password": "PASS_1", +// "nickname": "NAME_1", +// "create_time": gtime.Now().String(), +// }).Insert() +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// +// // rollback +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// _, err = tx.Model(table).Ctx(ctx).Data(g.Map{ +// "id": 2, +// "passport": "USER_2", +// "password": "PASS_2", +// "nickname": "NAME_2", +// "create_time": gtime.Now().String(), +// }).Insert() +// t.AssertNil(err) +// // panic makes this transaction rollback. +// panic("error") +// return err +// }) +// t.AssertNE(err, nil) +// return nil +// }) +// t.AssertNil(err) +// all, err := db.Model(table).All() +// t.AssertNil(err) +// t.Assert(len(all), 1) +// t.Assert(all[0]["id"], 1) +// +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// // commit +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// _, err = db.Model(table).Ctx(ctx).Data(g.Map{ +// "id": 3, +// "passport": "USER_1", +// "password": "PASS_1", +// "nickname": "NAME_1", +// "create_time": gtime.Now().String(), +// }).Insert() +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// return err +// }) +// t.AssertNil(err) +// +// // rollback +// err = db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { +// _, err = tx.Model(table).Ctx(ctx).Data(g.Map{ +// "id": 4, +// "passport": "USER_2", +// "password": "PASS_2", +// "nickname": "NAME_2", +// "create_time": gtime.Now().String(), +// }).Insert() +// t.AssertNil(err) +// // panic makes this transaction rollback. +// panic("error") +// return err +// }) +// t.AssertNE(err, nil) +// return nil +// }) +// t.AssertNil(err) +// +// all, err = db.Model(table).All() +// t.AssertNil(err) +// t.Assert(len(all), 2) +// t.Assert(all[0]["id"], 1) +// t.Assert(all[1]["id"], 3) +// }) +// } +// +// func Test_Transaction_Nested_SavePoint_RollbackTo(t *testing.T) { +// table := createTable() +// defer dropTable(table) +// +// gtest.C(t, func(t *gtest.T) { +// tx, err := db.Begin(ctx) +// t.AssertNil(err) +// // tx save point. +// _, err = tx.Model(table).Data(g.Map{ +// "id": 1, +// "passport": "user_1", +// "password": "pass_1", +// "nickname": "name_1", +// }).Insert() +// err = tx.SavePoint("MyPoint") +// t.AssertNil(err) +// _, err = tx.Model(table).Data(g.Map{ +// "id": 2, +// "passport": "user_2", +// "password": "pass_2", +// "nickname": "name_2", +// }).Insert() +// // tx rollback to. +// err = tx.RollbackTo("MyPoint") +// t.AssertNil(err) +// // tx commit. +// err = tx.Commit() +// t.AssertNil(err) +// +// // check data. +// all, err := db.Model(table).All() +// t.AssertNil(err) +// t.Assert(len(all), 1) +// t.Assert(all[0]["id"], 1) +// }) +// } func Test_Transaction_Method(t *testing.T) { table := createTable() @@ -1126,7 +972,7 @@ func Test_Transaction_Method(t *testing.T) { t.AssertNil(err) _, err = db.Ctx(ctx).Exec(ctx, fmt.Sprintf( - "insert into %s(`passport`,`password`,`nickname`,`create_time`,`id`) "+ + "insert into %s(passport , password , nickname , create_time , id ) "+ "VALUES('t2','25d55ad283aa400af464c76d713c07ad','T2','2021-08-25 21:53:00',2) ", table)) t.AssertNil(err) From f7be1306227de2e27a4b6422d754fe4e9d5f6659 Mon Sep 17 00:00:00 2001 From: oldme Date: Wed, 9 Oct 2024 17:22:14 +0800 Subject: [PATCH 3/5] up --- .../mssql/mssql_z_unit_transaction_test.go | 538 +++++++++--------- 1 file changed, 267 insertions(+), 271 deletions(-) diff --git a/contrib/drivers/mssql/mssql_z_unit_transaction_test.go b/contrib/drivers/mssql/mssql_z_unit_transaction_test.go index bb52668fbba..883d10b36fc 100644 --- a/contrib/drivers/mssql/mssql_z_unit_transaction_test.go +++ b/contrib/drivers/mssql/mssql_z_unit_transaction_test.go @@ -20,67 +20,67 @@ import ( ) func Test_TX_Query(t *testing.T) { - tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if _, err = tx.Query("SELECT ?", 1); err != nil { - gtest.Error(err) - } - if _, err = tx.Query("SELECT ?+?", 1, 2); err != nil { - gtest.Error(err) - } - if _, err = tx.Query("SELECT ?+?", g.Slice{1, 2}); err != nil { - gtest.Error(err) - } - if _, err = tx.Query("ERROR"); err == nil { - gtest.Error("FAIL") - } - if err = tx.Commit(); err != nil { - gtest.Error(err) - } + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + t.AssertNil(err) + + _, err = tx.Query("SELECT ?", 1) + t.AssertNil(err) + + _, err = tx.Query("SELECT ?+?", 1, 2) + t.AssertNil(err) + + _, err = tx.Query("SELECT ?+?", g.Slice{1, 2}) + t.AssertNil(err) + + _, err = tx.Query("ERROR") + t.AssertNil(err) + + err = tx.Commit() + t.AssertNil(err) + }) } func Test_TX_Exec(t *testing.T) { - tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if _, err := tx.Exec("SELECT ?", 1); err != nil { - gtest.Error(err) - } - if _, err := tx.Exec("SELECT ?+?", 1, 2); err != nil { - gtest.Error(err) - } - if _, err := tx.Exec("SELECT ?+?", g.Slice{1, 2}); err != nil { - gtest.Error(err) - } - if _, err := tx.Exec("ERROR"); err == nil { - gtest.Error("FAIL") - } - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + t.AssertNil(err) + + _, err = tx.Exec("SELECT ?", 1) + t.AssertNil(err) + + _, err = tx.Exec("SELECT ?+?", 1, 2) + t.AssertNil(err) + + _, err = tx.Exec("SELECT ?+?", g.Slice{1, 2}) + t.AssertNil(err) + + _, err = tx.Exec("ERROR") + t.AssertNil(err) + + err = tx.Commit() + t.AssertNil(err) + }) } func Test_TX_Commit(t *testing.T) { - tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + t.AssertNil(err) + + err = tx.Commit() + t.AssertNil(err) + }) } func Test_TX_Rollback(t *testing.T) { - tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if err := tx.Rollback(); err != nil { - gtest.Error(err) - } + gtest.C(t, func(t *gtest.T) { + tx, err := db.Begin(ctx) + t.AssertNil(err) + + err = tx.Rollback() + t.AssertNil(err) + }) } func Test_TX_Prepare(t *testing.T) { @@ -98,6 +98,7 @@ func Test_TX_Prepare(t *testing.T) { if rows.Next() { err = rows.Scan(&value) t.AssertNil(err) + } t.Assert(value, 100) @@ -115,38 +116,34 @@ func Test_TX_Insert(t *testing.T) { gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } + t.AssertNil(err) + user := tx.Model(table) - if _, err := user.Data(g.Map{ + _, err = user.Data(g.Map{ "id": 1, "passport": "t1", "password": "25d55ad283aa400af464c76d713c07ad", "nickname": "T1", "create_time": gtime.Now().String(), - }).Insert(); err != nil { - gtest.Error(err) - } - if _, err := tx.Insert(table, g.Map{ + }).Insert() + t.AssertNil(err) + + _, err = tx.Insert(table, g.Map{ "id": 2, "passport": "t1", "password": "25d55ad283aa400af464c76d713c07ad", "nickname": "T1", "create_time": gtime.Now().String(), - }); err != nil { - gtest.Error(err) - } + }) + t.AssertNil(err) - if n, err := tx.Model(table).Count(); err != nil { - gtest.Error(err) - } else { - t.Assert(n, int64(2)) - } + n, err := tx.Model(table).Count() + t.AssertNil(err) - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + t.Assert(n, int64(2)) + + err = tx.Commit() + t.AssertNil(err) }) } @@ -156,10 +153,9 @@ func Test_TX_BatchInsert(t *testing.T) { gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if _, err := tx.Insert(table, g.List{ + t.AssertNil(err) + + _, err = tx.Insert(table, g.List{ { "id": 2, "passport": "t", @@ -174,17 +170,16 @@ func Test_TX_BatchInsert(t *testing.T) { "nickname": "T3", "create_time": gtime.Now().String(), }, - }, 10); err != nil { - gtest.Error(err) - } - if err := tx.Commit(); err != nil { - gtest.Error(err) - } - if n, err := db.Model(table).Count(); err != nil { - gtest.Error(err) - } else { - t.Assert(n, int64(2)) - } + }, 10) + t.AssertNil(err) + + err = tx.Commit() + t.AssertNil(err) + + n, err := db.Model(table).Count() + t.AssertNil(err) + + t.Assert(n, int64(2)) }) } @@ -194,26 +189,23 @@ func Test_TX_Update(t *testing.T) { gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if result, err := tx.Update(table, "create_time='2019-10-24 10:00:00'", "id=3"); err != nil { - gtest.Error(err) - } else { - n, _ := result.RowsAffected() - t.Assert(n, 1) - } - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + t.AssertNil(err) + + result, err := tx.Update(table, "create_time='2019-10-24 10:00:00'", "id=3") + t.AssertNil(err) + + n, _ := result.RowsAffected() + t.Assert(n, 1) + err = tx.Commit() + t.AssertNil(err) + _, err = tx.Model(table).Fields("create_time").Where("id", 3).Value() t.AssertNE(err, nil) - if value, err := db.Model(table).Fields("create_time").Where("id", 3).Value(); err != nil { - gtest.Error(err) - } else { - t.Assert(value.String(), "2019-10-24 10:00:00") - } + value, err := db.Model(table).Fields("create_time").Where("id", 3).Value() + t.AssertNil(err) + + t.Assert(value.String(), "2019-10-24 10:00:00") }) } @@ -223,17 +215,14 @@ func Test_TX_GetAll(t *testing.T) { gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if result, err := tx.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1); err != nil { - gtest.Error(err) - } else { - t.Assert(len(result), 1) - } - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + t.AssertNil(err) + + result, err := tx.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1) + t.AssertNil(err) + + t.Assert(len(result), 1) + err = tx.Commit() + t.AssertNil(err) }) } @@ -243,20 +232,16 @@ func Test_TX_GetOne(t *testing.T) { gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if record, err := tx.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE passport=?", table), "user_2"); err != nil { - gtest.Error(err) - } else { - if record == nil { - gtest.Error("FAIL") - } - t.Assert(record["NICKNAME"].String(), "name_2") - } - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + t.AssertNil(err) + + record, err := tx.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE passport=?", table), "user_2") + t.AssertNil(err) + + t.AssertNE(record, nil) + t.Assert(record["NICKNAME"].String(), "name_2") + + err = tx.Commit() + t.AssertNil(err) }) } @@ -266,17 +251,14 @@ func Test_TX_GetValue(t *testing.T) { gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if value, err := tx.GetValue(fmt.Sprintf("SELECT id FROM %s WHERE passport=?", table), "user_3"); err != nil { - gtest.Error(err) - } else { - t.Assert(value.Int(), 3) - } - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + t.AssertNil(err) + + value, err := tx.GetValue(fmt.Sprintf("SELECT id FROM %s WHERE passport=?", table), "user_3") + t.AssertNil(err) + + t.Assert(value.Int(), 3) + err = tx.Commit() + t.AssertNil(err) }) } @@ -286,17 +268,14 @@ func Test_TX_GetCount(t *testing.T) { gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if count, err := tx.GetCount("SELECT * FROM " + table); err != nil { - gtest.Error(err) - } else { - t.Assert(count, int64(TableSize)) - } - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + t.AssertNil(err) + + count, err := tx.GetCount("SELECT * FROM " + table) + t.AssertNil(err) + + t.Assert(count, int64(TableSize)) + err = tx.Commit() + t.AssertNil(err) }) } @@ -306,9 +285,8 @@ func Test_TX_GetStruct(t *testing.T) { gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } + t.AssertNil(err) + type User struct { Id int Passport string @@ -317,20 +295,18 @@ func Test_TX_GetStruct(t *testing.T) { CreateTime gtime.Time } user := new(User) - if err := tx.GetStruct(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3); err != nil { - gtest.Error(err) - } + err = tx.GetStruct(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3) + t.AssertNil(err) + t.Assert(user.NickName, "name_3") t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + err = tx.Commit() + t.AssertNil(err) }) gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } + t.AssertNil(err) + type User struct { Id int Passport string @@ -339,14 +315,13 @@ func Test_TX_GetStruct(t *testing.T) { CreateTime *gtime.Time } user := new(User) - if err := tx.GetStruct(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3); err != nil { - gtest.Error(err) - } + err = tx.GetStruct(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3) + t.AssertNil(err) + t.Assert(user.NickName, "name_3") t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + err = tx.Commit() + t.AssertNil(err) }) } @@ -356,9 +331,8 @@ func Test_TX_GetStructs(t *testing.T) { gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } + t.AssertNil(err) + type User struct { Id int Passport string @@ -367,9 +341,9 @@ func Test_TX_GetStructs(t *testing.T) { CreateTime gtime.Time } var users []User - if err := tx.GetStructs(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>=?", table), 1); err != nil { - gtest.Error(err) - } + err = tx.GetStructs(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>=?", table), 1) + t.AssertNil(err) + t.Assert(len(users), TableSize) t.Assert(users[0].Id, 1) t.Assert(users[1].Id, 2) @@ -378,16 +352,14 @@ func Test_TX_GetStructs(t *testing.T) { t.Assert(users[1].NickName, "name_2") t.Assert(users[2].NickName, "name_3") t.Assert(users[2].CreateTime.String(), "2018-10-24 10:00:00") - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + err = tx.Commit() + t.AssertNil(err) }) gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } + t.AssertNil(err) + type User struct { Id int Passport string @@ -396,9 +368,9 @@ func Test_TX_GetStructs(t *testing.T) { CreateTime *gtime.Time } var users []User - if err := tx.GetStructs(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>=?", table), 1); err != nil { - gtest.Error(err) - } + err = tx.GetStructs(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>=?", table), 1) + t.AssertNil(err) + t.Assert(len(users), TableSize) t.Assert(users[0].Id, 1) t.Assert(users[1].Id, 2) @@ -407,9 +379,8 @@ func Test_TX_GetStructs(t *testing.T) { t.Assert(users[1].NickName, "name_2") t.Assert(users[2].NickName, "name_3") t.Assert(users[2].CreateTime.String(), "2018-10-24 10:00:00") - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + err = tx.Commit() + t.AssertNil(err) }) } @@ -419,9 +390,8 @@ func Test_TX_GetScan(t *testing.T) { gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } + t.AssertNil(err) + type User struct { Id int Passport string @@ -430,20 +400,18 @@ func Test_TX_GetScan(t *testing.T) { CreateTime gtime.Time } user := new(User) - if err := tx.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3); err != nil { - gtest.Error(err) - } + err = tx.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3) + t.AssertNil(err) + t.Assert(user.NickName, "name_3") t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + err = tx.Commit() + t.AssertNil(err) }) gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } + t.AssertNil(err) + type User struct { Id int Passport string @@ -452,21 +420,19 @@ func Test_TX_GetScan(t *testing.T) { CreateTime *gtime.Time } user := new(User) - if err := tx.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3); err != nil { - gtest.Error(err) - } + err = tx.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3) + t.AssertNil(err) + t.Assert(user.NickName, "name_3") t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + err = tx.Commit() + t.AssertNil(err) }) gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } + t.AssertNil(err) + type User struct { Id int Passport string @@ -475,9 +441,9 @@ func Test_TX_GetScan(t *testing.T) { CreateTime gtime.Time } var users []User - if err := tx.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>=?", table), 1); err != nil { - gtest.Error(err) - } + err = tx.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>=?", table), 1) + t.AssertNil(err) + t.Assert(len(users), TableSize) t.Assert(users[0].Id, 1) t.Assert(users[1].Id, 2) @@ -486,16 +452,14 @@ func Test_TX_GetScan(t *testing.T) { t.Assert(users[1].NickName, "name_2") t.Assert(users[2].NickName, "name_3") t.Assert(users[2].CreateTime.String(), "2018-10-24 10:00:00") - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + err = tx.Commit() + t.AssertNil(err) }) gtest.C(t, func(t *gtest.T) { tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } + t.AssertNil(err) + type User struct { Id int Passport string @@ -504,9 +468,9 @@ func Test_TX_GetScan(t *testing.T) { CreateTime *gtime.Time } var users []User - if err := tx.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>=?", table), 1); err != nil { - gtest.Error(err) - } + err = tx.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>=?", table), 1) + t.AssertNil(err) + t.Assert(len(users), TableSize) t.Assert(users[0].Id, 1) t.Assert(users[1].Id, 2) @@ -515,9 +479,8 @@ func Test_TX_GetScan(t *testing.T) { t.Assert(users[1].NickName, "name_2") t.Assert(users[2].NickName, "name_3") t.Assert(users[2].CreateTime.String(), "2018-10-24 10:00:00") - if err := tx.Commit(); err != nil { - gtest.Error(err) - } + err = tx.Commit() + t.AssertNil(err) }) } @@ -526,21 +489,18 @@ func Test_TX_Delete(t *testing.T) { table := createInitTable() defer dropTable(table) tx, err := db.Begin(ctx) - if err != nil { - gtest.Error(err) - } - if _, err := tx.Delete(table, "1=1"); err != nil { - gtest.Error(err) - } - if err := tx.Commit(); err != nil { - gtest.Error(err) - } - if n, err := db.Model(table).Count(); err != nil { - gtest.Error(err) - } else { - t.Assert(n, int64(0)) - } + t.AssertNil(err) + + _, err = tx.Delete(table, "1=1") + t.AssertNil(err) + + err = tx.Commit() + t.AssertNil(err) + + n, err := db.Model(table).Count() + t.AssertNil(err) + t.Assert(n, int64(0)) t.Assert(tx.IsClosed(), true) }) @@ -551,24 +511,21 @@ func Test_TX_Delete(t *testing.T) { if err != nil { gtest.Error(err) } - if _, err := tx.Delete(table, "1=1"); err != nil { - gtest.Error(err) - } - if n, err := tx.Model(table).Count(); err != nil { - gtest.Error(err) - } else { - t.Assert(n, int64(0)) - } - if err := tx.Rollback(); err != nil { - gtest.Error(err) - } - if n, err := db.Model(table).Count(); err != nil { - gtest.Error(err) - } else { - t.Assert(n, int64(TableSize)) - t.AssertNE(n, int64(0)) - } + _, err = tx.Delete(table, "1=1") + t.AssertNil(err) + + n, err := tx.Model(table).Count() + t.AssertNil(err) + t.Assert(n, int64(0)) + err = tx.Rollback() + t.AssertNil(err) + + n, err = db.Model(table).Count() + t.AssertNil(err) + + t.Assert(n, int64(TableSize)) + t.AssertNE(n, int64(0)) t.Assert(tx.IsClosed(), true) }) } @@ -580,25 +537,23 @@ func Test_Transaction(t *testing.T) { gtest.C(t, func(t *gtest.T) { ctx := context.TODO() err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - if _, err := tx.Ctx(ctx).Replace(table, g.Map{ + _, err := tx.Ctx(ctx).Replace(table, g.Map{ "id": 1, "passport": "USER_1", "password": "PASS_1", "nickname": "NAME_1", "create_time": gtime.Now().String(), - }); err != nil { - t.Error(err) - } + }) + t.AssertNil(err) + t.Assert(tx.IsClosed(), false) return gerror.New("error") }) t.AssertNE(err, nil) - if value, err := db.Model(table).Ctx(ctx).Fields("nickname").Where("id", 1).Value(); err != nil { - gtest.Error(err) - } else { - t.Assert(value.String(), "name_1") - } + value, err := db.Model(table).Ctx(ctx).Fields("nickname").Where("id", 1).Value() + t.AssertNil(err) + t.Assert(value.String(), "name_1") }) } @@ -609,25 +564,24 @@ func Test_Transaction_Panic(t *testing.T) { gtest.C(t, func(t *gtest.T) { ctx := context.TODO() err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - if _, err := tx.Replace(table, g.Map{ + _, err := tx.Replace(table, g.Map{ "id": 1, "passport": "USER_1", "password": "PASS_1", "nickname": "NAME_1", "create_time": gtime.Now().String(), - }); err != nil { - t.Error(err) - } + }) + t.AssertNil(err) + panic("error") return nil }) t.AssertNE(err, nil) - if value, err := db.Model(table).Fields("nickname").Where("id", 1).Value(); err != nil { - gtest.Error(err) - } else { - t.Assert(value.String(), "name_1") - } + value, err := db.Model(table).Fields("nickname").Where("id", 1).Value() + t.AssertNil(err) + + t.Assert(value.String(), "name_1") }) } @@ -643,9 +597,11 @@ func Test_Transaction_Panic(t *testing.T) { // gtest.C(t, func(t *gtest.T) { // tx, err := db.Begin(ctx) // t.AssertNil(err) +// // // tx begin. // err = tx.Begin() // t.AssertNil(err) +// // // tx rollback. // _, err = tx.Model(table).Data(g.Map{ // "id": 1, @@ -655,6 +611,7 @@ func Test_Transaction_Panic(t *testing.T) { // }).Insert() // err = tx.Rollback() // t.AssertNil(err) +// // // tx commit. // _, err = tx.Model(table).Data(g.Map{ // "id": 2, @@ -664,9 +621,11 @@ func Test_Transaction_Panic(t *testing.T) { // }).Insert() // err = tx.Commit() // t.AssertNil(err) +// // // check data. // all, err := db.Model(table).All() // t.AssertNil(err) +// // t.Assert(len(all), 1) // t.Assert(all[0]["id"], 2) // }) @@ -699,21 +658,27 @@ func Test_Transaction_Panic(t *testing.T) { // "create_time": gtime.Now().String(), // }).Insert() // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // // rollback // err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { // _, err = tx.Model(table).Data(g.Map{ @@ -724,6 +689,7 @@ func Test_Transaction_Panic(t *testing.T) { // "create_time": gtime.Now().String(), // }).Insert() // t.AssertNil(err) +// // panic("error") // return err // }) @@ -734,6 +700,7 @@ func Test_Transaction_Panic(t *testing.T) { // // all, err := db.Ctx(ctx).Model(table).All() // t.AssertNil(err) +// // t.Assert(len(all), 1) // t.Assert(all[0]["id"], 1) // @@ -753,21 +720,27 @@ func Test_Transaction_Panic(t *testing.T) { // "create_time": gtime.Now().String(), // }).Insert() // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // // rollback // err = tx.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { // _, err = tx.Model(table).Data(g.Map{ @@ -778,6 +751,7 @@ func Test_Transaction_Panic(t *testing.T) { // "create_time": gtime.Now().String(), // }).Insert() // t.AssertNil(err) +// // panic("error") // return err // }) @@ -788,6 +762,7 @@ func Test_Transaction_Panic(t *testing.T) { // // all, err = db.Ctx(ctx).Model(table).All() // t.AssertNil(err) +// // t.Assert(len(all), 2) // t.Assert(all[0]["id"], 1) // t.Assert(all[1]["id"], 3) @@ -821,18 +796,23 @@ func Test_Transaction_Panic(t *testing.T) { // "create_time": gtime.Now().String(), // }).Insert() // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) @@ -847,6 +827,7 @@ func Test_Transaction_Panic(t *testing.T) { // "create_time": gtime.Now().String(), // }).Insert() // t.AssertNil(err) +// // // panic makes this transaction rollback. // panic("error") // return err @@ -855,8 +836,10 @@ func Test_Transaction_Panic(t *testing.T) { // return nil // }) // t.AssertNil(err) +// // all, err := db.Model(table).All() // t.AssertNil(err) +// // t.Assert(len(all), 1) // t.Assert(all[0]["id"], 1) // @@ -875,18 +858,23 @@ func Test_Transaction_Panic(t *testing.T) { // "create_time": gtime.Now().String(), // }).Insert() // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) +// // return err // }) // t.AssertNil(err) @@ -901,6 +889,7 @@ func Test_Transaction_Panic(t *testing.T) { // "create_time": gtime.Now().String(), // }).Insert() // t.AssertNil(err) +// // // panic makes this transaction rollback. // panic("error") // return err @@ -912,6 +901,7 @@ func Test_Transaction_Panic(t *testing.T) { // // all, err = db.Model(table).All() // t.AssertNil(err) +// // t.Assert(len(all), 2) // t.Assert(all[0]["id"], 1) // t.Assert(all[1]["id"], 3) @@ -925,6 +915,7 @@ func Test_Transaction_Panic(t *testing.T) { // gtest.C(t, func(t *gtest.T) { // tx, err := db.Begin(ctx) // t.AssertNil(err) +// // // tx save point. // _, err = tx.Model(table).Data(g.Map{ // "id": 1, @@ -934,6 +925,7 @@ func Test_Transaction_Panic(t *testing.T) { // }).Insert() // err = tx.SavePoint("MyPoint") // t.AssertNil(err) +// // _, err = tx.Model(table).Data(g.Map{ // "id": 2, // "passport": "user_2", @@ -943,6 +935,7 @@ func Test_Transaction_Panic(t *testing.T) { // // tx rollback to. // err = tx.RollbackTo("MyPoint") // t.AssertNil(err) +// // // tx commit. // err = tx.Commit() // t.AssertNil(err) @@ -950,6 +943,7 @@ func Test_Transaction_Panic(t *testing.T) { // // check data. // all, err := db.Model(table).All() // t.AssertNil(err) +// // t.Assert(len(all), 1) // t.Assert(all[0]["id"], 1) // }) @@ -976,12 +970,14 @@ func Test_Transaction_Method(t *testing.T) { "VALUES('t2','25d55ad283aa400af464c76d713c07ad','T2','2021-08-25 21:53:00',2) ", table)) t.AssertNil(err) + return gerror.New("rollback") }) t.AssertNE(err, nil) count, err := db.Model(table).Count() t.AssertNil(err) + t.Assert(count, int64(0)) }) } From 84c6dbe37daa7d208a7d65032ff24fbcd07cd608 Mon Sep 17 00:00:00 2001 From: oldme Date: Thu, 10 Oct 2024 10:00:31 +0800 Subject: [PATCH 4/5] up --- contrib/drivers/mssql/mssql_z_unit_transaction_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/drivers/mssql/mssql_z_unit_transaction_test.go b/contrib/drivers/mssql/mssql_z_unit_transaction_test.go index 883d10b36fc..bb3ad90d781 100644 --- a/contrib/drivers/mssql/mssql_z_unit_transaction_test.go +++ b/contrib/drivers/mssql/mssql_z_unit_transaction_test.go @@ -34,7 +34,7 @@ func Test_TX_Query(t *testing.T) { t.AssertNil(err) _, err = tx.Query("ERROR") - t.AssertNil(err) + t.AssertNE(err, nil) err = tx.Commit() t.AssertNil(err) @@ -56,7 +56,7 @@ func Test_TX_Exec(t *testing.T) { t.AssertNil(err) _, err = tx.Exec("ERROR") - t.AssertNil(err) + t.AssertNE(err, nil) err = tx.Commit() t.AssertNil(err) From 4484b50659dddcf64f224a00d564da5c800addfc Mon Sep 17 00:00:00 2001 From: oldme Date: Thu, 10 Oct 2024 10:19:21 +0800 Subject: [PATCH 5/5] up --- contrib/drivers/mysql/mysql_z_unit_transaction_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/contrib/drivers/mysql/mysql_z_unit_transaction_test.go b/contrib/drivers/mysql/mysql_z_unit_transaction_test.go index f2903609257..beedd2ac5a4 100644 --- a/contrib/drivers/mysql/mysql_z_unit_transaction_test.go +++ b/contrib/drivers/mysql/mysql_z_unit_transaction_test.go @@ -768,15 +768,14 @@ func Test_Transaction_Panic(t *testing.T) { gtest.C(t, func(t *gtest.T) { ctx := context.TODO() err := db.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error { - if _, err := tx.Replace(table, g.Map{ + _, err := tx.Replace(table, g.Map{ "id": 1, "passport": "USER_1", "password": "PASS_1", "nickname": "NAME_1", "create_time": gtime.Now().String(), - }); err != nil { - t.Error(err) - } + }) + t.AssertNil(err) panic("error") return nil })