github.com/blend/go-sdk@v1.20220411.3/protoutil/any_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package protoutil 9 10 import ( 11 "testing" 12 13 "google.golang.org/protobuf/proto" 14 15 "github.com/blend/go-sdk/assert" 16 "github.com/blend/go-sdk/ex" 17 "github.com/blend/go-sdk/protoutil/testdata" 18 "github.com/blend/go-sdk/uuid" 19 ) 20 21 func Test_Any(t *testing.T) { 22 its := assert.New(t) 23 24 original := newTestMessage() 25 packed, err := Any(original) 26 its.Nil(err) 27 its.NotNil(packed) 28 its.Equal(TypeURLPrefix+"testdata.Message", packed.TypeUrl) 29 30 unpacked, err := FromAny(packed) 31 its.Nil(err) 32 its.NotNil(unpacked) 33 typed, ok := unpacked.(*testdata.Message) 34 its.True(ok) 35 its.Equal(original.Uid, typed.Uid) 36 its.Equal(FromTimestamp(original.TimestampUtc), FromTimestamp(typed.TimestampUtc)) 37 its.Equal(FromDuration(original.Elapsed), FromDuration(typed.Elapsed)) 38 its.Equal(original.StatusCode, typed.StatusCode) 39 its.Equal(original.Value, typed.Value) 40 its.Equal(original.Error, typed.Error) 41 42 // from any handles nil ... 43 unpacked, err = FromAny(nil) 44 its.Equal(ex.Class("cannot unpack message from nil *any.Any"), ex.ErrClass(err)) 45 its.Nil(unpacked) 46 47 // any handles bogus inputs 48 var bogus proto.Message 49 packed, err = Any(bogus) 50 its.NotNil(err) 51 its.Nil(packed) 52 } 53 54 func Test_FromTypeURL(t *testing.T) { 55 its := assert.New(t) 56 57 bareMessage, err := FromTypeURL("testdata.Message") 58 its.Nil(err) 59 its.NotNil(bareMessage) 60 typed, ok := bareMessage.(*testdata.Message) 61 its.True(ok) 62 its.NotNil(typed) 63 64 notFound, err := FromTypeURL(uuid.V4().String()) 65 its.NotNil(err) 66 its.Nil(notFound) 67 } 68 69 func Test_TypeURL(t *testing.T) { 70 its := assert.New(t) 71 72 its.Equal(TypeURLPrefix+"testdata.Message", TypeURL(new(testdata.Message))) 73 74 its.Equal("testdata.Message", MessageTypeFromTypeURL(TypeURL(new(testdata.Message)))) 75 }