Add new unit tests on rankings

This commit is contained in:
armirveliaj
2024-10-23 10:57:58 -04:00
committed by Dan Christian Bogos
parent 9a531776d0
commit 90ec7d15a2
2 changed files with 227 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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")
}
}

View File

@@ -19,10 +19,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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)
}
}