| 1 |
package tests_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
. "gorm.io/gorm/utils/tests" |
| 7 |
) |
| 8 |
|
| 9 |
func TestGroupBy(t *testing.T) { |
| 10 |
users := []User{{ |
| 11 |
Name: "groupby", |
| 12 |
Age: 10, |
| 13 |
Birthday: Now(), |
| 14 |
Active: true, |
| 15 |
}, { |
| 16 |
Name: "groupby", |
| 17 |
Age: 20, |
| 18 |
Birthday: Now(), |
| 19 |
}, { |
| 20 |
Name: "groupby", |
| 21 |
Age: 30, |
| 22 |
Birthday: Now(), |
| 23 |
Active: true, |
| 24 |
}, { |
| 25 |
Name: "groupby1", |
| 26 |
Age: 110, |
| 27 |
Birthday: Now(), |
| 28 |
}, { |
| 29 |
Name: "groupby1", |
| 30 |
Age: 220, |
| 31 |
Birthday: Now(), |
| 32 |
Active: true, |
| 33 |
}, { |
| 34 |
Name: "groupby1", |
| 35 |
Age: 330, |
| 36 |
Birthday: Now(), |
| 37 |
Active: true, |
| 38 |
}} |
| 39 |
|
| 40 |
if err := DB.Create(&users).Error; err != nil { |
| 41 |
t.Errorf("errors happened when create: %v", err) |
| 42 |
} |
| 43 |
|
| 44 |
var name string |
| 45 |
var total int |
| 46 |
if err := DB.Model(&User{}).Select("name, sum(age)").Where("name = ?", "groupby").Group("name").Row().Scan(&name, &total); err != nil { |
| 47 |
t.Errorf("no error should happen, but got %v", err) |
| 48 |
} |
| 49 |
|
| 50 |
if name != "groupby" || total != 60 { |
| 51 |
t.Errorf("name should be groupby, but got %v, total should be 60, but got %v", name, total) |
| 52 |
} |
| 53 |
|
| 54 |
if err := DB.Model(&User{}).Select("name, sum(age)").Where("name = ?", "groupby").Group("users.name").Row().Scan(&name, &total); err != nil { |
| 55 |
t.Errorf("no error should happen, but got %v", err) |
| 56 |
} |
| 57 |
|
| 58 |
if name != "groupby" || total != 60 { |
| 59 |
t.Errorf("name should be groupby, but got %v, total should be 60, but got %v", name, total) |
| 60 |
} |
| 61 |
|
| 62 |
if err := DB.Model(&User{}).Select("name, sum(age) as total").Where("name LIKE ?", "groupby%").Group("name").Having("name = ?", "groupby1").Row().Scan(&name, &total); err != nil { |
| 63 |
t.Errorf("no error should happen, but got %v", err) |
| 64 |
} |
| 65 |
|
| 66 |
if name != "groupby1" || total != 660 { |
| 67 |
t.Errorf("name should be groupby, but got %v, total should be 660, but got %v", name, total) |
| 68 |
} |
| 69 |
|
| 70 |
result := struct { |
| 71 |
Name string |
| 72 |
Total int64 |
| 73 |
}{} |
| 74 |
|
| 75 |
if err := DB.Model(&User{}).Select("name, sum(age) as total").Where("name LIKE ?", "groupby%").Group("name").Having("name = ?", "groupby1").Find(&result).Error; err != nil { |
| 76 |
t.Errorf("no error should happen, but got %v", err) |
| 77 |
} |
| 78 |
|
| 79 |
if result.Name != "groupby1" || result.Total != 660 { |
| 80 |
t.Errorf("name should be groupby, total should be 660, but got %+v", result) |
| 81 |
} |
| 82 |
|
| 83 |
if err := DB.Model(&User{}).Select("name, sum(age) as total").Where("name LIKE ?", "groupby%").Group("name").Having("name = ?", "groupby1").Scan(&result).Error; err != nil { |
| 84 |
t.Errorf("no error should happen, but got %v", err) |
| 85 |
} |
| 86 |
|
| 87 |
if result.Name != "groupby1" || result.Total != 660 { |
| 88 |
t.Errorf("name should be groupby, total should be 660, but got %+v", result) |
| 89 |
} |
| 90 |
|
| 91 |
var active bool |
| 92 |
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 { |
| 93 |
t.Errorf("no error should happen, but got %v", err) |
| 94 |
} |
| 95 |
|
| 96 |
if name != "groupby" || active != true || total != 40 { |
| 97 |
t.Errorf("group by two columns, name %v, age %v, active: %v", name, total, active) |
| 98 |
} |
| 99 |
|
| 100 |
if DB.Dialector.Name() == "mysql" { |
| 101 |
if err := DB.Model(&User{}).Select("name, age as total").Where("name LIKE ?", "groupby%").Having("total > ?", 300).Scan(&result).Error; err != nil { |
| 102 |
t.Errorf("no error should happen, but got %v", err) |
| 103 |
} |
| 104 |
|
| 105 |
if result.Name != "groupby1" || result.Total != 330 { |
| 106 |
t.Errorf("name should be groupby, total should be 660, but got %+v", result) |
| 107 |
} |
| 108 |
} |
| 109 |
} |