From 81f4fafae4c6a4237d8ad25d1b55340652d0c066 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 25 Jun 2020 23:37:49 +0800 Subject: [PATCH] Test group by with multiple columns --- tests/group_by_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/group_by_test.go b/tests/group_by_test.go index cb4c4f43..b08f48f1 100644 --- a/tests/group_by_test.go +++ b/tests/group_by_test.go @@ -11,6 +11,7 @@ func TestGroupBy(t *testing.T) { Name: "groupby", Age: 10, Birthday: Now(), + Active: true, }, { Name: "groupby", Age: 20, @@ -19,6 +20,7 @@ func TestGroupBy(t *testing.T) { Name: "groupby", Age: 30, Birthday: Now(), + Active: true, }, { Name: "groupby1", Age: 110, @@ -27,10 +29,12 @@ func TestGroupBy(t *testing.T) { Name: "groupby1", Age: 220, Birthday: Now(), + Active: true, }, { Name: "groupby1", Age: 330, Birthday: Now(), + Active: true, }} if err := DB.Create(&users).Error; err != nil { @@ -54,4 +58,13 @@ func TestGroupBy(t *testing.T) { if name != "groupby1" || total != 660 { 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) + } }