Test group with table name

This commit is contained in:
Jinzhu 2020-06-26 08:39:18 +08:00
parent 2476c0fbb4
commit cb5a35a807
2 changed files with 12 additions and 4 deletions

View File

@ -6,10 +6,10 @@ require (
github.com/google/uuid v1.1.1 github.com/google/uuid v1.1.1
github.com/jinzhu/now v1.1.1 github.com/jinzhu/now v1.1.1
github.com/lib/pq v1.6.0 github.com/lib/pq v1.6.0
gorm.io/driver/mysql v0.2.6 gorm.io/driver/mysql v0.2.7
gorm.io/driver/postgres v0.2.3 gorm.io/driver/postgres v0.2.4
gorm.io/driver/sqlite v1.0.7 gorm.io/driver/sqlite v1.0.8
gorm.io/driver/sqlserver v0.2.3 gorm.io/driver/sqlserver v0.2.4
gorm.io/gorm v0.2.9 gorm.io/gorm v0.2.9
) )

View File

@ -51,6 +51,14 @@ func TestGroupBy(t *testing.T) {
t.Errorf("name should be groupby, but got %v, total should be 60, but got %v", name, total) t.Errorf("name should be groupby, but got %v, total should be 60, but got %v", name, total)
} }
if err := DB.Model(&User{}).Select("name, sum(age)").Where("name = ?", "groupby").Group("users.name").Row().Scan(&name, &total); err != nil {
t.Errorf("no error should happen, but got %v", err)
}
if name != "groupby" || total != 60 {
t.Errorf("name should be groupby, but got %v, total should be 60, but got %v", name, total)
}
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 { 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 {
t.Errorf("no error should happen, but got %v", err) t.Errorf("no error should happen, but got %v", err)
} }