| 1 |
package tests_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"regexp" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"gorm.io/gorm" |
| 8 |
. "gorm.io/gorm/utils/tests" |
| 9 |
) |
| 10 |
|
| 11 |
func TestDistinct(t *testing.T) { |
| 12 |
users := []User{ |
| 13 |
*GetUser("distinct", Config{}), |
| 14 |
*GetUser("distinct", Config{}), |
| 15 |
*GetUser("distinct", Config{}), |
| 16 |
*GetUser("distinct-2", Config{}), |
| 17 |
*GetUser("distinct-3", Config{}), |
| 18 |
} |
| 19 |
users[0].Age = 20 |
| 20 |
|
| 21 |
if err := DB.Create(&users).Error; err != nil { |
| 22 |
t.Fatalf("errors happened when create users: %v", err) |
| 23 |
} |
| 24 |
|
| 25 |
var names []string |
| 26 |
DB.Table("users").Where("name like ?", "distinct%").Order("name").Pluck("name", &names) |
| 27 |
AssertEqual(t, names, []string{"distinct", "distinct", "distinct", "distinct-2", "distinct-3"}) |
| 28 |
|
| 29 |
var names1 []string |
| 30 |
DB.Model(&User{}).Where("name like ?", "distinct%").Distinct().Order("name").Pluck("Name", &names1) |
| 31 |
|
| 32 |
AssertEqual(t, names1, []string{"distinct", "distinct-2", "distinct-3"}) |
| 33 |
|
| 34 |
var names2 []string |
| 35 |
DB.Scopes(func(db *gorm.DB) *gorm.DB { |
| 36 |
return db.Table("users") |
| 37 |
}).Where("name like ?", "distinct%").Order("name").Pluck("name", &names2) |
| 38 |
AssertEqual(t, names2, []string{"distinct", "distinct", "distinct", "distinct-2", "distinct-3"}) |
| 39 |
|
| 40 |
var results []User |
| 41 |
if err := DB.Distinct("name", "age").Where("name like ?", "distinct%").Order("name, age desc").Find(&results).Error; err != nil { |
| 42 |
t.Errorf("failed to query users, got error: %v", err) |
| 43 |
} |
| 44 |
|
| 45 |
expects := []User{ |
| 46 |
{Name: "distinct", Age: 20}, |
| 47 |
{Name: "distinct", Age: 18}, |
| 48 |
{Name: "distinct-2", Age: 18}, |
| 49 |
{Name: "distinct-3", Age: 18}, |
| 50 |
} |
| 51 |
|
| 52 |
if len(results) != 4 { |
| 53 |
t.Fatalf("invalid results length found, expects: %v, got %v", len(expects), len(results)) |
| 54 |
} |
| 55 |
|
| 56 |
for idx, expect := range expects { |
| 57 |
AssertObjEqual(t, results[idx], expect, "Name", "Age") |
| 58 |
} |
| 59 |
|
| 60 |
var count int64 |
| 61 |
if err := DB.Model(&User{}).Where("name like ?", "distinct%").Count(&count).Error; err != nil || count != 5 { |
| 62 |
t.Errorf("failed to query users count, got error: %v, count: %v", err, count) |
| 63 |
} |
| 64 |
|
| 65 |
if err := DB.Model(&User{}).Distinct("name").Where("name like ?", "distinct%").Count(&count).Error; err != nil || count != 3 { |
| 66 |
t.Errorf("failed to query users count, got error: %v, count %v", err, count) |
| 67 |
} |
| 68 |
|
| 69 |
dryDB := DB.Session(&gorm.Session{DryRun: true}) |
| 70 |
r := dryDB.Distinct("u.id, u.*").Table("user_speaks as s").Joins("inner join users as u on u.id = s.user_id").Where("s.language_code ='US' or s.language_code ='ES'").Find(&User{}) |
| 71 |
if !regexp.MustCompile(`SELECT DISTINCT u\.id, u\.\* FROM user_speaks as s inner join users as u`).MatchString(r.Statement.SQL.String()) { |
| 72 |
t.Fatalf("Build Distinct with u.*, but got %v", r.Statement.SQL.String()) |
| 73 |
} |
| 74 |
} |