-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct_marshal_test.go
91 lines (81 loc) · 2.32 KB
/
struct_marshal_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
package riaken_struct
import (
"encoding/json"
"testing"
)
type EncodeData struct {
Email string `json:"email" riak:"index"`
Twitter string `json:"twitter" riak:"index"`
Data []byte `json:"data" riak:"index"`
AnInt int `json:"anint" riak:"index"`
AnInt8 int8 `json:"anint8" riak:"index"`
AnInt16 int16 `json:"anint16" riak:"index"`
AnInt32 int32 `json:"anint32" riak:"index"`
AnInt64 int64 `json:"anint64" riak:"index"`
AUInt uint `json:"auint" riak:"index"`
AUInt8 uint8 `json:"auint8" riak:"index"`
AUInt16 uint16 `json:"auint16" riak:"index"`
AUInt32 uint32 `json:"auint32" riak:"index"`
AUInt64 uint64 `json:"auint64" riak:"index"`
Byte byte `json:"abyte" riak:"index"`
Rune rune `json:"arune" riak:"index"`
NestedStruct NestedData `json:"nested_struct"`
unexported string
}
type NestedData struct {
Nested string `json:"nested" riak:"index"`
}
func TestCoder(t *testing.T) {
e := NewStructMarshal("json", JsonMarshaller, JsonUnmarshaller)
data := &EncodeData{
Email: "riak@example.com",
Twitter: "riak-twitter",
Data: []byte("riak-data"),
AnInt: 1,
AnInt8: 127,
AnInt16: 32767,
AnInt32: 2147483647,
AnInt64: 9223372036854775807,
AUInt: 1,
AUInt8: 255,
AUInt16: 65535,
AUInt32: 4294967295,
AUInt64: 18446744073709551615,
Byte: 255,
Rune: 2147483647,
NestedStruct: NestedData{
Nested: "nested-data",
},
unexported: "unexported",
}
encdata, err := e.Marshal(data)
if err != nil {
t.Error(err.Error())
}
key := string(encdata.GetIndexes()[0].GetKey())
if key != "email_bin" {
t.Errorf("Expected email_bin, got %s", key)
}
var found bool
for _, v := range encdata.GetIndexes() {
if string(v.GetKey()) == "nestedstruct_nested_bin" {
found = true
break
}
}
if !found {
t.Error("Expected a nestedstruct_nested_bin index")
}
jsondata, err := json.Marshal(data)
if err != nil {
t.Error(err.Error())
}
if string(encdata.GetValue()) != string(jsondata) {
t.Errorf("Expected %s, got %s", string(encdata.GetValue()), string(jsondata))
}
result := &EncodeData{}
e.Unmarshal(jsondata, result)
if result.Email != data.Email {
t.Errorf("Expected %s, got %s", data.Email, result.Email)
}
}