MCPcopy
hub / github.com/go-gorm/gorm / TestPreparedStmt

Function TestPreparedStmt

tests/prepared_stmt_test.go:14–54  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func TestPreparedStmt(t *testing.T) {
15 tx := DB.Session(&gorm.Session{PrepareStmt: true})
16
17 if _, ok := tx.ConnPool.(*gorm.PreparedStmtDB); !ok {
18 t.Fatalf("should assign PreparedStatement Manager back to database when using PrepareStmt mode")
19 }
20
21 ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
22 defer cancel()
23 txCtx := tx.WithContext(ctx)
24
25 user := *GetUser("prepared_stmt", Config{})
26
27 txCtx.Create(&user)
28
29 var result1 User
30 if err := txCtx.Find(&result1, user.ID).Error; err != nil {
31 t.Fatalf("no error should happen but got %v", err)
32 }
33
34 time.Sleep(time.Second)
35
36 var result2 User
37 if err := tx.Find(&result2, user.ID).Error; err != nil {
38 t.Fatalf("no error should happen but got %v", err)
39 }
40
41 user2 := *GetUser("prepared_stmt2", Config{})
42 if err := txCtx.Create(&user2).Error; err == nil {
43 t.Fatalf("should failed to create with timeout context")
44 }
45
46 if err := tx.Create(&user2).Error; err != nil {
47 t.Fatalf("failed to create, got error %v", err)
48 }
49
50 var result3 User
51 if err := tx.Find(&result3, user2.ID).Error; err != nil {
52 t.Fatalf("no error should happen but got %v", err)
53 }
54}
55
56func TestPreparedStmtFromTransaction(t *testing.T) {
57 db := DB.Session(&gorm.Session{PrepareStmt: true, SkipDefaultTransaction: true})

Callers

nothing calls this directly

Calls 5

GetUserFunction · 0.85
SessionMethod · 0.80
WithContextMethod · 0.80
CreateMethod · 0.65
FindMethod · 0.65

Tested by

no test coverage detected