forked from gobuffalo/pop
/
belongs_to_association_test.go
71 lines (54 loc) · 1.45 KB
/
belongs_to_association_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package associations_test
import (
"reflect"
"testing"
"github.com/gobuffalo/pop/v6/associations"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/require"
)
type fooBelongsTo struct {
ID uuid.UUID `db:"id"`
}
func (f fooBelongsTo) TableName() string {
return "foosy"
}
type barBelongsTo struct {
FooID uuid.UUID `db:"foo_id"`
Foo fooBelongsTo `belongs_to:"foo"`
}
type barBelongsToNullable struct {
FooID uuid.NullUUID `db:"foo_id"`
Foo *fooBelongsTo `belongs_to:"foo"`
}
func Test_Belongs_To_Association(t *testing.T) {
a := require.New(t)
id, _ := uuid.NewV1()
bar := barBelongsTo{FooID: id}
as, err := associations.ForStruct(&bar, "Foo")
a.NoError(err)
a.Equal(len(as), 1)
a.Equal(reflect.Struct, as[0].Kind())
where, args := as[0].Constraint()
a.Equal("id = ?", where)
a.Equal(id, args[0].(uuid.UUID))
bar2 := barBelongsTo{FooID: uuid.Nil}
as, err = associations.ForStruct(&bar2, "Foo")
a.NoError(err)
a.Equal(len(as), 1)
a.Equal(reflect.Struct, as[0].Kind())
before := as.AssociationsBeforeCreatable()
for index := range before {
a.Equal(nil, before[index].BeforeInterface())
}
}
func Test_Belongs_To_Nullable_Association(t *testing.T) {
a := require.New(t)
id, _ := uuid.NewV1()
bar := barBelongsToNullable{Foo: &fooBelongsTo{id}}
as, err := associations.ForStruct(&bar, "Foo")
a.NoError(err)
before := as.AssociationsBeforeCreatable()
for index := range before {
a.Equal(nil, before[index].BeforeSetup())
}
}