This repository was archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamo_db_test.go
More file actions
131 lines (120 loc) · 5.21 KB
/
dynamo_db_test.go
File metadata and controls
131 lines (120 loc) · 5.21 KB
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
package xlambda_test
import (
"strconv"
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/require"
"github.com/gofor-little/xlambda"
)
func TestUnmarshalDynamoDBAttributeValueMap(t *testing.T) {
type childTestData struct {
Bool bool `json:"bool"`
ByteSlice []byte `json:"byteSlice"`
SliceOfByteSlices [][]byte `json:"sliceOfByteSlices"`
Slice []interface{} `json:"slice"`
Int int `json:"int"`
IntSlice []int `json:"intSlice"`
Float float64 `json:"float"`
FloatSlice []float64 `json:"floatSlice"`
String string `json:"string"`
StringSlice []string `json:"stringSlice"`
Nil *string `json:"nil"`
}
type testData struct {
Bool bool `json:"bool"`
ByteSlice []byte `json:"byteSlice"`
SliceOfByteSlices [][]byte `json:"sliceOfByteSlices"`
Slice []interface{} `json:"slice"`
Struct childTestData `json:"struct"`
Int int `json:"int"`
IntSlice []int `json:"intSlice"`
Float float64 `json:"float"`
FloatSlice []float64 `json:"floatSlice"`
String string `json:"string"`
StringSlice []string `json:"stringSlice"`
Nil *string `json:"nil"`
}
want := &testData{
Bool: true,
ByteSlice: []byte("byteSlice"),
SliceOfByteSlices: stringSliceToSliceOfByteSlices("sliceOfByteSlices1", "sliceOfByteSlices2"),
Slice: []interface{}{
true,
"string",
// 1, // Doesn't work, an int will come back as a float64.
1.5,
},
Struct: childTestData{
Bool: true,
ByteSlice: []byte("byteSlice"),
SliceOfByteSlices: stringSliceToSliceOfByteSlices("sliceOfByteSlices1", "sliceOfByteSlices2"),
Slice: []interface{}{
true,
"string",
// 1, // Doesn't work, an int will come back as a float64.
1.5,
},
Int: 1,
IntSlice: []int{1, 2, 3},
Float: 1.5,
FloatSlice: []float64{1.5, 2.5, 3.5},
String: "string",
StringSlice: []string{"string1", "string2", "string3"},
Nil: nil,
},
Int: 1,
IntSlice: []int{1, 2, 3},
Float: 1.5,
FloatSlice: []float64{1.5, 2.5, 3.5},
String: "string",
StringSlice: []string{"string1", "string2", "string3"},
Nil: nil,
}
td := &testData{}
require.NoError(t, xlambda.UnmarshalDynamoDBEventAttributeValues(map[string]events.DynamoDBAttributeValue{
"bool": events.NewBooleanAttribute(want.Bool),
"byteSlice": events.NewBinaryAttribute(want.ByteSlice),
"sliceOfByteSlices": events.NewBinarySetAttribute(want.SliceOfByteSlices),
"slice": events.NewListAttribute([]events.DynamoDBAttributeValue{
events.NewBooleanAttribute(want.Slice[0].(bool)),
events.NewStringAttribute(want.Slice[1].(string)),
events.NewNumberAttribute(float64ToString(want.Slice[2].(float64))),
}),
"struct": events.NewMapAttribute(map[string]events.DynamoDBAttributeValue{
"bool": events.NewBooleanAttribute(want.Bool),
"byteSlice": events.NewBinaryAttribute(want.ByteSlice),
"sliceOfByteSlices": events.NewBinarySetAttribute(want.SliceOfByteSlices),
"slice": events.NewListAttribute([]events.DynamoDBAttributeValue{
events.NewBooleanAttribute(want.Slice[0].(bool)),
events.NewStringAttribute(want.Slice[1].(string)),
events.NewNumberAttribute(float64ToString(want.Slice[2].(float64))),
}),
"int": events.NewNumberAttribute(strconv.Itoa(want.Int)),
"intSlice": events.NewNumberSetAttribute([]string{strconv.Itoa(want.IntSlice[0]), strconv.Itoa(want.IntSlice[1]), strconv.Itoa(want.IntSlice[2])}),
"float": events.NewNumberAttribute(float64ToString(want.Float)),
"floatSlice": events.NewNumberSetAttribute([]string{float64ToString(want.FloatSlice[0]), float64ToString(want.FloatSlice[1]), float64ToString(want.FloatSlice[2])}),
"string": events.NewStringAttribute(want.String),
"stringSlice": events.NewStringSetAttribute(want.StringSlice),
"nil": events.NewNullAttribute(),
}),
"int": events.NewNumberAttribute(strconv.Itoa(want.Int)),
"intSlice": events.NewNumberSetAttribute([]string{strconv.Itoa(want.IntSlice[0]), strconv.Itoa(want.IntSlice[1]), strconv.Itoa(want.IntSlice[2])}),
"float": events.NewNumberAttribute(float64ToString(want.Float)),
"floatSlice": events.NewNumberSetAttribute([]string{float64ToString(want.FloatSlice[0]), float64ToString(want.FloatSlice[1]), float64ToString(want.FloatSlice[2])}),
"string": events.NewStringAttribute(want.String),
"stringSlice": events.NewStringSetAttribute(want.StringSlice),
"nil": events.NewNullAttribute(),
}, td))
require.NotNil(t, td)
require.Equal(t, want, td)
}
func stringSliceToSliceOfByteSlices(stringSlice ...string) [][]byte {
sliceOfByteSlices := make([][]byte, len(stringSlice))
for i := 0; i < len(stringSlice); i++ {
sliceOfByteSlices[i] = []byte(stringSlice[i])
}
return sliceOfByteSlices
}
func float64ToString(f float64) string {
return strconv.FormatFloat(f, 'E', -1, 64)
}