gorm/tests/count_test.go

71 lines
2.4 KiB
Go
Raw Normal View History

2020-05-24 06:32:59 +03:00
package tests_test
import (
"fmt"
"regexp"
2020-05-24 06:32:59 +03:00
"testing"
2020-06-23 17:41:41 +03:00
"gorm.io/gorm"
2020-06-02 05:34:50 +03:00
. "gorm.io/gorm/utils/tests"
2020-05-24 06:32:59 +03:00
)
func TestCount(t *testing.T) {
var (
user1 = *GetUser("count-1", Config{})
user2 = *GetUser("count-2", Config{})
user3 = *GetUser("count-3", Config{})
users []User
count, count1, count2 int64
)
DB.Save(&user1).Save(&user2).Save(&user3)
if err := DB.Where("name = ?", user1.Name).Or("name = ?", user3.Name).Find(&users).Count(&count).Error; err != nil {
t.Errorf(fmt.Sprintf("Count should work, but got err %v", err))
2020-07-01 05:19:52 +03:00
}
if count != int64(len(users)) {
t.Errorf("Count() method should get correct value, expect: %v, got %v", count, len(users))
}
if err := DB.Model(&User{}).Where("name = ?", user1.Name).Or("name = ?", user3.Name).Count(&count).Find(&users).Error; err != nil {
t.Errorf(fmt.Sprintf("Count should work, but got err %v", err))
2020-05-24 06:32:59 +03:00
}
if count != int64(len(users)) {
t.Errorf("Count() method should get correct value, expect: %v, got %v", count, len(users))
}
DB.Model(&User{}).Where("name = ?", user1.Name).Count(&count1).Or("name in ?", []string{user2.Name, user3.Name}).Count(&count2)
if count1 != 1 || count2 != 3 {
t.Errorf("multiple count in chain should works")
}
2020-06-23 17:41:41 +03:00
tx := DB.Model(&User{}).Where("name = ?", user1.Name).Session(&gorm.Session{WithConditions: true})
tx.Count(&count1)
tx.Or("name in ?", []string{user2.Name, user3.Name}).Count(&count2)
if count1 != 1 || count2 != 3 {
t.Errorf("count after new session should works")
}
2020-05-24 06:32:59 +03:00
var count3 int64
if err := DB.Model(&User{}).Where("name in ?", []string{user2.Name, user2.Name, user3.Name}).Group("id").Count(&count3).Error; err != nil {
2020-05-24 12:24:23 +03:00
t.Errorf("Error happened when count with group, but got %v", err)
2020-05-24 06:32:59 +03:00
}
if count3 != 2 {
t.Errorf("Should get correct count for count with group, but got %v", count3)
}
dryDB := DB.Session(&gorm.Session{DryRun: true})
result := dryDB.Table("users").Select("name").Count(&count)
if !regexp.MustCompile(`SELECT COUNT\(.name.\) FROM .*users.*`).MatchString(result.Statement.SQL.String()) {
t.Fatalf("Build count with select, but got %v", result.Statement.SQL.String())
}
result = dryDB.Table("users").Distinct("name").Count(&count)
if !regexp.MustCompile(`SELECT COUNT\(DISTINCT\(.name.\)\) FROM .*users.*`).MatchString(result.Statement.SQL.String()) {
t.Fatalf("Build count with select, but got %v", result.Statement.SQL.String())
}
2020-05-24 06:32:59 +03:00
}