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

Function TestConnPoolWrapper

tests/connpool_test.go:85–174  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

83}
84
85func TestConnPoolWrapper(t *testing.T) {
86 dialect := os.Getenv("GORM_DIALECT")
87 if dialect != "mysql" {
88 t.SkipNow()
89 }
90
91 dbDSN := os.Getenv("GORM_DSN")
92 if dbDSN == "" {
93 dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"
94 }
95 nativeDB, err := sql.Open("mysql", dbDSN)
96 if err != nil {
97 t.Fatalf("Should open db success, but got %v", err)
98 }
99
100 conn := &wrapperConnPool{
101 db: nativeDB,
102 expect: []string{
103 "SELECT VERSION()",
104 "INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`) VALUES (?,?,?,?,?,?,?,?,?)",
105 "SELECT * FROM `users` WHERE name = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT ?",
106 "INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`) VALUES (?,?,?,?,?,?,?,?,?)",
107 "SELECT * FROM `users` WHERE name = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT ?",
108 "SELECT * FROM `users` WHERE name = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT ?",
109 "INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`) VALUES (?,?,?,?,?,?,?,?,?)",
110 "SELECT * FROM `users` WHERE name = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT ?",
111 "SELECT * FROM `users` WHERE name = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT ?",
112 },
113 }
114
115 defer func() {
116 if !reflect.DeepEqual(conn.got, conn.expect) {
117 t.Errorf("expect %#v but got %#v", conn.expect, conn.got)
118 }
119 }()
120
121 db, err := gorm.Open(mysql.New(mysql.Config{Conn: conn, DisableWithReturning: true}))
122 db.Logger = DB.Logger
123 if err != nil {
124 t.Fatalf("Should open db success, but got %v", err)
125 }
126
127 tx := db.Begin()
128 user := *GetUser("transaction", Config{})
129
130 if err = tx.Save(&user).Error; err != nil {
131 t.Fatalf("No error should raise, but got %v", err)
132 }
133
134 if err = tx.First(&User{}, "name = ?", "transaction").Error; err != nil {
135 t.Fatalf("Should find saved record, but got %v", err)
136 }
137
138 user1 := *GetUser("transaction1-1", Config{})
139
140 if err = tx.Save(&user1).Error; err != nil {
141 t.Fatalf("No error should raise, but got %v", err)
142 }

Callers

nothing calls this directly

Calls 10

OpenFunction · 0.92
GetUserFunction · 0.85
BeginMethod · 0.80
SaveMethod · 0.80
SessionMethod · 0.80
NewMethod · 0.65
FirstMethod · 0.65
RollbackMethod · 0.65
WhereMethod · 0.65
CommitMethod · 0.65

Tested by

no test coverage detected