Add unit tests for Dispatcher,Engine,Agents

This commit is contained in:
armirveliaj
2024-05-24 10:52:10 -04:00
committed by Dan Christian Bogos
parent 12b2e659b9
commit cc98314426
5 changed files with 123 additions and 0 deletions

View File

@@ -27,6 +27,7 @@ import (
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/sessions"
"github.com/cgrates/cgrates/utils"
"github.com/google/go-cmp/cmp"
)
var hangupEv string = `Event-Name: CHANNEL_HANGUP_COMPLETE
@@ -1116,3 +1117,24 @@ func TestFsEvV1TerminateSessionArgs(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", expected.TerminateSession, rcv.TerminateSession)
}
}
func TestAgentsFSEventGetSessionIds(t *testing.T) {
uuid := "test-uuid"
fsev := FSEvent{UUID: uuid}
got := fsev.GetSessionIds()
want := []string{uuid}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("GetSessionIds() returned incorrect result (-got +want):\n%s", diff)
}
}
func TestAgentsFSEventString(t *testing.T) {
fsev := &FSEvent{
"key1": "value1",
}
got := fsev.String()
want := "key1 = value1\n=============================================================="
if got != want {
t.Errorf("fsev.String() = %q, want %q", got, want)
}
}

View File

@@ -26,6 +26,7 @@ import (
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/sessions"
"github.com/cgrates/cgrates/utils"
"github.com/google/go-cmp/cmp"
)
var kamEv = KamEvent{KamTRIndex: "29223", KamTRLabel: "698469260",
@@ -504,3 +505,31 @@ func TestKamEvAsKamProcessEventReply(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
}
func TestAgentsNewKamSessionDisconnect(t *testing.T) {
hEntry := "entry123"
hID := "id123"
reason := "test reason"
got := NewKamSessionDisconnect(hEntry, hID, reason)
want := &KamSessionDisconnect{
Event: CGR_SESSION_DISCONNECT,
HashEntry: hEntry,
HashId: hID,
Reason: reason,
}
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("NewKamSessionDisconnect() mismatch (-got +want):\n%s", diff)
}
}
func TestAgentsKamEvent_String(t *testing.T) {
ke := KamEvent{
"EventName": "TestEvent",
"Data": "TestData",
}
got := ke.String()
want := `{"Data":"TestData","EventName":"TestEvent"}`
if got != want {
t.Errorf("KamEvent.String() = %v, want %v", got, want)
}
}

View File

@@ -384,3 +384,29 @@ func TestDspResponderGetCostOnRatingPlans(t *testing.T) {
// t.Error(err)
// }
// }
func TestDspResponderGetMaxSessionTimeOnAccountsNil(t *testing.T) {
cgrCfg := config.NewDefaultCGRConfig()
dspSrv := NewDispatcherService(nil, cgrCfg, nil, nil)
cgrCfg.DispatcherSCfg().AttributeSConns = []string{"test"}
CGREvent := &utils.GetMaxSessionTimeOnAccountsArgs{}
var reply *map[string]any
err := dspSrv.ResponderGetMaxSessionTimeOnAccounts(context.Background(), CGREvent, reply)
expected := "MANDATORY_IE_MISSING: [ApiKey]"
if err == nil || err.Error() != expected {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
}
}
func TestDspResponderGetMaxSessionTimeOnAccountsErrorNil(t *testing.T) {
cgrCfg := config.NewDefaultCGRConfig()
dspSrv := NewDispatcherService(nil, cgrCfg, nil, nil)
CGREvent := &utils.GetMaxSessionTimeOnAccountsArgs{}
var reply *map[string]any
err := dspSrv.ResponderGetMaxSessionTimeOnAccounts(context.Background(), CGREvent, reply)
expected := "DISPATCHER_ERROR:NO_DATABASE_CONNECTION"
if err == nil || err.Error() != expected {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err)
}
}

View File

@@ -26,6 +26,7 @@ import (
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
"github.com/google/go-cmp/cmp"
)
func TestConvertExternalToProfile(t *testing.T) {
@@ -244,3 +245,21 @@ func TestLibAttributesTenantIDMetaPrefix(t *testing.T) {
t.Errorf("expected: <%+v>, \nreceived: <%+v>", exp, rcv)
}
}
func TestEngineAttributeProfilesSort(t *testing.T) {
unsorted := AttributeProfiles{
{Weight: 10},
{Weight: 2},
{Weight: 15},
}
expected := AttributeProfiles{
{Weight: 15},
{Weight: 10},
{Weight: 2},
}
unsorted.Sort()
if !cmp.Equal(unsorted, expected) {
t.Errorf("Sort failed. Expected %v, got %v", expected, unsorted)
}
}

View File

@@ -226,3 +226,30 @@ func TestModelsAsMapStringInterface(t *testing.T) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, result)
}
}
func TestEngineSharedGroupMdlTableName(t *testing.T) {
testStruct := SharedGroupMdl{}
exp := utils.TBLTPSharedGroups
result := testStruct.TableName()
if !reflect.DeepEqual(exp, result) {
t.Errorf("\nExpected: <%+v>\nReceived: <%+v>", exp, result)
}
}
func TestEngineResourceMdlTableName(t *testing.T) {
testStruct := ResourceMdl{}
exp := utils.TBLTPResources
result := testStruct.TableName()
if !reflect.DeepEqual(exp, result) {
t.Errorf("\nExpected: <%+v>\nReceived: <%+v>", exp, result)
}
}
func TestEngineStatMdlTableName(t *testing.T) {
testStruct := StatMdl{}
exp := utils.TBLTPStats
result := testStruct.TableName()
if !reflect.DeepEqual(exp, result) {
t.Errorf("\nExpected: <%+v>\nReceived: <%+v>", exp, result)
}
}