github.com/thiagoyeds/go-cloud@v0.26.0/docstore/awsdynamodb/codec_test.go (about) 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 dyn "github.com/aws/aws-sdk-go/service/dynamodb" 22 dynattr "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" 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 func TestEncodeValue(t *testing.T) { 30 av := func() *dyn.AttributeValue { return &dyn.AttributeValue{} } 31 avn := func(s string) *dyn.AttributeValue { return av().SetN(s) } 32 avl := func(avs ...*dyn.AttributeValue) *dyn.AttributeValue { return av().SetL(avs) } 33 34 var seven int32 = 7 35 var nullptr *int 36 37 for _, test := range []struct { 38 in interface{} 39 want *dyn.AttributeValue 40 }{ 41 {nil, nullValue}, 42 {0, avn("0")}, 43 {uint64(999), avn("999")}, 44 {3.5, avn("3.5")}, 45 {"", nullValue}, 46 {"x", av().SetS("x")}, 47 {true, av().SetBOOL(true)}, 48 {nullptr, nullValue}, 49 {seven, avn("7")}, 50 {&seven, avn("7")}, 51 {[]int(nil), nullValue}, 52 {[]int{}, av().SetL([]*dyn.AttributeValue{})}, 53 {[]int{1, 2}, avl(avn("1"), avn("2"))}, 54 {[...]int{1, 2}, avl(avn("1"), avn("2"))}, 55 {[]interface{}{nil, false}, avl(nullValue, av().SetBOOL(false))}, 56 {map[string]int(nil), nullValue}, 57 {map[string]int{}, av().SetM(map[string]*dyn.AttributeValue{})}, 58 { 59 map[string]int{"a": 1, "b": 2}, 60 av().SetM(map[string]*dyn.AttributeValue{ 61 "a": avn("1"), 62 "b": avn("2"), 63 }), 64 }, 65 } { 66 var e encoder 67 if err := driver.Encode(reflect.ValueOf(test.in), &e); err != nil { 68 t.Fatal(err) 69 } 70 got := e.av 71 if !cmp.Equal(got, test.want, cmpopts.IgnoreUnexported(dyn.AttributeValue{})) { 72 t.Errorf("%#v: got %#v, want %#v", test.in, got, test.want) 73 } 74 } 75 } 76 77 func TestDecodeErrorOnUnsupported(t *testing.T) { 78 av := func() *dyn.AttributeValue { return &dyn.AttributeValue{} } 79 sptr := func(s string) *string { return &s } 80 for _, tc := range []struct { 81 in *dyn.AttributeValue 82 out interface{} 83 }{ 84 {av().SetSS([]*string{sptr("foo"), sptr("bar")}), []string{}}, 85 {av().SetNS([]*string{sptr("1.1"), sptr("-2.2"), sptr("3.3")}), []float64{}}, 86 {av().SetBS([][]byte{{4}, {5}, {6}}), [][]byte{}}, 87 } { 88 d := decoder{av: tc.in} 89 if err := driver.Decode(reflect.ValueOf(tc.out), &d); err == nil { 90 t.Error("got nil error, want unsupported error") 91 } 92 } 93 } 94 95 type codecTester struct{} 96 97 func (ct *codecTester) UnsupportedTypes() []drivertest.UnsupportedType { 98 return []drivertest.UnsupportedType{drivertest.BinarySet} 99 } 100 101 func (ct *codecTester) NativeEncode(obj interface{}) (interface{}, error) { 102 return dynattr.Marshal(obj) 103 } 104 105 func (ct *codecTester) NativeDecode(value, dest interface{}) error { 106 return dynattr.Unmarshal(value.(*dyn.AttributeValue), dest) 107 } 108 109 func (ct *codecTester) DocstoreEncode(obj interface{}) (interface{}, error) { 110 return encodeDoc(drivertest.MustDocument(obj)) 111 } 112 113 func (ct *codecTester) DocstoreDecode(value, dest interface{}) error { 114 return decodeDoc(value.(*dyn.AttributeValue), drivertest.MustDocument(dest)) 115 }