From 9a4941ba7021bcbac0c85d0ca54c635eeeec554c Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 5 Jul 2020 22:12:52 +0800 Subject: [PATCH] Test Order/GroupBy --- clause/select.go | 2 +- tests/group_by_test.go | 21 +++++++++++++++++++++ tests/joins_test.go | 1 + tests/query_test.go | 21 +++++++++++++++++---- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/clause/select.go b/clause/select.go index a1b77de8..9c2bc625 100644 --- a/clause/select.go +++ b/clause/select.go @@ -14,7 +14,7 @@ func (s Select) Name() string { func (s Select) Build(builder Builder) { if len(s.Columns) > 0 { if s.Distinct { - builder.WriteString(" DISTINCT ") + builder.WriteString("DISTINCT ") } for idx, column := range s.Columns { diff --git a/tests/group_by_test.go b/tests/group_by_test.go index 6d0ed39c..7e41e94a 100644 --- a/tests/group_by_test.go +++ b/tests/group_by_test.go @@ -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) } + 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 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) diff --git a/tests/joins_test.go b/tests/joins_test.go index f01c8211..e54d3784 100644 --- a/tests/joins_test.go +++ b/tests/joins_test.go @@ -101,6 +101,7 @@ func TestJoinsWithSelect(t *testing.T) { DB.Save(&user) 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) sort.Slice(results, func(i, j int) bool { diff --git a/tests/query_test.go b/tests/query_test.go index 5a8bbef2..1db490b7 100644 --- a/tests/query_test.go +++ b/tests/query_test.go @@ -223,17 +223,17 @@ func TestOr(t *testing.T) { 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()) { - 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{}) 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{}) 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) { users := []User{ {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) { t.Errorf("Offset should work without limit.") } - } func TestSearchWithMap(t *testing.T) {