-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_type_test.go
156 lines (151 loc) · 4.28 KB
/
map_type_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
155
156
package sqldav
import (
"errors"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/google/go-cmp/cmp"
"testing"
)
func TestMap_Scan(t *testing.T) {
type testCase struct {
sut Map
args interface{}
want error
expectedState Map
}
tests := map[string]testCase{
"happy-path/empty-map": {
sut: Map{},
args: map[string]interface{}{},
expectedState: Map{},
},
"happy-path/single-value": {
sut: Map{},
args: map[string]interface{}{"a": 1},
expectedState: Map{"a": 1},
},
"happy-path/multiple-values": {
sut: Map{},
args: map[string]interface{}{"a": 1, "b": "2"},
expectedState: Map{"a": 1, "b": "2"},
},
"unhappy-path/non-map-value": {
sut: Map{},
args: "non-map",
want: ErrFailedToCast,
},
"unhappy-path/sut-is-not-empty": {
sut: Map{"existing": 1},
args: map[string]interface{}{"a": 1},
want: ErrCollectionAlreadyContainsItem,
expectedState: Map{"existing": 1},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
err := tt.sut.Scan(tt.args)
if !errors.Is(err, tt.want) {
t.Errorf("Scan() error = %v, want %v", err, tt.want)
return
}
if diff := cmp.Diff(tt.expectedState, tt.sut); diff != "" {
t.Errorf("Scan() mismatch (-want +got):\n%s", diff)
return
}
})
}
}
func TestMap_ResolveNestedCollections(t *testing.T) {
type testCase struct {
sut Map
expectedState Map
want error
}
tests := map[string]testCase{
"happy-path/empty-map": {
sut: Map{},
expectedState: Map{},
},
"happy-path/single-nested-map": {
sut: Map{"a": map[string]interface{}{"b": 1}},
expectedState: Map{"a": Map{"b": 1}},
},
"happy-path/multiple-nested-maps": {
sut: Map{"a": map[string]interface{}{"b": 1}, "c": map[string]interface{}{"d": 2}},
expectedState: Map{"a": Map{"b": 1}, "c": Map{"d": 2}},
},
"happy-path/nested-list": {
sut: Map{"a": []interface{}{1, "b"}},
expectedState: Map{"a": List{1, "b"}},
},
"happy-path/nested-int-sets": {
sut: Map{"a": []float64{1, 2, 3}},
expectedState: Map{"a": Set[int]{1, 2, 3}},
},
"happy-path/nested-float-sets": {
sut: Map{"a": []float64{1.1, 2.1, 3.1}},
expectedState: Map{"a": Set[float64]{1.1, 2.1, 3.1}},
},
"happy-path/nested-string-sets": {
sut: Map{"a": []string{"1", "2", "3"}},
expectedState: Map{"a": Set[string]{"1", "2", "3"}},
},
"happy-path/nested-binary-sets": {
sut: Map{"a": [][]byte{[]byte("1"), []byte("2"), []byte("3")}},
expectedState: Map{"a": Set[[]byte]{[]byte("1"), []byte("2"), []byte("3")}},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
err := resolveCollectionsNestedInMap(&tt.sut)
if !errors.Is(err, tt.want) {
t.Errorf("ResolveNestedDocument() error = %v, want %v", err, tt.want)
return
}
if diff := cmp.Diff(tt.expectedState, tt.sut); diff != "" {
t.Errorf("ResolveNestedDocument() mismatch (-want +got):\n%s", diff)
return
}
})
}
}
func TestMap_Value(t *testing.T) {
type want struct {
value *types.AttributeValueMemberM
error error
}
type test struct {
sut Map
want want
}
tests := map[string]test{
"happy-path": {
sut: Map{"a": 1},
want: want{
value: &types.AttributeValueMemberM{Value: map[string]types.AttributeValue{
"a": &types.AttributeValueMemberN{Value: "1"},
}},
},
},
}
opts := []cmp.Option{
cmp.AllowUnexported(types.AttributeValueMemberS{}),
cmp.AllowUnexported(types.AttributeValueMemberSS{}),
cmp.AllowUnexported(types.AttributeValueMemberN{}),
cmp.AllowUnexported(types.AttributeValueMemberNS{}),
cmp.AllowUnexported(types.AttributeValueMemberB{}),
cmp.AllowUnexported(types.AttributeValueMemberBS{}),
cmp.AllowUnexported(types.AttributeValueMemberL{}),
cmp.AllowUnexported(types.AttributeValueMemberM{}),
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := tt.sut.Value()
if !errors.Is(err, tt.want.error) {
t.Errorf("Value() error = %v, want %v", err, tt.want.error)
}
if diff := cmp.Diff(tt.want.value, got, opts...); diff != "" {
t.Errorf("Value() mismatch (-want +got):\n%s", diff)
}
})
}
}