mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add new unit tests on engine
This commit is contained in:
committed by
Dan Christian Bogos
parent
d3cc121d86
commit
8ab27e69d2
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package engine
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
@@ -4976,3 +4977,50 @@ func TestEvenCostProcessEventCostField(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertToEventCost(t *testing.T) {
|
||||
eventCost := &EventCost{
|
||||
CGRID: "cgrid",
|
||||
RunID: "run_id",
|
||||
StartTime: time.Now(),
|
||||
}
|
||||
convertedEventCost, err := ConvertToEventCost(eventCost)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
if convertedEventCost != eventCost {
|
||||
t.Error("Expected identical EventCost objects")
|
||||
}
|
||||
|
||||
eventCostJSON, _ := json.Marshal(eventCost)
|
||||
convertedEventCost, err = ConvertToEventCost(string(eventCostJSON))
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
tStruct := struct {
|
||||
CGRID string `json:"CGRID"`
|
||||
RunID string `json:"RunID"`
|
||||
}{
|
||||
CGRID: "cgrid",
|
||||
RunID: "run_id",
|
||||
}
|
||||
convertedEventCost, err = ConvertToEventCost(tStruct)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
if convertedEventCost.CGRID != "cgrid" || convertedEventCost.RunID != "run_id" {
|
||||
t.Error("Expected correct values for CGRID and RunID")
|
||||
}
|
||||
|
||||
_, err = ConvertToEventCost(123)
|
||||
if err == nil {
|
||||
t.Error("Expected error for invalid input type")
|
||||
}
|
||||
|
||||
invalidJSON := []byte("invalid_json")
|
||||
_, err = ConvertToEventCost(invalidJSON)
|
||||
if err == nil {
|
||||
t.Error("Expected error for invalid JSON")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1497,3 +1497,112 @@ func TestFieldAsInterfaces(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBalanceCharge(t *testing.T) {
|
||||
balanceCharge := BalanceCharge{
|
||||
AccountID: "account123",
|
||||
BalanceUUID: "uuid",
|
||||
RatingID: "ID1001",
|
||||
Units: 10.0,
|
||||
BalanceFactor: 0.5,
|
||||
ExtraChargeID: "1",
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
path []string
|
||||
expect interface{}
|
||||
}{
|
||||
{[]string{"AccountID"}, "account123"},
|
||||
{[]string{"BalanceUUID"}, "uuid"},
|
||||
{[]string{"RatingID"}, "ID1001"},
|
||||
{[]string{"Units"}, 10.0},
|
||||
{[]string{"BalanceFactor"}, 0.5},
|
||||
{[]string{"ExtraChargeID"}, "1"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
val, err := balanceCharge.FieldAsInterface(test.path)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error for path %v: %v", test.path, err)
|
||||
continue
|
||||
}
|
||||
if val != test.expect {
|
||||
t.Errorf("Expected %v for path %v, got %v", test.expect, test.path, val)
|
||||
}
|
||||
}
|
||||
|
||||
_, err := balanceCharge.FieldAsInterface([]string{"invalid_field"})
|
||||
if err == nil {
|
||||
t.Error("Expected error for invalid field path")
|
||||
}
|
||||
|
||||
_, err = balanceCharge.FieldAsInterface([]string{})
|
||||
if err == nil {
|
||||
t.Error("Expected error for empty field path")
|
||||
}
|
||||
|
||||
balanceCharge.BalanceFactor = 0
|
||||
val, err := balanceCharge.FieldAsInterface([]string{"BalanceFactor"})
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error for BalanceFactor: %v", err)
|
||||
}
|
||||
if val != 1.0 {
|
||||
t.Errorf("Expected 1.0 for BalanceFactor, got %v", val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRatingFieldAsInterface(t *testing.T) {
|
||||
rating := Rating{
|
||||
"rating1": &RatingUnit{
|
||||
ConnectFee: 1.23,
|
||||
RoundingMethod: "ROUNDING",
|
||||
RoundingDecimals: 2,
|
||||
MaxCost: 100,
|
||||
MaxCostStrategy: "CAP",
|
||||
TimingID: "timing1",
|
||||
RatesID: "rates1",
|
||||
RatingFiltersID: "filters1",
|
||||
},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
path []string
|
||||
expect interface{}
|
||||
}{
|
||||
{[]string{"rating1"}, rating["rating1"]},
|
||||
{[]string{"rating1", "ConnectFee"}, 1.23},
|
||||
{[]string{"rating1", "RoundingMethod"}, "ROUNDING"},
|
||||
{[]string{"rating1", "RoundingDecimals"}, 2},
|
||||
{[]string{"rating1", "MaxCost"}, 100.0},
|
||||
{[]string{"rating1", "MaxCostStrategy"}, "CAP"},
|
||||
{[]string{"rating1", "TimingID"}, "timing1"},
|
||||
{[]string{"rating1", "RatesID"}, "rates1"},
|
||||
{[]string{"rating1", "RatingFiltersID"}, "filters1"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
val, err := rating.FieldAsInterface(test.path)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error for path %v: %v", test.path, err)
|
||||
continue
|
||||
}
|
||||
if !reflect.DeepEqual(val, test.expect) {
|
||||
t.Errorf("Expected %v for path %v, got %v", test.expect, test.path, val)
|
||||
}
|
||||
}
|
||||
|
||||
_, err := rating.FieldAsInterface([]string{"invalid_rating"})
|
||||
if err == nil {
|
||||
t.Error("Expected error for invalid rating ID")
|
||||
}
|
||||
|
||||
_, err = rating.FieldAsInterface([]string{})
|
||||
if err == nil {
|
||||
t.Error("Expected error for empty field path")
|
||||
}
|
||||
|
||||
_, err = rating.FieldAsInterface([]string{"rating1", "invalid_field"})
|
||||
if err == nil {
|
||||
t.Error("Expected error for invalid field within rating")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6226,3 +6226,53 @@ func TestAPItoModelTrends(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrendProfileToAPIs(t *testing.T) {
|
||||
sampleProfile := &TrendProfile{
|
||||
Tenant: "tenant",
|
||||
ID: "1",
|
||||
Schedule: "schedule",
|
||||
StatID: "1001",
|
||||
ThresholdIDs: []string{"threshold1", "threshold2"},
|
||||
Metrics: []string{"metric1", "metric2"},
|
||||
QueueLength: 10,
|
||||
MinItems: 5,
|
||||
CorrelationType: "last",
|
||||
Tolerance: 0.1,
|
||||
Stored: true,
|
||||
TTL: time.Hour * 24,
|
||||
}
|
||||
|
||||
apiProfile := TrendProfileToAPI(sampleProfile)
|
||||
|
||||
if apiProfile.Tenant != sampleProfile.Tenant {
|
||||
t.Errorf("Expected Tenant to be %s, got %s", sampleProfile.Tenant, apiProfile.Tenant)
|
||||
}
|
||||
if apiProfile.ID != sampleProfile.ID {
|
||||
t.Errorf("Expected ID to be %s, got %s", sampleProfile.ID, apiProfile.ID)
|
||||
}
|
||||
if apiProfile.Schedule != sampleProfile.Schedule {
|
||||
t.Errorf("Expected Schedule to be %s, got %s", sampleProfile.Schedule, apiProfile.Schedule)
|
||||
}
|
||||
if apiProfile.StatID != sampleProfile.StatID {
|
||||
t.Errorf("Expected StatID to be %s, got %s", sampleProfile.StatID, apiProfile.StatID)
|
||||
}
|
||||
|
||||
if len(apiProfile.ThresholdIDs) != len(sampleProfile.ThresholdIDs) {
|
||||
t.Errorf("Expected ThresholdIDs to have length %d, got %d", len(sampleProfile.ThresholdIDs), len(apiProfile.ThresholdIDs))
|
||||
}
|
||||
if len(apiProfile.Metrics) != len(sampleProfile.Metrics) {
|
||||
t.Errorf("Expected Metrics to have length %d, got %d", len(sampleProfile.Metrics), len(apiProfile.Metrics))
|
||||
}
|
||||
|
||||
if sampleProfile.TTL == time.Duration(0) {
|
||||
if apiProfile.TTL != "" {
|
||||
t.Errorf("Expected TTL to be empty string for zero duration, got %s", apiProfile.TTL)
|
||||
}
|
||||
} else {
|
||||
if apiProfile.TTL != sampleProfile.TTL.String() {
|
||||
t.Errorf("Expected TTL to be %s, got %s", sampleProfile.TTL.String(), apiProfile.TTL)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user