From 16340546c914690723c4312e47a4ce00c5e10caf Mon Sep 17 00:00:00 2001 From: NikolasPetriti Date: Mon, 7 Aug 2023 17:01:10 +0200 Subject: [PATCH] Add coverage tests for engine --- engine/model_helpers_test.go | 155 ++++++++++++++++++++ engine/rateinterval_test.go | 264 +++++++++++++++++++++++++++++++++++ engine/version_test.go | 104 ++++++++++++++ 3 files changed, 523 insertions(+) diff --git a/engine/model_helpers_test.go b/engine/model_helpers_test.go index f74df6b56..d9797ec6c 100644 --- a/engine/model_helpers_test.go +++ b/engine/model_helpers_test.go @@ -3257,3 +3257,158 @@ func TestModelHelperscsvDump(t *testing.T) { } } } + +func TestModelHelpersAsTPResources(t *testing.T) { + tps := TpResources{{ + ActivationInterval: "test;test", + }} + + rcv := tps.AsTPResources() + exp := []*utils.TPResourceProfile{{ + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: str, + ExpiryTime: str, + }, + }} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + +func TestModelHelpersAsTPThreshold(t *testing.T) { + tps := TpThresholds{{ + ActivationInterval: "test;test", + }} + + rcv := tps.AsTPThreshold() + exp := []*utils.TPThresholdProfile{{ + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: str, + ExpiryTime: str, + }, + }} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + +func TestModelHelpersAsTPFilter(t *testing.T) { + tps := TpFilterS{{ + ActivationInterval: "test;test", + }} + + rcv := tps.AsTPFilter() + exp := []*utils.TPFilterProfile{{ + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: str, + ExpiryTime: str, + }, + }} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + +func TestModelHelpersAsTPSuppliers(t *testing.T) { + tps := TpSuppliers{{ + ActivationInterval: "test;test", + }} + + rcv := tps.AsTPSuppliers() + exp := []*utils.TPSupplierProfile{{ + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: str, + ExpiryTime: str, + }, + SortingParameters: []string{}, + }} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + +func TestModelHelpersAsTPAttributes(t *testing.T) { + tps := TPAttributes{{ + ActivationInterval: "test;test", + }} + + rcv := tps.AsTPAttributes() + exp := []*utils.TPAttributeProfile{{ + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: str, + ExpiryTime: str, + }, + }} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + +func TestModelHelpersAsTPChargers(t *testing.T) { + tps := TPChargers{{ + ActivationInterval: "test;test", + }} + + rcv := tps.AsTPChargers() + exp := []*utils.TPChargerProfile{{ + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: str, + ExpiryTime: str, + }, + }} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + +func TestModelHelpersAsTPDispatcherProfiles(t *testing.T) { + tps := TPDispatcherProfiles{{ + ActivationInterval: "test;test", + StrategyParameters: str, + }} + + rcv := tps.AsTPDispatcherProfiles() + exp := []*utils.TPDispatcherProfile{{ + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: str, + ExpiryTime: str, + }, + StrategyParams: []any{str}, + }} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + +func TestModelHelpersAsTPStats(t *testing.T) { + tps := TpStats{{ + ActivationInterval: "test;test", + MetricIDs: str, + MetricFilterIDs: str, + }} + + rcv := tps.AsTPStats() + exp := []*utils.TPStatProfile{{ + ActivationInterval: &utils.TPActivationInterval{ + ActivationTime: str, + ExpiryTime: str, + }, + Metrics: []*utils.MetricWithFilters{ + { + FilterIDs: []string{str}, + MetricID: str, + }, + }, + }} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} diff --git a/engine/rateinterval_test.go b/engine/rateinterval_test.go index cc53eda56..380be2c63 100644 --- a/engine/rateinterval_test.go +++ b/engine/rateinterval_test.go @@ -18,6 +18,7 @@ along with this program. If not, see package engine import ( + "fmt" "reflect" "testing" "time" @@ -538,6 +539,269 @@ func TestRIRateClone(t *testing.T) { } } +func TestRateIntervalCronString(t *testing.T) { + rit := &RITiming{ + StartTime: "test", + } + + rcv := rit.CronString() + + if rcv != "* * * * * * *" { + t.Error(rcv) + } +} + +func TestRateIntervalIsActive(t *testing.T) { + rit := &RITiming{ + Years: utils.Years{2021}, + Months: utils.Months{9}, + MonthDays: utils.MonthDays{2}, + WeekDays: utils.WeekDays{2}, + StartTime: "00:00:00", + EndTime: "02:02:02", + cronString: str, + tag: str, + } + + rcv := rit.IsActive() + + if rcv != false { + t.Error(rcv) + } +} + +func TestRateIntervalFieldAsInterface(t *testing.T) { + r := &Rate{ + GroupIntervalStart: 1 * time.Millisecond, + Value: fl, + RateIncrement: 1 * time.Millisecond, + RateUnit: 1 * time.Millisecond, + } + + tests := []struct { + name string + arg []string + exp any + err string + }{ + { + name: "empty field path", + arg: []string{}, + exp: nil, + err: "NOT_FOUND", + }, + { + name: "default case", + arg: []string{str}, + exp: nil, + err: "unsupported field prefix: ", + }, + { + name: "GroupIntervalStart case", + arg: []string{"GroupIntervalStart"}, + exp: 1 * time.Millisecond, + err: "", + }, + { + name: "RateIncrement case", + arg: []string{"RateIncrement"}, + exp: 1 * time.Millisecond, + err: "", + }, + { + name: "RateUnit case", + arg: []string{"RateUnit"}, + exp: 1 * time.Millisecond, + err: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rcv, err := r.FieldAsInterface(tt.arg) + + if err != nil { + if err.Error() != tt.err { + t.Fatal(err) + } + } + + if rcv != tt.exp { + t.Error(rcv) + } + }) + } +} + +func TestRateIntervalRateGroupsEqual(t *testing.T) { + r := &Rate{ + GroupIntervalStart: 1 * time.Millisecond, + Value: fl, + RateIncrement: 1 * time.Millisecond, + RateUnit: 1 * time.Millisecond, + } + r2 := &Rate{ + GroupIntervalStart: 1 * time.Millisecond, + Value: 3.5, + RateIncrement: 1 * time.Millisecond, + RateUnit: 1 * time.Millisecond, + } + pg := RateGroups{r} + of := RateGroups{} + of2 := RateGroups{r2} + of3 := RateGroups{r} + + tests := []struct { + name string + arg RateGroups + exp bool + }{ + { + name: "different lengths", + arg: of, + exp: false, + }, + { + name: "not equal", + arg: of2, + exp: false, + }, + { + name: "equal", + arg: of3, + exp: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rcv := pg.Equal(tt.arg) + + if rcv != tt.exp { + t.Error(rcv) + } + }) + } +} + +func TestRateIntervalAddRate(t *testing.T) { + r := &Rate{ + GroupIntervalStart: 1 * time.Millisecond, + Value: fl, + RateIncrement: 1 * time.Millisecond, + RateUnit: 1 * time.Millisecond, + } + r2 := &Rate{ + GroupIntervalStart: 1 * time.Millisecond, + Value: 3.5, + RateIncrement: 1 * time.Millisecond, + RateUnit: 1 * time.Millisecond, + } + pg := RateGroups{r} + + pg.AddRate(r2) + exp := RateGroups{r, r2} + + if !reflect.DeepEqual(pg, exp) { + t.Errorf("expected %s, received %s", utils.ToJSON(exp), utils.ToJSON(pg)) + } + + pg.AddRate(r2) + + if !reflect.DeepEqual(pg, exp) { + t.Errorf("expected %s, received %s", utils.ToJSON(exp), utils.ToJSON(pg)) + } +} + +func TestRateIntervalString_DISABLED(t *testing.T) { + rit := &RITiming{ + Years: utils.Years{2021}, + Months: utils.Months{9}, + MonthDays: utils.MonthDays{2}, + WeekDays: utils.WeekDays{2}, + StartTime: "00:00:00", + EndTime: "02:02:02", + cronString: str, + tag: str, + } + i := &RateInterval{ + Timing: rit, + } + + rcv := i.String_DISABLED() + exp := fmt.Sprintf("%v %v %v %v %v %v", i.Timing.Years, i.Timing.Months, i.Timing.MonthDays, i.Timing.WeekDays, i.Timing.StartTime, i.Timing.EndTime) + + if rcv != exp { + t.Errorf("expected %s, received %s", exp, rcv) + } +} + +func TestRateIntervalEqual2(t *testing.T) { + rit := &RITiming{ + Years: utils.Years{2021}, + Months: utils.Months{9}, + MonthDays: utils.MonthDays{2}, + WeekDays: utils.WeekDays{2}, + StartTime: "00:00:00", + EndTime: "02:02:02", + cronString: str, + tag: str, + } + i := &RateInterval{ + Timing: rit, + } + + rcv := i.Equal(nil) + + if rcv != false { + t.Error(rcv) + } +} + +func TestRateIntervalGetMaxCost(t *testing.T) { + var ri RateInterval + + fl, str := ri.GetMaxCost() + + if fl != 0.0 { + t.Error(fl) + } + + if str != "" { + t.Error(str) + } +} + +func TestRateIntervalRateClone(t *testing.T) { + var r *Rate + + rcv := r.Clone() + + if rcv != nil { + t.Error(rcv) + } +} + +func TestRateIntervalGetRateParameters(t *testing.T) { + r := &Rate{ + GroupIntervalStart: 1 * time.Millisecond, + Value: fl, + RateIncrement: 1 * time.Millisecond, + RateUnit: 1 * time.Millisecond, + } + i := &RateInterval{ + Rating: &RIRate{ + Rates: RateGroups{r}, + }, + } + + fl, ri, ru := i.GetRateParameters(0 * time.Millisecond) + + if fl != -1 || ri != -1 || ru != -1 { + t.Error(fl, ri, ru) + } +} + /*********************************Benchmarks**************************************/ func BenchmarkRateIntervalContainsDate(b *testing.B) { diff --git a/engine/version_test.go b/engine/version_test.go index ba7a2aa57..e23bbd0e5 100644 --- a/engine/version_test.go +++ b/engine/version_test.go @@ -18,8 +18,10 @@ along with this program. If not, see package engine import ( + "reflect" "testing" + "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" ) @@ -71,3 +73,105 @@ func TestVersionCompare(t *testing.T) { } } + +func TestVarsionCheckVersions(t *testing.T) { + defaultCfg, _ := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, true, defaultCfg.DataDbCfg().Items) + + err := CheckVersions(data) + + if err != nil { + t.Error(err) + } +} + +func TestVersionSetDBVersions(t *testing.T) { + defaultCfg, _ := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, true, defaultCfg.DataDbCfg().Items) + + err := SetDBVersions(data) + + if err != nil { + t.Error(err) + } +} + +func TestVersionCurrentDBVersions(t *testing.T) { + type args struct { + storType string + isDataDB bool + } + tests := []struct { + name string + args args + exp Versions + }{ + { + name: "CurrentDataDBVersions", + args: args{ + storType: utils.MetaMongo, + isDataDB: true, + }, + exp: CurrentDataDBVersions(), + }, + { + name: "CurrentStorDBVersions", + args: args{ + storType: utils.MetaMongo, + isDataDB: false, + }, + exp: CurrentStorDBVersions(), + }, + { + name: "CurrentStorDBVersions", + args: args{ + storType: utils.MetaPostgres, + isDataDB: false, + }, + exp: CurrentStorDBVersions(), + }, + { + name: "CurrentDataDBVersions", + args: args{ + storType: utils.MetaRedis, + isDataDB: false, + }, + exp: CurrentDataDBVersions(), + }, + { + name: "default", + args: args{ + storType: str, + isDataDB: false, + }, + exp: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rcv := CurrentDBVersions(tt.args.storType, tt.args.isDataDB) + + if !reflect.DeepEqual(rcv, tt.exp) { + t.Errorf("expected %s, received %s", utils.ToJSON(tt.exp), utils.ToJSON(rcv)) + } + }) + } +} + +func TestVersionCompare2(t *testing.T) { + vers := Versions{} + current := Versions{} + + rcv := vers.Compare(current, utils.MetaInternal, false) + + if rcv != "" { + t.Error(rcv) + } + + rcv = vers.Compare(current, utils.MetaRedis, false) + + if rcv != "" { + t.Error(rcv) + } +}