Add coverage tests on engine

This commit is contained in:
armirveliaj
2024-12-10 10:52:00 -05:00
committed by Dan Christian Bogos
parent b0aa272f75
commit 258146f7c2
3 changed files with 145 additions and 0 deletions

View File

@@ -618,3 +618,33 @@ func TestBalanceFilterGetFactors(t *testing.T) {
}
})
}
func TestBalanceFilterString(t *testing.T) {
uuid := "1234-uuid"
id := "5678-id"
bfType := "type"
weight := 10.5
disabled := true
blocker := false
factors := ValueFactors{
"factor1": 1.5,
"factor2": 2.5,
}
balanceFilter := &BalanceFilter{
Uuid: &uuid,
ID: &id,
Type: &bfType,
Weight: &weight,
Disabled: &disabled,
Blocker: &blocker,
Factors: &factors,
}
expectedJSON := `{"Uuid":"1234-uuid","ID":"5678-id","Type":"type","Value":null,"ExpirationDate":null,"Weight":10.5,"DestinationIDs":null,"RatingSubject":null,"Categories":null,"SharedGroups":null,"TimingIDs":null,"Timings":null,"Disabled":true,"Factors":{"factor1":1.5,"factor2":2.5},"Blocker":false}`
result := balanceFilter.String()
if result != expectedJSON {
t.Errorf("Expected JSON: %s, but got: %s", expectedJSON, result)
}
}

View File

@@ -1432,3 +1432,33 @@ func TestRoutesSortedRoutesListAsNavigableMap(t *testing.T) {
}
}
}
func TestSortWeight(t *testing.T) {
routes := []*SortedRoute{
{
RouteID: "route1",
SortingData: map[string]any{"Weight": 10.5},
sortingDataF64: map[string]float64{"Weight": 10.5},
},
{
RouteID: "route2",
SortingData: map[string]any{"Weight": 20.3},
sortingDataF64: map[string]float64{"Weight": 20.3},
},
{
RouteID: "route3",
SortingData: map[string]any{"Weight": 15.7},
sortingDataF64: map[string]float64{"Weight": 15.7},
},
}
sortedRoutes := &SortedRoutes{
Routes: routes,
}
sortedRoutes.SortWeight()
if sortedRoutes.Routes[0].RouteID != "route2" || sortedRoutes.Routes[1].RouteID != "route3" || sortedRoutes.Routes[2].RouteID != "route1" {
t.Errorf("Expected sorted routes to be route2, route3, route1, but got %v", sortedRoutes.Routes)
}
}

View File

@@ -20,6 +20,7 @@ package engine
import (
"encoding/csv"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
@@ -410,3 +411,87 @@ func TestNewURLCSVStorage(t *testing.T) {
}
}
func TestNewGoogleCSVStorageInvalidSpreadsheetID(t *testing.T) {
sep := ','
spreadsheetID := "invalid-id"
storage, err := NewGoogleCSVStorage(sep, spreadsheetID)
if err == nil {
t.Error("Expected error, but got nil")
}
if storage != nil {
t.Error("Expected nil storage, but got a non-nil value")
}
}
func TestGetAllFoldersInvalidPath(t *testing.T) {
nonExistentPath := "/non/existent/path"
paths, err := getAllFolders(nonExistentPath)
if err == nil {
t.Error("Expected error, but got nil")
}
if len(paths) != 0 {
t.Errorf("Expected no paths, but got: %v", paths)
}
}
func TestJoinURLValidBaseURL(t *testing.T) {
baseURL := "https://cgrates.com/path"
fn := "subpath"
expected := "https://cgrates.com/path/subpath"
result := joinURL(baseURL, fn)
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
}
func TestGetCfgJSONData(t *testing.T) {
t.Run("Empty raw JSON", func(t *testing.T) {
raw := json.RawMessage{}
data, err := getCfgJSONData(raw)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
if len(data) != 0 {
t.Errorf("Expected no data, but got: %v", data)
}
})
t.Run("Valid raw JSON", func(t *testing.T) {
raw := json.RawMessage(`{"key": "value"}`)
data, err := getCfgJSONData(raw)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
if string(data) != string(raw) {
t.Errorf("Expected data %s, but got %s", string(raw), string(data))
}
})
t.Run("Valid file path", func(t *testing.T) {
path := "/tmp/mockConfigPath.json"
expectedContent := []byte(`{"key": "value"}`)
err := os.WriteFile(path, expectedContent, 0600)
if err != nil {
t.Fatalf("Failed to create mock file: %v", err)
}
defer os.Remove(path)
raw := json.RawMessage(`"` + path + `"`)
data, err := getCfgJSONData(raw)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
if string(data) != string(expectedContent) {
t.Errorf("Expected data %s, but got %s", string(expectedContent), string(data))
}
})
t.Run("Invalid file path", func(t *testing.T) {
raw := json.RawMessage(`"/nonexistent/path/to/file.json"`)
data, err := getCfgJSONData(raw)
if err == nil {
t.Errorf("Expected error, but got none")
}
if data != nil {
t.Errorf("Expected no data, but got: %v", data)
}
})
}