-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdbToJson_test.go
219 lines (189 loc) · 8.55 KB
/
dbToJson_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package onedb
import (
"errors"
"testing"
"time"
"unicode"
)
func TestGetJson(t *testing.T) {
// success
rows := NewRowsScanner([]SimpleData{{1, "hello"}, {2, "world"}})
json, _ := getJSON(rows)
if json != `[{"IntVal":1,"StringVal":"hello"},{"IntVal":2,"StringVal":"world"}]` {
t.Error("expected valid json", json)
}
// scan error
rows = NewRowsScanner([]SimpleData{{1, "hello"}, {2, "world"}})
rows.(*mockRowsScanner).ScanErr = errors.New("fail")
_, err := getJSON(rows)
if err == nil {
t.Error("expected error")
}
// err error
rows = NewRowsScanner(nil)
_, err = getJSON(rows)
if err == nil {
t.Error("expected error")
}
}
func TestGetJsonRow(t *testing.T) {
// success
rows := NewRowsScanner([]SimpleData{{1, "hello"}})
json, _ := getJSONRow(rows)
if json != `{"IntVal":1,"StringVal":"hello"}` {
t.Error("expected valid json", json)
}
// scan error
rows = NewRowsScanner([]SimpleData{{1, "hello"}})
rows.(*mockRowsScanner).ScanErr = errors.New("fail")
_, err := getJSONRow(rows)
if err == nil {
t.Error("expected error")
}
// err error
rows = NewRowsScanner(nil)
_, err = getJSONRow(rows)
if err == nil {
t.Error("expected error")
}
}
func TestGetColumnNamesAndValues(t *testing.T) {
// row err
rows := NewRowsScanner(nil)
_, _, err := getColumnNamesAndValues(rows, true)
if err == nil {
t.Error("expected failure")
}
// columns err
rows = &mockRowsScanner{ColumnsErr: errors.New("fail")}
_, _, err = getColumnNamesAndValues(rows, true)
if err == nil {
t.Error("expected failure")
}
// json columns
rows = NewRowsScanner([]SimpleData{{1, "hello"}})
cols, vals, _ := getColumnNamesAndValues(rows, true)
if len(cols) != 2 || cols[0] != `"IntVal":` || cols[1] != `"StringVal":` || len(vals) != 2 {
t.Error("expected valid column names and values array", cols, vals)
}
// non-json columns
rows = NewRowsScanner([]SimpleData{{1, "hello"}})
cols, vals, _ = getColumnNamesAndValues(rows, false)
if len(cols) != 2 || cols[0] != "IntVal" || cols[1] != "StringVal" || len(vals) != 2 {
t.Error("expected valid column names and values array", cols, vals)
}
}
func TestGetJsonValue(t *testing.T) {
p := new(interface{})
if actual := getJSONValue(p); actual != "null" {
t.Error("expected value: null", actual)
}
*p = true
if actual := getJSONValue(p); actual != "true" {
t.Error("expected value: true", actual)
}
*p = false
if actual := getJSONValue(p); actual != "false" {
t.Error("expected value: false", actual)
}
*p = []byte("byte") // "byte" base64 encoded: Ynl0ZQ==
if actual := getJSONValue(p); actual != `"Ynl0ZQ=="` {
t.Error("expected value: \"Ynl0ZQ==\"", actual)
}
*p = time.Date(2000, 1, 2, 3, 4, 5, 123456789, time.UTC)
if actual := getJSONValue(p); actual != `"2000-01-02 03:04:05.123"` {
t.Error("expected value: \"2000-01-02 03:04:05.123\"", actual)
}
*p = 12
if actual := getJSONValue(p); actual != "12" {
t.Error("expected value: 12", actual)
}
*p = "hello"
if actual := getJSONValue(p); actual != `"hello"` {
t.Error("expected value \"hello\"", actual)
}
*p = []string{"hello", "world"}
if actual := getJSONValue(p); actual != `"[hello world]"` {
t.Error("expected value \"[hello world]\"", actual)
}
}
func TestEncodeByteSlice(t *testing.T) {
if actual := encodeByteSlice([]byte{}); actual != "null" {
t.Error("expected value: null", actual)
}
if actual := encodeByteSlice([]byte("byte")); actual != `"Ynl0ZQ=="` {
t.Error("expected value: \"Ynl0ZQ==\"", actual)
}
b := []byte("really long byte slice. Must be > 1024 for this to trigger...................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................")
if actual := encodeByteSlice(b); actual != `"cmVhbGx5IGxvbmcgYnl0ZSBzbGljZS4gTXVzdCBiZSA+IDEwMjQgZm9yIHRoaXMgdG8gdHJpZ2dlci4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi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4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLg=="` {
t.Error("expected value differs", actual)
}
}
// taken from encoding/json encode_test.go. Useless test, but it hits every part of the code
func TestEncodeString(t *testing.T) {
var r []rune
for i := '\u0000'; i <= unicode.MaxRune; i++ {
r = append(r, i)
}
s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
encodeString(s)
}
// should complete in less than .05s
func TestGetJsonWith10000SqlRows(t *testing.T) {
rows := &MockRows{NumRows: 10000}
getJSON(rows)
}
func TestGetJsonWithFakeRows(t *testing.T) {
rows := &MockRows{NumRows: 2}
json, _ := getJSON(rows)
if json != `[{"str":"string\n\twith carriage return","int":1,"date":"2000-01-01 12:00:00","true":true,"false":false,"byte":"Ynl0ZQ=="},{"str":"string\n\twith carriage return","int":1,"date":"2000-01-01 12:00:00","true":true,"false":false,"byte":"Ynl0ZQ=="}]` {
t.Fatal("expected matching json", json)
}
}
/******************************* Mocks ***************************************/
type TestData struct {
Nil interface{}
Str string
Int int
Date time.Time
True bool
False bool
Byte []byte
}
type MockRows struct {
NumRows int
}
func (m *MockRows) Columns() ([]string, error) {
return []string{"nil", "str", "int", "date", "true", "false", "byte"}, nil
}
func (m *MockRows) Next() bool {
m.NumRows--
if m.NumRows < 0 {
return false
}
return true
}
func (m *MockRows) Close() error {
return nil
}
func (m *MockRows) Scan(dest ...interface{}) error {
var nilVal interface{}
var strVal interface{} = `string
with carriage return`
var intVal interface{} = 1
var dateVal interface{} = time.Date(2000, 01, 01, 12, 0, 0, 0, time.Local)
var trueVal interface{} = true
var falseVal interface{} = false
var byteVal interface{} = []byte("byte") // will base64 encode to Ynl0ZQ==
dest[0] = &nilVal
dest[1] = &strVal
dest[2] = &intVal
dest[3] = &dateVal
dest[4] = &trueVal
dest[5] = &falseVal
dest[6] = &byteVal
return nil
}
func (m *MockRows) Err() error {
return nil
}