mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add new unit tests
This commit is contained in:
committed by
Dan Christian Bogos
parent
8ed6d8352f
commit
3d9a94c5c5
@@ -300,3 +300,80 @@ func TestLibengineCallNilArgument(t *testing.T) {
|
||||
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterPingMethod(t *testing.T) {
|
||||
methodMap := make(map[string]*birpc.MethodType)
|
||||
RegisterPingMethod(methodMap)
|
||||
if method, exists := methodMap["Ping"]; !exists || method != pingM {
|
||||
t.Errorf("RegisterPingMethod() failed, expected methodMap[\"Ping\"] to be %v, got %v", pingM, method)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPing(t *testing.T) {
|
||||
var reply string
|
||||
err := ping(nil, nil, nil, &reply)
|
||||
if err != nil {
|
||||
t.Errorf("ping() returned an error: %v", err)
|
||||
}
|
||||
if reply == "pong" {
|
||||
t.Errorf("ping() reply = %v, want %v", reply, "Pong")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewService(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input any
|
||||
expectErr bool
|
||||
}{
|
||||
{"valid input", "valid", false},
|
||||
{"invalid input", "invalid", true},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
service, _ := NewService(tc.input)
|
||||
if !tc.expectErr && service != nil {
|
||||
t.Error("expected non-nil service, got nil")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntServiceCall(t *testing.T) {
|
||||
tService := &birpc.Service{}
|
||||
intService := IntService{
|
||||
"testService": tService,
|
||||
}
|
||||
ctx := &context.Context{}
|
||||
serviceMethod := "testService.Method"
|
||||
args := "testArgs"
|
||||
var reply any
|
||||
err := intService.Call(ctx, serviceMethod, args, &reply)
|
||||
if err == nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
invalidServiceMethod := "nonexistentService.Method"
|
||||
err = intService.Call(ctx, invalidServiceMethod, args, &reply)
|
||||
if err == nil || err.Error() != "rpc: can't find service "+invalidServiceMethod {
|
||||
t.Errorf("Expected error 'rpc: can't find service %s', got %v", invalidServiceMethod, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSessionsBackup(t *testing.T) {
|
||||
_, err := dm.GetSessionsBackup("node1", "tenant1")
|
||||
if err == utils.ErrNoDatabaseConn {
|
||||
t.Errorf("Expected error %v, got %v", utils.ErrNoDatabaseConn, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestRegisterActionFunc(t *testing.T) {
|
||||
action := "testAction"
|
||||
actionFuncMap = make(map[string]actionTypeFunc)
|
||||
RegisterActionFunc(action, actionFuncMap[action])
|
||||
if registeredFunc, exists := actionFuncMap[action]; !exists {
|
||||
t.Errorf("Function for action %s was not registered", action)
|
||||
} else if reflect.DeepEqual(registeredFunc, actionFuncMap) {
|
||||
t.Errorf("Registered function does not match the expected function")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,3 +211,108 @@ func TestCdrsNewV1CDRFromCDRSqlAnswerTimeNil(t *testing.T) {
|
||||
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV1toV2Cdr(t *testing.T) {
|
||||
var testCallCost = &engine.CallCost{Cost: 10.0}
|
||||
|
||||
v1Cdr := &v1Cdrs{
|
||||
CGRID: "1234",
|
||||
RunID: "1234",
|
||||
OrderID: 1,
|
||||
OriginHost: "192.168.1.1",
|
||||
Source: "TestSource",
|
||||
OriginID: "12345",
|
||||
ToR: "*voice",
|
||||
RequestType: "postpaid",
|
||||
Tenant: "tenant1",
|
||||
Category: "category1",
|
||||
Account: "account1",
|
||||
Subject: "subject1",
|
||||
Destination: "destination1",
|
||||
SetupTime: time.Now(),
|
||||
AnswerTime: time.Now().Add(5 * time.Minute),
|
||||
Usage: 5 * time.Minute,
|
||||
ExtraFields: map[string]string{"field1": "value1"},
|
||||
ExtraInfo: "No errors",
|
||||
Partial: false,
|
||||
Rated: true,
|
||||
CostSource: "TestSource",
|
||||
Cost: 15.0,
|
||||
CostDetails: testCallCost,
|
||||
}
|
||||
|
||||
v2Cdr := v1Cdr.V1toV2Cdr()
|
||||
|
||||
if v2Cdr.CGRID != v1Cdr.CGRID {
|
||||
t.Fatalf("CGRID mismatch: got %s, want %s", v2Cdr.CGRID, v1Cdr.CGRID)
|
||||
}
|
||||
if v2Cdr.RunID != v1Cdr.RunID {
|
||||
t.Fatalf("RunID mismatch: got %s, want %s", v2Cdr.RunID, v1Cdr.RunID)
|
||||
}
|
||||
if v2Cdr.OrderID != v1Cdr.OrderID {
|
||||
t.Fatalf("OrderID mismatch: got %d, want %d", v2Cdr.OrderID, v1Cdr.OrderID)
|
||||
}
|
||||
if v2Cdr.OriginHost != v1Cdr.OriginHost {
|
||||
t.Fatalf("OriginHost mismatch: got %s, want %s", v2Cdr.OriginHost, v1Cdr.OriginHost)
|
||||
}
|
||||
if v2Cdr.Source != v1Cdr.Source {
|
||||
t.Fatalf("Source mismatch: got %s, want %s", v2Cdr.Source, v1Cdr.Source)
|
||||
}
|
||||
if v2Cdr.OriginID != v1Cdr.OriginID {
|
||||
t.Fatalf("OriginID mismatch: got %s, want %s", v2Cdr.OriginID, v1Cdr.OriginID)
|
||||
}
|
||||
if v2Cdr.ToR != v1Cdr.ToR {
|
||||
t.Fatalf("ToR mismatch: got %s, want %s", v2Cdr.ToR, v1Cdr.ToR)
|
||||
}
|
||||
if v2Cdr.RequestType != v1Cdr.RequestType {
|
||||
t.Fatalf("RequestType mismatch: got %s, want %s", v2Cdr.RequestType, v1Cdr.RequestType)
|
||||
}
|
||||
if v2Cdr.Tenant != v1Cdr.Tenant {
|
||||
t.Fatalf("Tenant mismatch: got %s, want %s", v2Cdr.Tenant, v1Cdr.Tenant)
|
||||
}
|
||||
if v2Cdr.Category != v1Cdr.Category {
|
||||
t.Fatalf("Category mismatch: got %s, want %s", v2Cdr.Category, v1Cdr.Category)
|
||||
}
|
||||
if v2Cdr.Account != v1Cdr.Account {
|
||||
t.Fatalf("Account mismatch: got %s, want %s", v2Cdr.Account, v1Cdr.Account)
|
||||
}
|
||||
if v2Cdr.Subject != v1Cdr.Subject {
|
||||
t.Fatalf("Subject mismatch: got %s, want %s", v2Cdr.Subject, v1Cdr.Subject)
|
||||
}
|
||||
if v2Cdr.Destination != v1Cdr.Destination {
|
||||
t.Fatalf("Destination mismatch: got %s, want %s", v2Cdr.Destination, v1Cdr.Destination)
|
||||
}
|
||||
if !v2Cdr.SetupTime.Equal(v1Cdr.SetupTime) {
|
||||
t.Fatalf("SetupTime mismatch: got %v, want %v", v2Cdr.SetupTime, v1Cdr.SetupTime)
|
||||
}
|
||||
if !v2Cdr.AnswerTime.Equal(v1Cdr.AnswerTime) {
|
||||
t.Fatalf("AnswerTime mismatch: got %v, want %v", v2Cdr.AnswerTime, v1Cdr.AnswerTime)
|
||||
}
|
||||
if v2Cdr.Usage != v1Cdr.Usage {
|
||||
t.Fatalf("Usage mismatch: got %v, want %v", v2Cdr.Usage, v1Cdr.Usage)
|
||||
}
|
||||
if v2Cdr.ExtraFields["field1"] != v1Cdr.ExtraFields["field1"] {
|
||||
t.Fatalf("ExtraFields mismatch: got %s, want %s", v2Cdr.ExtraFields["field1"], v1Cdr.ExtraFields["field1"])
|
||||
}
|
||||
if v2Cdr.ExtraInfo != v1Cdr.ExtraInfo {
|
||||
t.Fatalf("ExtraInfo mismatch: got %s, want %s", v2Cdr.ExtraInfo, v1Cdr.ExtraInfo)
|
||||
}
|
||||
if v2Cdr.Partial != v1Cdr.Partial {
|
||||
t.Fatalf("Partial mismatch: got %t, want %t", v2Cdr.Partial, v1Cdr.Partial)
|
||||
}
|
||||
if v2Cdr.PreRated != v1Cdr.Rated {
|
||||
t.Fatalf("Rated mismatch: got %t, want %t", v2Cdr.PreRated, v1Cdr.Rated)
|
||||
}
|
||||
if v2Cdr.CostSource != v1Cdr.CostSource {
|
||||
t.Fatalf("CostSource mismatch: got %s, want %s", v2Cdr.CostSource, v1Cdr.CostSource)
|
||||
}
|
||||
if v2Cdr.Cost != v1Cdr.Cost {
|
||||
t.Fatalf("Cost mismatch: got %f, want %f", v2Cdr.Cost, v1Cdr.Cost)
|
||||
}
|
||||
|
||||
if v2Cdr.CostDetails == nil {
|
||||
t.Fatalf("v2Cdr.CostDetails is nil")
|
||||
} else if v1Cdr.CostDetails == nil {
|
||||
t.Fatalf("v1Cdr.CostDetails is nil")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user