mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Added unit tests in utils
This commit is contained in:
committed by
Dan Christian Bogos
parent
fcffdd68a7
commit
f2e5c2f9c2
@@ -1371,3 +1371,11 @@ func TestCoreUtilsGenerateDBItemOpts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSliceStringPointer(t *testing.T) {
|
||||
sliceTest := []string{"ELEMENT_1", "ELEMENT_2"}
|
||||
rcv := SliceStringPointer(sliceTest)
|
||||
if fmt.Sprintf("%T", rcv) != "*[]string" {
|
||||
t.Error("Could not convert to pointer slice")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,3 +126,24 @@ func TestCloneDynamicWeights(t *testing.T) {
|
||||
t.Errorf("Expected %+v, received %+v", dynWeigh, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDynamicWeightEquals(t *testing.T) {
|
||||
//Test the case where one of the fields is nil
|
||||
dW := &DynamicWeight{
|
||||
FilterIDs: nil,
|
||||
Weight: 10,
|
||||
}
|
||||
dnWg := &DynamicWeight{
|
||||
FilterIDs: []string{"fltr1", "fltr2"},
|
||||
Weight: 10,
|
||||
}
|
||||
if rcv := dW.Equals(dnWg); rcv {
|
||||
t.Error("FilterIDs should not match")
|
||||
}
|
||||
|
||||
//Test the case where filters don't match
|
||||
dW.FilterIDs = []string{"fltr1", "fltr3"}
|
||||
if rcv := dW.Equals(dnWg); rcv {
|
||||
t.Error("FilterIDs should not match")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,3 +190,57 @@ func TestNewErrNotConnected(t *testing.T) {
|
||||
t.Errorf("Expected %v \n but received %v\n", exp, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewErrResourceS(t *testing.T) {
|
||||
err := errors.New("TEST_RESOURCES_ERROR")
|
||||
exp := "RESOURCES_ERROR:TEST_RESOURCES_ERROR"
|
||||
if rcv := NewErrResourceS(err); rcv.Error() != exp {
|
||||
t.Errorf("Expected %v \n but received %v\n", exp, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewErrRouteS(t *testing.T) {
|
||||
err := errors.New("TEST_ROUTES_ERROR")
|
||||
exp := "ROUTES_ERROR:TEST_ROUTES_ERROR"
|
||||
if rcv := NewErrRouteS(err); rcv.Error() != exp {
|
||||
t.Errorf("Expected %v \n but received %v\n", exp, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewAttributeS(t *testing.T) {
|
||||
err := errors.New("TEST_ATTRIBUTES_ERROR")
|
||||
exp := "ATTRIBUTES_ERROR:TEST_ATTRIBUTES_ERROR"
|
||||
if rcv := NewErrAttributeS(err); rcv.Error() != exp {
|
||||
t.Errorf("Expected %v \n but received %v\n", exp, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDispatcherS(t *testing.T) {
|
||||
err := errors.New("TEST_DISPATCHER_ERROR")
|
||||
exp := "DISPATCHER_ERROR:TEST_DISPATCHER_ERROR"
|
||||
if rcv := NewErrDispatcherS(err); rcv.Error() != exp {
|
||||
t.Errorf("Expected %v \n but received %v\n", exp, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPIErrorHandler(t *testing.T) {
|
||||
errIn := &CGRError{
|
||||
context: "*sessions",
|
||||
apiError: "API_ERROR",
|
||||
shortError: "SHORT_ERROR",
|
||||
longError: "LONG_ERROR",
|
||||
errorMessage: "ERROR_MESSAGE",
|
||||
}
|
||||
exp := "API_ERROR"
|
||||
if rcv := APIErrorHandler(errIn); rcv.Error() != exp {
|
||||
t.Errorf("Expected %v \n but received %v\n", exp, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPIErrorHandlerCase2(t *testing.T) {
|
||||
errIn := errors.New("NEW_ERROR")
|
||||
exp := "SERVER_ERROR: NEW_ERROR"
|
||||
if rcv := APIErrorHandler(errIn); rcv.Error() != exp {
|
||||
t.Errorf("Expected %v \n but received %v\n", exp, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,3 +259,30 @@ func TestFlagsWithParamsClone(t *testing.T) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", ToJSON(fWp), ToJSON(cln))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapStringStringEqual(t *testing.T) {
|
||||
v1 := map[string]string{
|
||||
"testMap": "value1",
|
||||
}
|
||||
|
||||
v2 := map[string]string{
|
||||
"testMap": "value1",
|
||||
}
|
||||
|
||||
//Matching maps
|
||||
if rcv := MapStringStringEqual(v1, v2); !rcv {
|
||||
t.Error("The maps should match")
|
||||
}
|
||||
|
||||
//Different map values
|
||||
v2["testMap"] = "value2"
|
||||
if rcv := MapStringStringEqual(v1, v2); rcv {
|
||||
t.Error("The maps should match")
|
||||
}
|
||||
|
||||
//Different map length
|
||||
v1["testMap2"] = "value1"
|
||||
if rcv := MapStringStringEqual(v1, v2); rcv {
|
||||
t.Error("The maps should match")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -870,3 +870,136 @@ func TestOrderedNavigableMapOrderedFields(t *testing.T) {
|
||||
t.Errorf("Expected %+v, received %+v", exp2, rcv2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedNavigableMapString(t *testing.T) {
|
||||
onm := &OrderedNavigableMap{
|
||||
nm: &DataNode{
|
||||
Type: NMMapType,
|
||||
Map: map[string]*DataNode{
|
||||
"Node1": {
|
||||
Value: &DataLeaf{
|
||||
Data: "value",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
exp := `{"Map":{"Node1":{"Value":{"Data":"value"}}}}`
|
||||
rcv := onm.String()
|
||||
if rcv != exp {
|
||||
t.Errorf("Expected %v but received %v", exp, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedNavigableMapInterface(t *testing.T) {
|
||||
onm := &OrderedNavigableMap{
|
||||
nm: &DataNode{
|
||||
Type: NMMapType,
|
||||
Map: map[string]*DataNode{
|
||||
"Node1": {
|
||||
Value: &DataLeaf{
|
||||
Data: "value",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
// exp := `{"Map":{"Node1":{"Value":{"Data":"value"}}}}`
|
||||
rcv := onm.Interface()
|
||||
if rcv != onm.nm {
|
||||
t.Errorf("Expected %v but received %v", onm.nm, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedNavigableMapSetAsSlice(t *testing.T) {
|
||||
nm := NewOrderedNavigableMap()
|
||||
nm.Set(&FullPath{
|
||||
PathSlice: []string{},
|
||||
Path: "Field1.Field2[0]",
|
||||
}, NewLeafNode("1003"))
|
||||
if err := nm.SetAsSlice(&FullPath{
|
||||
PathSlice: []string{},
|
||||
Path: "Field1.Field2[0]",
|
||||
}, nil); err != ErrWrongPath {
|
||||
t.Errorf("Exptected %v but received %v", err, ErrWrongPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedNavigableMapFieldAsStringError(t *testing.T) {
|
||||
onm := &OrderedNavigableMap{
|
||||
nm: &DataNode{
|
||||
Type: NMDataType,
|
||||
Map: map[string]*DataNode{
|
||||
"Node1": {
|
||||
Value: &DataLeaf{
|
||||
Data: "value",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := onm.FieldAsString([]string{"path1"}); err == nil || err != ErrNotFound {
|
||||
t.Errorf("Expected %v but received %v", ErrNotFound, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedNavigableMapAppend(t *testing.T) {
|
||||
onm := &OrderedNavigableMap{
|
||||
nm: &DataNode{
|
||||
Type: NMDataType,
|
||||
Map: map[string]*DataNode{
|
||||
"Node1": {
|
||||
Value: &DataLeaf{
|
||||
Data: "value",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if err := onm.Append(&FullPath{
|
||||
PathSlice: []string{"path1"},
|
||||
Path: "Field1.Field2[0]",
|
||||
}, nil); err == nil || err != ErrWrongPath {
|
||||
t.Errorf("Expected %v but received %v", ErrWrongPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedNavigableMapCompose(t *testing.T) {
|
||||
onm := &OrderedNavigableMap{
|
||||
nm: &DataNode{
|
||||
Type: NMMapType,
|
||||
Map: map[string]*DataNode{
|
||||
"Node1": {
|
||||
Value: &DataLeaf{
|
||||
Data: "value",
|
||||
},
|
||||
},
|
||||
},
|
||||
Value: &DataLeaf{
|
||||
Data: "Leaf1",
|
||||
},
|
||||
},
|
||||
orderRef: map[string][]*PathItemElement{
|
||||
"Field1.Field2[0]": {
|
||||
{
|
||||
Value: []string{"orderRefString"},
|
||||
},
|
||||
},
|
||||
},
|
||||
orderIdx: &PathItemList{
|
||||
len: 1,
|
||||
},
|
||||
}
|
||||
|
||||
if err := onm.Compose(&FullPath{
|
||||
PathSlice: []string{"Node1"},
|
||||
Path: "Field1.Field2[0]",
|
||||
}, &DataLeaf{Data: "Leaf2"}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if onm.nm.Map["Node1"].Value.Data != "valueLeaf2" {
|
||||
t.Errorf("Expected %v but received %v", "valueLeaf2", onm.nm.Value.Data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ericlagergren/decimal"
|
||||
)
|
||||
|
||||
func TestGreaterThan(t *testing.T) {
|
||||
@@ -1197,3 +1199,189 @@ func TestMultiplyInt64Error(t *testing.T) {
|
||||
t.Errorf("Expected <strconv.ParseInt: parsing \"cat\": invalid syntax> ,received: <%+v>", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIfaceAsBig(t *testing.T) {
|
||||
timeDur := time.Duration(1)
|
||||
rcv, _ := IfaceAsBig(timeDur)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(1)) {
|
||||
t.Errorf("Expected %v but received %v", 1, rcv)
|
||||
}
|
||||
|
||||
testInt := 2
|
||||
rcv, _ = IfaceAsBig(testInt)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(2)) {
|
||||
t.Errorf("Expected %v but received %v", 2, rcv)
|
||||
}
|
||||
|
||||
testInt8 := int8(3)
|
||||
rcv, _ = IfaceAsBig(testInt8)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(3)) {
|
||||
t.Errorf("Expected %v but received %v", 3, rcv)
|
||||
}
|
||||
|
||||
testInt16 := int16(4)
|
||||
rcv, _ = IfaceAsBig(testInt16)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(4)) {
|
||||
t.Errorf("Expected %v but received %v", 4, rcv)
|
||||
}
|
||||
|
||||
testInt32 := int32(5)
|
||||
rcv, _ = IfaceAsBig(testInt32)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(5)) {
|
||||
t.Errorf("Expected %v but received %v", 5, rcv)
|
||||
}
|
||||
|
||||
testInt64 := int64(6)
|
||||
rcv, _ = IfaceAsBig(testInt64)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(6)) {
|
||||
t.Errorf("Expected %v but received %v", 6, rcv)
|
||||
}
|
||||
|
||||
uTestInt := uint(2)
|
||||
rcv, _ = IfaceAsBig(uTestInt)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(2)) {
|
||||
t.Errorf("Expected %v but received %v", 2, rcv)
|
||||
}
|
||||
|
||||
uTestInt8 := uint8(3)
|
||||
rcv, _ = IfaceAsBig(uTestInt8)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(3)) {
|
||||
t.Errorf("Expected %v but received %v", 3, rcv)
|
||||
}
|
||||
|
||||
uTestInt16 := uint16(4)
|
||||
rcv, _ = IfaceAsBig(uTestInt16)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(4)) {
|
||||
t.Errorf("Expected %v but received %v", 4, rcv)
|
||||
}
|
||||
|
||||
uTestInt32 := uint32(5)
|
||||
rcv, _ = IfaceAsBig(uTestInt32)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(5)) {
|
||||
t.Errorf("Expected %v but received %v", 5, rcv)
|
||||
}
|
||||
|
||||
uTestInt64 := uint64(6)
|
||||
rcv, _ = IfaceAsBig(uTestInt64)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(6)) {
|
||||
t.Errorf("Expected %v but received %v", 6, rcv)
|
||||
}
|
||||
|
||||
testFloat64 := float64(12.2)
|
||||
rcv, _ = IfaceAsBig(testFloat64)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetFloat64(12.2)) {
|
||||
t.Errorf("Expected %v but received %v", 12.2, rcv)
|
||||
}
|
||||
|
||||
testFloat32 := float32(12)
|
||||
rcv, _ = IfaceAsBig(testFloat32)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetFloat64(float64(12))) {
|
||||
t.Errorf("Expected %v but received %v", new(decimal.Big).SetFloat64(float64(12)), rcv)
|
||||
}
|
||||
|
||||
timeSuffix := "not_valid_timems"
|
||||
_, err := IfaceAsBig(timeSuffix)
|
||||
errExpect := `time: invalid duration "not_valid_timems"`
|
||||
if err == nil || err.Error() != errExpect {
|
||||
t.Errorf("Expected %v but received %v", errExpect, err)
|
||||
}
|
||||
|
||||
num := "123"
|
||||
rcv, err = IfaceAsBig(num)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(123)) {
|
||||
t.Errorf("Expected %v but received %v", new(decimal.Big).SetUint64(123), rcv)
|
||||
}
|
||||
|
||||
decBig := new(decimal.Big).SetUint64(21)
|
||||
rcv, _ = IfaceAsBig(decBig)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(21)) {
|
||||
t.Errorf("Expected %v but received %v", new(decimal.Big).SetUint64(21), rcv)
|
||||
}
|
||||
|
||||
dec := NewDecimal(10, 0)
|
||||
rcv, _ = IfaceAsBig(dec)
|
||||
if !reflect.DeepEqual(rcv, new(decimal.Big).SetUint64(10)) {
|
||||
t.Errorf("Expected %v but received %v", new(decimal.Big).SetUint64(10), rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflectFieldMethodInterfaceStruct(t *testing.T) {
|
||||
//Make obj a struct
|
||||
type Obj struct {
|
||||
Field1 string
|
||||
Field2 string
|
||||
}
|
||||
|
||||
obj := Obj{
|
||||
Field1: "field1_string",
|
||||
}
|
||||
|
||||
rcv, err := ReflectFieldMethodInterface(obj, "Field1")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if rcv != "field1_string" {
|
||||
t.Errorf("Expected %v but received %v", "field_string1", rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflectFieldMethodInterfaceMap(t *testing.T) {
|
||||
//Make obj a map[string]string
|
||||
type Obj map[string]string
|
||||
|
||||
obj := &Obj{
|
||||
"MapField1": "map_field_1",
|
||||
}
|
||||
|
||||
rcv, err := ReflectFieldMethodInterface(obj, "MapField1")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if rcv != "map_field_1" {
|
||||
t.Errorf("Expected %v but received %v", "map_field_1", rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflectFieldMethodInterfaceSlice(t *testing.T) {
|
||||
obj := []string{"sliceField1"}
|
||||
rcv, err := ReflectFieldMethodInterface(obj, "0")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if rcv != "sliceField1" {
|
||||
t.Errorf("Expected %v but received %v", "sliceField1", rcv)
|
||||
}
|
||||
|
||||
_, err = ReflectFieldMethodInterface(obj, "invalid_index")
|
||||
if err == nil || err.Error() != `strconv.Atoi: parsing "invalid_index": invalid syntax` {
|
||||
t.Errorf("Expected %v but received %v", `strconv.Atoi: parsing "invalid_index": invalid syntax`, err)
|
||||
}
|
||||
|
||||
_, err = ReflectFieldMethodInterface(obj, "2")
|
||||
if err == nil || err.Error() != "index out of range" {
|
||||
t.Errorf("Expected %v but received %v", "index out of range", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflectFieldMethodInterfaceDefault(t *testing.T) {
|
||||
invalidKind := 2
|
||||
_, err := ReflectFieldMethodInterface(invalidKind, "2")
|
||||
if err == nil || err.Error() != "unsupported field kind: int" {
|
||||
t.Errorf("Expected %v but received %v", "unsupported field kind: int", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflectFieldMethodInterfaceInvalidField(t *testing.T) {
|
||||
type Obj map[string][]string
|
||||
|
||||
obj := &Obj{
|
||||
"MapField1": []string{""},
|
||||
}
|
||||
_, err := ReflectFieldMethodInterface(obj, "NotExistingField")
|
||||
if err == nil || err != ErrNotFound {
|
||||
t.Errorf("Expected %v but received %v", ErrNotFound, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user