Add Unit Tests for Engine

This commit is contained in:
armirveliaj
2024-05-27 10:52:36 -04:00
committed by Dan Christian Bogos
parent cc98314426
commit 70f4f44421
6 changed files with 188 additions and 0 deletions

View File

@@ -3324,3 +3324,21 @@ func TestAccEnableAccountAction(t *testing.T) {
})
}
}
func TestEngineToStringJSON(t *testing.T) {
acc := &Account{
ID: "acc_123",
AllowNegative: false,
Disabled: true,
UpdateTime: time.Now(),
executingTriggers: false,
}
want, err := json.Marshal(acc)
if err != nil {
t.Errorf("Error marshalling Account to JSON: %v", err)
}
got := acc.String()
if got != string(want) {
t.Errorf("Expected JSON: %s, got: %s", want, got)
}
}

View File

@@ -19,6 +19,7 @@ package engine
import (
"bytes"
"encoding/json"
"errors"
"log"
"os"
@@ -2733,3 +2734,37 @@ func TestCallDescGetRatingPlansForPrefix(t *testing.T) {
t.Error(err)
}
}
func TestEngineCallDescriptorString(t *testing.T) {
TestCallDescriptor := &CallDescriptor{
Category: "call",
Tenant: "cgrates.org",
Subject: "user",
Account: "testAccount",
Destination: "local",
TimeStart: time.Date(2022, time.January, 7, 16, 60, 0, 0, time.UTC),
TimeEnd: time.Date(2022, time.January, 7, 16, 60, 0, 0, time.UTC),
LoopIndex: 1.0,
DurationIndex: time.Minute * 3,
FallbackSubject: "testfallbacksubject",
ToR: utils.MetaVoice,
ExtraFields: map[string]string{"key1": "value1"},
MaxRate: 10.0,
MaxRateUnit: time.Minute,
MaxCostSoFar: 0.5,
CgrID: "grid1",
RunID: "runID123",
ForceDuration: false,
PerformRounding: true,
DenyNegativeAccount: true,
DryRun: false,
}
want, err := json.Marshal(TestCallDescriptor)
if err != nil {
t.Errorf("Error marshalling CallDescriptor to JSON: %v", err)
}
got := TestCallDescriptor.String()
if got != string(want) {
t.Errorf("Expected CallDescriptor.String() to return %s, got %s", want, got)
}
}

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import (
"encoding/json"
"reflect"
"strconv"
"testing"
@@ -882,3 +883,35 @@ func TestCDRAddDefaults(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", eCDR, cdr)
}
}
func TestEngineCDRString(t *testing.T) {
TestCDR := CDR{
CGRID: "grid1",
RunID: "runid123",
OrderID: 12345,
OriginHost: "10.1.1.1",
Source: "testsrouce",
OriginID: "origin123",
ToR: "tortest",
RequestType: "prepaid",
Tenant: "cgrates.org",
Category: "rating",
Account: "testaccount",
Subject: "user",
Destination: "+1234567890",
SetupTime: time.Now(),
AnswerTime: time.Now().Add(time.Minute * 5),
Usage: time.Minute * 3,
ExtraFields: map[string]string{"key1": "value1"},
ExtraInfo: "some additional information",
Partial: false,
PreRated: false,
CostSource: "rating_engine",
Cost: 1.50,
}
want, _ := json.Marshal(TestCDR)
got := TestCDR.String()
if string(got) != string(want) {
t.Errorf("Expected CDR.String() to return %s, got %s", want, got)
}
}

View File

@@ -253,3 +253,12 @@ func TestEngineStatMdlTableName(t *testing.T) {
t.Errorf("\nExpected: <%+v>\nReceived: <%+v>", exp, result)
}
}
func TestEngineThresholdMdlTableName(t *testing.T) {
testStruct := ThresholdMdl{}
exp := utils.TBLTPThresholds
result := testStruct.TableName()
if !reflect.DeepEqual(exp, result) {
t.Errorf("\nExpected: <%+v>\nReceived: <%+v>", exp, result)
}
}

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import (
"reflect"
"testing"
"time"
@@ -463,3 +464,66 @@ func TestRatingProfileGetRatingPlansPrefixAny(t *testing.T) {
}
}
func TestEngineSwapRpasIndex(t *testing.T) {
activationTime1 := time.Now()
activationTime2 := activationTime1.Add(time.Hour)
plan1 := &RatingPlanActivation{
ActivationTime: activationTime1,
RatingPlanId: "planTest1",
FallbackKeys: []string{"key1", "key2"},
}
plan2 := &RatingPlanActivation{
ActivationTime: activationTime2,
RatingPlanId: "planTest2",
FallbackKeys: []string{"key3"},
}
initialRPAs := RatingPlanActivations{plan1, plan2}
index1 := 0
index2 := 1
want := RatingPlanActivations{plan2, plan1}
rpas := make(RatingPlanActivations, len(initialRPAs))
copy(rpas, initialRPAs)
rpas.Swap(index1, index2)
if !reflect.DeepEqual(rpas, want) {
t.Errorf("Expected RatingPlanActivations after swap: %v, got: %v", want, rpas)
}
}
func TestEngineEqualRpaOrpa(t *testing.T) {
activationTime1 := time.Now()
activationTime2 := activationTime1.Add(time.Hour)
plan1 := &RatingPlanActivation{
ActivationTime: activationTime1,
RatingPlanId: "planId1",
FallbackKeys: []string{"key1", "key2"},
}
plan2 := &RatingPlanActivation{
ActivationTime: activationTime2,
RatingPlanId: "planId2",
FallbackKeys: []string{"key3"},
}
tests := []struct {
name string
rpa *RatingPlanActivation
orpa *RatingPlanActivation
expected bool
}{
{name: "Equal plans", rpa: plan1, orpa: plan1, expected: true},
{name: "Different activation time", rpa: plan1, orpa: plan2, expected: false},
{name: "Nil arguments (both nil)", rpa: nil, orpa: nil, expected: true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var result bool
if tc.rpa == nil && tc.orpa == nil {
result = tc.expected
} else {
result = tc.rpa.Equal(tc.orpa)
}
if result != tc.expected {
t.Errorf("Expected Equal(%v, %v) to be %v, got %v", tc.rpa, tc.orpa, tc.expected, result)
}
})
}
}

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import (
"encoding/json"
"reflect"
"testing"
@@ -949,3 +950,31 @@ func TestUnitCounterFilterFieldAsString(t *testing.T) {
}
}
func TestEngineCounterFilterString(t *testing.T) {
testFilter := CounterFilter{
Value: 12.5,
}
want, err := json.Marshal(testFilter)
if err != nil {
t.Errorf("Error marshalling CounterFilter to JSON: %v", err)
}
got := testFilter.String()
if got != string(want) {
t.Errorf("Expected JSON: %s, got: %s", want, got)
}
}
func TestEngineUnitCounterString(t *testing.T) {
testCounter := UnitCounter{
CounterType: "event",
}
want, err := json.Marshal(testCounter)
if err != nil {
t.Errorf("Error marshalling UnitCounter to JSON: %v", err)
}
got := testCounter.String()
if got != string(want) {
t.Errorf("Expected JSON: %s, got: %s", want, got)
}
}