github.com/paweljw/pop@v4.13.1+incompatible/associations/has_many_association_test.go (about) 1 package associations_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/gobuffalo/nulls" 8 "github.com/gobuffalo/pop/associations" 9 "github.com/stretchr/testify/require" 10 ) 11 12 type FooHasMany struct { 13 ID int `db:"id"` 14 BarHasManies *barHasManies `has_many:"bar_has_manies"` 15 } 16 17 type barHasMany struct { 18 Title string `db:"title"` 19 FooHasManyID nulls.Int `db:"foo_has_many_id"` 20 } 21 22 type barHasManies []barHasMany 23 24 func Test_Has_Many_Association(t *testing.T) { 25 a := require.New(t) 26 27 id := 1 28 foo := FooHasMany{ID: 1} 29 30 as, err := associations.ForStruct(&foo) 31 32 a.NoError(err) 33 a.Equal(len(as), 1) 34 a.Equal(reflect.Slice, as[0].Kind()) 35 36 where, args := as[0].Constraint() 37 a.Equal("foo_has_many_id = ?", where) 38 a.Equal(id, args[0].(int)) 39 } 40 41 func Test_Has_Many_SetValue(t *testing.T) { 42 a := require.New(t) 43 foo := FooHasMany{ID: 1, BarHasManies: &barHasManies{{Title: "bar"}}} 44 45 as, _ := associations.ForStruct(&foo) 46 a.Equal(len(as), 1) 47 48 ca, ok := as[0].(associations.AssociationAfterCreatable) 49 a.True(ok) 50 51 a.NoError(ca.AfterSetup()) 52 a.Equal(foo.ID, (*foo.BarHasManies)[0].FooHasManyID.Interface().(int)) 53 }