forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorp_mapping_test.go
137 lines (128 loc) · 2.95 KB
/
gorp_mapping_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//Original: https://github.com/paulquerna-okta/gorp/blob/1fb48e4c1f26abac8d69b7b2cca5aeafde0c3532/mapping_test.go
package gorp
import (
"reflect"
"sync"
"testing"
"time"
)
type testUser struct {
ID uint64 `db:"id"`
Username string `db:"user_name"`
HashedPassword []byte `db:"hashed_password"`
EMail string `db:"email"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type testCoolUser struct {
testUser
IsCool bool `db:"is_cool"`
BestFriends []string `db:"best_friends"`
}
func BenchmarkColumnToFieldIndex(b *testing.B) {
structType := reflect.TypeOf(testUser{})
dbmap := &DbMap{Cache: &sync.Map{}}
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := columnToFieldIndex(dbmap,
structType,
[]string{
"user_name",
"email",
"created_at",
"updated_at",
"id",
})
if err != nil {
panic(err)
}
}
}
func TestColumnToFieldIndexBasic(t *testing.T) {
structType := reflect.TypeOf(testUser{})
dbmap := &DbMap{}
cols, err := columnToFieldIndex(dbmap,
structType,
[]string{
"email",
})
if err != nil {
t.Fatal(err)
}
if len(cols) != 1 {
t.Fatal("cols should have 1 result", cols)
}
if cols[0][0] != 3 {
t.Fatal("cols[0][0] should map to email field in testUser", cols)
}
}
func TestColumnToFieldIndexSome(t *testing.T) {
structType := reflect.TypeOf(testUser{})
dbmap := &DbMap{}
cols, err := columnToFieldIndex(dbmap,
structType,
[]string{
"id",
"email",
"created_at",
})
if err != nil {
t.Fatal(err)
}
if len(cols) != 3 {
t.Fatal("cols should have 3 results", cols)
}
if cols[0][0] != 0 {
t.Fatal("cols[0][0] should map to id field in testUser", cols)
}
if cols[1][0] != 3 {
t.Fatal("cols[1][0] should map to email field in testUser", cols)
}
if cols[2][0] != 4 {
t.Fatal("cols[2][0] should map to created_at field in testUser", cols)
}
}
func TestColumnToFieldIndexEmbedded(t *testing.T) {
structType := reflect.TypeOf(testCoolUser{})
dbmap := &DbMap{}
cols, err := columnToFieldIndex(dbmap,
structType,
[]string{
"id",
"email",
"is_cool",
})
if err != nil {
t.Fatal(err)
}
if len(cols) != 3 {
t.Fatal("cols should have 3 results", cols)
}
if cols[0][0] != 0 && cols[0][1] != 0 {
t.Fatal("cols[0][0] should map to id field in testCoolUser", cols)
}
if cols[1][0] != 0 && cols[1][1] != 3 {
t.Fatal("cols[1][0] should map to email field in testCoolUser", cols)
}
if cols[2][0] != 1 {
t.Fatal("cols[2][0] should map to is_cool field in testCoolUser", cols)
}
}
func TestColumnToFieldIndexEmbeddedFriends(t *testing.T) {
structType := reflect.TypeOf(testCoolUser{})
dbmap := &DbMap{}
cols, err := columnToFieldIndex(dbmap,
structType,
[]string{
"best_friends",
})
if err != nil {
t.Fatal(err)
}
if len(cols) != 1 {
t.Fatal("cols should have 1 results", cols)
}
if cols[0][0] != 2 {
t.Fatal("cols[0][0] should map to BestFriends field in testCoolUser", cols)
}
}