From 8b59c4dae6936bb8a2350187758a58ef53bef3b8 Mon Sep 17 00:00:00 2001 From: gezimbll Date: Mon, 17 Oct 2022 10:46:55 -0400 Subject: [PATCH] utils tests --- cmd/cgr-engine/cover.out | 1 + utils/cgrevent_test.go | 32 ++++++++++ utils/map_test.go | 130 +++++++++++++++++++++++++++++++++++++++ utils/slice_test.go | 27 ++++++++ 4 files changed, 190 insertions(+) create mode 100644 cmd/cgr-engine/cover.out diff --git a/cmd/cgr-engine/cover.out b/cmd/cgr-engine/cover.out new file mode 100644 index 000000000..5f02b1119 --- /dev/null +++ b/cmd/cgr-engine/cover.out @@ -0,0 +1 @@ +mode: set diff --git a/utils/cgrevent_test.go b/utils/cgrevent_test.go index e41dde65b..55e2eb1da 100644 --- a/utils/cgrevent_test.go +++ b/utils/cgrevent_test.go @@ -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) + + } + +} diff --git a/utils/map_test.go b/utils/map_test.go index 02e1417d9..84e5f5f00 100644 --- a/utils/map_test.go +++ b/utils/map_test.go @@ -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)) + } + +} diff --git a/utils/slice_test.go b/utils/slice_test.go index a80a46df8..f0ac5e5b4 100644 --- a/utils/slice_test.go +++ b/utils/slice_test.go @@ -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) + } + +}