mirror of https://github.com/go-gorm/gorm.git
Test group by with multiple columns
This commit is contained in:
parent
af632199cf
commit
81f4fafae4
|
@ -11,6 +11,7 @@ func TestGroupBy(t *testing.T) {
|
||||||
Name: "groupby",
|
Name: "groupby",
|
||||||
Age: 10,
|
Age: 10,
|
||||||
Birthday: Now(),
|
Birthday: Now(),
|
||||||
|
Active: true,
|
||||||
}, {
|
}, {
|
||||||
Name: "groupby",
|
Name: "groupby",
|
||||||
Age: 20,
|
Age: 20,
|
||||||
|
@ -19,6 +20,7 @@ func TestGroupBy(t *testing.T) {
|
||||||
Name: "groupby",
|
Name: "groupby",
|
||||||
Age: 30,
|
Age: 30,
|
||||||
Birthday: Now(),
|
Birthday: Now(),
|
||||||
|
Active: true,
|
||||||
}, {
|
}, {
|
||||||
Name: "groupby1",
|
Name: "groupby1",
|
||||||
Age: 110,
|
Age: 110,
|
||||||
|
@ -27,10 +29,12 @@ func TestGroupBy(t *testing.T) {
|
||||||
Name: "groupby1",
|
Name: "groupby1",
|
||||||
Age: 220,
|
Age: 220,
|
||||||
Birthday: Now(),
|
Birthday: Now(),
|
||||||
|
Active: true,
|
||||||
}, {
|
}, {
|
||||||
Name: "groupby1",
|
Name: "groupby1",
|
||||||
Age: 330,
|
Age: 330,
|
||||||
Birthday: Now(),
|
Birthday: Now(),
|
||||||
|
Active: true,
|
||||||
}}
|
}}
|
||||||
|
|
||||||
if err := DB.Create(&users).Error; err != nil {
|
if err := DB.Create(&users).Error; err != nil {
|
||||||
|
@ -54,4 +58,13 @@ func TestGroupBy(t *testing.T) {
|
||||||
if name != "groupby1" || total != 660 {
|
if name != "groupby1" || total != 660 {
|
||||||
t.Errorf("name should be groupby, but got %v, total should be 660, but got %v", name, total)
|
t.Errorf("name should be groupby, but got %v, total should be 660, but got %v", name, total)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var active bool
|
||||||
|
if err := DB.Model(&User{}).Select("name, active, sum(age)").Where("name = ? and active = ?", "groupby", true).Group("name").Group("active").Row().Scan(&name, &active, &total); err != nil {
|
||||||
|
t.Errorf("no error should happen, but got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if name != "groupby" || active != true || total != 40 {
|
||||||
|
t.Errorf("group by two columns, name %v, age %v, active: %v", name, total, active)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue