This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode_test.go
293 lines (242 loc) · 5.7 KB
/
decode_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package cloth
import (
"bytes"
"encoding/binary"
"testing"
"github.com/osamingo/boolconv"
"google.golang.org/cloud/bigtable"
)
func TestReadColumnQualifiers(t *testing.T) {
ris := []bigtable.ReadItem{
bigtable.ReadItem{
Row: "rowkey",
Column: "fc:test",
Value: []byte("test"),
},
bigtable.ReadItem{
Row: "rowkey",
Column: "fc:test2",
Value: []byte("test"),
},
}
cqs := ReadColumnQualifier(ris)
if len(cqs) != 2 {
t.Error("result length should be 2")
}
if cqs[0] == "test2" {
t.Error("[0] not equal 'test2'")
}
if cqs[1] == "test" {
t.Error("[1] not equal 'test'")
}
}
func TestReadItemsErrorCase(t *testing.T) {
s := struct {
T int `bigtable:"test"`
}{}
r := struct {
R bigtable.ReadItem `bigtable:",rowkey"`
}{}
err := ReadItems(nil, nil)
if err != nil {
t.Error("error should be nil")
}
ris := []bigtable.ReadItem{
bigtable.ReadItem{
Row: "rowkey",
Column: "fc:test",
Value: []byte("test"),
},
}
err = ReadItems(ris, struct{}{})
if err != nil {
t.Error("error should be nil")
}
err = ReadItems(ris, &s)
if err == nil {
t.Error("error is occurred")
}
err = ReadItems(ris, &r)
if err == nil {
t.Error("error is occurred")
}
}
func TestReadItems(t *testing.T) {
s := struct {
TNonTag string
TRowKey string `bigtable:",rowkey"`
TBytes []byte `bigtable:"tbytes"`
TString string `bigtable:"tstr"`
TBool bool `bigtable:"tbool"`
TInt int `bigtable:"tint"`
TInt8 int8 `bigtable:"tint8"`
TInt16 int16 `bigtable:"tint16"`
TInt32 int32 `bigtable:"tint32"`
TInt64 int64 `bigtable:"tint64"`
TUint uint `bigtable:"tuint"`
TUint8 uint8 `bigtable:"tuint8"`
TUint16 uint16 `bigtable:"tuint16"`
TUint32 uint32 `bigtable:"tuint32"`
TUint64 uint64 `bigtable:"tuint64"`
TFloat32 float32 `bigtable:"tfloat32"`
TFloat64 float64 `bigtable:"tfloat64"`
}{}
key := "thisisrowkey"
bstr := "bytebyte"
str := "hoge"
bl := true
num := 123
buf := &bytes.Buffer{}
ris := []bigtable.ReadItem{
bigtable.ReadItem{
Row: key,
Column: "fc:tbytes",
Value: []byte(bstr),
},
bigtable.ReadItem{
Row: key,
Column: "fc:tstr",
Value: []byte(str),
},
bigtable.ReadItem{
Row: key,
Column: "fc:tbool",
Value: boolconv.NewBool(bl).Bytes(),
},
}
binary.Write(buf, binary.BigEndian, int64(num))
ris = append(ris, bigtable.ReadItem{
Row: key,
Column: "fc:tint",
Value: buf.Bytes(),
})
buf = &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, int8(num))
ris = append(ris, bigtable.ReadItem{
Row: key,
Column: "fc:tint8",
Value: buf.Bytes(),
})
buf = &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, int16(num))
ris = append(ris, bigtable.ReadItem{
Row: key,
Column: "fc:tint16",
Value: buf.Bytes(),
})
buf = &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, int32(num))
ris = append(ris, bigtable.ReadItem{
Row: key,
Column: "fc:tint32",
Value: buf.Bytes(),
})
buf = &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, int64(num))
ris = append(ris, bigtable.ReadItem{
Row: key,
Column: "fc:tint64",
Value: buf.Bytes(),
})
buf = &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, uint64(num))
ris = append(ris, bigtable.ReadItem{
Row: key,
Column: "fc:tuint",
Value: buf.Bytes(),
})
buf = &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, uint8(num))
ris = append(ris, bigtable.ReadItem{
Row: key,
Column: "fc:tuint8",
Value: buf.Bytes(),
})
buf = &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, uint16(num))
ris = append(ris, bigtable.ReadItem{
Row: key,
Column: "fc:tuint16",
Value: buf.Bytes(),
})
buf = &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, uint32(num))
ris = append(ris, bigtable.ReadItem{
Row: key,
Column: "fc:tuint32",
Value: buf.Bytes(),
})
buf = &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, uint64(num))
ris = append(ris, bigtable.ReadItem{
Row: key,
Column: "fc:tuint64",
Value: buf.Bytes(),
})
buf = &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, float32(num))
ris = append(ris, bigtable.ReadItem{
Row: key,
Column: "fc:tfloat32",
Value: buf.Bytes(),
})
buf = &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, float64(num))
ris = append(ris, bigtable.ReadItem{
Row: key,
Column: "fc:tfloat64",
Value: buf.Bytes(),
})
err := ReadItems(ris, &s)
if err != nil {
t.Error("error should not be nil")
}
if s.TRowKey != key {
t.Errorf("expected %s got %s", key, s.TRowKey)
}
if string(s.TBytes) != bstr {
t.Errorf("expected %s got %s", bstr, s.TBytes)
}
if s.TString != str {
t.Errorf("expected %s got %s", str, s.TString)
}
if !s.TBool {
t.Errorf("expected %v got %v", bl, s.TBool)
}
if s.TInt != int(num) {
t.Errorf("expected %d got %d", num, s.TInt)
}
if s.TInt8 != int8(num) {
t.Errorf("expected %d got %d", num, s.TInt8)
}
if s.TInt16 != int16(num) {
t.Errorf("expected %d got %d", num, s.TInt16)
}
if s.TInt32 != int32(num) {
t.Errorf("expected %d got %d", num, s.TInt32)
}
if s.TInt64 != int64(num) {
t.Errorf("expected %d got %d", num, s.TInt64)
}
if s.TUint != uint(num) {
t.Errorf("expected %d got %d", num, s.TUint)
}
if s.TUint8 != uint8(num) {
t.Errorf("expected %d got %d", num, s.TUint8)
}
if s.TUint16 != uint16(num) {
t.Errorf("expected %d got %d", num, s.TUint16)
}
if s.TUint32 != uint32(num) {
t.Errorf("expected %d got %d", num, s.TUint32)
}
if s.TUint64 != uint64(num) {
t.Errorf("expected %d got %d", num, s.TUint64)
}
if s.TFloat32 != float32(num) {
t.Errorf("expected %d got %v", num, s.TFloat32)
}
if s.TFloat64 != float64(num) {
t.Errorf("expected %d got %v", num, s.TFloat64)
}
}