-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_test.go
129 lines (109 loc) · 3.71 KB
/
events_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 awsl
import (
"encoding/base64"
"encoding/json"
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/assert"
)
func toAV(t *testing.T, s string) map[string]events.DynamoDBAttributeValue {
av := events.DynamoDBAttributeValue{}
err := json.Unmarshal([]byte(s), &av)
assert.Nil(t, err)
m := map[string]events.DynamoDBAttributeValue{"av": av}
return m
}
// @covers getValue
func Test_FromDynamoDBMap(ts *testing.T) {
ts.Run("Binary", func(t *testing.T) {
m := FromDynamoDBMap(toAV(t, `{"B": "AAEqQQ=="}`))
v := m["av"].([]byte)
assert.Equal(t, "AAEqQQ==", base64.StdEncoding.EncodeToString(v))
})
ts.Run("BinarySet", func(t *testing.T) {
m := FromDynamoDBMap(toAV(t, `{"BS": ["AAEqQQ==", "AAEqQQ=="]}`))
v := m["av"].([][]byte)[1]
assert.Equal(t, "AAEqQQ==", base64.StdEncoding.EncodeToString(v))
})
ts.Run("Boolean", func(t *testing.T) {
m := FromDynamoDBMap(toAV(t, `{"BOOL": true}`))
assert.Equal(t, true, m["av"].(bool))
})
ts.Run("List", func(t *testing.T) {
m := FromDynamoDBMap(toAV(t, `{"L": [{"S": "foo"}, {"S": "bar"}, {"N": "3.14159"}]}`))
assert.Equal(t, "bar", m["av"].([]interface{})[1])
})
ts.Run("List in list", func(t *testing.T) {
s := `{"L": [{"S": "foo"}, {"S": "bar"}, {"N": "3.14159"}, {"L": [{ "S": "cookies"}]}]}`
m := FromDynamoDBMap(toAV(t, s))
v := m["av"].([]interface{})[3].([]interface{})[0]
assert.Equal(t, "cookies", v)
})
ts.Run("Map", func(t *testing.T) {
m := FromDynamoDBMap(toAV(t, `{"M": {"foo": {"S": "bar"}, "bar": {"S": "foo"}}}`))
v := m["av"].(map[string]interface{})["foo"]
assert.Equal(t, "bar", v)
})
ts.Run("Map simple", func(t *testing.T) {
m := FromDynamoDBMap(toAV(t, `{"M": {"foo": {"S": "bar"}}}`))
v := m["av"].(map[string]interface{})["foo"]
assert.Equal(t, "bar", v)
})
ts.Run("Map in map", func(t *testing.T) {
s := `{"M": {"bar": {"M": {"name": {"S": "foo"}}}}}`
m := FromDynamoDBMap(toAV(t, s))
v := m["av"].(map[string]interface{})["bar"].(map[string]interface{})["name"]
assert.Equal(t, "foo", v)
})
ts.Run("Map in map in map", func(t *testing.T) {
s := `{"M": {"map": {"M": {"s": {"S": "str"}, "map2": {"M": {"100": {"N": "NOT BLANK"}}}}}}}`
m := FromDynamoDBMap(toAV(t, s))
v := m["av"].(map[string]interface{})["map"].(map[string]interface{})["map2"]
v2 := v.(map[string]interface{})["100"]
assert.Equal(t, "NOT BLANK", v2)
})
ts.Run("Null", func(t *testing.T) {
m := FromDynamoDBMap(toAV(t, `{"NULL": true}`))
assert.Equal(t, nil, m["av"])
})
ts.Run("Number", func(t *testing.T) {
m := FromDynamoDBMap(toAV(t, `{ "N": "-123.45"}`))
assert.Equal(t, "-123.45", m["av"])
})
ts.Run("NumberSet", func(t *testing.T) {
m := FromDynamoDBMap(toAV(t, `{"NS": ["1234", "567.8"]}`))
assert.Equal(t, "567.8", m["av"].([]string)[1])
})
ts.Run("String", func(t *testing.T) {
m := FromDynamoDBMap(toAV(t, `{"S": "it works"}`))
assert.Equal(t, "it works", m["av"])
})
ts.Run("StringSet", func(t *testing.T) {
m := FromDynamoDBMap(toAV(t, `{"SS": ["foo", "bar"]}`))
assert.Equal(t, "bar", m["av"].([]string)[1])
})
ts.Run("Complicated test case 1", func(t *testing.T) {
s := `{"L": [
{"BOOL":false},
{"NULL":true},
{"N":"-256"},
{"N":"0.999"},
{"S":""},
{"SS":["baz"]},
{"L":[{"S":"ok"}]},
{"M":{"x":{"S":"y"}}},
{"NS":["-99"]}
]}`
m := FromDynamoDBMap(toAV(t, s))
v := m["av"].([]interface{})
assert.Equal(t, false, v[0].(bool))
assert.Equal(t, nil, v[1])
assert.Equal(t, "-256", v[2])
assert.Equal(t, "0.999", v[3])
assert.Equal(t, "", v[4])
assert.Equal(t, "baz", v[5].([]string)[0])
assert.Equal(t, "ok", v[6].([]interface{})[0])
assert.Equal(t, "y", v[7].(map[string]interface{})["x"])
assert.Equal(t, "-99", v[8].([]string)[0])
})
}