Debian Codegraph @

tests/count_test.go

raw
1 package tests_test
2
3 import (
4 "regexp"
5 "sort"
6 "strings"
7 "testing"
8
9 "gorm.io/gorm"
10 . "gorm.io/gorm/utils/tests"
11 )
12
13 func TestCountWithGroup(t *testing.T) {
14 DB.Create([]Company{
15 {Name: "company_count_group_a"},
16 {Name: "company_count_group_a"},
17 {Name: "company_count_group_a"},
18 {Name: "company_count_group_b"},
19 {Name: "company_count_group_c"},
20 })
21
22 var count1 int64
23 if err := DB.Model(&Company{}).Where("name = ?", "company_count_group_a").Group("name").Count(&count1).Error; err != nil {
24 t.Errorf("Count should work, but got err %v", err)
25 }
26 if count1 != 1 {
27 t.Errorf("Count with group should be 1, but got count: %v", count1)
28 }
29
30 var count2 int64
31 if err := DB.Model(&Company{}).Where("name in ?", []string{"company_count_group_b", "company_count_group_c"}).Group("name").Count(&count2).Error; err != nil {
32 t.Errorf("Count should work, but got err %v", err)
33 }
34 if count2 != 2 {
35 t.Errorf("Count with group should be 2, but got count: %v", count2)
36 }
37 }
38
39 func TestCount(t *testing.T) {
40 var (
41 user1 = *GetUser("count-1", Config{})
42 user2 = *GetUser("count-2", Config{})
43 user3 = *GetUser("count-3", Config{})
44 users []User
45 count, count1, count2 int64
46 )
47
48 DB.Save(&user1).Save(&user2).Save(&user3)
49
50 if err := DB.Where("name = ?", user1.Name).Or("name = ?", user3.Name).Find(&users).Count(&count).Error; err != nil {
51 t.Errorf("Count should work, but got err %v", err)
52 }
53
54 if count != int64(len(users)) {
55 t.Errorf("Count() method should get correct value, expect: %v, got %v", count, len(users))
56 }
57
58 if err := DB.Model(&User{}).Where("name = ?", user1.Name).Or("name = ?", user3.Name).Count(&count).Find(&users).Error; err != nil {
59 t.Errorf("Count should work, but got err %v", err)
60 }
61
62 if count != int64(len(users)) {
63 t.Errorf("Count() method should get correct value, expect: %v, got %v", count, len(users))
64 }
65
66 DB.Model(&User{}).Where("name = ?", user1.Name).Count(&count1).Or("name in ?", []string{user2.Name, user3.Name}).Count(&count2)
67 if count1 != 1 || count2 != 3 {
68 t.Errorf("multiple count in chain should works")
69 }
70
71 tx := DB.Model(&User{}).Where("name = ?", user1.Name).Session(&gorm.Session{})
72 tx.Count(&count1)
73 tx.Or("name in ?", []string{user2.Name, user3.Name}).Count(&count2)
74 if count1 != 1 || count2 != 3 {
75 t.Errorf("count after new session should works")
76 }
77
78 var count3 int64
79 if err := DB.Model(&User{}).Where("name in ?", []string{user2.Name, user2.Name, user3.Name}).Group("id").Count(&count3).Error; err != nil {
80 t.Errorf("Error happened when count with group, but got %v", err)
81 }
82
83 if count3 != 2 {
84 t.Errorf("Should get correct count for count with group, but got %v", count3)
85 }
86
87 dryDB := DB.Session(&gorm.Session{DryRun: true})
88 result := dryDB.Table("users").Select("name").Count(&count)
89 if !regexp.MustCompile(`SELECT COUNT\(.name.\) FROM .*users.*`).MatchString(result.Statement.SQL.String()) {
90 t.Fatalf("Build count with select, but got %v", result.Statement.SQL.String())
91 }
92
93 result = dryDB.Table("users").Distinct("name").Count(&count)
94 if !regexp.MustCompile(`SELECT COUNT\(DISTINCT\(.name.\)\) FROM .*users.*`).MatchString(result.Statement.SQL.String()) {
95 t.Fatalf("Build count with select, but got %v", result.Statement.SQL.String())
96 }
97
98 var count4 int64
99 if err := DB.Table("users").Joins("LEFT JOIN companies on companies.name = users.name").Where("users.name = ?", user1.Name).Count(&count4).Error; err != nil || count4 != 1 {
100 t.Errorf("count with join, got error: %v, count %v", err, count4)
101 }
102
103 var count5 int64
104 if err := DB.Table("users").Where("users.name = ?", user1.Name).Order("name").Count(&count5).Error; err != nil || count5 != 1 {
105 t.Errorf("count with join, got error: %v, count %v", err, count)
106 }
107
108 var count6 int64
109 if err := DB.Model(&User{}).Where("name in ?", []string{user1.Name, user2.Name, user3.Name}).Select(
110 "(CASE WHEN name=? THEN ? ELSE ? END) as name", "count-1", "main", "other",
111 ).Count(&count6).Find(&users).Error; err != nil || count6 != 3 {
112 t.Fatalf("Count should work, but got err %v", err)
113 }
114
115 expects := []User{{Name: "main"}, {Name: "other"}, {Name: "other"}}
116 sort.SliceStable(users, func(i, j int) bool {
117 return strings.Compare(users[i].Name, users[j].Name) < 0
118 })
119
120 AssertEqual(t, users, expects)
121
122 var count7 int64
123 if err := DB.Model(&User{}).Where("name in ?", []string{user1.Name, user2.Name, user3.Name}).Select(
124 "(CASE WHEN name=? THEN ? ELSE ? END) as name, age", "count-1", "main", "other",
125 ).Count(&count7).Find(&users).Error; err != nil || count7 != 3 {
126 t.Fatalf("Count should work, but got err %v", err)
127 }
128
129 expects = []User{{Name: "main", Age: 18}, {Name: "other", Age: 18}, {Name: "other", Age: 18}}
130 sort.SliceStable(users, func(i, j int) bool {
131 return strings.Compare(users[i].Name, users[j].Name) < 0
132 })
133
134 AssertEqual(t, users, expects)
135
136 var count8 int64
137 if err := DB.Model(&User{}).Where("name in ?", []string{user1.Name, user2.Name, user3.Name}).Select(
138 "(CASE WHEN age=18 THEN 1 ELSE 2 END) as age", "name",
139 ).Count(&count8).Find(&users).Error; err != nil || count8 != 3 {
140 t.Fatalf("Count should work, but got err %v", err)
141 }
142
143 expects = []User{{Name: "count-1", Age: 1}, {Name: "count-2", Age: 1}, {Name: "count-3", Age: 1}}
144 sort.SliceStable(users, func(i, j int) bool {
145 return strings.Compare(users[i].Name, users[j].Name) < 0
146 })
147
148 AssertEqual(t, users, expects)
149
150 var count9 int64
151 if err := DB.Scopes(func(tx *gorm.DB) *gorm.DB {
152 return tx.Table("users")
153 }).Where("name in ?", []string{user1.Name, user2.Name, user3.Name}).Count(&count9).Find(&users).Error; err != nil || count9 != 3 {
154 t.Fatalf("Count should work, but got err %v", err)
155 }
156
157 var count10 int64
158 if err := DB.Model(&User{}).Select("*").Where("name in ?", []string{user1.Name, user2.Name, user3.Name}).Count(&count10).Error; err != nil || count10 != 3 {
159 t.Fatalf("Count should be 3, but got count: %v err %v", count10, err)
160 }
161
162 var count11 int64
163 sameUsers := make([]*User, 0)
164 for i := 0; i < 3; i++ {
165 sameUsers = append(sameUsers, GetUser("count-4", Config{}))
166 }
167 DB.Create(sameUsers)
168
169 if err := DB.Model(&User{}).Where("name = ?", "count-4").Group("name").Count(&count11).Error; err != nil || count11 != 1 {
170 t.Fatalf("Count should be 1, but got count: %v err %v", count11, err)
171 }
172
173 var count12 int64
174 if err := DB.Table("users").
175 Where("name in ?", []string{user1.Name, user2.Name, user3.Name}).
176 Preload("Toys", func(db *gorm.DB) *gorm.DB {
177 return db.Table("toys").Select("name")
178 }).Count(&count12).Error; err == nil {
179 t.Errorf("error should raise when using preload without schema")
180 }
181
182 var count13 int64
183 if err := DB.Model(User{}).
184 Where("name in ?", []string{user1.Name, user2.Name, user3.Name}).
185 Preload("Toys", func(db *gorm.DB) *gorm.DB {
186 return db.Table("toys").Select("name")
187 }).Count(&count13).Error; err != nil {
188 t.Errorf("no error should raise when using count with preload, but got %v", err)
189 }
190 }