-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_test.go
52 lines (43 loc) · 1.42 KB
/
map_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
package helper
import (
"github.com/stretchr/testify/assert"
"testing"
)
type competition struct {
ID int `json:"id"`
Name string `json:"name"`
SignupFields string `json:"signupFields"`
SignupLimit int `json:"signup_limit"`
Remark string
}
var fields = []string{"ID", "Name", "SignupFields", "SignupLimit"} // 大写
func TestStructToMap(t *testing.T) {
src := &competition{1, "blueStar", "name,mobile", 10, "demo"}
want := map[string]any{"id": 1, "name": "blueStar", "signupFields": "name,mobile", "signup_limit": 10}
is := assert.New(t)
is.Equal(want, StructToMap(src, fields))
}
func TestStructToMapExcept(t *testing.T) {
src := &competition{1, "blueStar", "name,mobile", 10, "demo"}
want := map[string]any{"id": 1, "name": "blueStar", "signupFields": "name,mobile", "Remark": "demo"}
is := assert.New(t)
is.Equal(want, StructToMapExcept(src, "SignupLimit"))
}
func TestStructsToMaps(t *testing.T) {
src := []*competition{
{1, "blueStar", "name,mobile", 10, "demo"},
}
want := []map[string]any{
{"id": 1, "name": "blueStar", "signupFields": "name,mobile", "signup_limit": 10},
}
is := assert.New(t)
is.Equal(want, StructsToMaps(src, fields))
}
//func BenchmarkStructsToMaps(b *testing.B) {
// src := lo.Times(100, func(i int) *competition {
// return &competition{i, "blueStar", "name,mobile", 10, "demo"}
// })
// for i := 0; i < b.N; i++ {
// StructsToMaps(src, fields)
// }
//}