
commit 759038a126122d5b3323979fdd7d867a4ab85585 Author: Jinzhu <wosmvp@gmail.com> Date: Mon Aug 31 12:06:31 2020 +0800 Add PreparedStmt tests commit 066d54db1fc93ea58c190195104a2d7086623f69 Author: 王岚 <wanglan.backend@bytedance.com> Date: Fri Aug 28 18:40:59 2020 +0800 prepare_stmt add ctx
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package tests_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
. "gorm.io/gorm/utils/tests"
|
|
)
|
|
|
|
func TestPreparedStmt(t *testing.T) {
|
|
tx := DB.Session(&gorm.Session{PrepareStmt: true})
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
defer cancel()
|
|
txCtx := tx.WithContext(ctx)
|
|
|
|
user := *GetUser("prepared_stmt", Config{})
|
|
|
|
txCtx.Create(&user)
|
|
|
|
var result1 User
|
|
if err := txCtx.Find(&result1, user.ID).Error; err != nil {
|
|
t.Fatalf("no error should happen but got %v", err)
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
var result2 User
|
|
if err := tx.Find(&result2, user.ID).Error; err != nil {
|
|
t.Fatalf("no error should happen but got %v", err)
|
|
}
|
|
|
|
user2 := *GetUser("prepared_stmt2", Config{})
|
|
if err := txCtx.Create(&user2).Error; err == nil {
|
|
t.Fatalf("should failed to create with timeout context")
|
|
}
|
|
|
|
if err := tx.Create(&user2).Error; err != nil {
|
|
t.Fatalf("failed to create, got error %v", err)
|
|
}
|
|
|
|
var result3 User
|
|
if err := tx.Find(&result3, user2.ID).Error; err != nil {
|
|
t.Fatalf("no error should happen but got %v", err)
|
|
}
|
|
}
|