From 4780f3bbef33a33d02f8956e0799d9b722b92ed2 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Wed, 4 Dec 2019 14:36:37 +0200 Subject: [PATCH] Added tests for MissingMapFields and IfaceAsSliceString --- utils/map.go | 18 ------- utils/reflect.go | 10 +++- utils/reflect_test.go | 121 ++++++++++++++++++++++++++++++++++++++++++ utils/struct_test.go | 28 +++++++++- 4 files changed, 155 insertions(+), 22 deletions(-) diff --git a/utils/map.go b/utils/map.go index 4adad4a4f..226ac626e 100644 --- a/utils/map.go +++ b/utils/map.go @@ -192,24 +192,6 @@ func (sm StringMap) GetSlice() (result []string) { return } -/* -func NoDots(m map[string]struct{}) map[string]struct{} { - return MapKeysReplace(m, ".", ".") -} - -func YesDots(m map[string]struct{}) map[string]struct{} { - return MapKeysReplace(m, ".", ".") -} - -func MapKeysReplace(m map[string]struct{}, old, new string) map[string]struct{} { - for key, val := range m { - delete(m, key) - key = strings.Replace(key, old, new, -1) - m[key] = val - } - return m -} -*/ // Used to merge multiple maps (eg: output of struct having ExtraFields) func MergeMapsStringIface(mps ...map[string]interface{}) (outMp map[string]interface{}) { outMp = make(map[string]interface{}) diff --git a/utils/reflect.go b/utils/reflect.go index 87a06b98f..be29d0ef0 100644 --- a/utils/reflect.go +++ b/utils/reflect.go @@ -292,6 +292,11 @@ func IfaceAsSliceString(fld interface{}) (out []string, err error) { for i, val := range value { out[i] = strconv.FormatInt(val, 10) } + case []uint: + out = make([]string, len(value)) + for i, val := range value { + out[i] = strconv.FormatUint(uint64(val), 10) + } case []uint32: out = make([]string, len(value)) for i, val := range value { @@ -344,8 +349,8 @@ func IfaceAsSliceString(fld interface{}) (out []string, err error) { for i, val := range value { out[i] = IfaceAsString(val) } - default: // Maybe we are lucky and the value converts to string - err = fmt.Errorf("cannot convert field: %+v to bool", value) + default: + err = fmt.Errorf("cannot convert field: %T to []string", value) } return } @@ -383,6 +388,7 @@ func GetUniformType(item interface{}) (interface{}, error) { } return item, nil } + func GetBasicType(item interface{}) interface{} { valItm := reflect.ValueOf(item) switch valItm.Kind() { // convert evreting to float64 diff --git a/utils/reflect_test.go b/utils/reflect_test.go index e4419c93b..4b4564682 100644 --- a/utils/reflect_test.go +++ b/utils/reflect_test.go @@ -18,6 +18,7 @@ along with this program. If not, see package utils import ( + "fmt" "net" "reflect" "strings" @@ -745,3 +746,123 @@ func TestReflectFieldMethodInterface(t *testing.T) { t.Error(err) } } + +func TestIfaceAsSliceString(t *testing.T) { + var attrs interface{} + var expected []string + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + + attrs = []int{1, 2, 3} + expected = []string{"1", "2", "3"} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = []int32{1, 2, 3} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = []int64{1, 2, 3} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = []uint{1, 2, 3} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = []uint{1, 2, 3} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = []uint32{1, 2, 3} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = []uint64{1, 2, 3} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = []float32{1, 2, 3} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = []float64{1, 2, 3} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = []string{"1", "2", "3"} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = [][]byte{[]byte("1"), []byte("2"), []byte("3")} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = []bool{true, false} + expected = []string{"true", "false"} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + + attrs = []time.Duration{time.Second, time.Minute} + expected = []string{"1s", "1m0s"} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + tNow := time.Now() + attrs = []time.Time{tNow} + expected = []string{tNow.Format(time.RFC3339)} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = []net.IP{net.ParseIP("127.0.0.1")} + expected = []string{"127.0.0.1"} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = []interface{}{true, 10, "two"} + expected = []string{"true", "10", "two"} + if rply, err := IfaceAsSliceString(attrs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting: %s ,received: %s", expected, rply) + } + attrs = "notSlice" + expError := fmt.Errorf("cannot convert field: %T to []string", attrs) + if _, err := IfaceAsSliceString(attrs); err == nil || err.Error() != expError.Error() { + t.Errorf("Expected error %s ,received: %v", expError, err) + } +} diff --git a/utils/struct_test.go b/utils/struct_test.go index 1b50a4c89..f762ccafa 100644 --- a/utils/struct_test.go +++ b/utils/struct_test.go @@ -19,6 +19,7 @@ package utils import ( "reflect" + "sort" "testing" ) @@ -28,10 +29,10 @@ func TestMissingStructFieldsCorrect(t *testing.T) { Direction string Account string Type string - ActionTimingsId string + ActionTimingsID string }{"bevoip.eu", "OUT", "danconns0001", META_PREPAID, "mama"} if missing := MissingStructFields(&attr, - []string{"Tenant", "Direction", "Account", "Type", "ActionTimingsId"}); len(missing) != 0 { + []string{"Tenant", "Direction", "Account", "Type", "ActionTimingsID"}); len(missing) != 0 { t.Error("Found missing field on correct struct", missing) } } @@ -275,3 +276,26 @@ func TestToMapMapStringInterface(t *testing.T) { t.Errorf("expecting: %+v, received: %+v", expMapStruct, mapStruct) } }*/ + +func TestMissingMapFields(t *testing.T) { + var attr = map[string]interface{}{ + Tenant: "cgrates.org", + Direction: "OUT", + Account: "1001", + "Type": META_PREPAID, + "ActionTimingsID": "*asap", + } + if missing := MissingMapFields(attr, + []string{"Tenant", "Direction", "Account", "Type", "ActionTimingsID"}); len(missing) != 0 { + t.Error("Found missing field on correct struct", missing) + } + attr["ActionTimingsID"] = "" + delete(attr, "Type") + expected := []string{"ActionTimingsID", "Type"} + missing := MissingMapFields(attr, + []string{"Tenant", "Direction", "Account", "Type", "ActionTimingsID"}) + sort.Strings(missing) + if !reflect.DeepEqual(expected, missing) { + t.Errorf("Expected %s ,received: %s", expected, missing) + } +}