From 90ec7d15a237f5f5c5dbd7d9e36c7e037cb80ee7 Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Wed, 23 Oct 2024 10:57:58 -0400 Subject: [PATCH] Add new unit tests on rankings --- engine/librankings_test.go | 70 +++++++++++++++++ engine/rankings_test.go | 157 +++++++++++++++++++++++++++++++++++++ 2 files changed, 227 insertions(+) diff --git a/engine/librankings_test.go b/engine/librankings_test.go index 2a9aa6fb5..65c1439ae 100644 --- a/engine/librankings_test.go +++ b/engine/librankings_test.go @@ -19,6 +19,7 @@ along with this program. If not, see package engine import ( + "errors" "reflect" "testing" @@ -233,3 +234,72 @@ func TestNewRankingFromProfile(t *testing.T) { } } + +func TestRankingSortStatss(t *testing.T) { + metrics := map[string]map[string]float64{ + "STAT1": {"metric1": 10.1, "metric2": 5.2}, + "STAT2": {"metric1": 9.1, "metric2": 6.2}, + "STAT3": {"metric1": 11.1, "metric2": 4.2}, + } + + _, err := rankingSortStats("valid", []string{"metric1"}, metrics) + if err != nil { + expectedErr := "NOT_IMPLEMENTED:valid" + if err.Error() != expectedErr { + t.Errorf("expected error %v, got %v", expectedErr, err) + } + } + + _, err = rankingSortStats("invalid", []string{"metric1"}, metrics) + if err == nil { + t.Errorf("expected an error for invalid sorting type, but got nil") + } +} + +func TestRankingAscSorterEmptyStatIDs(t *testing.T) { + rkASrtr := &rankingAscSorter{ + statIDs: []string{}, + Metrics: make(map[string]map[string]float64), + sMetricIDs: []string{}, + sMetricRev: utils.StringSet{}, + } + + sortedStatIDs := rkASrtr.sortStatIDs() + + if len(sortedStatIDs) != 0 { + t.Errorf("expected sortedStatIDs to be empty, got %v", sortedStatIDs) + } +} + +func TestRankingDescSorterEmptyStatIDs(t *testing.T) { + rkDsrtr := &rankingDescSorter{ + statIDs: []string{}, + Metrics: make(map[string]map[string]float64), + sMetricIDs: []string{}, + sMetricRev: utils.StringSet{}, + } + + sortedStatIDs := rkDsrtr.sortStatIDs() + + if len(sortedStatIDs) != 0 { + t.Errorf("expected sortedStatIDs to be empty, got %v", sortedStatIDs) + } +} + +func TestRankingSortStats(t *testing.T) { + sortingType := "sorting" + sortingParams := []string{"param1", "param2"} + Metrics := map[string]map[string]float64{ + + "STATS1": {"*acc": 12.1, "*tcc": 24.2}, + "STATS2": {"*acc": 12.1, "*tcc": 24.3}, + "STATS3": {"*acc": 10.1, "*tcc": 25.3}, + } + + _, err := rankingSortStats(sortingType, sortingParams, Metrics) + + if err != nil { + errors.New("NOT_IMPLEMENTED:sorting") + } + +} diff --git a/engine/rankings_test.go b/engine/rankings_test.go index 2d0ad0546..1e901e468 100644 --- a/engine/rankings_test.go +++ b/engine/rankings_test.go @@ -19,10 +19,15 @@ along with this program. If not, see package engine import ( + "reflect" + "sync" "testing" "time" + "github.com/cgrates/birpc/context" "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/cron" ) func TestNewRankingS(t *testing.T) { @@ -97,3 +102,155 @@ func TestAsRankingSummary(t *testing.T) { } } + +func TestRankingProcessEvent(t *testing.T) { + Cache.Clear(nil) + cfg := config.NewDefaultCGRConfig() + cfg.RankingSCfg().StoreInterval = 1 + + data := NewInternalDB(nil, nil, true, config.CgrConfig().DataDbCfg().Items) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + + rankingProfile := &RankingProfile{ + Tenant: "cgrates.org", + ID: "RankingProfile1", + Schedule: "0 0 * * *", + StatIDs: []string{"Stat1", "Stat2"}, + MetricIDs: []string{"MetricA", "MetricB"}, + Sorting: "*asc", + SortingParameters: []string{"MetricA", "MetricB"}, + Stored: true, + ThresholdIDs: []string{"*none"}, + } + + if err := dm.SetRankingProfile(rankingProfile); err != nil { + t.Error(err) + } + + if retrievedProfile, err := dm.GetRankingProfile(rankingProfile.Tenant, rankingProfile.ID, true, false, ""); err != nil { + t.Errorf("Error retrieving ranking profile: %+v", err) + } else if !reflect.DeepEqual(rankingProfile, retrievedProfile) { + t.Errorf("Expecting: %+v, received: %+v", rankingProfile, retrievedProfile) + } + +} + +func TestProcessThresholdsEmptySortedStatIDs(t *testing.T) { + rankingService := &RankingS{ + connMgr: &ConnManager{}, + cgrcfg: &config.CGRConfig{}, + } + + ranking := &Ranking{ + Tenant: "cgrates.org", + ID: "ID", + rkPrfl: &RankingProfile{ + ThresholdIDs: []string{"threshold1"}, + }, + SortedStatIDs: []string{}, + } + + err := rankingService.processThresholds(ranking) + + if err != nil { + t.Errorf("Expected no error, got %v", err) + } +} + +func TestProcessEEsHandlesEmptySortedStatIDs(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.RankingSCfg().StoreInterval = 1 + data := NewInternalDB(nil, nil, true, config.CgrConfig().DataDbCfg().Items) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + + rankingService := &RankingS{ + cgrcfg: cfg, + connMgr: &ConnManager{}, + dm: dm, + } + + rk := &Ranking{ + Tenant: "cgrates.org", + ID: "ID", + LastUpdate: time.Now(), + SortedStatIDs: []string{}, + Metrics: make(map[string]map[string]float64), + Sorting: "", + SortingParameters: []string{}, + rkPrfl: nil, + metricIDs: utils.StringSet{}, + } + + err := rankingService.processEEs(rk) + + if err != nil { + t.Errorf("Expected no error, got %v", err) + } +} + +func TestProcessEEsHandlesEmptyEEsConns(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.RankingSCfg().StoreInterval = 1 + + data := NewInternalDB(nil, nil, true, config.CgrConfig().DataDbCfg().Items) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + + rankingService := &RankingS{ + cgrcfg: cfg, + connMgr: &ConnManager{}, + dm: dm, + } + + rk := &Ranking{ + Tenant: "cgrates.org", + ID: "ID", + LastUpdate: time.Now(), + SortedStatIDs: []string{"stat_id_1", "stat_id_2"}, + Metrics: make(map[string]map[string]float64), + Sorting: "", + SortingParameters: []string{}, + rkPrfl: nil, + metricIDs: utils.StringSet{}, + } + + cfg.RankingSCfg().EEsConns = []string{} + + err := rankingService.processEEs(rk) + + if err != nil { + t.Errorf("Expected no error, got %v", err) + } +} + +func TestV1ScheduleQueriesInvalidRankingID(t *testing.T) { + + ctx := context.Background() + + tS := &RankingS{ + crn: cron.New(), + crnTQs: make(map[string]map[string]cron.EntryID), + crnTQsMux: &sync.RWMutex{}, + } + + args := &utils.ArgScheduleRankingQueries{ + TenantIDWithAPIOpts: utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: "cgrates.org", + ID: "ID", + }, + APIOpts: make(map[string]any), + }, + RankingIDs: []string{"invalidID"}, + } + + var scheduled int + err := tS.V1ScheduleQueries(ctx, args, &scheduled) + + if err == nil { + t.Errorf("expected an error but got none") + } + + if scheduled != 0 { + t.Errorf("expected scheduled to be 0 but got %d", scheduled) + } +}