forked from t-tiger/gorm-bulk-insert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk_insert_test.go
60 lines (50 loc) · 1.42 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
package gormbulk
import (
"database/sql"
"github.com/stretchr/testify/assert"
"sort"
"testing"
"time"
)
type fakeRelationDB struct{}
type fakeDB struct {
ID int
Name string
Email string `gorm:"default:[email protected]"`
Relation *fakeRelationDB `gorm:"foreignkey:RelationID"`
Relations []fakeRelationDB `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
}
value := fakeDB{
Name: "name1",
Email: "[email protected]",
Relation: &fakeRelationDB{},
Message: sql.NullString{String: "message1", Valid: true},
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)
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")
}