Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Changed the String formatter from %s to %v and added more unit tests (#…
Browse files Browse the repository at this point in the history
…122)

Signed-off-by: pmahindrakar-oss <[email protected]>

Co-authored-by: pmahindrakar-oss <[email protected]>
  • Loading branch information
pmahindrakar-oss and pmahindrakar-oss authored Mar 16, 2021
1 parent 9116f22 commit 528d45e
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 12 deletions.
4 changes: 2 additions & 2 deletions clients/go/coreutils/literals.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,11 @@ func MakeLiteralForType(t *core.LiteralType, v interface{}) (*core.Literal, erro
}
case *core.LiteralType_Simple:
newT := t.Type.(*core.LiteralType_Simple)
lv, err := MakeLiteralForSimpleType(newT.Simple, fmt.Sprintf("%s", v))
lv, err := MakeLiteralForSimpleType(newT.Simple, fmt.Sprintf("%v", v))
if err != nil {
return nil, err
}
return lv, nil
}
return l, nil
}
}
136 changes: 126 additions & 10 deletions clients/go/coreutils/literals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package coreutils

import (
"fmt"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -367,18 +368,133 @@ func TestMakeLiteralForBlob(t *testing.T) {
}
}

func TestFoo(t *testing.T) {
func TestMakeLiteralForType(t *testing.T) {
t.Run("SimpleInteger", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_INTEGER}}
val, err := MakeLiteralForType(literalType, 1)
assert.NoError(t, err)
literalVal := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_Integer{Integer: 1}}}}}}
expectedVal, _ := ExtractFromLiteral(literalVal)
actualVal, _ := ExtractFromLiteral(val)
assert.Equal(t, expectedVal, actualVal)
})

t.Run("SimpleFloat", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_FLOAT}}
val, err := MakeLiteralForType(literalType, 1)
assert.NoError(t, err)
literalVal := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_FloatValue{FloatValue: 1.0}}}}}}
expectedVal, _ := ExtractFromLiteral(literalVal)
actualVal, _ := ExtractFromLiteral(val)
assert.Equal(t, expectedVal, actualVal)
})

t.Run("ArrayStrings", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_CollectionType{
CollectionType: &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_STRING}}}}
strArray := []interface{}{"hello", "world"}
val, err := MakeLiteralForType(literalType, strArray)
assert.NoError(t, err)
literalVal1 := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_StringValue{StringValue: "hello"}}}}}}
literalVal2 := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_StringValue{StringValue: "world"}}}}}}
literalCollection := []*core.Literal{literalVal1, literalVal2}
literalVal := &core.Literal{Value: &core.Literal_Collection{Collection: &core.LiteralCollection{Literals: literalCollection}}}
expectedVal, _ := ExtractFromLiteral(literalVal)
actualVal, _ := ExtractFromLiteral(val)
assert.Equal(t, expectedVal, actualVal)
})

t.Run("ArrayOfArrayStringsNotSupported", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_CollectionType{
CollectionType: &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_STRING}}}}
strArrayOfArray := [][]interface{}{{"hello1", "world1"}, {"hello2", "world2"}}
_, err := MakeLiteralForType(literalType, strArrayOfArray)
expectedErrorf := fmt.Errorf("collection type expected but found [][]interface {}")
assert.Equal(t, expectedErrorf, err)
})

t.Run("ArrayOfArrayStringsTypeErasure", func(t *testing.T) {
var collectionType = &core.LiteralType{Type: &core.LiteralType_CollectionType{
CollectionType: &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_STRING}}}}
var literalType = &core.LiteralType{Type: &core.LiteralType_CollectionType{
CollectionType: collectionType}}

createList1 := func() interface{} {
return []interface{}{"hello1", "world1"}
}
createList2 := func() interface{} {
return []interface{}{"hello2", "world2"}
}
createNestedList := func() interface{} {
return []interface{}{createList1(), createList2()}
}
var strArrayOfArray = createNestedList()
val, err := MakeLiteralForType(literalType, strArrayOfArray)
assert.NoError(t, err)
literalVal11 := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_StringValue{StringValue: "hello1"}}}}}}
literalVal12 := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_StringValue{StringValue: "world1"}}}}}}
literalCollection1Val := []*core.Literal{literalVal11, literalVal12}

literalCollection1 := &core.Literal{Value: &core.Literal_Collection{Collection: &core.LiteralCollection{Literals: literalCollection1Val}}}

literalVal21 := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_StringValue{StringValue: "hello2"}}}}}}
literalVal22 := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_StringValue{StringValue: "world2"}}}}}}
literalCollection2Val := []*core.Literal{literalVal21, literalVal22}
literalCollection2 := &core.Literal{Value: &core.Literal_Collection{Collection: &core.LiteralCollection{Literals: literalCollection2Val}}}
literalCollection := []*core.Literal{literalCollection1, literalCollection2}

literalVal := &core.Literal{Value: &core.Literal_Collection{Collection: &core.LiteralCollection{Literals: literalCollection}}}
expectedVal, _ := ExtractFromLiteral(literalVal)
actualVal, _ := ExtractFromLiteral(val)
assert.Equal(t, expectedVal, actualVal)
})

t.Run("MapStrings", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_MapValueType{
MapValueType: &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_STRING}}}}
mapVal := map[string]interface{}{"hello1": "world1", "hello2": "world2"}
val, err := MakeLiteralForType(literalType, mapVal)
assert.NoError(t, err)
literalVal1 := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_StringValue{StringValue: "world1"}}}}}}
literalVal2 := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_StringValue{StringValue: "world2"}}}}}}
literalMapVal := map[string]*core.Literal{"hello1": literalVal1, "hello2": literalVal2}
literalVal := &core.Literal{Value: &core.Literal_Map{Map: &core.LiteralMap{Literals: literalMapVal}}}
expectedVal, _ := ExtractFromLiteral(literalVal)
actualVal, _ := ExtractFromLiteral(val)
assert.Equal(t, expectedVal, actualVal)
})

t.Run("MapArrayOfStringsFail", func(t *testing.T) {
var collectionType *core.LiteralType
collectionType = &core.LiteralType{Type: &core.LiteralType_CollectionType{
var literalType = &core.LiteralType{Type: &core.LiteralType_MapValueType{
MapValueType: &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_STRING}}}}
strArray := map[string][]interface{}{"hello1": {"world11", "world12"}, "hello2": {"world21", "world22"}}
_, err := MakeLiteralForType(literalType, strArray)
expectedErrorf := fmt.Errorf("map value types can only be of type map[string]interface{}, but found map[string][]interface {}")
assert.Equal(t, expectedErrorf, err)
})

t.Run("MapArrayOfStringsTypeErasure", func(t *testing.T) {
var collectionType = &core.LiteralType{Type: &core.LiteralType_CollectionType{
CollectionType: &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_STRING}}}}
var literalType *core.LiteralType
literalType = &core.LiteralType{Type: &core.LiteralType_MapValueType{
var literalType = &core.LiteralType{Type: &core.LiteralType_MapValueType{
MapValueType: collectionType}}
createList := func() interface{} {
return []interface{} {"world11", "world12"}
createList1 := func() interface{} {
return []interface{}{"world11", "world12"}
}
createList2 := func() interface{} {
return []interface{}{"world21", "world22"}
}
strArray := map[string]interface{} {"hello1": createList(), "hello2": createList()}
strArray := map[string]interface{}{"hello1": createList1(), "hello2": createList2()}
val, err := MakeLiteralForType(literalType, strArray)
assert.NoError(t, err)
literalVal11 := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Expand All @@ -393,10 +509,10 @@ func TestFoo(t *testing.T) {
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_StringValue{StringValue: "world22"}}}}}}
literalCollection2 := []*core.Literal{literalVal21, literalVal22}
literalVal2 := &core.Literal{Value: &core.Literal_Collection{Collection: &core.LiteralCollection{Literals: literalCollection2}}}
literalMapVal := map[string]*core.Literal{"hello1":literalVal1, "hello2":literalVal2}
literalMapVal := map[string]*core.Literal{"hello1": literalVal1, "hello2": literalVal2}
literalVal := &core.Literal{Value: &core.Literal_Map{Map: &core.LiteralMap{Literals: literalMapVal}}}
expectedVal, _ := ExtractFromLiteral(literalVal)
actualVal, _ := ExtractFromLiteral(val)
assert.Equal(t, expectedVal, actualVal)
})
}
}

0 comments on commit 528d45e

Please sign in to comment.