Add new unit tests on TrendS

This commit is contained in:
armirveliaj
2024-10-10 11:08:14 -04:00
committed by Dan Christian Bogos
parent 2b8a8676b6
commit 4bf49be2be
2 changed files with 83 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import (
"errors"
"testing"
"time"
@@ -279,3 +280,72 @@ func TestLibTrendsComputeIndexes(t *testing.T) {
t.Errorf("Expected metric3 total to be 2.0, got %f", total)
}
}
func TestGetTrendGrowthNotFound(t *testing.T) {
trend := &Trend{
mLast: map[string]time.Time{},
Metrics: map[time.Time]map[string]*MetricWithTrend{},
}
tG, err := trend.getTrendGrowth("nonexistentID", 100, utils.MetaLast, 2)
if tG != -1.0 {
t.Errorf("Expected trend growth to be -1.0, but got: %v", tG)
}
if !errors.Is(err, utils.ErrNotFound) {
t.Errorf("Expected error to be ErrNotFound, but got: %v", err)
}
}
func TestGetTrendGrowthMetricNotFound(t *testing.T) {
trend := &Trend{
mLast: map[string]time.Time{
"metricID": time.Now(),
},
Metrics: map[time.Time]map[string]*MetricWithTrend{
time.Now(): {},
},
}
tG, err := trend.getTrendGrowth("metricID", 150, utils.MetaLast, 2)
if tG != -1.0 {
t.Errorf("Expected trend growth to be -1.0, but got: %v", tG)
}
if !errors.Is(err, utils.ErrNotFound) {
t.Errorf("Expected error to be ErrNotFound, but got: %v", err)
}
}
func TestGetTrendLabel_DefaultCase(t *testing.T) {
trend := &Trend{}
tGrowth := 0.0
tolerance := 1.0
lbl := trend.getTrendLabel(tGrowth, tolerance)
if lbl != utils.MetaConstant {
t.Errorf("Expected label to be %s, but got: %s", utils.MetaConstant, lbl)
}
}
func TestTrendCompile(t *testing.T) {
trend := &Trend{
mTotals: nil,
}
cleanTtl := time.Minute
qLength := 10
trend.Compile(cleanTtl, qLength)
if trend.mTotals == nil {
t.Error("Expected mTotals to be initialized, but it is nil")
}
}

View File

@@ -96,3 +96,16 @@ func TestTrendServiceIsRunning(t *testing.T) {
t.Errorf("Expected IsRunning to return false, got %v", result)
}
}
func TestTrendServiceStartAlreadyRunning(t *testing.T) {
trendService := &TrendService{}
trendService.trs = &engine.TrendS{}
err := trendService.Start()
if err != utils.ErrServiceAlreadyRunning {
t.Errorf("Expected error '%v', got '%v'", utils.ErrServiceAlreadyRunning, err)
}
}