From 8921bf8b99bcdabb51f899d4b7850060b774aa8b Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Fri, 20 Dec 2024 10:54:40 -0500 Subject: [PATCH] Revise && add new unit tests --- config/accountscfg_test.go | 10 +-- config/actionscfg_test.go | 14 ++-- config/attributescfg_test.go | 10 +-- config/generalcfg_test.go | 8 +-- config/ratescfg_test.go | 14 ++-- config/statscfg_test.go | 16 ++--- config/thresholdscfg_test.go | 10 +-- engine/libdynopts_test.go | 12 ++-- engine/stats_test.go | 6 +- engine/thresholds_test.go | 4 +- rates/rates_test.go | 131 +++++++++++++++++++++++++++++++---- utils/libdynamics_test.go | 16 ++--- 12 files changed, 176 insertions(+), 75 deletions(-) diff --git a/config/accountscfg_test.go b/config/accountscfg_test.go index e251ecfc7..a618d6ddc 100644 --- a/config/accountscfg_test.go +++ b/config/accountscfg_test.go @@ -84,7 +84,7 @@ func TestAccountSCfgLoadFromJSONCfgOptsErr(t *testing.T) { accOpts := &AccountsOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{"1001", "1002"}, + Values: []string{"1001", "1002"}, }, }, Usage: []*utils.DynamicDecimalBigOpt{ @@ -245,7 +245,7 @@ func TestDiffAccountSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"ACC1"}, + Values: []string{"ACC1"}, }, }, Usage: []*utils.DynamicDecimalBigOpt{ @@ -279,7 +279,7 @@ func TestDiffAccountSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"ACC2"}, + Values: []string{"ACC2"}, }, }, Usage: []*utils.DynamicDecimalBigOpt{ @@ -313,7 +313,7 @@ func TestDiffAccountSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"ACC2"}, + Values: []string{"ACC2"}, }, }, Usage: []*utils.DynamicStringOpt{ @@ -376,7 +376,7 @@ func TestAccountSCloneSection(t *testing.T) { Opts: &AccountsOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{"ACC1"}, + Values: []string{"ACC1"}, }, }, }, diff --git a/config/actionscfg_test.go b/config/actionscfg_test.go index ed270dbf3..48553126e 100644 --- a/config/actionscfg_test.go +++ b/config/actionscfg_test.go @@ -82,7 +82,7 @@ func TestActionoSLoadConfigFromJSONOpts(t *testing.T) { actOpts := &ActionsOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{}, + Values: []string{}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -94,7 +94,7 @@ func TestActionoSLoadConfigFromJSONOpts(t *testing.T) { exp := &ActionsOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{}, + Values: []string{}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -233,7 +233,7 @@ func TestDiffActionSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"prf1"}, + Values: []string{"prf1"}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -263,7 +263,7 @@ func TestDiffActionSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"prf2"}, + Values: []string{"prf2"}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -293,7 +293,7 @@ func TestDiffActionSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"prf2"}, + Values: []string{"prf2"}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -339,7 +339,7 @@ func TestActionSCloneSection(t *testing.T) { Opts: &ActionsOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{}, + Values: []string{}, }, }, }, @@ -361,7 +361,7 @@ func TestActionSCloneSection(t *testing.T) { Opts: &ActionsOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{}, + Values: []string{}, }, }, }, diff --git a/config/attributescfg_test.go b/config/attributescfg_test.go index 77d481111..f05f28351 100644 --- a/config/attributescfg_test.go +++ b/config/attributescfg_test.go @@ -75,7 +75,7 @@ func TestAttributeSLoadFromJsonCfgOpts(t *testing.T) { attrOpt := &AttributesOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{}, + Values: []string{}, }, }, ProcessRuns: []*utils.DynamicIntOpt{ @@ -98,7 +98,7 @@ func TestAttributeSLoadFromJsonCfgOpts(t *testing.T) { exp := &AttributesOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{}, + Values: []string{}, }, }, ProcessRuns: []*utils.DynamicIntOpt{ @@ -304,7 +304,7 @@ func TestDiffAttributeSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"prf1"}, + Values: []string{"prf1"}, }, }, ProcessRuns: []*utils.DynamicIntOpt{ @@ -342,7 +342,7 @@ func TestDiffAttributeSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"prf2"}, + Values: []string{"prf2"}, }, }, ProcessRuns: []*utils.DynamicIntOpt{ @@ -380,7 +380,7 @@ func TestDiffAttributeSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"prf2"}, + Values: []string{"prf2"}, }, }, ProcessRuns: []*utils.DynamicIntOpt{ diff --git a/config/generalcfg_test.go b/config/generalcfg_test.go index f49e97f2f..2944c499f 100644 --- a/config/generalcfg_test.go +++ b/config/generalcfg_test.go @@ -235,7 +235,7 @@ func TestGeneralCfgClone(t *testing.T) { Opts: &GeneralOpts{ ExporterIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{"*ees"}, + Values: []string{"*ees"}, }, }, }, @@ -273,7 +273,7 @@ func TestDiffGeneralJsonCfg(t *testing.T) { Opts: &GeneralOpts{ ExporterIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{"*ees"}, + Values: []string{"*ees"}, }, }, }, @@ -306,7 +306,7 @@ func TestDiffGeneralJsonCfg(t *testing.T) { Opts: &GeneralOpts{ ExporterIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{"*syslog"}, + Values: []string{"*syslog"}, }, }, }, @@ -339,7 +339,7 @@ func TestDiffGeneralJsonCfg(t *testing.T) { Opts: &GeneralOptsJson{ ExporterIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{"*syslog"}, + Values: []string{"*syslog"}, }, }, }, diff --git a/config/ratescfg_test.go b/config/ratescfg_test.go index d238141ef..0503bf00d 100644 --- a/config/ratescfg_test.go +++ b/config/ratescfg_test.go @@ -235,7 +235,7 @@ func TestDiffRateSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"RP1"}, + Values: []string{"RP1"}, }, }, StartTime: []*utils.DynamicStringOpt{ @@ -282,7 +282,7 @@ func TestDiffRateSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"RP2"}, + Values: []string{"RP2"}, }, }, StartTime: []*utils.DynamicStringOpt{ @@ -329,7 +329,7 @@ func TestDiffRateSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"RP2"}, + Values: []string{"RP2"}, }, }, StartTime: []*utils.DynamicStringOpt{ @@ -391,7 +391,7 @@ func TestRateSCloneSection(t *testing.T) { Opts: &RatesOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{"RP1"}, + Values: []string{"RP1"}, }, }, }, @@ -413,7 +413,7 @@ func TestRateSCloneSection(t *testing.T) { Opts: &RatesOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{"RP1"}, + Values: []string{"RP1"}, }, }, }, @@ -429,7 +429,7 @@ func TestRatesOptsLoadFromJSON(t *testing.T) { rateOpts := &RatesOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{}, + Values: []string{}, }, }, StartTime: []*utils.DynamicStringOpt{ @@ -463,7 +463,7 @@ func TestRatesOptsLoadFromJSON(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"RP2"}, + Values: []string{"RP2"}, }, }, Usage: []*utils.DynamicStringOpt{ diff --git a/config/statscfg_test.go b/config/statscfg_test.go index 00152d55f..e07a0093a 100644 --- a/config/statscfg_test.go +++ b/config/statscfg_test.go @@ -75,7 +75,7 @@ func TestStatSCfgloadFromJsonCfgOptsNil(t *testing.T) { statsOpt := &StatsOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{}, + Values: []string{}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -88,7 +88,7 @@ func TestStatSCfgloadFromJsonCfgOptsNil(t *testing.T) { exp := &StatsOpts{ ProfileIDs: []*utils.DynamicStringSliceOpt{ { - Value: []string{}, + Values: []string{}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -237,7 +237,7 @@ func TestDiffStatServJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"statsid1"}, + Values: []string{"statsid1"}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -255,7 +255,7 @@ func TestDiffStatServJsonCfg(t *testing.T) { PrometheusStatIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"statsid1"}, + Values: []string{"statsid1"}, }, }, }, @@ -275,7 +275,7 @@ func TestDiffStatServJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"statsid2"}, + Values: []string{"statsid2"}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -293,7 +293,7 @@ func TestDiffStatServJsonCfg(t *testing.T) { PrometheusStatIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"statsid2"}, + Values: []string{"statsid2"}, }, }, }, @@ -313,7 +313,7 @@ func TestDiffStatServJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"statsid2"}, + Values: []string{"statsid2"}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -331,7 +331,7 @@ func TestDiffStatServJsonCfg(t *testing.T) { PrometheusStatIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"statsid2"}, + Values: []string{"statsid2"}, }, }, }, diff --git a/config/thresholdscfg_test.go b/config/thresholdscfg_test.go index 40e78871c..537c50881 100644 --- a/config/thresholdscfg_test.go +++ b/config/thresholdscfg_test.go @@ -73,7 +73,7 @@ func TestThresholdSLoadFromJsonOpts(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"thsd_p1"}, + Values: []string{"thsd_p1"}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -87,7 +87,7 @@ func TestThresholdSLoadFromJsonOpts(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"thsd_p1"}, + Values: []string{"thsd_p1"}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -219,7 +219,7 @@ func TestDiffThresholdSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"thsr_p1"}, + Values: []string{"thsr_p1"}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -244,7 +244,7 @@ func TestDiffThresholdSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"thsr_p2"}, + Values: []string{"thsr_p2"}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ @@ -269,7 +269,7 @@ func TestDiffThresholdSJsonCfg(t *testing.T) { ProfileIDs: []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.net", - Value: []string{"thsr_p2"}, + Values: []string{"thsr_p2"}, }, }, ProfileIgnoreFilters: []*utils.DynamicBoolOpt{ diff --git a/engine/libdynopts_test.go b/engine/libdynopts_test.go index 976825842..668df799f 100644 --- a/engine/libdynopts_test.go +++ b/engine/libdynopts_test.go @@ -1041,18 +1041,18 @@ func TestLibFiltersGetStringSliceOptsReturnConfigOpt(t *testing.T) { { FilterIDs: []string{"*string:~*req.Account:1001"}, Tenant: "cgrates.net", - Value: []string{"value1"}, + Values: []string{"value1"}, }, // filter will not pass, will ignore this opt { FilterIDs: []string{"*string:~*req.Account:1002"}, Tenant: "cgrates.org", - Value: []string{"value2"}, + Values: []string{"value2"}, }, { FilterIDs: []string{"*string:~*req.Account:1001"}, Tenant: "cgrates.org", - Value: []string{"value3"}, + Values: []string{"value3"}, }, } @@ -1083,7 +1083,7 @@ func TestLibFiltersGetStringSliceOptsFilterCheckErr(t *testing.T) { { FilterIDs: []string{"*string.invalid:filter"}, Tenant: "cgrates.org", - Value: []string{"value2"}, + Values: []string{"value2"}, }, } @@ -1113,7 +1113,7 @@ func TestLibFiltersGetStringSliceOptsReturnDefaultOpt(t *testing.T) { { FilterIDs: []string{"*string:~*req.Account:1002"}, Tenant: "cgrates.org", - Value: []string{"value2"}, + Values: []string{"value2"}, }, } @@ -1146,7 +1146,7 @@ func TestLibFiltersGetStringSliceOptsReturnOptFromAPIOpts(t *testing.T) { { FilterIDs: []string{"*string:~*req.Account:1001"}, Tenant: "cgrates.org", - Value: []string{"value3"}, + Values: []string{"value3"}, }, } diff --git a/engine/stats_test.go b/engine/stats_test.go index bceec2702..303eee291 100644 --- a/engine/stats_test.go +++ b/engine/stats_test.go @@ -3524,7 +3524,7 @@ func TestStatSV1GetStatQueuesForEventsqIDsErr(t *testing.T) { { FilterIDs: []string{"*string.invalid:filter"}, Tenant: "cgrates.org", - Value: []string{"value2"}, + Values: []string{"value2"}, }, } data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items) @@ -3792,7 +3792,7 @@ func TestStatQueueProcessEventProfileIDsErr(t *testing.T) { { FilterIDs: []string{"*string.invalid:filter"}, Tenant: "cgrates.org", - Value: []string{"value2"}, + Values: []string{"value2"}, }, } @@ -3877,7 +3877,7 @@ func TestStatQueueProcessEventPrometheusStatIDsErr(t *testing.T) { { FilterIDs: []string{"*string.invalid:filter"}, Tenant: "cgrates.org", - Value: []string{"value2"}, + Values: []string{"value2"}, }, } diff --git a/engine/thresholds_test.go b/engine/thresholds_test.go index 5f237303f..79e12c3f0 100644 --- a/engine/thresholds_test.go +++ b/engine/thresholds_test.go @@ -3349,7 +3349,7 @@ func TestThresholdSmatchingThresholdsForEventGetOptsErr(t *testing.T) { { FilterIDs: []string{"*string"}, Tenant: "cgrates.org", - Value: []string{"ProfIdVal"}, + Values: []string{"ProfIdVal"}, }, } @@ -3391,7 +3391,7 @@ func TestThresholdSmatchingThresholdsForEventWeightErr(t *testing.T) { cfg.ThresholdSCfg().Opts.ProfileIDs = []*utils.DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"ACC1"}, + Values: []string{"ACC1"}, }, } cfg.ThresholdSCfg().Opts.ProfileIgnoreFilters = []*utils.DynamicBoolOpt{ diff --git a/rates/rates_test.go b/rates/rates_test.go index 688ac83e0..104b13e03 100644 --- a/rates/rates_test.go +++ b/rates/rates_test.go @@ -303,6 +303,7 @@ func TestMatchingRateProfileEvent(t *testing.T) { t.Error(err) } + var ignoredRPIDs utils.StringSet if rtPRf, err := rate.matchingRateProfileForEvent(context.TODO(), "cgrates.org", []string{}, &utils.CGREvent{ Tenant: "cgrates.org", @@ -312,7 +313,7 @@ func TestMatchingRateProfileEvent(t *testing.T) { utils.Destination: 1002, utils.AnswerTime: t1.Add(-10 * time.Second), }, - }, false); err != nil { + }, false, ignoredRPIDs); err != nil { t.Error(err) } else if !reflect.DeepEqual(rtPRf, rpp) { t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(rpp), utils.ToJSON(rtPRf)) @@ -327,7 +328,7 @@ func TestMatchingRateProfileEvent(t *testing.T) { utils.Destination: 2002, utils.AnswerTime: t1.Add(-10 * time.Second), }, - }, false); err != utils.ErrNotFound { + }, false, ignoredRPIDs); err != utils.ErrNotFound { t.Error(err) } if _, err := rate.matchingRateProfileForEvent(context.TODO(), "cgrates.org", []string{}, @@ -339,7 +340,7 @@ func TestMatchingRateProfileEvent(t *testing.T) { utils.Destination: 2002, utils.AnswerTime: t1.Add(10 * time.Second), }, - }, false); err != utils.ErrNotFound { + }, false, ignoredRPIDs); err != utils.ErrNotFound { t.Error(err) } if _, err := rate.matchingRateProfileForEvent(context.TODO(), "cgrates.org", []string{}, @@ -351,7 +352,7 @@ func TestMatchingRateProfileEvent(t *testing.T) { utils.Destination: 1002, utils.AnswerTime: t1.Add(-10 * time.Second), }, - }, false); err != utils.ErrNotFound { + }, false, ignoredRPIDs); err != utils.ErrNotFound { t.Error(err) } @@ -364,7 +365,7 @@ func TestMatchingRateProfileEvent(t *testing.T) { utils.Destination: 1002, utils.AnswerTime: t1.Add(-10 * time.Second), }, - }, false); err != utils.ErrNotFound { + }, false, ignoredRPIDs); err != utils.ErrNotFound { t.Error(err) } rpp.FilterIDs = []string{"*string:~*req.Account:1001|1002|1003", "*gt:~*req.Cost{*:10"} @@ -377,7 +378,7 @@ func TestMatchingRateProfileEvent(t *testing.T) { utils.Cost: 1002, utils.AnswerTime: t1.Add(-10 * time.Second), }, - }, false); err.Error() != "invalid converter terminator in rule: <~*req.Cost{*>" { + }, false, ignoredRPIDs); err.Error() != "invalid converter terminator in rule: <~*req.Cost{*>" { t.Error(err) } rpp.FilterIDs = []string{"*string:~*req.Account:1001|1002|1003"} @@ -392,7 +393,7 @@ func TestMatchingRateProfileEvent(t *testing.T) { utils.Destination: 1002, utils.AnswerTime: t1.Add(-10 * time.Second), }, - }, false); err != utils.ErrNoDatabaseConn { + }, false, ignoredRPIDs); err != utils.ErrNoDatabaseConn { t.Error(err) } @@ -680,13 +681,6 @@ func BenchmarkRateS_V1CostForEventSingleRate(b *testing.B) { b.StopTimer() } -func TestRatesShutDown(t *testing.T) { - rateS := new(RateS) - if err := rateS.Shutdown(); err != nil { - t.Error(err) - } -} - func TestRateProfileCostForEventInvalidUsage(t *testing.T) { engine.Cache.Clear(nil) cfg := config.NewDefaultCGRConfig() @@ -914,6 +908,7 @@ func TestRateSMatchingRateProfileForEventErrFltr(t *testing.T) { if err != nil { t.Error(err) } + var ignoredRPfIDs utils.StringSet _, err = rateS.matchingRateProfileForEvent(context.TODO(), "cgrates.org", []string{}, &utils.CGREvent{ Tenant: "cgrates.org", @@ -923,7 +918,7 @@ func TestRateSMatchingRateProfileForEventErrFltr(t *testing.T) { utils.Destination: 1002, utils.AnswerTime: time.Date(9999, 7, 21, 10, 0, 0, 0, time.UTC).Add(-10 * time.Second), }, - }, false) + }, false, ignoredRPfIDs) expectedErr := "NOT_FOUND" if err == nil || err.Error() != expectedErr { t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expectedErr, err) @@ -1320,3 +1315,109 @@ func TestRateProfileCostForEventProfileIgnoreFilters(t *testing.T) { t.Error(err) } } + +func TestMatchingRateProfileFallbacks(t *testing.T) { + + cfg := config.NewDefaultCGRConfig() + data := engine.NewInternalDB(nil, nil, cfg.DataDbCfg().Items) + dm := engine.NewDataManager(data, config.CgrConfig().CacheCfg(), nil) + filters := engine.NewFilterS(cfg, nil, dm) + rate := RateS{ + cfg: cfg, + fltrS: filters, + dm: dm, + } + t1 := time.Date(2020, 7, 21, 10, 0, 0, 0, time.UTC) + + rpp1 := &utils.RateProfile{ + Tenant: "cgrates.org", + ID: "RP1", + Weights: utils.DynamicWeights{ + { + Weight: 10, + }, + }, + FilterIDs: []string{"*string:~*req.Account:1001|1002|1003", "*prefix:~*req.Destination:10"}, + } + + rpp2 := &utils.RateProfile{ + Tenant: "cgrates.org", + ID: "RP2", + Weights: utils.DynamicWeights{ + { + Weight: 5, + }, + }, + FilterIDs: []string{"*string:~*req.Account:1001|1002|1003", "*prefix:~*req.Destination:10"}, + } + + // Set both rate profiles + err := dm.SetRateProfile(context.Background(), rpp1, false, true) + if err != nil { + t.Error(err) + } + err = dm.SetRateProfile(context.Background(), rpp2, false, true) + if err != nil { + t.Error(err) + } + + // Initialize ignoredRPIDs as a new empty map + ignoredRPIDs := make(utils.StringSet) + + // 1. Test without any ignored profiles (should match RP1 with higher weight) + if rtPRf, err := rate.matchingRateProfileForEvent(context.TODO(), "cgrates.org", []string{}, + &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "ID", + Event: map[string]any{ + utils.AccountField: "1001", + utils.Destination: 1002, + utils.AnswerTime: t1.Add(-10 * time.Second), + }, + }, false, ignoredRPIDs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rtPRf, rpp1) { + t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(rpp1), utils.ToJSON(rtPRf)) + } + + // 2. Test with ignoredRPIDs containing RP1 (should match RP2 ) + ignoredRPIDs.Add(rpp1.ID) + if rtPRf, err := rate.matchingRateProfileForEvent(context.TODO(), "cgrates.org", []string{}, + &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "ID", + Event: map[string]any{ + utils.AccountField: "1001", + utils.Destination: 1002, + utils.AnswerTime: t1.Add(-10 * time.Second), + }, + }, false, ignoredRPIDs); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rtPRf, rpp2) { + t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(rpp2), utils.ToJSON(rtPRf)) + } + + // 3. Test with both RP1 and RP2 in ignoredRPIDs (should return ErrNotFound) + ignoredRPIDs.Add(rpp2.ID) + if _, err := rate.matchingRateProfileForEvent(context.TODO(), "cgrates.org", []string{}, + &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "ID", + Event: map[string]any{ + utils.AccountField: "1001", + utils.Destination: 1002, + utils.AnswerTime: t1.Add(-10 * time.Second), + }, + }, false, ignoredRPIDs); err != utils.ErrNotFound { + t.Errorf("Expected error %v, got %v", utils.ErrNotFound, err) + } + + err = dm.RemoveRateProfile(context.Background(), rpp1.Tenant, rpp1.ID, true) + if err != nil { + t.Error(err) + } + err = dm.RemoveRateProfile(context.Background(), rpp2.Tenant, rpp2.ID, true) + if err != nil { + t.Error(err) + } +} diff --git a/utils/libdynamics_test.go b/utils/libdynamics_test.go index fe55f6432..0d7ef5bf9 100644 --- a/utils/libdynamics_test.go +++ b/utils/libdynamics_test.go @@ -29,11 +29,11 @@ import ( func TestCloneDynamicStringsSliceOpt(t *testing.T) { in := []*DynamicStringSliceOpt{ { - Value: []string{"VAL_1", "VAL_2"}, + Values: []string{"VAL_1", "VAL_2"}, FilterIDs: []string{"fltr1"}, }, { - Value: []string{"VAL_3", "VAL_4"}, + Values: []string{"VAL_3", "VAL_4"}, FilterIDs: []string{"fltr2"}, }, } @@ -168,12 +168,12 @@ func TestDynamicStringSliceOptEqual(t *testing.T) { v1 := []*DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"VAL_1", "VAL_2"}, + Values: []string{"VAL_1", "VAL_2"}, FilterIDs: []string{"fltr1"}, }, { Tenant: "cgrates.org", - Value: []string{"VAL_3", "VAL_4"}, + Values: []string{"VAL_3", "VAL_4"}, FilterIDs: []string{"fltr2"}, }, } @@ -181,12 +181,12 @@ func TestDynamicStringSliceOptEqual(t *testing.T) { v2 := []*DynamicStringSliceOpt{ { Tenant: "cgrates.org", - Value: []string{"VAL_1", "VAL_2"}, + Values: []string{"VAL_1", "VAL_2"}, FilterIDs: []string{"fltr1"}, }, { Tenant: "cgrates.org", - Value: []string{"VAL_3", "VAL_4"}, + Values: []string{"VAL_3", "VAL_4"}, FilterIDs: []string{"fltr2"}, }, } @@ -197,7 +197,7 @@ func TestDynamicStringSliceOptEqual(t *testing.T) { } //Test if different - v1[0].Value = append(v1[0].Value, "VAL_3") + v1[0].Values = append(v1[0].Values, "VAL_3") if DynamicStringSliceOptEqual(v1, v2) { t.Error("Expected slices to differ") } @@ -217,7 +217,7 @@ func TestDynamicStringSliceOptEqual(t *testing.T) { //Test if different lengths v1 = append(v1, &DynamicStringSliceOpt{ - Value: []string{"VAL_1", "VAL_2"}, + Values: []string{"VAL_1", "VAL_2"}, FilterIDs: []string{"fltr1"}, }) if DynamicStringSliceOptEqual(v1, v2) {