mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add unit tests on engine
This commit is contained in:
committed by
Dan Christian Bogos
parent
eb11b51c5e
commit
6c7c54ab58
@@ -777,3 +777,110 @@ func TestBalancesEqual(t *testing.T) {
|
||||
t.Errorf("Expected balances1 to not equal balances4, but they are equal")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBalancesFieldAsString(t *testing.T) {
|
||||
bc := Balances{
|
||||
&Balance{
|
||||
Uuid: "uuid123",
|
||||
ID: "balance123",
|
||||
Value: 100.0,
|
||||
ExpirationDate: time.Date(2024, time.December, 31, 23, 59, 59, 0, time.UTC),
|
||||
Weight: 1.5,
|
||||
DestinationIDs: utils.StringMap{},
|
||||
RatingSubject: "ratingSub",
|
||||
Categories: utils.StringMap{},
|
||||
SharedGroups: utils.StringMap{},
|
||||
Timings: []*RITiming{},
|
||||
TimingIDs: utils.StringMap{},
|
||||
Disabled: false,
|
||||
Blocker: true,
|
||||
precision: 2,
|
||||
account: nil,
|
||||
dirty: false,
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("empty field path", func(t *testing.T) {
|
||||
val, err := bc.FieldAsString([]string{})
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
}
|
||||
if val != "" {
|
||||
t.Errorf("expected empty string, got '%v'", val)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid field path", func(t *testing.T) {
|
||||
val, err := bc.FieldAsString([]string{"invalid"})
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
}
|
||||
if val != "" {
|
||||
t.Errorf("expected empty string, got '%v'", val)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("valid field path for non-existing ID", func(t *testing.T) {
|
||||
val, err := bc.FieldAsString([]string{"2", "ID"})
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
}
|
||||
if val != "" {
|
||||
t.Errorf("expected empty string, got '%v'", val)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestBalancesIsActiveAt(t *testing.T) {
|
||||
testTime := time.Now()
|
||||
|
||||
t.Run("balance is disabled", func(t *testing.T) {
|
||||
balance := &Balance{
|
||||
Disabled: true,
|
||||
}
|
||||
if balance.IsActiveAt(testTime) {
|
||||
t.Errorf("expected false, got true")
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestBalancesFieldAsInterfaceIndexPath(t *testing.T) {
|
||||
|
||||
bc := Balances{
|
||||
&Balance{
|
||||
Uuid: "uuid123",
|
||||
ID: "balance123",
|
||||
Value: 100.0,
|
||||
ExpirationDate: time.Date(2024, time.December, 31, 23, 59, 59, 0, time.UTC),
|
||||
Weight: 1.5,
|
||||
DestinationIDs: utils.StringMap{},
|
||||
RatingSubject: "ratingSub",
|
||||
Categories: utils.StringMap{},
|
||||
SharedGroups: utils.StringMap{},
|
||||
Timings: []*RITiming{},
|
||||
TimingIDs: utils.StringMap{},
|
||||
Disabled: false,
|
||||
Blocker: true,
|
||||
precision: 2,
|
||||
account: nil,
|
||||
dirty: false,
|
||||
},
|
||||
}
|
||||
|
||||
t.Run("test index path in FieldAsInterface", func(t *testing.T) {
|
||||
|
||||
idx := "0"
|
||||
val, err := bc.FieldAsInterface([]string{idx})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
expected := bc[0]
|
||||
if val != expected {
|
||||
t.Errorf("expected '%v', got '%v'", expected, val)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -2383,3 +2385,134 @@ func TestCdrsSetCloneable(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCdrsSetCloneableEvent(t *testing.T) {
|
||||
arg := &ArgV1ProcessEvent{}
|
||||
arg.SetCloneable(true)
|
||||
if !arg.clnb {
|
||||
t.Errorf("expected clnb to be true, got false")
|
||||
}
|
||||
arg.SetCloneable(false)
|
||||
|
||||
if arg.clnb {
|
||||
t.Errorf("expected clnb to be false, got true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCdrsRPCClone(t *testing.T) {
|
||||
|
||||
arg := &ArgV1ProcessEvent{}
|
||||
|
||||
cloned1, err1 := arg.RPCClone()
|
||||
|
||||
if err1 != nil {
|
||||
t.Errorf("unexpected error: %v", err1)
|
||||
}
|
||||
if cloned1 != arg {
|
||||
t.Errorf("expected cloned object to be identical, got different objects")
|
||||
}
|
||||
arg.SetCloneable(true)
|
||||
cloned2, err2 := arg.RPCClone()
|
||||
if err2 != nil {
|
||||
t.Errorf("unexpected error: %v", err2)
|
||||
}
|
||||
if cloned2 == arg {
|
||||
t.Errorf("expected cloned object to be different, got identical objects")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCdrsRPCCloneArgs(t *testing.T) {
|
||||
arg := &ArgV1ProcessEvents{}
|
||||
|
||||
cloned1, err1 := arg.RPCClone()
|
||||
if err1 != nil {
|
||||
t.Errorf("unexpected error: %v", err1)
|
||||
}
|
||||
if cloned1 != arg {
|
||||
t.Errorf("expected cloned object to be same, got different")
|
||||
}
|
||||
arg.SetCloneable(true)
|
||||
cloned2, err2 := arg.RPCClone()
|
||||
if err2 != nil {
|
||||
t.Errorf("unexpected error: %v", err2)
|
||||
}
|
||||
if cloned2 == arg {
|
||||
t.Errorf("expected cloned object to be different, got same")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewMapEventFromReqForm(t *testing.T) {
|
||||
form := url.Values{}
|
||||
form.Add("key1", "value1")
|
||||
form.Add("key2", "value2")
|
||||
|
||||
req, err := http.NewRequest("POST", "http://cgrates.com", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create request: %v", err)
|
||||
}
|
||||
req.Form = form
|
||||
mp, err := newMapEventFromReqForm(req)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
expectedRemoteAddr := req.RemoteAddr
|
||||
if mp[utils.Source] != expectedRemoteAddr {
|
||||
t.Errorf("expected mp[%q] to be %q, got %q", utils.Source, expectedRemoteAddr, mp[utils.Source])
|
||||
}
|
||||
if mp["key1"] != "value1" {
|
||||
t.Errorf("expected mp[%q] to be %q, got %q", "key1", "value1", mp["key1"])
|
||||
}
|
||||
if mp["key2"] != "value2" {
|
||||
t.Errorf("expected mp[%q] to be %q, got %q", "key2", "value2", mp["key2"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCDRSV1ProcessEvents(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
cdrS := &CDRServer{
|
||||
cgrCfg: cfg,
|
||||
}
|
||||
arg := &ArgV1ProcessEvents{
|
||||
Flags: []string{},
|
||||
CGREvents: []*utils.CGREvent{},
|
||||
APIOpts: make(map[string]any),
|
||||
}
|
||||
expectedReply := utils.OK
|
||||
var reply string
|
||||
err := cdrS.V1ProcessEvents(ctx, arg, &reply)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
if reply != expectedReply {
|
||||
t.Errorf("Expected reply %q, got %q", expectedReply, reply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCDRSCallValidServiceMethod(t *testing.T) {
|
||||
cdrS := &CDRServer{}
|
||||
|
||||
args := struct{}{}
|
||||
reply := new(struct{})
|
||||
|
||||
err := cdrS.Call(nil, "CDRServer.testMethod", args, reply)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("UNSUPPORTED_SERVICE_METHOD, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCDRSCallInvalidServiceMethod(t *testing.T) {
|
||||
cdrS := &CDRServer{}
|
||||
|
||||
args := struct{}{}
|
||||
reply := new(struct{})
|
||||
|
||||
err := cdrS.Call(nil, "CDRServer.InvalidMethod", args, reply)
|
||||
|
||||
if err != rpcclient.ErrUnsupporteServiceMethod {
|
||||
t.Errorf("Expected error %v, got %v", rpcclient.ErrUnsupporteServiceMethod, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1682,3 +1682,24 @@ func TestRouteSortDispatcher(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibRouteIDs(t *testing.T) {
|
||||
route1 := &SortedRoute{RouteID: "route1"}
|
||||
route2 := &SortedRoute{RouteID: "route2"}
|
||||
route3 := &SortedRoute{RouteID: "route3"}
|
||||
sortedRoutes := &SortedRoutes{
|
||||
ProfileID: "profile1",
|
||||
Sorting: "testsorting",
|
||||
Routes: []*SortedRoute{route1, route2, route3},
|
||||
}
|
||||
expectedRouteIDs := []string{"route1", "route2", "route3"}
|
||||
actualRouteIDs := sortedRoutes.RouteIDs()
|
||||
if len(actualRouteIDs) != len(expectedRouteIDs) {
|
||||
t.Errorf("Expected %d route IDs, got %d", len(expectedRouteIDs), len(actualRouteIDs))
|
||||
}
|
||||
for i, expectedID := range expectedRouteIDs {
|
||||
if actualRouteIDs[i] != expectedID {
|
||||
t.Errorf("Expected route ID at index %d to be %s, got %s", i, expectedID, actualRouteIDs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user