-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_row_test.go
178 lines (151 loc) · 4.05 KB
/
json_row_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
169
170
171
172
173
174
175
176
177
178
package hake_test
import (
"bytes"
"testing"
"cloud.google.com/go/spanner"
. "github.com/gcpug/hake"
"github.com/xeipuuv/gojsonschema"
)
func TestJSONRow_MarshalJSON(t *testing.T) {
cases := []struct {
name string
row *spanner.Row
want string
}{
{"null", row(t, nil), toJSON(t, nil)},
{"empty", row(t, R{}), toJSON(t, R{})},
{"single", row(t, R{"col1", 100}), toJSON(t, R{"col1", "100"})},
{"multiple", row(t, R{"col1", 100, "col2", 10.5}), toJSON(t, R{"col1", "100", "col2", 10.5})},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := toJSON(t, (*JSONRow)(tt.row))
if got != tt.want {
t.Errorf("want %s but got %s", tt.want, got)
}
})
}
}
func TestRows(t *testing.T) {
cases := []struct {
name string
rows []*spanner.Row
want string
}{
{"null", rows(t, nil), toJSON(t, nil)},
{"empties", rows(t, []R{{}, {}}), toJSON(t, []R{{}, {}})},
{"singles", rows(t, []R{{"col1", 100}, {"col1", 200}}), toJSON(t, []R{{"col1", "100"}, {"col1", "200"}})},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got := toJSON(t, JSONRows(tt.rows))
if got != tt.want {
t.Errorf("want %s but got %s", tt.want, got)
}
})
}
}
type noopFormatCheker struct{}
func (noopFormatCheker) IsFormat(_ interface{}) bool {
return true
}
func init() {
gojsonschema.FormatCheckers.Add("datetime", noopFormatCheker{})
gojsonschema.FormatCheckers.Add("textarea", noopFormatCheker{})
}
func TestJSONRow_JSONSchema(t *testing.T) {
type T struct {
N int
S string
}
type NT struct {
T T
}
cases := []struct {
name string
row *spanner.Row
isErr bool
}{
{"int", row(t, R{"col1", 100}), false},
{"int string", row(t, R{"col1", 100, "col2", "string"}), false},
{"nested struct", row(t, R{"col1", 100, "col2", T{N: 100, S: ""}}), false},
{"timestamp", row(t, R{"col1", 100, "col2", timestamp(t, "2002-10-02T10:00:00Z")}), false},
{"bytes", row(t, R{"col1", []byte("test")}), false},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
var got bytes.Buffer
err := (*JSONRow)(tt.row).JSONSchema(&got)
switch {
case tt.isErr && err == nil:
t.Errorf("expected error does not occur")
case !tt.isErr && err != nil:
t.Errorf("unexpected error %v", err)
}
l := gojsonschema.NewStringLoader(got.String())
s, err := gojsonschema.NewSchema(l)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
rowJSON := toJSON(t, (*JSONRow)(tt.row))
r, err := s.Validate(gojsonschema.NewStringLoader(rowJSON))
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if !r.Valid() {
t.Errorf("invalid JSON Schema: %s", got.String())
}
})
}
}
func TestJSONRows_JSONSchema(t *testing.T) {
type T struct {
N int
S string
}
type NT struct {
T T
}
cases := []struct {
name string
rows []*spanner.Row
isErr bool
}{
{"empty", rows(t, []R{}), false},
{"int", rows(t, []R{{"col1", 100}}), false},
{"int int", rows(t, []R{{"col1", 100}, {"col1", 200}}), false},
{"int string", rows(t, []R{{"col1", 100, "col2", "string"}}), false},
{"nested struct", rows(t, []R{{"col1", 100, "col2", T{N: 100, S: ""}}}), false},
{"timestamp", rows(t, []R{{"col1", 100, "col2", timestamp(t, "2002-10-02T10:00:00Z")}}), false},
{"bytes", rows(t, []R{{"col1", []byte("test")}}), false},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
var got bytes.Buffer
err := JSONRows(tt.rows).JSONSchema(&got)
switch {
case tt.isErr && err == nil:
t.Errorf("expected error does not occur")
case !tt.isErr && err != nil:
t.Errorf("unexpected error %v", err)
}
l := gojsonschema.NewStringLoader(got.String())
s, err := gojsonschema.NewSchema(l)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
rowJSON := toJSON(t, JSONRows(tt.rows))
r, err := s.Validate(gojsonschema.NewStringLoader(rowJSON))
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if !r.Valid() {
t.Errorf("invalid JSON Schema: %s", got.String())
}
})
}
}