get.porter.sh/porter@v1.3.0/pkg/storage/pluginstore/grpc_test.go (about) 1 package pluginstore 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 "go.mongodb.org/mongo-driver/bson" 9 ) 10 11 func TestConvertFloatToInt(t *testing.T) { 12 src := map[string]interface{}{ 13 "a": map[string]interface{}{ 14 "b": float64(1), 15 "c": []interface{}{ 16 float64(1), 17 float64(-1), 18 }, 19 }, 20 "d": []interface{}{ 21 map[string]interface{}{"e": "cat"}, 22 }, 23 } 24 25 dest := ConvertFloatToInt(src) 26 27 wantDest := map[string]interface{}{ 28 "a": map[string]interface{}{ 29 "b": int64(1), 30 "c": []interface{}{int64(1), int64(-1)}}, 31 "d": []interface{}{map[string]interface{}{ 32 "e": "cat"}, 33 }, 34 } 35 assert.Equal(t, wantDest, dest) 36 } 37 38 func TestConvertBsonM(t *testing.T) { 39 // Check that AsMap fixes float->int 40 src := map[string]interface{}{ 41 "a": map[string]interface{}{ 42 "b": float64(1), 43 "c": []interface{}{ 44 float64(1), 45 float64(-1), 46 }, 47 }, 48 } 49 50 tmp := NewStruct(src) 51 dest := AsMap(tmp) 52 53 wantDest := bson.M{ 54 "a": map[string]interface{}{ // right now we only convert the top level to the expected bson type. Mongo doesn't care if farther down we use primitives 55 "b": int64(1), 56 "c": []interface{}{ 57 int64(1), 58 int64(-1), 59 }, 60 }, 61 } 62 require.Equal(t, wantDest, dest) 63 } 64 65 func TestConvertBsonD(t *testing.T) { 66 src := bson.D{ 67 {Key: "a", Value: "1"}, 68 {Key: "b", Value: bson.D{ 69 {Key: "c", Value: 1}, 70 }}, 71 } 72 73 tmp := FromOrderedMap(src) 74 dest := AsOrderedMap(tmp, ConvertSliceToBsonD) 75 76 wantDest := bson.D{ 77 {Key: "a", Value: "1"}, 78 {Key: "b", Value: bson.D{{Key: "c", Value: int64(1)}}}, 79 } 80 require.Equal(t, wantDest, dest) 81 }