From 2137e141f6f639c5d58deb36dc1408f717aec2dc Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Fri, 11 Oct 2024 10:39:49 -0400 Subject: [PATCH] Add coverage tests on engine --- engine/model_helpers_test.go | 161 +++++++++++++++++++++++++++++++++++ engine/trends_test.go | 74 ++++++++++++++++ 2 files changed, 235 insertions(+) diff --git a/engine/model_helpers_test.go b/engine/model_helpers_test.go index c528da00c..ad88c0bca 100644 --- a/engine/model_helpers_test.go +++ b/engine/model_helpers_test.go @@ -6063,3 +6063,164 @@ func TestRankingProfileToAPI(t *testing.T) { t.Errorf("Expected QueryInterval %s, got %s", expected.QueryInterval, result.QueryInterval) } } + +func TestAPItoModelTPRanking(t *testing.T) { + tests := []struct { + name string + input *utils.TPRankingProfile + expected RankingsMdls + }{ + { + name: "Nil Input", + input: nil, + expected: RankingsMdls{}, + }, + { + name: "No StatIDs", + input: &utils.TPRankingProfile{ + TPid: "tpid1", + Tenant: "cgrates.org", + ID: "id1", + QueryInterval: "1h", + Sorting: "asc", + ThresholdIDs: []string{"threshold1", "threshold2"}, + MetricIDs: []string{"metric1", "metric2"}, + SortingParameters: []string{"param1", "param2"}, + StatIDs: []string{}, + }, + expected: RankingsMdls{ + &RankingsMdl{ + Tpid: "tpid1", + Tenant: "cgrates.org", + ID: "id1", + QueryInterval: "1h", + Sorting: "asc", + StatIDs: "", + ThresholdIDs: "threshold1" + utils.InfieldSep + "threshold2", + MetricIDs: "metric1" + utils.InfieldSep + "metric2", + SortingParameters: "param1" + utils.InfieldSep + "param2", + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual := APItoModelTPRanking(tt.input) + + if len(actual) != len(tt.expected) { + t.Errorf("Expected %d models, got %d", len(tt.expected), len(actual)) + return + } + + for i := range tt.expected { + if *tt.expected[i] != *actual[i] { + t.Errorf("Expected model %+v, got %+v", *tt.expected[i], *actual[i]) + } + } + }) + } +} + +func TestAPItoModelTrends(t *testing.T) { + tests := []struct { + name string + input *utils.TPTrendsProfile + expected TrendsMdls + }{ + { + name: "Nil Input", + input: nil, + expected: TrendsMdls{}, + }, + { + name: "Valid Input", + input: &utils.TPTrendsProfile{ + TPid: "tpid1", + Tenant: "cgrates.org", + ID: "id1", + Schedule: "daily", + QueueLength: 10, + StatID: "stat1", + TTL: "3600", + MinItems: 5, + CorrelationType: "type1", + Tolerance: 0.1, + Stored: true, + ThresholdIDs: []string{"threshold1", "threshold2"}, + Metrics: []string{"metric1", "metric2"}, + }, + expected: TrendsMdls{ + &TrendsMdl{ + Tpid: "tpid1", + Tenant: "cgrates.org", + ID: "id1", + Schedule: "daily", + QueueLength: 10, + StatID: "stat1", + TTL: "3600", + MinItems: 5, + CorrelationType: "type1", + Tolerance: 0.1, + Stored: true, + ThresholdIDs: "threshold1" + utils.InfieldSep + "threshold2", + Metrics: "metric1" + utils.InfieldSep + "metric2", + CreatedAt: time.Time{}, + }, + }, + }, + { + name: "Empty ThresholdIDs and Metrics", + input: &utils.TPTrendsProfile{ + TPid: "tpid2", + Tenant: "tenant2", + ID: "id2", + Schedule: "weekly", + QueueLength: 15, + StatID: "stat2", + TTL: "7200", + MinItems: 10, + CorrelationType: "type2", + Tolerance: 0.2, + Stored: false, + ThresholdIDs: []string{}, + Metrics: []string{}, + }, + expected: TrendsMdls{ + &TrendsMdl{ + Tpid: "tpid2", + Tenant: "tenant2", + ID: "id2", + Schedule: "weekly", + QueueLength: 15, + StatID: "stat2", + TTL: "7200", + MinItems: 10, + CorrelationType: "type2", + Tolerance: 0.2, + Stored: false, + ThresholdIDs: "", + Metrics: "", + CreatedAt: time.Time{}, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actual := APItoModelTrends(tt.input) + + if len(actual) != len(tt.expected) { + t.Errorf("Expected %d models, got %d", len(tt.expected), len(actual)) + return + } + + for i := range tt.expected { + if *tt.expected[i] != *actual[i] { + t.Errorf("Expected model %+v, got %+v", *tt.expected[i], *actual[i]) + } + } + }) + } +} diff --git a/engine/trends_test.go b/engine/trends_test.go index 8c123cb3f..5847d6d61 100644 --- a/engine/trends_test.go +++ b/engine/trends_test.go @@ -19,9 +19,14 @@ along with this program. If not, see package engine import ( + "errors" + "sync" "testing" + "github.com/cgrates/birpc/context" "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/cron" ) func TestTrendProfileTenantID(t *testing.T) { @@ -85,3 +90,72 @@ func TestNewTrendS(t *testing.T) { } } + +func TestProcessEEsWithError(t *testing.T) { + + trend := &Trend{ + ID: "ID", + Tenant: "cgrates.org", + } + + mockConnMgr := &ConnManager{} + trendService := &TrendS{ + cgrcfg: &config.CGRConfig{}, + connMgr: mockConnMgr, + } + + err := trendService.processEEs(trend) + if err != nil || errors.Is(err, utils.ErrPartiallyExecuted) { + t.Errorf("Expected error %v, got %v", utils.ErrPartiallyExecuted, err) + } + +} + +func TestV1ScheduleQueriesInvalidTrendID(t *testing.T) { + + ctx := context.Background() + + tS := &TrendS{ + crn: cron.New(), + crnTQs: make(map[string]map[string]cron.EntryID), + crnTQsMux: &sync.RWMutex{}, + } + + args := &utils.ArgScheduleTrendQueries{ + TenantIDWithAPIOpts: utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: "cgrates.org", + ID: "ID", + }, + APIOpts: make(map[string]any), + }, + TrendIDs: []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) + } +} + +func TestProcessThresholds_OptsInitialization(t *testing.T) { + tS := &TrendS{} + + trnd := &Trend{ + Tenant: "cgrates.org", + ID: "ID", + } + + err := tS.processThresholds(trnd) + + if err != nil { + t.Errorf("expected no error but got: %v", err) + } + +}