2020-05-23 18:50:48 +03:00
package tests_test
import (
2020-08-23 05:40:37 +03:00
"regexp"
2020-05-23 18:50:48 +03:00
"sort"
"testing"
2020-06-02 04:16:07 +03:00
"gorm.io/gorm"
2020-06-02 05:34:50 +03:00
. "gorm.io/gorm/utils/tests"
2020-05-23 18:50:48 +03:00
)
func TestJoins ( t * testing . T ) {
user := * GetUser ( "joins-1" , Config { Company : true , Manager : true , Account : true } )
DB . Create ( & user )
var user2 User
if err := DB . Joins ( "Company" ) . Joins ( "Manager" ) . Joins ( "Account" ) . First ( & user2 , "users.name = ?" , user . Name ) . Error ; err != nil {
t . Fatalf ( "Failed to load with joins, got error: %v" , err )
}
CheckUser ( t , user2 , user )
}
func TestJoinsForSlice ( t * testing . T ) {
users := [ ] User {
* GetUser ( "slice-joins-1" , Config { Company : true , Manager : true , Account : true } ) ,
* GetUser ( "slice-joins-2" , Config { Company : true , Manager : true , Account : true } ) ,
* GetUser ( "slice-joins-3" , Config { Company : true , Manager : true , Account : true } ) ,
}
DB . Create ( & users )
var userIDs [ ] uint
for _ , user := range users {
userIDs = append ( userIDs , user . ID )
}
var users2 [ ] User
if err := DB . Joins ( "Company" ) . Joins ( "Manager" ) . Joins ( "Account" ) . Find ( & users2 , "users.id IN ?" , userIDs ) . Error ; err != nil {
t . Fatalf ( "Failed to load with joins, got error: %v" , err )
} else if len ( users2 ) != len ( users ) {
t . Fatalf ( "Failed to load join users, got: %v, expect: %v" , len ( users2 ) , len ( users ) )
}
sort . Slice ( users2 , func ( i , j int ) bool {
return users2 [ i ] . ID > users2 [ j ] . ID
} )
sort . Slice ( users , func ( i , j int ) bool {
return users [ i ] . ID > users [ j ] . ID
} )
for idx , user := range users {
CheckUser ( t , user , users2 [ idx ] )
}
}
2020-05-31 15:21:52 +03:00
func TestJoinConds ( t * testing . T ) {
var user = * GetUser ( "joins-conds" , Config { Account : true , Pets : 3 } )
DB . Save ( & user )
var users1 [ ] User
2020-12-07 05:31:06 +03:00
DB . Joins ( "inner join pets on pets.user_id = users.id" ) . Where ( "users.name = ?" , user . Name ) . Find ( & users1 )
2020-05-31 15:21:52 +03:00
if len ( users1 ) != 3 {
t . Errorf ( "should find two users using left join, but got %v" , len ( users1 ) )
}
var users2 [ ] User
2020-12-07 05:31:06 +03:00
DB . Joins ( "inner join pets on pets.user_id = users.id AND pets.name = ?" , user . Pets [ 0 ] . Name ) . Where ( "users.name = ?" , user . Name ) . First ( & users2 )
2020-05-31 15:21:52 +03:00
if len ( users2 ) != 1 {
t . Errorf ( "should find one users using left join with conditions, but got %v" , len ( users2 ) )
}
var users3 [ ] User
2020-12-07 05:31:06 +03:00
DB . Joins ( "inner join pets on pets.user_id = users.id AND pets.name = ?" , user . Pets [ 0 ] . Name ) . Joins ( "join accounts on accounts.user_id = users.id AND accounts.number = ?" , user . Account . Number ) . Where ( "users.name = ?" , user . Name ) . First ( & users3 )
2020-05-31 15:21:52 +03:00
if len ( users3 ) != 1 {
t . Errorf ( "should find one users using multiple left join conditions, but got %v" , len ( users3 ) )
}
var users4 [ ] User
2020-12-07 05:31:06 +03:00
DB . Joins ( "inner join pets on pets.user_id = users.id AND pets.name = ?" , user . Pets [ 0 ] . Name ) . Joins ( "join accounts on accounts.user_id = users.id AND accounts.number = ?" , user . Account . Number + "non-exist" ) . Where ( "users.name = ?" , user . Name ) . First ( & users4 )
2020-05-31 15:21:52 +03:00
if len ( users4 ) != 0 {
t . Errorf ( "should find no user when searching with unexisting credit card, but got %v" , len ( users4 ) )
}
var users5 [ ] User
2020-12-07 05:31:06 +03:00
db5 := DB . Joins ( "inner join pets on pets.user_id = users.id AND pets.name = ?" , user . Pets [ 0 ] . Name ) . Joins ( "join accounts on accounts.user_id = users.id AND accounts.number = ?" , user . Account . Number ) . Where ( User { Model : gorm . Model { ID : 1 } } ) . Where ( Account { Model : gorm . Model { ID : 1 } } ) . Not ( Pet { Model : gorm . Model { ID : 1 } } ) . Find ( & users5 )
2020-05-31 15:21:52 +03:00
if db5 . Error != nil {
t . Errorf ( "Should not raise error for join where identical fields in different tables. Error: %s" , db5 . Error . Error ( ) )
}
2020-08-23 05:40:37 +03:00
2020-12-07 05:31:06 +03:00
var users6 [ ] User
DB . Joins ( "inner join pets on pets.user_id = users.id AND pets.name = @Name" , user . Pets [ 0 ] ) . Where ( "users.name = ?" , user . Name ) . First ( & users6 )
if len ( users6 ) != 1 {
t . Errorf ( "should find one users using left join with conditions, but got %v" , len ( users6 ) )
}
2020-08-23 05:40:37 +03:00
dryDB := DB . Session ( & gorm . Session { DryRun : true } )
stmt := dryDB . Joins ( "left join pets on pets.user_id = users.id AND pets.name = ?" , user . Pets [ 0 ] . Name ) . Joins ( "join accounts on accounts.user_id = users.id AND accounts.number = ?" , user . Account . Number ) . Where ( User { Model : gorm . Model { ID : 1 } } ) . Where ( Account { Model : gorm . Model { ID : 1 } } ) . Not ( Pet { Model : gorm . Model { ID : 1 } } ) . Find ( & users5 ) . Statement
if ! regexp . MustCompile ( "SELECT .* FROM .users. left join pets.*join accounts.*" ) . MatchString ( stmt . SQL . String ( ) ) {
t . Errorf ( "joins should be ordered, but got %v" , stmt . SQL . String ( ) )
}
2020-05-31 15:21:52 +03:00
}
2021-07-17 16:45:15 +03:00
func TestJoinOn ( t * testing . T ) {
var user = * GetUser ( "joins-on" , Config { Pets : 2 } )
DB . Save ( & user )
var user1 User
onQuery := DB . Select ( "id" ) . Where ( "user_id = users.id AND name = ?" , "joins-on_pet_1" ) . Model ( & Pet { } )
2021-08-28 11:27:19 +03:00
if err := DB . Joins ( "NamedPet" , onQuery ) . Where ( "users.name = ?" , user . Name ) . First ( & user1 ) . Error ; err != nil {
2021-07-17 16:45:15 +03:00
t . Fatalf ( "Failed to load with joins on, got error: %v" , err )
}
AssertEqual ( t , user1 . NamedPet . Name , "joins-on_pet_1" )
onQuery2 := DB . Select ( "id" ) . Where ( "user_id = users.id AND name = ?" , "joins-on_pet_2" ) . Model ( & Pet { } )
var user2 User
2021-08-28 11:27:19 +03:00
if err := DB . Joins ( "NamedPet" , onQuery2 ) . Where ( "users.name = ?" , user . Name ) . First ( & user2 ) . Error ; err != nil {
2021-07-17 16:45:15 +03:00
t . Fatalf ( "Failed to load with joins on, got error: %v" , err )
}
AssertEqual ( t , user2 . NamedPet . Name , "joins-on_pet_2" )
}
2020-05-31 15:21:52 +03:00
func TestJoinsWithSelect ( t * testing . T ) {
type result struct {
2020-05-31 15:42:07 +03:00
ID uint
PetID uint
Name string
2020-05-31 15:21:52 +03:00
}
user := * GetUser ( "joins_with_select" , Config { Pets : 2 } )
DB . Save ( & user )
var results [ ] result
2020-07-05 17:12:52 +03:00
2020-05-31 15:42:07 +03:00
DB . Table ( "users" ) . Select ( "users.id, pets.id as pet_id, pets.name" ) . Joins ( "left join pets on pets.user_id = users.id" ) . Where ( "users.name = ?" , "joins_with_select" ) . Scan ( & results )
sort . Slice ( results , func ( i , j int ) bool {
return results [ i ] . PetID > results [ j ] . PetID
} )
sort . Slice ( results , func ( i , j int ) bool {
return user . Pets [ i ] . ID > user . Pets [ j ] . ID
} )
2020-05-31 15:21:52 +03:00
if len ( results ) != 2 || results [ 0 ] . Name != user . Pets [ 0 ] . Name || results [ 1 ] . Name != user . Pets [ 1 ] . Name {
2020-05-31 15:42:07 +03:00
t . Errorf ( "Should find all two pets with Join select, got %+v" , results )
2020-05-31 15:21:52 +03:00
}
}