Add new unit tests for TrendS

This commit is contained in:
armirveliaj
2024-10-02 10:23:55 -04:00
committed by Dan Christian Bogos
parent 37322e9c4c
commit 9321888d3f
3 changed files with 202 additions and 0 deletions

View File

@@ -249,3 +249,72 @@ func TestTrendTenantID(t *testing.T) {
t.Errorf("Expected QueueLength 10, but got %d", trend.tP.QueueLength)
}
}
func TestTComputeIndexes(t *testing.T) {
runTime1 := time.Now()
runTime2 := runTime1.Add(time.Minute)
trend := &Trend{
RunTimes: []time.Time{runTime1, runTime2},
Metrics: map[time.Time]map[string]*MetricWithTrend{
runTime1: {
"metric1": &MetricWithTrend{ID: "metric1", Value: 10.0},
"metric2": &MetricWithTrend{ID: "metric2", Value: 20.0},
},
runTime2: {
"metric1": &MetricWithTrend{ID: "metric1", Value: 15.0},
},
},
}
trend.computeIndexes()
if trend.mLast["metric1"] != runTime2 {
t.Errorf("Expected last time for metric1 to be %v, got %v", runTime2, trend.mLast["metric1"])
}
if trend.mCounts["metric1"] != 2 {
t.Errorf("Expected count for metric1 to be 2, got %d", trend.mCounts["metric1"])
}
if trend.mTotals["metric1"] != 25.0 {
t.Errorf("Expected total for metric1 to be 25.0, got %f", trend.mTotals["metric1"])
}
if trend.mLast["metric2"] != runTime1 {
t.Errorf("Expected last time for metric2 to be %v, got %v", runTime1, trend.mLast["metric2"])
}
if trend.mCounts["metric2"] != 1 {
t.Errorf("Expected count for metric2 to be 1, got %d", trend.mCounts["metric2"])
}
if trend.mTotals["metric2"] != 20.0 {
t.Errorf("Expected total for metric2 to be 20.0, got %f", trend.mTotals["metric2"])
}
}
func TestGetTrendLabel(t *testing.T) {
trend := &Trend{}
tests := []struct {
tGrowth float64
tolerance float64
expected string
}{
{1.0, 0.5, utils.MetaPositive},
{-1.0, 0.5, utils.MetaNegative},
{0.0, 0.5, utils.MetaConstant},
{0.3, 0.5, utils.MetaConstant},
{-0.3, 0.5, utils.MetaConstant},
{0.6, 0.5, utils.MetaPositive},
{-0.6, 0.5, utils.MetaNegative},
}
for _, test := range tests {
result := trend.getTrendLabel(test.tGrowth, test.tolerance)
if result != test.expected {
t.Errorf("For tGrowth: %f and tolerance: %f, expected %s, got %s", test.tGrowth, test.tolerance, test.expected, result)
}
}
}

66
engine/trends_test.go Normal file
View File

@@ -0,0 +1,66 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package engine
import (
"testing"
"github.com/cgrates/cgrates/config"
)
func TestNewTrendService(t *testing.T) {
dm := &DataManager{}
cfg := &config.CGRConfig{}
filterS := &FilterS{}
connMgr := &ConnManager{}
trendService := NewTrendService(dm, cfg, filterS, connMgr)
if trendService == nil {
t.Errorf("Expected non-nil TrendS, got nil")
}
if trendService.dm != dm {
t.Errorf("Expected dm to be %v, got %v", dm, trendService.dm)
}
if trendService.cfg != cfg {
t.Errorf("Expected cfg to be %v, got %v", cfg, trendService.cfg)
}
if trendService.fltrS != filterS {
t.Errorf("Expected filterS to be %v, got %v", filterS, trendService.fltrS)
}
if trendService.connMgr != connMgr {
t.Errorf("Expected connMgr to be %v, got %v", connMgr, trendService.connMgr)
}
if trendService.crnTQs == nil {
t.Errorf("Expected crnTQs to be non-nil, got nil")
}
if trendService.crnTQsMux == nil {
t.Errorf("Expected crnTQsMux to be non-nil, got nil")
}
if trendService.loopStopped == nil {
t.Errorf("Expected loopStopped to be non-nil, got nil")
}
}

View File

@@ -72,3 +72,70 @@ func TestCurrentDBVersions(t *testing.T) {
}
}
func TestCurrentStorDBVersions(t *testing.T) {
expected := Versions{
utils.CostDetails: 2,
utils.SessionSCosts: 3,
utils.CDRs: 2,
utils.TpFilters: 1,
utils.TpThresholds: 1,
utils.TpRoutes: 1,
utils.TpStats: 1,
utils.TpResources: 1,
utils.TpResource: 1,
utils.TpChargers: 1,
utils.TpDispatchers: 1,
utils.TpRateProfiles: 1,
utils.TpActionProfiles: 1,
}
actual := CurrentStorDBVersions()
if len(actual) != len(expected) {
t.Fatalf("Expected %d versions, got %d", len(expected), len(actual))
}
for key, expectedValue := range expected {
actualValue, exists := actual[key]
if !exists {
t.Errorf("Expected version for %s not found", key)
} else if actualValue != expectedValue {
t.Errorf("For %s, expected %d, got %d", key, expectedValue, actualValue)
}
}
}
func TestCurrentAllDBVersions(t *testing.T) {
expected := Versions{
utils.Stats: 4,
utils.Accounts: 3,
utils.Actions: 2,
utils.Thresholds: 4,
utils.Routes: 2,
utils.Attributes: 7,
utils.RQF: 5,
utils.Resource: 1,
utils.Subscribers: 1,
utils.Chargers: 2,
utils.Dispatchers: 2,
utils.LoadIDsVrs: 1,
utils.RateProfiles: 1,
utils.ActionProfiles: 1,
}
actual := CurrentAllDBVersions()
if len(actual) != len(expected) {
t.Fatalf("Expected %d versions, got %d", len(expected), len(actual))
}
for key, expectedValue := range expected {
actualValue, exists := actual[key]
if !exists {
t.Errorf("Expected version for %s not found", key)
} else if actualValue != expectedValue {
t.Errorf("For %s, expected %d, got %d", key, expectedValue, actualValue)
}
}
}