| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package awsdynamodb |
| 16 |
|
| 17 |
import ( |
| 18 |
"reflect" |
| 19 |
"testing" |
| 20 |
|
| 21 |
dynattr "github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" |
| 22 |
dyn2Types "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" |
| 23 |
"github.com/google/go-cmp/cmp" |
| 24 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 25 |
"gocloud.dev/docstore/driver" |
| 26 |
"gocloud.dev/docstore/drivertest" |
| 27 |
) |
| 28 |
|
| 29 |
var compareIgnoreAttributeUnexported = cmpopts.IgnoreUnexported( |
| 30 |
dyn2Types.AttributeValueMemberB{}, |
| 31 |
dyn2Types.AttributeValueMemberBOOL{}, |
| 32 |
dyn2Types.AttributeValueMemberBS{}, |
| 33 |
dyn2Types.AttributeValueMemberL{}, |
| 34 |
dyn2Types.AttributeValueMemberM{}, |
| 35 |
dyn2Types.AttributeValueMemberN{}, |
| 36 |
dyn2Types.AttributeValueMemberNS{}, |
| 37 |
dyn2Types.AttributeValueMemberNULL{}, |
| 38 |
dyn2Types.AttributeValueMemberS{}, |
| 39 |
dyn2Types.AttributeValueMemberSS{}, |
| 40 |
) |
| 41 |
|
| 42 |
func TestEncodeValue(t *testing.T) { |
| 43 |
avn := func(s string) dyn2Types.AttributeValue { return &dyn2Types.AttributeValueMemberN{Value: s} } |
| 44 |
avl := func(avs ...dyn2Types.AttributeValue) dyn2Types.AttributeValue { |
| 45 |
return &dyn2Types.AttributeValueMemberL{Value: avs} |
| 46 |
} |
| 47 |
|
| 48 |
var seven int32 = 7 |
| 49 |
var nullptr *int |
| 50 |
|
| 51 |
for _, test := range []struct { |
| 52 |
in any |
| 53 |
want dyn2Types.AttributeValue |
| 54 |
}{ |
| 55 |
// null |
| 56 |
{nil, nullValue}, |
| 57 |
{nullptr, nullValue}, |
| 58 |
// number |
| 59 |
{0, avn("0")}, |
| 60 |
{uint64(999), avn("999")}, |
| 61 |
{3.5, avn("3.5")}, |
| 62 |
{seven, avn("7")}, |
| 63 |
{&seven, avn("7")}, |
| 64 |
// string |
| 65 |
{"", nullValue}, |
| 66 |
{"x", &dyn2Types.AttributeValueMemberS{Value: "x"}}, |
| 67 |
{"abc123", &dyn2Types.AttributeValueMemberS{Value: "abc123"}}, |
| 68 |
{"abc 123", &dyn2Types.AttributeValueMemberS{Value: "abc 123"}}, |
| 69 |
// bool |
| 70 |
{true, &dyn2Types.AttributeValueMemberBOOL{Value: true}}, |
| 71 |
{false, &dyn2Types.AttributeValueMemberBOOL{Value: false}}, |
| 72 |
// list |
| 73 |
{[]int(nil), nullValue}, |
| 74 |
{[]int{}, &dyn2Types.AttributeValueMemberL{Value: []dyn2Types.AttributeValue{}}}, |
| 75 |
{[]int{1, 2}, avl(avn("1"), avn("2"))}, |
| 76 |
{[...]int{1, 2}, avl(avn("1"), avn("2"))}, |
| 77 |
{[]any{nil, false}, avl(nullValue, &dyn2Types.AttributeValueMemberBOOL{Value: false})}, |
| 78 |
// map |
| 79 |
{map[string]int(nil), nullValue}, |
| 80 |
{map[string]int{}, &dyn2Types.AttributeValueMemberM{Value: map[string]dyn2Types.AttributeValue{}}}, |
| 81 |
{ |
| 82 |
map[string]int{"a": 1, "b": 2}, |
| 83 |
&dyn2Types.AttributeValueMemberM{Value: map[string]dyn2Types.AttributeValue{ |
| 84 |
"a": avn("1"), |
| 85 |
"b": avn("2"), |
| 86 |
}}, |
| 87 |
}, |
| 88 |
} { |
| 89 |
var e encoder |
| 90 |
if err := driver.Encode(reflect.ValueOf(test.in), &e); err != nil { |
| 91 |
t.Fatal(err) |
| 92 |
} |
| 93 |
got := e.av |
| 94 |
if !cmp.Equal(got, test.want, compareIgnoreAttributeUnexported) { |
| 95 |
t.Errorf("%#v: got %#v, want %#v", test.in, got, test.want) |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func TestDecodeValue(t *testing.T) { |
| 101 |
avn := func(s string) dyn2Types.AttributeValue { return &dyn2Types.AttributeValueMemberN{Value: s} } |
| 102 |
avl := func(vals ...dyn2Types.AttributeValue) dyn2Types.AttributeValue { |
| 103 |
return &dyn2Types.AttributeValueMemberL{Value: vals} |
| 104 |
} |
| 105 |
|
| 106 |
for _, test := range []struct { |
| 107 |
in dyn2Types.AttributeValue |
| 108 |
want any |
| 109 |
}{ |
| 110 |
// null |
| 111 |
// {nullValue, nil}, // cant reflect new, how best to test? |
| 112 |
// bool |
| 113 |
{&dyn2Types.AttributeValueMemberBOOL{Value: false}, false}, |
| 114 |
{&dyn2Types.AttributeValueMemberBOOL{Value: true}, true}, |
| 115 |
// string |
| 116 |
{&dyn2Types.AttributeValueMemberS{Value: "x"}, "x"}, |
| 117 |
// int64 |
| 118 |
{avn("7"), int64(7)}, |
| 119 |
{avn("-7"), int64(-7)}, |
| 120 |
{avn("0"), int64(0)}, |
| 121 |
// uint64 |
| 122 |
{avn("7"), uint64(7)}, |
| 123 |
{avn("0"), uint64(0)}, |
| 124 |
// float64 |
| 125 |
{avn("7"), float64(7)}, |
| 126 |
{avn("0"), float64(0)}, |
| 127 |
{avn("3.1415"), float64(3.1415)}, |
| 128 |
// []byte |
| 129 |
{&dyn2Types.AttributeValueMemberB{Value: []byte(`123`)}, []byte(`123`)}, |
| 130 |
// List |
| 131 |
{avl(avn("12"), avn("37")), []int64{12, 37}}, |
| 132 |
{avl(avn("12"), avn("37")), []uint64{12, 37}}, |
| 133 |
{avl(avn("12.8"), avn("37.1")), []float64{12.8, 37.1}}, |
| 134 |
// Map |
| 135 |
{ |
| 136 |
&dyn2Types.AttributeValueMemberM{Value: map[string]dyn2Types.AttributeValue{}}, |
| 137 |
map[string]int{}, |
| 138 |
}, |
| 139 |
{ |
| 140 |
&dyn2Types.AttributeValueMemberM{Value: map[string]dyn2Types.AttributeValue{"a": avn("1"), "b": avn("2")}}, |
| 141 |
map[string]int{"a": 1, "b": 2}, |
| 142 |
}, |
| 143 |
} { |
| 144 |
var ( |
| 145 |
target = reflect.New(reflect.TypeOf(test.want)) |
| 146 |
) |
| 147 |
|
| 148 |
dec := decoder{av: test.in} |
| 149 |
if err := driver.Decode(target.Elem(), dec); err != nil { |
| 150 |
t.Errorf(" error decoding value %#v, got error: %#v", test.in, err) |
| 151 |
continue |
| 152 |
} |
| 153 |
|
| 154 |
if !cmp.Equal(target.Elem().Interface(), test.want, compareIgnoreAttributeUnexported) { |
| 155 |
t.Errorf(" %#v: got %#v, want %#v", test.in, target.Elem().Interface(), test.want) |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func TestDecodeErrorOnUnsupported(t *testing.T) { |
| 161 |
for _, tc := range []struct { |
| 162 |
in dyn2Types.AttributeValue |
| 163 |
out any |
| 164 |
}{ |
| 165 |
{&dyn2Types.AttributeValueMemberSS{Value: []string{"foo", "bar"}}, []string{}}, |
| 166 |
{&dyn2Types.AttributeValueMemberNS{Value: []string{"1.1", "-2.2", "3.3"}}, []float64{}}, |
| 167 |
{&dyn2Types.AttributeValueMemberBS{Value: [][]byte{{4}, {5}, {6}}}, [][]byte{}}, |
| 168 |
} { |
| 169 |
d := decoder{av: tc.in} |
| 170 |
if err := driver.Decode(reflect.ValueOf(tc.out), &d); err == nil { |
| 171 |
t.Error("got nil error, want unsupported error") |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
type codecTester struct{} |
| 177 |
|
| 178 |
func (ct *codecTester) UnsupportedTypes() []drivertest.UnsupportedType { |
| 179 |
return []drivertest.UnsupportedType{drivertest.BinarySet} |
| 180 |
} |
| 181 |
|
| 182 |
func (ct *codecTester) NativeEncode(obj any) (any, error) { |
| 183 |
return dynattr.Marshal(obj) |
| 184 |
} |
| 185 |
|
| 186 |
func (ct *codecTester) NativeDecode(value, dest any) error { |
| 187 |
return dynattr.Unmarshal(value.(dyn2Types.AttributeValue), dest) |
| 188 |
} |
| 189 |
|
| 190 |
func (ct *codecTester) DocstoreEncode(obj any) (any, error) { |
| 191 |
return encodeDoc(drivertest.MustDocument(obj)) |
| 192 |
} |
| 193 |
|
| 194 |
func (ct *codecTester) DocstoreDecode(value, dest any) error { |
| 195 |
return decodeDoc(value.(dyn2Types.AttributeValue), drivertest.MustDocument(dest)) |
| 196 |
} |