From 2b52938bfb63b5c201cf1b4185304716407ca17c Mon Sep 17 00:00:00 2001 From: gezimbll Date: Tue, 21 Mar 2023 11:59:30 -0400 Subject: [PATCH] Adding and improving unit tests at engine --- engine/datamanager_test.go | 157 +++++++++++++++++++++++++++++++++++++ engine/responder_test.go | 8 +- engine/stats_test.go | 14 +++- engine/storage_test.go | 63 +++++++++++++++ 4 files changed, 238 insertions(+), 4 deletions(-) diff --git a/engine/datamanager_test.go b/engine/datamanager_test.go index 8d2778ed5..958c1e125 100644 --- a/engine/datamanager_test.go +++ b/engine/datamanager_test.go @@ -378,6 +378,68 @@ func TestCacheDataFromDBFilterIndexes(t *testing.T) { if err := dm.CacheDataFromDB(utils.ThresholdFilterIndexes, nil, false); err != nil { t.Error(err) } + suppFltr := &Filter{ + Tenant: "cgrates.org", + ID: "FLTR_SUPP_1", + Rules: []*FilterRule{ + { + Type: utils.MetaString, + Element: "~*req.Supplier", + Values: []string{"SupplierProfile2"}, + }}, + } + dm.SetFilter(suppFltr) + supp := &SupplierProfile{ + Tenant: "cgrates.org", + ID: "SPP_1", + FilterIDs: []string{"FLTR_SUPP_1"}, + + Sorting: utils.MetaLC, + SortingParameters: []string{}, + Suppliers: []*Supplier{ + { + ID: "supplier1", + RatingPlanIDs: []string{"RPL_2"}, + ResourceIDs: []string{"ResGroup2", "ResGroup4"}, + StatIDs: []string{"Stat3"}, + Weight: 10, + Blocker: false, + SupplierParameters: utils.EmptyString, + }, + }, + Weight: 20, + } + dm.SetSupplierProfile(supp, true) + if err := dm.CacheDataFromDB(utils.SupplierFilterIndexes, nil, false); err != nil { + t.Error(err) + } + ddpFlt := &Filter{ + Tenant: "cgrates.org", + ID: "DSP_FLT", + Rules: []*FilterRule{ + { + Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Account, + Type: utils.MetaString, + Values: []string{"2009"}, + }, + }, + } + dm.SetFilter(ddpFlt) + dpp := &DispatcherProfile{ + Tenant: "cgrates.org", + ID: "DSP_Test1", + FilterIDs: []string{"DSP_FLT"}, + Strategy: utils.MetaFirst, + Subsystems: []string{utils.MetaAttributes, utils.MetaSessionS}, + Weight: 20, + } + if err := dm.SetDispatcherProfile(dpp, true); err != nil { + t.Error(err) + } + if err := dm.CacheDataFromDB(utils.DispatcherFilterIndexes, nil, false); err != nil { + t.Error(err) + } + } func TestFilterIndexesRmtRpl(t *testing.T) { @@ -447,3 +509,98 @@ func TestFilterIndexesRmtRpl(t *testing.T) { t.Errorf("Expected %+v,Received %+v", utils.ToJSON(expIndx), utils.ToJSON(idx)) } } + +func TestStatQueueProfileIndx(t *testing.T) { + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + fltrs := []*Filter{ + { + Tenant: "cgrates.org", + ID: "SQ_FLT_1", + Rules: []*FilterRule{ + { + Type: utils.MetaString, + Element: "~*req.Destination", + Values: []string{"1002", "1003", "1004"}, + }, + }, + }, + { + Tenant: "cgrates.org", + ID: "SQ_FLT_2", + Rules: []*FilterRule{ + + { + Type: utils.MetaGreaterOrEqual, + Element: "~*req.UsageInterval", + Values: []string{(1 * time.Second).String()}, + }, + { + Type: utils.MetaGreaterOrEqual, + Element: "~*req." + utils.Weight, + Values: []string{"9.0"}, + }, + }, + }} + for _, flt := range fltrs { + if err := dm.SetFilter(flt); err != nil { + t.Error(err) + } + } + sqP := &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "SQ_1", + FilterIDs: []string{"SQ_FLT_1"}, + QueueLength: 10, + TTL: time.Duration(0) * time.Second, + Metrics: []*MetricWithFilters{ + { + MetricID: "*asr", + }, + { + MetricID: utils.MetaACD, + }, + { + MetricID: "*acc", + }, + }, + ThresholdIDs: []string{"Test"}, + Blocker: false, + Stored: true, + Weight: float64(0), + MinItems: 0, + } + if err := dm.SetStatQueueProfile(sqP, true); err != nil { + t.Error(err) + } + sqP = &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "SQ_1", + FilterIDs: []string{"SQ_FLT_2"}, + QueueLength: 10, + TTL: time.Duration(0) * time.Second, + Metrics: []*MetricWithFilters{ + { + MetricID: "*asr", + }, + { + MetricID: utils.MetaACD, + }, + { + MetricID: "*acc", + }, + }, + ThresholdIDs: []string{"Test"}, + Blocker: false, + Stored: true, + Weight: float64(0), + MinItems: 0, + } + if err := dm.SetStatQueueProfile(sqP, true); err != nil { + t.Error(err) + } + if err := dm.RemoveStatQueueProfile("cgrates.org", "SQ_1", utils.NonTransactional, true); err != nil { + t.Error(err) + } +} diff --git a/engine/responder_test.go b/engine/responder_test.go index e96542585..d57e5c550 100644 --- a/engine/responder_test.go +++ b/engine/responder_test.go @@ -505,7 +505,7 @@ func TestResponderGetMaxSessionTimePass(t *testing.T) { &Balance{ Weight: 30, Value: 12, - DestinationIDs: utils.NewStringMap("1002"), + DestinationIDs: utils.NewStringMap("DEST"), }, }, }, @@ -575,10 +575,12 @@ func TestResponderGetMaxSessionTimePass(t *testing.T) { SetDataStorage(dm) var reply time.Duration - if err := rsponder.GetMaxSessionTime(arg, &reply); err == nil { + if err := rsponder.GetMaxSessionTime(arg, &reply); err != nil { t.Error(err) + } else if reply != 0 { + t.Errorf("Expected 0,Received %v", reply) } - //unfinished + } func TestMaxSessionTimeOnAccounts(t *testing.T) { diff --git a/engine/stats_test.go b/engine/stats_test.go index fd410f26e..0d9f15d0e 100644 --- a/engine/stats_test.go +++ b/engine/stats_test.go @@ -464,7 +464,9 @@ func TestStatStoreStatQueue(t *testing.T) { t.Error(err) } sq := &StatQueue{ - dirty: utils.BoolPointer(true), + Tenant: "cgrates.org", + ID: "SQ_1", + dirty: utils.BoolPointer(true), SQMetrics: map[string]StatMetric{ utils.MetaASR: &StatASR{ Answered: 1, @@ -483,6 +485,16 @@ func TestStatStoreStatQueue(t *testing.T) { } else if *sq.dirty { t.Error("Expected false") } + args := &utils.TenantIDWithArgDispatcher{ + TenantID: &utils.TenantID{ + Tenant: "cgrates.org", + ID: "SQ_1", + }, + } + var reply StatQueue + if err := sS.V1GetStatQueue(args, &reply); err != nil { + t.Error(err) + } } func TestStatsGetStatQueuesForEvent(t *testing.T) { diff --git a/engine/storage_test.go b/engine/storage_test.go index ce773d796..cc07d42dd 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -646,3 +646,66 @@ func TestTPDispatcher(t *testing.T) { } } } + +func TestTpRLoadRatingProfilesFiltered(t *testing.T) { + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + tpr, err := NewTpReader(db, db, "TP1", "UTC", nil, nil) + if err != nil { + t.Error(err) + } + tpRpl := []*utils.TPRatingPlan{ + { + + TPid: "TP1", + ID: "TEST_RPLAN1", + RatingPlanBindings: []*utils.TPRatingPlanBinding{ + { + DestinationRatesId: "RateId", + TimingId: "TimingID", + Weight: 12, + }, + }, + }, + { + TPid: "TP1", + ID: "TEST_RPLAN2", + RatingPlanBindings: []*utils.TPRatingPlanBinding{ + { + DestinationRatesId: "TEST_DSTRATE1", + TimingId: "TEST_TIMING1", + Weight: 10.0}, + }, + }, + } + if err := db.SetTPRatingPlans(tpRpl); err != nil { + t.Error(err) + } + qriedRpf := &utils.TPRatingProfile{ + TPid: "TP1", + LoadId: "TEST_LOADID", + Tenant: "cgrates.org", + Category: "call", + Subject: "1001", + RatingPlanActivations: []*utils.TPRatingActivation{ + { + ActivationTime: "2014-01-14T00:00:00Z", + RatingPlanId: "TEST_RPLAN1", + FallbackSubjects: "subj1;subj2"}, + { + ActivationTime: "2014-01-15T00:00:00Z", + RatingPlanId: "TEST_RPLAN2", + FallbackSubjects: "subj1;subj2"}, + }, + } + tpRpf := []*utils.TPRatingProfile{ + qriedRpf, + } + if err := db.SetTPRatingProfiles(tpRpf); err != nil { + t.Error(err) + } + if _, err := tpr.LoadRatingProfilesFiltered(qriedRpf); err == nil { + t.Error(err) + } + //unfinished +}