github.com/paweljw/pop@v4.13.1+incompatible/associations/belongs_to_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 fooBelongsTo struct {
    13  	ID uuid.UUID `db:"id"`
    14  }
    15  
    16  func (f fooBelongsTo) TableName() string {
    17  	return "foosy"
    18  }
    19  
    20  type barBelongsTo struct {
    21  	FooID uuid.UUID    `db:"foo_id"`
    22  	Foo   fooBelongsTo `belongs_to:"foo"`
    23  }
    24  
    25  func Test_Belongs_To_Association(t *testing.T) {
    26  	a := require.New(t)
    27  
    28  	id, _ := uuid.NewV1()
    29  	bar := barBelongsTo{FooID: id}
    30  
    31  	as, err := associations.ForStruct(&bar, "Foo")
    32  	a.NoError(err)
    33  	a.Equal(len(as), 1)
    34  	a.Equal(reflect.Struct, as[0].Kind())
    35  
    36  	where, args := as[0].Constraint()
    37  	a.Equal("id = ?", where)
    38  	a.Equal(id, args[0].(uuid.UUID))
    39  
    40  	bar2 := barBelongsTo{FooID: uuid.Nil}
    41  	as, err = associations.ForStruct(&bar2, "Foo")
    42  
    43  	a.NoError(err)
    44  	a.Equal(len(as), 1)
    45  	a.Equal(reflect.Struct, as[0].Kind())
    46  
    47  	before := as.AssociationsBeforeCreatable()
    48  
    49  	for index := range before {
    50  		a.Equal(nil, before[index].BeforeInterface())
    51  	}
    52  }