Add coverage tests on apier/v1

This commit is contained in:
armirveliaj
2024-08-28 09:59:40 -04:00
committed by Dan Christian Bogos
parent 0a5a7b7d61
commit ddb25433f7
3 changed files with 263 additions and 0 deletions

122
apier/v1/apier_test.go Normal file
View File

@@ -0,0 +1,122 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package v1
import (
"testing"
"github.com/cgrates/cgrates/utils"
)
func TestCheckDefaultTiming(t *testing.T) {
tests := []struct {
name string
tStr string
wantID string
wantIsDef bool
}{
{"Every Minute", utils.MetaEveryMinute, utils.MetaEveryMinute, true},
{"Hourly", utils.MetaHourly, utils.MetaHourly, true},
{"Daily", utils.MetaDaily, utils.MetaDaily, true},
{"Weekly", utils.MetaWeekly, utils.MetaWeekly, true},
{"Monthly", utils.MetaMonthly, utils.MetaMonthly, true},
{"Monthly Estimated", utils.MetaMonthlyEstimated, utils.MetaMonthlyEstimated, true},
{"Month End", utils.MetaMonthEnd, utils.MetaMonthEnd, true},
{"Yearly", utils.MetaYearly, utils.MetaYearly, true},
{"Unknown", "unknown", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, isDef := checkDefaultTiming(tt.tStr)
if isDef != tt.wantIsDef {
t.Errorf("checkDefaultTiming(%q) isDefault = %v, want %v", tt.tStr, isDef, tt.wantIsDef)
}
if isDef && got.ID != tt.wantID {
t.Errorf("checkDefaultTiming(%q) got.ID = %v, want %v", tt.tStr, got.ID, tt.wantID)
}
if !isDef && got != nil {
t.Errorf("checkDefaultTiming(%q) expected nil, got non-nil", tt.tStr)
}
})
}
}
func TestGetId(t *testing.T) {
tests := []struct {
name string
attr AttrRemoveRatingProfile
expectedID string
}{
{
name: "All fields provided",
attr: AttrRemoveRatingProfile{
Tenant: "cgrates.org",
Category: "category1",
Subject: "subject1",
},
expectedID: "*out:cgrates.org:category1:subject1",
},
{
name: "Empty Tenant and Category",
attr: AttrRemoveRatingProfile{
Tenant: utils.EmptyString,
Category: utils.EmptyString,
Subject: "subject1",
},
expectedID: "*out:",
},
{
name: "Tenant and Category are MetaAny",
attr: AttrRemoveRatingProfile{
Tenant: utils.MetaAny,
Category: utils.MetaAny,
Subject: "subject1",
},
expectedID: "*out:",
},
{
name: "Only Subject provided",
attr: AttrRemoveRatingProfile{
Tenant: utils.EmptyString,
Category: utils.EmptyString,
Subject: "subject1",
},
expectedID: "*out:",
},
{
name: "No fields provided",
attr: AttrRemoveRatingProfile{
Tenant: utils.EmptyString,
Category: utils.EmptyString,
Subject: utils.EmptyString,
},
expectedID: "*out:",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.attr.GetId()
if result != tt.expectedID {
t.Errorf("expected %v, but got %v", tt.expectedID, result)
}
})
}
}

View File

@@ -0,0 +1,84 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package v1
import (
"testing"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/dispatchers"
"github.com/cgrates/cgrates/utils"
)
func TestPing(t *testing.T) {
dispatcher := &DispatcherSv1{}
ctx := context.Background()
var reply string
err := dispatcher.Ping(ctx, nil, &reply)
if err != nil {
t.Errorf("Ping method returned an error: %v", err)
}
if reply != utils.Pong {
t.Errorf("Expected reply %s, got %s", utils.Pong, reply)
}
}
func TestNewDispatcherEeSv1(t *testing.T) {
dispatcherService := &dispatchers.DispatcherService{}
dispatcher := NewDispatcherEeSv1(dispatcherService)
if dispatcher == nil {
t.Fatal("Expected NewDispatcherEeSv1 to return a non-nil DispatcherEeSv1")
}
if dispatcher.dS != dispatcherService {
t.Errorf("Expected dS to be %v, got %v", dispatcherService, dispatcher.dS)
}
}
func TestNewDispatcherCoreSv1(t *testing.T) {
mockService := &dispatchers.DispatcherService{}
dispatcher := NewDispatcherCoreSv1(mockService)
if dispatcher == nil {
t.Fatal("Expected dispatcher to be non-nil")
}
if dispatcher.dS != mockService {
t.Errorf("Expected dispatcher.dS to be %v, got %v", mockService, dispatcher.dS)
}
}
func TestNewDispatcherSv1(t *testing.T) {
mockDispatcherService := &dispatchers.DispatcherService{}
dispatcher := NewDispatcherSv1(mockDispatcherService)
if dispatcher == nil {
t.Fatal("Expected a non-nil DispatcherSv1, got nil")
}
if dispatcher.dS != mockDispatcherService {
t.Errorf("Expected DispatcherService to be %v, got %v", mockDispatcherService, dispatcher.dS)
}
}
func TestNewDispatcherErSv1(t *testing.T) {
mockDispatcherService := &dispatchers.DispatcherService{}
dispatcherErSv1 := NewDispatcherErSv1(mockDispatcherService)
if dispatcherErSv1 == nil {
t.Fatal("Expected a non-nil DispatcherErSv1, got nil")
}
if dispatcherErSv1.dS != mockDispatcherService {
t.Errorf("Expected DispatcherService to be %v, got %v", mockDispatcherService, dispatcherErSv1.dS)
}
}

View File

@@ -0,0 +1,57 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package v1
import (
"testing"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/utils"
)
func TestSchedulerSv1Ping(t *testing.T) {
scheduler := &SchedulerSv1{}
ctx := context.Background()
ign := &utils.CGREvent{}
reply := ""
err := scheduler.Ping(ctx, ign, &reply)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if reply != utils.Pong {
t.Errorf("Expected reply to be %v, got %v", utils.Pong, reply)
}
}
func TestSchedulerSv1Call(t *testing.T) {
scheduler := &SchedulerSv1{}
ctx := context.Background()
serviceMethod := "ServiceMethod"
args := "Args"
var reply string
err := scheduler.Call(ctx, serviceMethod, args, &reply)
if err == nil {
t.Fatalf("Expected error")
}
}