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

Function TestRowsWithGroup

tests/sql_builder_test.go:113–140  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

111}
112
113func TestRowsWithGroup(t *testing.T) {
114 users := []User{
115 {Name: "having_user_1", Age: 1},
116 {Name: "having_user_2", Age: 10},
117 {Name: "having_user_1", Age: 20},
118 {Name: "having_user_1", Age: 30},
119 }
120
121 DB.Create(&users)
122
123 rows, err := DB.Select("name, count(*) as total").Table("users").Group("name").Having("name IN ?", []string{users[0].Name, users[1].Name}).Rows()
124 if err != nil {
125 t.Fatalf("got error %v", err)
126 }
127
128 defer rows.Close()
129 for rows.Next() {
130 var name string
131 var total int64
132 rows.Scan(&name, &total)
133
134 if name == users[0].Name && total != 3 {
135 t.Errorf("Should have one user having name %v", users[0].Name)
136 } else if name == users[1].Name && total != 1 {
137 t.Errorf("Should have two users having name %v", users[1].Name)
138 }
139 }
140}
141
142func TestQueryRaw(t *testing.T) {
143 users := []*User{

Callers

nothing calls this directly

Calls 9

NextMethod · 0.80
CreateMethod · 0.65
RowsMethod · 0.65
HavingMethod · 0.65
GroupMethod · 0.65
TableMethod · 0.65
SelectMethod · 0.65
CloseMethod · 0.65
ScanMethod · 0.65

Tested by

no test coverage detected