forked from valkey-io/valkey-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_test.go
86 lines (78 loc) · 1.93 KB
/
schema_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
package om
import (
"reflect"
"strings"
"testing"
)
type s1 struct {
A int `valkey:",key"`
}
type s2 struct {
A string `valkey:",ver"`
}
type s3 struct {
A string `json:"-" valkey:",key"`
B int64 `valkey:",ver"`
private int64
}
type s4 struct {
A string `valkey:",key"`
B int64 `json:"-" valkey:",ver"`
private int64
}
type s5 struct {
A string `valkey:",key"`
B int64 `valkey:",ver"`
C int64 `valkey:",exat"`
}
func TestSchema(t *testing.T) {
t.Run("non struct", func(t *testing.T) {
if v := recovered(func() {
newSchema(reflect.TypeOf(map[string]string{}))
}); !strings.Contains(v, "should be a struct") {
t.Fatalf("unexpected msg %v", v)
}
})
t.Run("non string `valkey:\",key\"`", func(t *testing.T) {
if v := recovered(func() {
newSchema(reflect.TypeOf(s1{}))
}); !strings.Contains(v, "should be a string") {
t.Fatalf("unexpected msg %v", v)
}
})
t.Run("non string `valkey:\",ver\"`", func(t *testing.T) {
if v := recovered(func() {
newSchema(reflect.TypeOf(s2{}))
}); !strings.Contains(v, "should be a int64") {
t.Fatalf("unexpected msg %v", v)
}
})
t.Run("missing `valkey:\",key\"`", func(t *testing.T) {
if v := recovered(func() {
newSchema(reflect.TypeOf(s3{}))
}); !strings.Contains(v, "should have one field with `valkey:\",key\"` tag") {
t.Fatalf("unexpected msg %v", v)
}
})
t.Run("missing `valkey:\",ver\"`", func(t *testing.T) {
if v := recovered(func() {
newSchema(reflect.TypeOf(s4{}))
}); !strings.Contains(v, "should have one field with `valkey:\",ver\"` tag") {
t.Fatalf("unexpected msg %v", v)
}
})
t.Run("non time.Time `valkey:\",exat\"`", func(t *testing.T) {
if v := recovered(func() {
newSchema(reflect.TypeOf(s5{}))
}); !strings.Contains(v, "should be a time.Time") {
t.Fatalf("unexpected msg %v", v)
}
})
}
func recovered(fn func()) (msg string) {
defer func() {
msg = recover().(string)
}()
fn()
return msg
}