utils tests

This commit is contained in:
gezimbll
2022-10-17 10:46:55 -04:00
committed by Dan Christian Bogos
parent 857596e536
commit 8b59c4dae6
4 changed files with 190 additions and 0 deletions

1
cmd/cgr-engine/cover.out Normal file
View File

@@ -0,0 +1 @@
mode: set

View File

@@ -627,3 +627,35 @@ func TestNMAsCGREvent(t *testing.T) {
t.Errorf("expecting: %+v, \nreceived: %+v", ToJSON(eEv), ToJSON(cgrEv.Event))
}
}
func TestCGREventRPCClone(t *testing.T) {
att := &CGREvent{
Tenant: "cgrates.org",
ID: "234",
clnb: false,
Time: TimePointer(time.Date(2013, 12, 30, 15, 0, 1, 0, time.UTC)),
Event: map[string]interface{}{
"key": "value",
},
APIOpts: map[string]interface{}{
"rand": "we",
},
}
if rec, _ := att.RPCClone(); !reflect.DeepEqual(rec, att) {
t.Errorf("expected %v and received %v ", ToJSON(att), ToJSON(rec))
}
att.SetCloneable(true)
if att.clnb != true {
t.Error("expected true")
}
rec, _ := att.RPCClone()
att.SetCloneable(false)
if !reflect.DeepEqual(rec, att) {
t.Errorf("expected %v and received %v ", att, rec)
}
}

View File

@@ -424,3 +424,133 @@ func TestFlagsWithParamsClone(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", ToJSON(fWp), ToJSON(cln))
}
}
// my test
func TestStringMapFieldAsInterfaceNotNil(t *testing.T) {
sm := StringMap{}
fldPath := []string{"first", "second"}
if val, _ := sm.FieldAsInterface(fldPath); val != nil {
t.Error("expected nil")
}
sm["first"] = true
fldPath = []string{"second"}
if val, _ := sm.FieldAsInterface(fldPath); val != nil {
t.Error("Should be nil")
}
fldPath[0] = "first"
if _, val := sm.FieldAsInterface(fldPath); val != nil {
t.Error("Should not be nil")
}
}
func TestStringMapFieldAsString(t *testing.T) {
sm := StringMap{}
fldPath := []string{"Hello"}
if _, val := sm.FieldAsString(fldPath); val == nil {
t.Error("Should not be nil")
}
sm["first"] = true
fldPath[0] = "first"
if _, val := sm.FieldAsString(fldPath); val != nil {
t.Error("Should be nil")
}
}
func TestStringMapHasKey(t *testing.T) {
sm := StringMap{"first": true}
key := "first"
exp := true
if v := sm.HasKey(key); v != exp {
t.Error("Expected true")
}
exp = false
if v := sm.HasKey(key); v == exp {
t.Error("Expected false")
}
}
func TestStringMapStringToInt64(t *testing.T) {
mymap := map[string]string{
"first": "1",
"second": "two",
}
exp := map[string]int64{
"first": 1,
"second": 2,
}
if val, _ := MapStringToInt64(mymap); reflect.DeepEqual(val, exp) == true {
t.Errorf("expected %v got %v", ToJSON(exp), ToJSON(val))
}
mymap["second"] = "2"
if val, _ := MapStringToInt64(mymap); reflect.DeepEqual(val, exp) == false {
t.Errorf("expected %v got %v", ToJSON(exp), ToJSON(val))
}
}
func TestStringMapIsEmpty(t *testing.T) {
sm := StringMap{"foo": true}
exp := false
if val := sm.IsEmpty(); val != exp {
t.Error("Should be false")
}
}
func TestStringMapFromSlice(t *testing.T) {
s := []string{"foo", "", "!baz", "bar"}
exp := map[string]bool{
"foo": true,
"baz": false,
"bar": true,
}
if val := StringMapFromSlice(s); reflect.DeepEqual(val, exp) {
t.Errorf("expected %v got %v", ToJSON(exp), ToJSON(val))
}
}
func TestStringMapNewStringMap(t *testing.T) {
s := []string{"foo", "", "!baz", "bar"}
exp := map[string]bool{
"foo": true,
"baz": false,
"bar": true,
}
if val := NewStringMap(s...); reflect.DeepEqual(val, exp) {
t.Errorf("expected %v got %v", ToJSON(exp), ToJSON(val))
}
}
func TestStringMapMapParse(t *testing.T) {
s := MetaZero
exp := make(StringMap)
if val := ParseStringMap(s); !reflect.DeepEqual(exp, val) {
t.Errorf("expected %v go t %v", ToJSON(exp), ToJSON(val))
}
s = "foo;!bar;baz"
exp = StringMap{
"foo": true,
"bar": false,
"baz": true,
}
if val := ParseStringMap(s); !reflect.DeepEqual(exp, val) {
t.Errorf("expected %v got %v", ToJSON(exp), ToJSON(val))
}
}

View File

@@ -74,3 +74,30 @@ func TestSliceStringToIface(t *testing.T) {
t.Errorf("Expected: %s ,received: %s", ToJSON(exp), ToJSON(rply))
}
}
func TestSliceStringEqualDiffLength(t *testing.T) {
v1 := []string{"foo", "bar"}
v2 := []string{"baz"}
if rcv := SliceStringEqual(v1, v2); rcv != false {
t.Errorf("expected false ,received : %v", rcv)
}
}
func TestSliceStringEqualElementEqualFalse(t *testing.T) {
v1 := []string{"foo", "bar"}
v2 := []string{"foo", "baz"}
if rcv := SliceStringEqual(v1, v2); rcv != false {
t.Errorf("expected false ,received : %v", rcv)
}
}
func TestSliceStringEqualTrue(t *testing.T) {
v1 := []string{"foo", "bar"}
v2 := []string{"foo", "bar"}
if rcv := SliceStringEqual(v1, v2); rcv != true {
t.Errorf("expected false ,received : %v", rcv)
}
}