-
Notifications
You must be signed in to change notification settings - Fork 63
/
bulk_insert_test.go
168 lines (143 loc) · 3.95 KB
/
bulk_insert_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package gormbulk
import (
"database/sql"
"sort"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type fakeRelationTable struct{}
type fakeTable struct {
ID int `gorm:"primary_key;auto_increment"`
Name string
Email string `gorm:"default:default@mail.com"`
Relation *fakeRelationTable `gorm:"foreignkey:RelationID"`
Relations []fakeRelationTable `gorm:"foreignkey:UserRefer"`
Message sql.NullString
Publish bool
CreatedAt time.Time
UpdatedAt time.Time
}
func Test_extractMapValue(t *testing.T) {
collectKeys := func(val map[string]interface{}) []string {
keys := make([]string, 0, len(val))
for key := range val {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
notNow := time.Now().Add(-3600 * time.Second)
value := fakeTable{
Name: "name1",
Email: "test1@test.com",
Relation: &fakeRelationTable{},
Message: sql.NullString{String: "message1", Valid: true},
CreatedAt: notNow,
Publish: false,
}
// test without excluding columns
fullKeys := []string{"name", "email", "message", "publish", "created_at", "updated_at"}
sort.Strings(fullKeys)
mapVal, err := extractMapValue(value, []string{})
assert.NoError(t, err)
// Ensure we kept the CreatedAt time
createdAt, ok := mapVal["created_at"].(time.Time)
require.True(t, ok)
assert.True(t, createdAt.Before(time.Now().Add(-100*time.Second)))
// Ensure we set default UpdatedAt time
updatedAt, ok := mapVal["updated_at"].(time.Time)
require.True(t, ok)
assert.True(t, updatedAt.After(time.Now().Add(-1*time.Second)))
mapKeys := collectKeys(mapVal)
assert.Equal(t, fullKeys, mapKeys)
// test with excluding columns
excludedVal, err := extractMapValue(value, []string{"Email", "CreatedAt"})
assert.NoError(t, err)
excludedKeys := collectKeys(excludedVal)
assert.NotContains(t, excludedKeys, "email")
assert.NotContains(t, excludedKeys, "created_at")
}
func Test_insertObject(t *testing.T) {
type Table struct {
RegularColumn string
Custom string `gorm:"column:ThisIsCamelCase"`
}
db, mock, err := sqlmock.New()
require.NoError(t, err)
defer db.Close()
gdb, err := gorm.Open("mysql", db)
require.NoError(t, err)
mock.ExpectExec(
"INSERT INTO `tables` \\(`ThisIsCamelCase`, `regular_column`\\)",
).WithArgs(
"first custom", "first regular",
"second custom", "second regular",
).WillReturnResult(
sqlmock.NewResult(1, 1),
)
err = insertObjSet(gdb, []interface{}{
Table{
RegularColumn: "first regular",
Custom: "first custom",
},
Table{
RegularColumn: "second regular",
Custom: "second custom",
},
})
require.NoError(t, err)
}
func Test_fieldIsAutoIncrement(t *testing.T) {
type explicitSetTable struct {
ID int `gorm:"column:id;auto_increment"`
}
type notSpecifiedTable struct {
ID int `gorm:"column:id"`
}
type primaryKeyTable struct {
ID int `gorm:"column:id;primary_key"`
}
type autoIncrementTable struct {
ID int `gorm:"column:id;primary_key;auto_increment:false"`
}
cases := []struct {
Value interface{}
Expected bool
}{
{explicitSetTable{}, true},
{notSpecifiedTable{}, false},
{primaryKeyTable{}, false},
{autoIncrementTable{}, false},
}
for _, c := range cases {
for _, field := range (&gorm.Scope{Value: c.Value}).Fields() {
assert.Equal(t, fieldIsAutoIncrement(field), c.Expected)
}
}
}
func Test_fieldIsPrimaryAndBlank(t *testing.T) {
type notPrimaryTable struct {
Dummy int
}
type primaryKeyTable struct {
ID int `gorm:"column:id;primary_key"`
}
cases := []struct {
Value interface{}
Expected bool
}{
{notPrimaryTable{Dummy: 0}, false},
{notPrimaryTable{Dummy: 1}, false},
{primaryKeyTable{ID: 0}, true},
{primaryKeyTable{ID: 1}, false},
}
for _, c := range cases {
for _, field := range (&gorm.Scope{Value: c.Value}).Fields() {
assert.Equal(t, fieldIsPrimaryAndBlank(field), c.Expected)
}
}
}