2015-02-28 09:16:51 +03:00
package gorm_test
import (
"fmt"
"testing"
"time"
"github.com/jinzhu/gorm"
)
type Person struct {
Id int
Name string
Addresses [ ] * Address ` gorm:"many2many:person_addresses;" `
}
type PersonAddress struct {
2015-03-20 06:11:30 +03:00
gorm . JoinTableHandler
2015-02-28 09:16:51 +03:00
PersonID int
AddressID int
2016-01-10 16:33:15 +03:00
DeletedAt * time . Time
2015-02-28 09:16:51 +03:00
CreatedAt time . Time
}
2015-06-19 06:32:11 +03:00
func ( * PersonAddress ) Add ( handler gorm . JoinTableHandlerInterface , db * gorm . DB , foreignValue interface { } , associationValue interface { } ) error {
2015-02-28 09:16:51 +03:00
return db . Where ( map [ string ] interface { } {
2015-03-19 13:23:54 +03:00
"person_id" : db . NewScope ( foreignValue ) . PrimaryKeyValue ( ) ,
"address_id" : db . NewScope ( associationValue ) . PrimaryKeyValue ( ) ,
2015-02-28 09:16:51 +03:00
} ) . Assign ( map [ string ] interface { } {
2015-03-19 13:23:54 +03:00
"person_id" : foreignValue ,
"address_id" : associationValue ,
2015-03-20 06:11:30 +03:00
"deleted_at" : gorm . Expr ( "NULL" ) ,
2015-02-28 09:16:51 +03:00
} ) . FirstOrCreate ( & PersonAddress { } ) . Error
}
2015-06-19 06:32:11 +03:00
func ( * PersonAddress ) Delete ( handler gorm . JoinTableHandlerInterface , db * gorm . DB , sources ... interface { } ) error {
2015-02-28 09:16:51 +03:00
return db . Delete ( & PersonAddress { } ) . Error
}
2015-06-19 06:32:11 +03:00
func ( pa * PersonAddress ) JoinWith ( handler gorm . JoinTableHandlerInterface , db * gorm . DB , source interface { } ) * gorm . DB {
2015-03-18 13:14:28 +03:00
table := pa . Table ( db )
2015-03-19 13:30:35 +03:00
return db . Table ( table ) . Joins ( "INNER JOIN person_addresses ON person_addresses.address_id = addresses.id" ) . Where ( fmt . Sprintf ( "%v.deleted_at IS NULL OR %v.deleted_at <= '0001-01-02'" , table , table ) )
2015-02-28 09:16:51 +03:00
}
func TestJoinTable ( t * testing . T ) {
DB . Exec ( "drop table person_addresses;" )
DB . AutoMigrate ( & Person { } )
2015-03-19 13:23:54 +03:00
DB . SetJoinTableHandler ( & Person { } , "Addresses" , & PersonAddress { } )
2015-02-28 09:16:51 +03:00
address1 := & Address { Address1 : "address 1" }
address2 := & Address { Address1 : "address 2" }
person := & Person { Name : "person" , Addresses : [ ] * Address { address1 , address2 } }
DB . Save ( person )
DB . Model ( person ) . Association ( "Addresses" ) . Delete ( address1 )
if DB . Find ( & [ ] PersonAddress { } , "person_id = ?" , person . Id ) . RowsAffected != 1 {
t . Errorf ( "Should found one address" )
}
2015-03-19 13:30:35 +03:00
if DB . Model ( person ) . Association ( "Addresses" ) . Count ( ) != 1 {
2015-02-28 09:16:51 +03:00
t . Errorf ( "Should found one address" )
}
if DB . Unscoped ( ) . Find ( & [ ] PersonAddress { } , "person_id = ?" , person . Id ) . RowsAffected != 2 {
t . Errorf ( "Found two addresses with Unscoped" )
}
if DB . Model ( person ) . Association ( "Addresses" ) . Clear ( ) ; DB . Model ( person ) . Association ( "Addresses" ) . Count ( ) != 0 {
t . Errorf ( "Should deleted all addresses" )
}
}