mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add new unit tests on rankings
This commit is contained in:
committed by
Dan Christian Bogos
parent
1a38e51a5d
commit
9a531776d0
@@ -21,6 +21,8 @@ package engine
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestRankingDescSorterSortStatIDs(t *testing.T) {
|
||||
@@ -88,3 +90,146 @@ func TestRankingAscSorterSortStatIDs(t *testing.T) {
|
||||
t.Errorf("Expecting: %v, received %v", eStatIDs, statIDs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTenantID(t *testing.T) {
|
||||
ranking := &Ranking{
|
||||
Tenant: "tenant1",
|
||||
ID: "ranking1",
|
||||
}
|
||||
expectedTenantID := utils.ConcatenatedKey(ranking.Tenant, ranking.ID)
|
||||
actualTenantID := ranking.TenantID()
|
||||
if actualTenantID != expectedTenantID {
|
||||
t.Errorf("Expected TenantID %q, got %q", expectedTenantID, actualTenantID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRankingSorter(t *testing.T) {
|
||||
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},
|
||||
"STATS4": {"*tcc": 26.3},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
sortingType string
|
||||
sortingParams []string
|
||||
expectErr bool
|
||||
expectSorterType string
|
||||
}{
|
||||
{
|
||||
sortingType: utils.MetaAsc,
|
||||
sortingParams: []string{"*acc"},
|
||||
expectErr: false,
|
||||
expectSorterType: "RankingAscSorter",
|
||||
},
|
||||
{
|
||||
sortingType: utils.MetaDesc,
|
||||
sortingParams: []string{"*tcc"},
|
||||
expectErr: false,
|
||||
expectSorterType: "RankingDescSorter",
|
||||
},
|
||||
{
|
||||
sortingType: "unsupported",
|
||||
sortingParams: []string{"*tcc"},
|
||||
expectErr: true,
|
||||
expectSorterType: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
rkSorter, err := newRankingSorter(test.sortingType, test.sortingParams, Metrics)
|
||||
|
||||
if test.expectErr {
|
||||
if err == nil {
|
||||
t.Errorf("Expected an error for sorting type %q, but got none", test.sortingType)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("Did not expect an error for sorting type %q, but got: %v", test.sortingType, err)
|
||||
}
|
||||
switch test.sortingType {
|
||||
case utils.MetaAsc:
|
||||
if _, ok := rkSorter.(*rankingAscSorter); !ok {
|
||||
t.Errorf("Expected sorter type 'rankingAscSorter', but got %T", rkSorter)
|
||||
}
|
||||
case utils.MetaDesc:
|
||||
if _, ok := rkSorter.(*rankingDescSorter); !ok {
|
||||
t.Errorf("Expected sorter type 'rankingDescSorter', but got %T", rkSorter)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRankingProfileClone(t *testing.T) {
|
||||
|
||||
t.Run("Empty fields", func(t *testing.T) {
|
||||
original := &RankingProfile{}
|
||||
|
||||
clone := original.Clone()
|
||||
|
||||
if clone.Tenant != "" ||
|
||||
clone.ID != "" ||
|
||||
clone.Schedule != "" ||
|
||||
clone.StatIDs != nil ||
|
||||
clone.MetricIDs != nil ||
|
||||
clone.Sorting != "" ||
|
||||
clone.SortingParameters != nil ||
|
||||
clone.Stored != false ||
|
||||
clone.ThresholdIDs != nil {
|
||||
t.Errorf("Clone method did not create an empty RankingProfile for empty original")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Nil slices", func(t *testing.T) {
|
||||
original := &RankingProfile{
|
||||
Tenant: "tenant",
|
||||
ID: "profile_id",
|
||||
Schedule: "0 * * * *",
|
||||
}
|
||||
|
||||
clone := original.Clone()
|
||||
|
||||
if clone.StatIDs != nil ||
|
||||
clone.MetricIDs != nil ||
|
||||
clone.SortingParameters != nil ||
|
||||
clone.ThresholdIDs != nil {
|
||||
t.Errorf("Clone method did not handle nil slices correctly")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewRankingFromProfile(t *testing.T) {
|
||||
rkP := &RankingProfile{
|
||||
Tenant: "tenant",
|
||||
ID: "profile_id",
|
||||
Schedule: "0 * * * *",
|
||||
StatIDs: []string{"stat1", "stat2"},
|
||||
MetricIDs: []string{"metricA", "metricB"},
|
||||
Sorting: "asc",
|
||||
SortingParameters: []string{"metricA:true", "metricB:false"},
|
||||
Stored: true,
|
||||
ThresholdIDs: []string{"threshold1", "threshold2"},
|
||||
}
|
||||
|
||||
expectedRk := &Ranking{
|
||||
Tenant: rkP.Tenant,
|
||||
ID: rkP.ID,
|
||||
Sorting: rkP.Sorting,
|
||||
Metrics: make(map[string]map[string]float64),
|
||||
rkPrfl: rkP,
|
||||
metricIDs: utils.NewStringSet(rkP.MetricIDs),
|
||||
}
|
||||
|
||||
rk := NewRankingFromProfile(rkP)
|
||||
|
||||
if rk.Tenant != expectedRk.Tenant ||
|
||||
rk.ID != expectedRk.ID ||
|
||||
rk.Sorting != expectedRk.Sorting ||
|
||||
rk.rkPrfl != expectedRk.rkPrfl ||
|
||||
!reflect.DeepEqual(rk.metricIDs, expectedRk.metricIDs) {
|
||||
t.Errorf("NewRankingFromProfile returned unexpected Ranking object")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user