-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
encoding_aws_test.go
154 lines (139 loc) · 3.39 KB
/
encoding_aws_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
package dynamo
import (
"reflect"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
type awsTestWidget struct {
UserID int // Hash key, a.k.a. partition key
Time time.Time // Range key, a.k.a. sort key
Msg string `dynamodbav:"Message"`
Count int `dynamodbav:",omitempty"`
Friends []string `dynamodbav:",stringset"` // Sets
SecretKey string `dynamodbav:"-"` // Ignored
}
func TestAWSEncoding(t *testing.T) {
w := awsTestWidget{
UserID: 555,
Time: time.Now().UTC(),
Msg: "hello",
Count: 0,
Friends: []string{"a", "b"},
SecretKey: "seeeekret",
}
av, err := Marshal(AWSEncoding(w))
if err != nil {
t.Error(err)
}
official, err := attributevalue.Marshal(w)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(av, official) {
t.Error("AWS marshal not equal")
}
blank := awsTestWidget{}
err = Unmarshal(official, AWSEncoding(&blank))
if err != nil {
t.Error(err)
}
w.SecretKey = ""
if !reflect.DeepEqual(blank, w) {
t.Error("AWS unmarshal not equal")
t.Logf("%#v != %#v", blank, w)
}
}
func TestAWSIfaces(t *testing.T) {
unix := attributevalue.UnixTime(time.Now())
av, err := Marshal(unix)
if err != nil {
t.Error(err)
}
official, err := attributevalue.Marshal(unix)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(av, official) {
t.Error("marshal not equal.", av, "≠", official)
}
var result, officialResult attributevalue.UnixTime
err = Unmarshal(official, &result)
if err != nil {
t.Error(err)
}
err = attributevalue.Unmarshal(official, &officialResult)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(result, officialResult) {
t.Error("unmarshal not equal.", result, "≠", officialResult)
}
}
func TestAWSItems(t *testing.T) {
type Foo struct {
ID string `dynamodbav:"id"`
}
item := Foo{
ID: "abcdefg",
}
result, err := marshalItem(AWSEncoding(item))
if err != nil {
t.Error(err)
}
official, err := attributevalue.MarshalMap(item)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(result, official) {
t.Error("marshal not equal.", result, "≠", official)
}
var unmarshaled, unmarshaledOfficial Foo
err = unmarshalItem(official, AWSEncoding(&unmarshaled))
if err != nil {
t.Error(err)
}
err = attributevalue.UnmarshalMap(official, &unmarshaledOfficial)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(unmarshaled, unmarshaledOfficial) {
t.Error("marshal not equal.", unmarshaled, "≠", unmarshaledOfficial)
}
}
func TestAWSUnmarshalAppend(t *testing.T) {
type foo struct {
A string `dynamo:"wrong1" dynamodbav:"one"`
B int `dynamo:"wrong2" dynamodbav:"two"`
}
var list []foo
expect1 := foo{
A: "test",
B: 555,
}
expect2 := foo{
A: "two",
B: 222,
}
err := unmarshalAppend(Item{
"one": &types.AttributeValueMemberS{Value: "test"},
"two": &types.AttributeValueMemberN{Value: "555"},
}, &list)
if err != nil {
t.Error(err)
}
if len(list) != 1 && reflect.DeepEqual(list, []foo{expect1}) {
t.Error("bad AWS unmarshal append:", list)
}
err = unmarshalAppend(Item{
"one": &types.AttributeValueMemberS{Value: ("two")},
"two": &types.AttributeValueMemberN{Value: ("222")},
}, &list)
if err != nil {
t.Error(err)
}
if len(list) != 2 && reflect.DeepEqual(list, []foo{expect1, expect2}) {
t.Error("bad AWS unmarshal append:", list)
}
}