forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_test.go
129 lines (112 loc) · 2.99 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
package dynamo
import (
"reflect"
"testing"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
func TestUnmarshalAppend(t *testing.T) {
var results []struct {
User int `dynamo:"UserID"`
Page int
Limit uint
Null interface{}
}
id := "12345"
page := "5"
limit := "20"
null := true
item := map[string]*dynamodb.AttributeValue{
"UserID": &dynamodb.AttributeValue{N: &id},
"Page": &dynamodb.AttributeValue{N: &page},
"Limit": &dynamodb.AttributeValue{N: &limit},
"Null": &dynamodb.AttributeValue{NULL: &null},
}
for range [15]struct{}{} {
err := unmarshalAppend(item, &results)
if err != nil {
t.Fatal(err)
}
}
for _, h := range results {
if h.User != 12345 || h.Page != 5 || h.Limit != 20 || h.Null != nil {
t.Error("invalid hit", h)
}
}
var mapResults []map[string]interface{}
for range [15]struct{}{} {
err := unmarshalAppend(item, &mapResults)
if err != nil {
t.Fatal(err)
}
}
for _, h := range mapResults {
if h["UserID"] != 12345.0 || h["Page"] != 5.0 || h["Limit"] != 20.0 || h["Null"] != nil {
t.Error("invalid interface{} hit", h)
}
}
}
func TestUnmarshal(t *testing.T) {
for _, tc := range encodingTests {
rv := reflect.New(reflect.TypeOf(tc.in))
err := unmarshalReflect(tc.out, rv.Elem())
if err != nil {
t.Errorf("%s: unexpected error: %v", tc.name, err)
}
if !reflect.DeepEqual(rv.Elem().Interface(), tc.in) {
t.Errorf("%s: bad result: %#v ≠ %#v", tc.name, rv.Elem().Interface(), tc.out)
}
}
}
func TestUnmarshalItem(t *testing.T) {
for _, tc := range itemEncodingTests {
rv := reflect.New(reflect.TypeOf(tc.in))
err := unmarshalItem(tc.out, rv.Interface())
if err != nil {
t.Errorf("%s: unexpected error: %v", tc.name, err)
}
if !reflect.DeepEqual(rv.Elem().Interface(), tc.in) {
t.Errorf("%s: bad result: %#v ≠ %#v", tc.name, rv.Elem().Interface(), tc.in)
}
}
}
func TestUnmarshalNULL(t *testing.T) {
tru := true
arbitrary := "hello world"
double := new(*int)
item := map[string]*dynamodb.AttributeValue{
"String": &dynamodb.AttributeValue{NULL: &tru},
"Slice": &dynamodb.AttributeValue{NULL: &tru},
"Array": &dynamodb.AttributeValue{NULL: &tru},
"StringPtr": &dynamodb.AttributeValue{NULL: &tru},
"DoublePtr": &dynamodb.AttributeValue{NULL: &tru},
"Map": &dynamodb.AttributeValue{NULL: &tru},
"Interface": &dynamodb.AttributeValue{NULL: &tru},
}
type resultType struct {
String string
Slice []string
Array [2]byte
StringPtr *string
DoublePtr **int
Map map[string]int
Interface interface{}
}
// dirty result, we want this to be reset
result := resultType{
String: "ABC",
Slice: []string{"A", "B"},
Array: [2]byte{'A', 'B'},
StringPtr: &arbitrary,
DoublePtr: double,
Map: map[string]int{
"A": 1,
},
Interface: "interface{}",
}
if err := UnmarshalItem(item, &result); err != nil {
t.Error(err)
}
if (!reflect.DeepEqual(result, resultType{})) {
t.Error("unmarshal null: bad result:", result, "≠", resultType{})
}
}