-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema_test.go
149 lines (124 loc) · 6.21 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
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
package schema
import (
"bytes"
"encoding/json"
"fmt"
"testing"
)
const jsBadReqString = `{"age":25}`
const jsBadReqInt = `{"name":"John"}`
const jsBadReqNested = `{"name":"John","age":25, "hair":{}}`
const jsTruncateString = `{"name":"Jonathon","age":25, "hair":{"color":"brown"}}`
const jsTruncateStringPtr = `{"name":"Jonathon","age":25, "hair":[{"color":"brown"},{"color":"red"}]}`
type Parent struct {
TestPtrs1 []TestPtr `json:"test_ptrs1" `
TestSlices1 []TestSlice `json:"test_slices1" `
Tests1 []Test `json:"tests1" `
TestPtrs2 []*TestPtr `json:"test_ptrs2" `
TestSlices2 []*TestSlice `json:"test_slices2" `
Tests2 []*Test `json:"tests2" `
TestPtrs3 *[]TestPtr `json:"test_ptrs3" `
TestSlices3 *[]TestSlice `json:"test_slices3" `
Tests3 *[]Test `json:"tests3" `
}
type Test struct {
Name string `json:"name" schema:"req,truncate(4)"`
Age int `json:"age" schema:"req"`
Hair Hair `json:"hair" schema:req`
}
type TestPtr struct {
Name string `json:"name" schema:"req,truncate(4)"`
Age int `json:"age" schema:"req"`
Hair []*Hair `json:"hair" schema:req`
}
type TestSlice struct {
Name string `json:"name" schema:"req,truncate(4)"`
Age int `json:"age" schema:"req"`
Hair []Hair `json:"hair" schema:req`
}
type Hair struct {
Color string `json:"color" schema:"req,truncate(2)"`
}
func TestTruncateReallyComplicatedStruct(t *testing.T) {
var p = &Parent{}
parent := `{"test_ptrs1":[{"name":"John","age":25,"hair":[{"color":"brown"}]},{"name":"John","age":25,"hair":[{"color":"brown"}]}],"test_ptrs2":[{"name":"John","age":25,"hair":[{"color":"brown"}]},{"name":"John","age":25,"hair":[{"color":"brown"}]}],"test_ptrs3":[{"name":"John","age":25,"hair":[{"color":"brown"}]},{"name":"John","age":25,"hair":[{"color":"brown"}]}],"test_slices1":[{"name":"John","age":25,"hair":[{"color":"brown"}]},{"name":"John","age":25,"hair":[{"color":"brown"}]}],"test_slices2":[{"name":"John","age":25,"hair":[{"color":"brown"}]},{"name":"John","age":25,"hair":[{"color":"brown"}]}],"test_slices3":[{"name":"John","age":25,"hair":[{"color":"brown"}]},{"name":"John","age":25,"hair":[{"color":"brown"}]}],"tests1":[{"name":"John","age":25,"hair":{"color":"brown"}},{"name":"John","age":25,"hair":{"color":"brown"}}],"tests2":[{"name":"John","age":25,"hair":{"color":"brown"}},{"name":"John","age":25,"hair":{"color":"brown"}}],"tests3":[{"name":"John","age":25,"hair":{"color":"brown"}},{"name":"John","age":25,"hair":{"color":"brown"}}]}`
expected := `{"test_ptrs1":[{"name":"John","age":25,"hair":[{"color":"br"}]},{"name":"John","age":25,"hair":[{"color":"br"}]}],"test_slices1":[{"name":"John","age":25,"hair":[{"color":"br"}]},{"name":"John","age":25,"hair":[{"color":"br"}]}],"tests1":[{"name":"John","age":25,"hair":{"color":"br"}},{"name":"John","age":25,"hair":{"color":"br"}}],"test_ptrs2":[{"name":"John","age":25,"hair":[{"color":"br"}]},{"name":"John","age":25,"hair":[{"color":"br"}]}],"test_slices2":[{"name":"John","age":25,"hair":[{"color":"br"}]},{"name":"John","age":25,"hair":[{"color":"br"}]}],"tests2":[{"name":"John","age":25,"hair":{"color":"br"}},{"name":"John","age":25,"hair":{"color":"br"}}],"test_ptrs3":[{"name":"John","age":25,"hair":[{"color":"br"}]},{"name":"John","age":25,"hair":[{"color":"br"}]}],"test_slices3":[{"name":"John","age":25,"hair":[{"color":"br"}]},{"name":"John","age":25,"hair":[{"color":"br"}]}],"tests3":[{"name":"John","age":25,"hair":{"color":"br"}},{"name":"John","age":25,"hair":{"color":"br"}}]}`
err := Unmarshal([]byte(parent), p)
if err != nil {
t.Error(err)
}
output, err := Marshal(p)
if !bytes.Equal([]byte(expected), output) {
var dst bytes.Buffer
json.Indent(&dst, output, "=", "\t")
t.Log(dst.String())
t.Error("Failed")
}
}
func TestTruncateComplicatedStruct(t *testing.T) {
var s = &TestSlice{}
testPtr := `{"name": "John", "age" : 25, "hair": [{"color": "brown"}, {"color": "blonde"}]}`
err := Unmarshal([]byte(testPtr), s)
if err != nil {
t.Error(err)
}
if s.Hair[0].Color != "br" || s.Hair[1].Color != "bl" {
t.Error("Fail")
}
}
func TestTruncateComplicatedStruct2(t *testing.T) {
var s = &TestPtr{}
testPtr := `{"name": "John", "age" : 25, "hair": [{"color": "brown"}]}`
err := Unmarshal([]byte(testPtr), s)
if err != nil {
t.Error(err)
}
if s.Hair[0].Color != "br" {
t.Error("Fail")
}
}
func TestRequired(t *testing.T) {
fixtures := []struct {
Expected error
s interface{}
json string
name string
msg string
}{
{nil, &Test{}, `{"name":"John","age":25, "hair":{"color":"brown"}}`, "Unmarshal proper json", "expect nil error, got: "},
{nil, &TestPtr{}, `{"name":"John","age":25, "hair":[{"color":"brown"}]}`, "Unmarshal proper json with slice pointer", "expect nil error, got: "},
{&SchemaError{Field: "Name", ErrType: "req"}, &Test{}, `{"age":25, "hair":{"color":"brown"}}`, "Omit required field 'name' (string)", "expect require error, got: "},
{&SchemaError{Field: "Color", ErrType: "req"}, &Test{}, `{"name":"John","age":25, "hair":{}}`, "Omit required nested field 'hair:color' (string)", "expect require error, got: "},
{&SchemaError{Field: "Color", ErrType: "req"}, &TestPtr{}, `{"name":"John","age":25, "hair":[{}]}`, "Omit required nested field pointer 'hair:color' (string)", "expect require error, got: "},
}
for _, f := range fixtures {
t.Log(f.name)
err := Unmarshal([]byte(f.json), f.s)
if fmt.Sprint(err) != fmt.Sprint(f.Expected) {
t.Error(f.name, f.msg, err)
}
}
}
func TestTruncateString(t *testing.T) {
fixtures := []struct {
ExpectedErr error
Expected func(*Test) bool
s interface{}
json string
name string
msg string
}{
{nil, func(t *Test) bool { return t.Name == "Jona" }, &Test{}, `{"name":"Jonathon","age":25, "hair":{"color":"brown"}}`, "Truncate String", "expect nil error & Name=Jona got:"},
{nil, func(t *Test) bool { return t.Hair.Color == "br" }, &Test{}, `{"name":"Jonathon","age":25, "hair":{"color":"brown"}}`, "Truncate String", "expect nil error & Color=br got:"},
}
for _, f := range fixtures {
t.Log(f.name)
err := Unmarshal([]byte(f.json), f.s)
if fmt.Sprint(err) != fmt.Sprint(f.ExpectedErr) {
t.Error(f.msg, err)
}
if f.Expected(f.s.(*Test)) != true {
t.Error(f.msg, f.s)
}
}
}