mirror of https://github.com/go-gorm/gorm.git
Test Order/GroupBy
This commit is contained in:
parent
4e066c9590
commit
9a4941ba70
|
@ -14,7 +14,7 @@ func (s Select) Name() string {
|
||||||
func (s Select) Build(builder Builder) {
|
func (s Select) Build(builder Builder) {
|
||||||
if len(s.Columns) > 0 {
|
if len(s.Columns) > 0 {
|
||||||
if s.Distinct {
|
if s.Distinct {
|
||||||
builder.WriteString(" DISTINCT ")
|
builder.WriteString("DISTINCT ")
|
||||||
}
|
}
|
||||||
|
|
||||||
for idx, column := range s.Columns {
|
for idx, column := range s.Columns {
|
||||||
|
|
|
@ -67,6 +67,27 @@ func TestGroupBy(t *testing.T) {
|
||||||
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 result = struct {
|
||||||
|
Name string
|
||||||
|
Total int64
|
||||||
|
}{}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
t.Errorf("no error should happen, but got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result.Name != "groupby1" || result.Total != 660 {
|
||||||
|
t.Errorf("name should be groupby, total should be 660, but got %+v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
t.Errorf("no error should happen, but got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result.Name != "groupby1" || result.Total != 660 {
|
||||||
|
t.Errorf("name should be groupby, total should be 660, but got %+v", result)
|
||||||
|
}
|
||||||
|
|
||||||
var active bool
|
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 {
|
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)
|
t.Errorf("no error should happen, but got %v", err)
|
||||||
|
|
|
@ -101,6 +101,7 @@ func TestJoinsWithSelect(t *testing.T) {
|
||||||
DB.Save(&user)
|
DB.Save(&user)
|
||||||
|
|
||||||
var results []result
|
var results []result
|
||||||
|
|
||||||
DB.Table("users").Select("users.id, pets.id as pet_id, pets.name").Joins("left join pets on pets.user_id = users.id").Where("users.name = ?", "joins_with_select").Scan(&results)
|
DB.Table("users").Select("users.id, pets.id as pet_id, pets.name").Joins("left join pets on pets.user_id = users.id").Where("users.name = ?", "joins_with_select").Scan(&results)
|
||||||
|
|
||||||
sort.Slice(results, func(i, j int) bool {
|
sort.Slice(results, func(i, j int) bool {
|
||||||
|
|
|
@ -223,17 +223,17 @@ func TestOr(t *testing.T) {
|
||||||
|
|
||||||
result := dryDB.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&User{})
|
result := dryDB.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&User{})
|
||||||
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ OR .*role.* = .+").MatchString(result.Statement.SQL.String()) {
|
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ OR .*role.* = .+").MatchString(result.Statement.SQL.String()) {
|
||||||
t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String())
|
t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
result = dryDB.Where("name = ?", "jinzhu").Or(User{Name: "jinzhu 2", Age: 18}).Find(&User{})
|
result = dryDB.Where("name = ?", "jinzhu").Or(User{Name: "jinzhu 2", Age: 18}).Find(&User{})
|
||||||
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* = .+ OR \\(.*name.* AND .*age.*\\)").MatchString(result.Statement.SQL.String()) {
|
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* = .+ OR \\(.*name.* AND .*age.*\\)").MatchString(result.Statement.SQL.String()) {
|
||||||
t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String())
|
t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
result = dryDB.Where("name = ?", "jinzhu").Or(map[string]interface{}{"name": "jinzhu 2", "age": 18}).Find(&User{})
|
result = dryDB.Where("name = ?", "jinzhu").Or(map[string]interface{}{"name": "jinzhu 2", "age": 18}).Find(&User{})
|
||||||
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* = .+ OR \\(.*age.* AND .*name.*\\)").MatchString(result.Statement.SQL.String()) {
|
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* = .+ OR \\(.*age.* AND .*name.*\\)").MatchString(result.Statement.SQL.String()) {
|
||||||
t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String())
|
t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -426,6 +426,20 @@ func TestSearchWithEmptyChain(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOrder(t *testing.T) {
|
||||||
|
dryDB := DB.Session(&gorm.Session{DryRun: true})
|
||||||
|
|
||||||
|
result := dryDB.Order("age desc, name").Find(&User{})
|
||||||
|
if !regexp.MustCompile("SELECT \\* FROM .*users.* ORDER BY age desc, name").MatchString(result.Statement.SQL.String()) {
|
||||||
|
t.Fatalf("Build Order condition, but got %v", result.Statement.SQL.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
result = dryDB.Order("age desc").Order("name").Find(&User{})
|
||||||
|
if !regexp.MustCompile("SELECT \\* FROM .*users.* ORDER BY age desc,name").MatchString(result.Statement.SQL.String()) {
|
||||||
|
t.Fatalf("Build Order condition, but got %v", result.Statement.SQL.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLimit(t *testing.T) {
|
func TestLimit(t *testing.T) {
|
||||||
users := []User{
|
users := []User{
|
||||||
{Name: "LimitUser1", Age: 1},
|
{Name: "LimitUser1", Age: 1},
|
||||||
|
@ -461,7 +475,6 @@ func TestOffset(t *testing.T) {
|
||||||
if (len(users1) != len(users4)) || (len(users1)-len(users2) != 3) || (len(users1)-len(users3) != 5) {
|
if (len(users1) != len(users4)) || (len(users1)-len(users2) != 3) || (len(users1)-len(users3) != 5) {
|
||||||
t.Errorf("Offset should work without limit.")
|
t.Errorf("Offset should work without limit.")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchWithMap(t *testing.T) {
|
func TestSearchWithMap(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue