From 17248c5dde03b627c34d685d473550f0cba39f14 Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Thu, 3 Apr 2025 10:43:42 -0400 Subject: [PATCH] Add coverage tests on engine --- engine/calldesc_test.go | 44 +++++++++++++++++++++++ engine/cdrs_test.go | 77 +++++++++++++++++++++++++++++++++++++++++ engine/libstats_test.go | 57 ++++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+) diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index c4c58c3a1..097b3a317 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -2822,3 +2822,47 @@ func TestCallDescriptorgetgetRatingPlansForPrefix(t *testing.T) { } Cache.Clear(nil) } + +func TestAddRatingInfos(t *testing.T) { + tests := []struct { + existingRating RatingInfos + newRating RatingInfos + expected RatingInfos + }{ + { + existingRating: RatingInfos{ + &RatingInfo{ActivationTime: time.Date(2025, 4, 3, 9, 0, 0, 0, time.UTC), RateIntervals: RateIntervalList{&RateInterval{}}}, + }, + newRating: RatingInfos{ + &RatingInfo{ActivationTime: time.Date(2025, 4, 3, 8, 30, 0, 0, time.UTC), RateIntervals: RateIntervalList{&RateInterval{}}}, + }, + expected: RatingInfos{ + &RatingInfo{ActivationTime: time.Date(2025, 4, 3, 8, 30, 0, 0, time.UTC), RateIntervals: RateIntervalList{&RateInterval{}}}, + &RatingInfo{ActivationTime: time.Date(2025, 4, 3, 9, 0, 0, 0, time.UTC), RateIntervals: RateIntervalList{&RateInterval{}}}, + }, + }, + } + + for _, tt := range tests { + t.Run("Add Rating Infos", func(t *testing.T) { + cd := &CallDescriptor{ + RatingInfos: tt.existingRating, + } + + cd.addRatingInfos(tt.newRating) + + if len(cd.RatingInfos) != len(tt.expected) { + t.Errorf("Expected %d RatingInfos, got %d", len(tt.expected), len(cd.RatingInfos)) + } + + for i, ri := range cd.RatingInfos { + if !ri.ActivationTime.Equal(tt.expected[i].ActivationTime) { + t.Errorf("Expected activation time %v, got %v", tt.expected[i].ActivationTime, ri.ActivationTime) + } + if !reflect.DeepEqual(ri.RateIntervals, tt.expected[i].RateIntervals) { + t.Errorf("Expected RateIntervals %v, got %v", tt.expected[i].RateIntervals, ri.RateIntervals) + } + } + }) + } +} diff --git a/engine/cdrs_test.go b/engine/cdrs_test.go index 14e99388e..97a39b21d 100644 --- a/engine/cdrs_test.go +++ b/engine/cdrs_test.go @@ -2604,3 +2604,80 @@ func TestNewMapEventFromReqForm_ParseForm(t *testing.T) { t.Fatalf("Expected no error, got %v", err) } } + +func TestArgV1ProcessEventsClone(t *testing.T) { + original := &ArgV1ProcessEvents{ + Flags: []string{"flag1", "flag2"}, + CGREvents: []*utils.CGREvent{ + { + ID: "event1", + Tenant: "cgrates.org", + Time: utils.TimePointer(time.Now()), + Event: map[string]any{"key": "value"}, + APIOpts: map[string]any{"opt1": "val1"}, + }, + { + ID: "event2", + Tenant: "cgrates.org2", + Time: utils.TimePointer(time.Now().Add(1 * time.Hour)), + Event: map[string]any{"key2": "value2"}, + APIOpts: map[string]any{"opt2": "val2"}, + }, + }, + APIOpts: map[string]any{"apiOpt1": "optValue"}, + } + + cloned := original.Clone() + + if cloned == original { + t.Errorf("Expected a new cloned object, but the cloned object is the same as the original.") + } + + if len(cloned.Flags) != len(original.Flags) { + t.Errorf("Expected Flags length to be %d, got %d", len(original.Flags), len(cloned.Flags)) + } else { + for i := range original.Flags { + if original.Flags[i] != cloned.Flags[i] { + t.Errorf("Expected Flags[%d] to be '%s', got '%s'", i, original.Flags[i], cloned.Flags[i]) + } + } + } + + if len(cloned.CGREvents) != len(original.CGREvents) { + t.Errorf("Expected CGEvents length to be %d, got %d", len(original.CGREvents), len(cloned.CGREvents)) + } else { + for i, originalEvent := range original.CGREvents { + clonedEvent := cloned.CGREvents[i] + if originalEvent == clonedEvent { + t.Errorf("Expected CGEvent[%d] to be a clone, but they are the same instance.", i) + } + if originalEvent.ID != clonedEvent.ID || originalEvent.Tenant != clonedEvent.Tenant { + t.Errorf("Expected CGEvent[%d] to have the same values, but got ID: %s and Tenant: %s in the clone", i, clonedEvent.ID, clonedEvent.Tenant) + } + } + } + + if len(cloned.APIOpts) != len(original.APIOpts) { + t.Errorf("Expected APIOpts length to be %d, got %d", len(original.APIOpts), len(cloned.APIOpts)) + } else { + for key, value := range original.APIOpts { + if cloned.APIOpts[key] != value { + t.Errorf("Expected APIOpts[%s] to be '%v', got '%v'", key, value, cloned.APIOpts[key]) + } + } + } + + original.Flags[0] = "modifiedFlag" + original.CGREvents[0].ID = "modifiedEvent" + original.APIOpts["apiOpt1"] = "modifiedValue" + + if cloned.Flags[0] == "modifiedFlag" { + t.Errorf("Expected cloned Flags to be unaffected, but it was modified.") + } + if cloned.CGREvents[0].ID == "modifiedEvent" { + t.Errorf("Expected cloned CGEvents to be unaffected, but it was modified.") + } + if cloned.APIOpts["apiOpt1"] == "modifiedValue" { + t.Errorf("Expected cloned APIOpts to be unaffected, but it was modified.") + } +} diff --git a/engine/libstats_test.go b/engine/libstats_test.go index f9fe14906..250eb3952 100644 --- a/engine/libstats_test.go +++ b/engine/libstats_test.go @@ -1462,3 +1462,60 @@ func TestSortWeight(t *testing.T) { t.Errorf("Expected sorted routes to be route2, route3, route1, but got %v", sortedRoutes.Routes) } } + +func TestSortQOS(t *testing.T) { + routes := []*SortedRoute{ + { + RouteID: "route1", + RouteParameters: "param1", + sortingDataF64: map[string]float64{ + utils.MetaPDD: 10, + utils.Weight: 5, + }, + }, + { + RouteID: "route2", + RouteParameters: "param2", + sortingDataF64: map[string]float64{ + utils.MetaPDD: 20, + utils.Weight: 5, + }, + }, + { + RouteID: "route3", + RouteParameters: "param3", + sortingDataF64: map[string]float64{ + utils.MetaPDD: 15, + utils.Weight: 3, + }, + }, + } + + sortedRoutes := &SortedRoutes{ + ProfileID: "testProfile", + Sorting: "QOS", + Routes: routes, + } + + sortedRoutes.SortQOS([]string{utils.MetaPDD}) + if sortedRoutes.Routes[0].RouteID != "route1" { + t.Errorf("Expected route1 at position 0, got %s", sortedRoutes.Routes[0].RouteID) + } + if sortedRoutes.Routes[1].RouteID != "route3" { + t.Errorf("Expected route3 at position 1, got %s", sortedRoutes.Routes[1].RouteID) + } + if sortedRoutes.Routes[2].RouteID != "route2" { + t.Errorf("Expected route2 at position 2, got %s", sortedRoutes.Routes[2].RouteID) + } + + sortedRoutes.SortQOS([]string{utils.MetaPDD, utils.Weight}) + if sortedRoutes.Routes[0].RouteID != "route1" { + t.Errorf("Expected route1 at position 0, got %s", sortedRoutes.Routes[0].RouteID) + } + if sortedRoutes.Routes[1].RouteID != "route3" { + t.Errorf("Expected route3 at position 1, got %s", sortedRoutes.Routes[1].RouteID) + } + if sortedRoutes.Routes[2].RouteID != "route2" { + t.Errorf("Expected route2 at position 2, got %s", sortedRoutes.Routes[2].RouteID) + } +}