github.com/tufanbarisyildirim/pop@v4.13.1+incompatible/associations/many_to_many_association_test.go (about) 1 package associations_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/gobuffalo/pop/associations" 8 "github.com/gofrs/uuid" 9 "github.com/stretchr/testify/require" 10 ) 11 12 type fooManyToMany struct { 13 ID uuid.UUID `db:"id"` 14 BarManyToManies barManyToManies `many_to_many:"foos_and_bars"` 15 } 16 17 type fooManyToMany2 struct { 18 ID uuid.UUID `db:"id"` 19 BarManyToManies barManyToManies `many_to_many:"foos_and_bars" primary_id:"fufu_id"` 20 } 21 22 type barManyToMany struct { 23 ID uuid.UUID `db:"id"` 24 } 25 26 type barManyToManies []barManyToMany 27 28 func (b barManyToManies) TableName() string { 29 return "bars" 30 } 31 32 func Test_Many_To_Many_Association(t *testing.T) { 33 a := require.New(t) 34 35 id, _ := uuid.NewV1() 36 foo := fooManyToMany{ID: id} 37 38 as, err := associations.ForStruct(&foo) 39 40 a.NoError(err) 41 a.Equal(len(as), 1) 42 43 a.Equal(reflect.Slice, as[0].Kind()) 44 45 where, args := as[0].Constraint() 46 a.Equal("id in (select bar_many_to_many_id from foos_and_bars where foo_many_to_many_id = ?)", where) 47 a.Equal(id, args[0].(uuid.UUID)) 48 } 49 50 func Test_Many_To_Many_PrimaryId_Tag(t *testing.T) { 51 a := require.New(t) 52 53 id, _ := uuid.NewV1() 54 foo := fooManyToMany2{ID: id} 55 56 as, err := associations.ForStruct(&foo) 57 58 a.NoError(err) 59 a.Equal(len(as), 1) 60 61 a.Equal(reflect.Slice, as[0].Kind()) 62 63 where, args := as[0].Constraint() 64 a.Equal("id in (select bar_many_to_many_id from foos_and_bars where fufu_id = ?)", where) 65 a.Equal(id, args[0].(uuid.UUID)) 66 }