2020-06-05 14:19:08 +03:00
package tests_test
import (
2020-07-17 06:24:24 +03:00
"regexp"
2020-06-05 14:19:08 +03:00
"testing"
2020-07-17 06:24:24 +03:00
"gorm.io/gorm"
2020-06-05 14:19:08 +03:00
. "gorm.io/gorm/utils/tests"
)
func TestDistinct ( t * testing . T ) {
2022-01-06 10:02:53 +03:00
users := [ ] User {
2020-06-05 14:19:08 +03:00
* GetUser ( "distinct" , Config { } ) ,
* GetUser ( "distinct" , Config { } ) ,
* GetUser ( "distinct" , Config { } ) ,
* GetUser ( "distinct-2" , Config { } ) ,
* GetUser ( "distinct-3" , Config { } ) ,
}
users [ 0 ] . Age = 20
if err := DB . Create ( & users ) . Error ; err != nil {
t . Fatalf ( "errors happened when create users: %v" , err )
}
var names [ ] string
2020-06-09 10:34:55 +03:00
DB . Table ( "users" ) . Where ( "name like ?" , "distinct%" ) . Order ( "name" ) . Pluck ( "name" , & names )
2020-06-05 14:19:08 +03:00
AssertEqual ( t , names , [ ] string { "distinct" , "distinct" , "distinct" , "distinct-2" , "distinct-3" } )
var names1 [ ] string
DB . Model ( & User { } ) . Where ( "name like ?" , "distinct%" ) . Distinct ( ) . Order ( "name" ) . Pluck ( "Name" , & names1 )
AssertEqual ( t , names1 , [ ] string { "distinct" , "distinct-2" , "distinct-3" } )
2021-09-27 17:11:29 +03:00
var names2 [ ] string
DB . Scopes ( func ( db * gorm . DB ) * gorm . DB {
return db . Table ( "users" )
} ) . Where ( "name like ?" , "distinct%" ) . Order ( "name" ) . Pluck ( "name" , & names2 )
AssertEqual ( t , names2 , [ ] string { "distinct" , "distinct" , "distinct" , "distinct-2" , "distinct-3" } )
2020-06-05 14:19:08 +03:00
var results [ ] User
if err := DB . Distinct ( "name" , "age" ) . Where ( "name like ?" , "distinct%" ) . Order ( "name, age desc" ) . Find ( & results ) . Error ; err != nil {
t . Errorf ( "failed to query users, got error: %v" , err )
}
expects := [ ] User {
{ Name : "distinct" , Age : 20 } ,
{ Name : "distinct" , Age : 18 } ,
{ Name : "distinct-2" , Age : 18 } ,
{ Name : "distinct-3" , Age : 18 } ,
}
if len ( results ) != 4 {
t . Fatalf ( "invalid results length found, expects: %v, got %v" , len ( expects ) , len ( results ) )
}
for idx , expect := range expects {
AssertObjEqual ( t , results [ idx ] , expect , "Name" , "Age" )
}
var count int64
if err := DB . Model ( & User { } ) . Where ( "name like ?" , "distinct%" ) . Count ( & count ) . Error ; err != nil || count != 5 {
t . Errorf ( "failed to query users count, got error: %v, count: %v" , err , count )
}
if err := DB . Model ( & User { } ) . Distinct ( "name" ) . Where ( "name like ?" , "distinct%" ) . Count ( & count ) . Error ; err != nil || count != 3 {
t . Errorf ( "failed to query users count, got error: %v, count %v" , err , count )
}
2020-07-17 06:24:24 +03:00
dryDB := DB . Session ( & gorm . Session { DryRun : true } )
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 { } )
if ! regexp . MustCompile ( ` SELECT DISTINCT u\.id, u\.\* FROM user_speaks as s inner join users as u ` ) . MatchString ( r . Statement . SQL . String ( ) ) {
t . Fatalf ( "Build Distinct with u.*, but got %v" , r . Statement . SQL . String ( ) )
}
2020-06-05 14:19:08 +03:00
}