Revise and add new unit tests on agents

This commit is contained in:
armirveliaj
2024-06-13 10:28:01 -04:00
committed by Dan Christian Bogos
parent 49f6c5982e
commit 5dfb49db71
2 changed files with 99 additions and 28 deletions

View File

@@ -1338,3 +1338,60 @@ func TestFSEventGetOptions(t *testing.T) {
})
}
}
func TestFseventGetADC(t *testing.T) {
type testCase struct {
name string
fsev FSEvent
fieldName string
expect time.Duration
err error
}
testCases := []testCase{
{
name: "ACD from Var-CGR-ACD with seconds",
fsev: FSEvent{"Var-CGR-ACD": "1640"},
fieldName: "ACD",
expect: 0 * time.Second,
},
{
name: "ACD from Var-CGR-ACD with empty string",
fsev: FSEvent{"Var-CGR-ACD": ""},
fieldName: "ACD",
expect: 0,
err: nil,
},
{
name: "Static value prefixed field",
fsev: FSEvent{"static_value": "30s"},
fieldName: "static_value",
expect: 30 * time.Second,
},
{
name: "Non-existent field",
fsev: FSEvent{},
fieldName: "non_existent",
expect: 0,
err: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, err := tc.fsev.GetADC(tc.fieldName)
if tc.err != nil {
if err == nil {
t.Errorf("Expected error: %v, got none", tc.err)
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
if got != tc.expect {
t.Errorf("Expected duration: %v, got: %v", tc.expect, got)
}
})
}
}

View File

@@ -243,48 +243,62 @@ func TestLibdnsNewDnsReply(t *testing.T) {
}
}
func TestLibDnsDPString(t *testing.T) {
want := "{\"key\":\"test\"}"
dp := dnsDP{
req: utils.MapStorage{
"key": "test",
},
}
got := dp.String()
if got != want {
t.Errorf("Expected String() to return %q, got %q", want, got)
}
}
func TestLibdnsUpdateDnsRRHeader(t *testing.T) {
type testCase struct {
name string
v *dns.RR_Header
path []string
value interface{}
err error
name string
rrHeader dns.RR_Header
path []string
value any
wantErr bool
expected dns.RR_Header
}
testCases := []testCase{
{
name: "Update Name",
v: &dns.RR_Header{},
path: []string{utils.DNSName},
value: "cgrates.org",
name: "Update Rrtype (invalid value)",
rrHeader: dns.RR_Header{},
path: []string{utils.DNSRrtype},
value: "invalid",
wantErr: true,
},
{
name: "Update Rrtype (valid)",
v: &dns.RR_Header{},
path: []string{utils.DNSRrtype},
value: 1,
name: "Update invalid path",
rrHeader: dns.RR_Header{},
path: []string{"invalid_path"},
value: "test",
wantErr: true,
},
{
name: "Wrong path",
v: &dns.RR_Header{},
path: []string{"invalid"},
value: "cgrates.org",
err: utils.ErrWrongPath,
},
{
name: "Empty path",
v: &dns.RR_Header{},
path: []string{},
value: "cgrates.org",
err: utils.ErrWrongPath,
name: "Empty path",
rrHeader: dns.RR_Header{},
path: []string{},
value: "test",
wantErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := updateDnsRRHeader(tc.v, tc.path, tc.value)
if err != tc.err {
t.Errorf("Expected error %v, got %v", tc.err, err)
originalHeader := tc.rrHeader
err := updateDnsRRHeader(&tc.rrHeader, tc.path, tc.value)
if tc.rrHeader != originalHeader {
t.Errorf("Expected original header to remain unchanged, got changed header")
}
if (err != nil) != tc.wantErr {
t.Errorf("Unexpected error: %v", err)
}
})
}
}