mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Added tests for MissingMapFields and IfaceAsSliceString
This commit is contained in:
18
utils/map.go
18
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{})
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user