gitee.com/eden-framework/sqlx@v0.0.3/task_test.go (about) 1 package sqlx_test 2 3 import ( 4 "database/sql/driver" 5 "fmt" 6 "testing" 7 8 "gitee.com/eden-framework/sqlx" 9 "gitee.com/eden-framework/sqlx/builder" 10 "gitee.com/eden-framework/sqlx/migration" 11 "github.com/google/uuid" 12 "github.com/onsi/gomega" 13 ) 14 15 func TestWithTasks(t *testing.T) { 16 dbTest := sqlx.NewDatabase("test_for_user") 17 18 t.Run("", func(t *testing.T) { 19 for _, connector := range []driver.Connector{ 20 mysqlConnector, 21 postgresConnector, 22 } { 23 db := dbTest.OpenDB(connector) 24 driverName := db.Dialect().DriverName() 25 26 dbTest.Register(&User{}) 27 err := migration.Migrate(db, nil) 28 gomega.NewWithT(t).Expect(err).To(gomega.BeNil()) 29 30 t.Run("rollback on task err", func(t *testing.T) { 31 taskList := sqlx.NewTasks(db) 32 33 taskList = taskList.With(func(db sqlx.DBExecutor) error { 34 user := User{ 35 Name: uuid.New().String(), 36 Gender: GenderMale, 37 } 38 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 39 return err 40 }) 41 42 taskList = taskList.With(func(db sqlx.DBExecutor) error { 43 subTaskList := sqlx.NewTasks(db) 44 45 subTaskList = subTaskList.With(func(db sqlx.DBExecutor) error { 46 user := User{ 47 Name: uuid.New().String(), 48 Gender: GenderMale, 49 } 50 51 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 52 return err 53 }) 54 55 subTaskList = subTaskList.With(func(db sqlx.DBExecutor) error { 56 return fmt.Errorf("rollback") 57 }) 58 59 return subTaskList.Do() 60 }) 61 62 err := taskList.Do() 63 gomega.NewWithT(t).Expect(err).NotTo(gomega.BeNil()) 64 }) 65 if driverName == "mysql" { 66 t.Run("skip rollback", func(t *testing.T) { 67 taskList := sqlx.NewTasks(db) 68 69 user := User{ 70 Name: uuid.New().String(), 71 Gender: GenderMale, 72 } 73 74 taskList = taskList.With(func(db sqlx.DBExecutor) error { 75 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 76 return err 77 }) 78 79 taskList = taskList.With(func(db sqlx.DBExecutor) error { 80 db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 81 return nil 82 }) 83 84 taskList = taskList.With(func(db sqlx.DBExecutor) error { 85 _, err := db.ExecExpr(sqlx.InsertToDB(db, &User{ 86 Name: uuid.New().String(), 87 Gender: GenderMale, 88 }, nil)) 89 return err 90 }) 91 92 err := taskList.Do() 93 gomega.NewWithT(t).Expect(err).To(gomega.BeNil()) 94 }) 95 } else { 96 t.Run("skip rollback in postgres", func(t *testing.T) { 97 taskList := sqlx.NewTasks(db) 98 99 user := User{ 100 Name: uuid.New().String(), 101 Gender: GenderMale, 102 } 103 104 taskList = taskList.With(func(db sqlx.DBExecutor) error { 105 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 106 return err 107 }) 108 109 taskList = taskList.With(func(db sqlx.DBExecutor) error { 110 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil, builder.OnConflict(builder.Cols("f_name")).DoNothing())) 111 return err 112 }) 113 114 taskList = taskList.With(func(db sqlx.DBExecutor) error { 115 _, err := db.ExecExpr(sqlx.InsertToDB(db, &User{ 116 Name: uuid.New().String(), 117 Gender: GenderMale, 118 }, nil)) 119 return err 120 }) 121 122 err := taskList.Do() 123 gomega.NewWithT(t).Expect(err).To(gomega.BeNil()) 124 }) 125 } 126 127 t.Run("transaction chain", func(t *testing.T) { 128 taskList := sqlx.NewTasks(db) 129 130 taskList = taskList.With(func(db sqlx.DBExecutor) error { 131 user := User{ 132 Name: uuid.New().String(), 133 Gender: GenderMale, 134 } 135 136 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 137 138 return err 139 }) 140 141 taskList = taskList.With(func(db sqlx.DBExecutor) error { 142 subTaskList := sqlx.NewTasks(db) 143 144 subTaskList = subTaskList.With(func(db sqlx.DBExecutor) error { 145 user := User{ 146 Name: uuid.New().String(), 147 Gender: GenderMale, 148 } 149 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 150 return err 151 }) 152 153 subTaskList = subTaskList.With(func(db sqlx.DBExecutor) error { 154 user := User{ 155 Name: uuid.New().String(), 156 Gender: GenderMale, 157 } 158 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 159 return err 160 }) 161 162 return subTaskList.Do() 163 }) 164 165 err := taskList.Do() 166 gomega.NewWithT(t).Expect(err).To(gomega.BeNil()) 167 }) 168 db.Tables.Range(func(table *builder.Table, idx int) { 169 _, err := db.ExecExpr(db.Dialect().DropTable(table)) 170 gomega.NewWithT(t).Expect(err).To(gomega.BeNil()) 171 }) 172 } 173 }) 174 }