forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities_test.go
67 lines (59 loc) · 1.33 KB
/
utilities_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
package gorm
import (
"context"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type Human struct {
Name string
Age uint32 `gorm:"column:years"`
Child *Child
}
func (p Human) TableName() string {
return "db_humans"
}
type Child struct {
Name string
}
func TestHandleFieldPath(t *testing.T) {
tests := []struct {
fieldPath []string
dbName string
assoc string
err bool
}{
{[]string{"name"}, "db_humans.name", "", false},
{[]string{"age"}, "db_humans.years", "", false},
{[]string{"child", "name"}, "child.name", "Child", false},
{[]string{}, "", "", true},
}
for _, test := range tests {
dbName, assoc, err := HandleFieldPath(context.Background(), test.fieldPath, &Human{})
if test.err {
assert.Equal(t, "", dbName)
assert.Equal(t, "", assoc)
assert.NotNil(t, err)
} else {
assert.Equal(t, test.dbName, dbName)
assert.Equal(t, test.assoc, assoc)
assert.Nil(t, err)
}
}
}
func TestIsModel(t *testing.T) {
for tName, tCase := range map[string]struct {
input reflect.Type
want bool
}{
"time.Time": {reflect.TypeOf(time.Time{}), false},
"*time.Time": {reflect.TypeOf(&time.Time{}), false},
} {
t.Run(tName, func(t *testing.T) {
if got, want := isModel(tCase.input), tCase.want; got != want {
t.Errorf("got %v; want %v", got, want)
}
})
}
}