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

Function TestSubQueryWithRaw

tests/query_test.go:1212–1259  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1210}
1211
1212func TestSubQueryWithRaw(t *testing.T) {
1213 users := []User{
1214 {Name: "subquery_raw_1", Age: 10},
1215 {Name: "subquery_raw_2", Age: 20},
1216 {Name: "subquery_raw_3", Age: 30},
1217 {Name: "subquery_raw_4", Age: 40},
1218 }
1219 DB.Create(&users)
1220
1221 var count int64
1222 err := DB.Raw("select count(*) from (?) tmp where 1 = ? AND name IN (?)", DB.Raw("select name from users where age >= ? and name in (?)", 10, []string{"subquery_raw_1", "subquery_raw_2", "subquery_raw_3"}), 1, DB.Raw("select name from users where age >= ? and name in (?)", 20, []string{"subquery_raw_1", "subquery_raw_2", "subquery_raw_3"})).Scan(&count).Error
1223 if err != nil {
1224 t.Errorf("Expected to get no errors, but got %v", err)
1225 }
1226
1227 if count != 2 {
1228 t.Errorf("Row count must be 2, instead got %d", count)
1229 }
1230
1231 err = DB.Raw("select count(*) from (?) tmp",
1232 DB.Table("users").
1233 Select("name").
1234 Where("age >= ? and name in (?)", 20, []string{"subquery_raw_1", "subquery_raw_3"}).
1235 Group("name"),
1236 ).Count(&count).Error
1237 if err != nil {
1238 t.Errorf("Expected to get no errors, but got %v", err)
1239 }
1240
1241 if count != 1 {
1242 t.Errorf("Row count must be 1, instead got %d", count)
1243 }
1244
1245 err = DB.Raw("select count(*) from (?) tmp",
1246 DB.Table("users").
1247 Select("name").
1248 Where("name LIKE ?", "subquery_raw%").
1249 Not("age <= ?", 10).Not("name IN (?)", []string{"subquery_raw_1", "subquery_raw_3"}).
1250 Group("name"),
1251 ).Count(&count).Error
1252 if err != nil {
1253 t.Errorf("Expected to get no errors, but got %v", err)
1254 }
1255
1256 if count != 2 {
1257 t.Errorf("Row count must be 2, instead got %d", count)
1258 }
1259}
1260
1261func TestSubQueryWithHaving(t *testing.T) {
1262 users := []User{

Callers

nothing calls this directly

Calls 9

CreateMethod · 0.65
ScanMethod · 0.65
RawMethod · 0.65
CountMethod · 0.65
GroupMethod · 0.65
WhereMethod · 0.65
SelectMethod · 0.65
TableMethod · 0.65
NotMethod · 0.65

Tested by

no test coverage detected