mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add new unit tests on engine
This commit is contained in:
committed by
Dan Christian Bogos
parent
d02df49f53
commit
a945fccad6
@@ -19,6 +19,7 @@ package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -666,3 +667,113 @@ func TestBalancesValueFactorsGetValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestBalancesStringJson(t *testing.T) {
|
||||
balances := 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,
|
||||
},
|
||||
}
|
||||
|
||||
result := balances.String()
|
||||
if result == "" {
|
||||
t.Error("Expected non-empty JSON string, but got empty string")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBalanceFieldAsString(t *testing.T) {
|
||||
balance := &Balance{
|
||||
Uuid: "uuid123",
|
||||
}
|
||||
val, err := balance.FieldAsString([]string{"Uuid"})
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error for field 'Uuid': %v", err)
|
||||
}
|
||||
expected := balance.Uuid
|
||||
if val != expected {
|
||||
t.Errorf("Expected value '%s' for field 'Uuid', but got '%s'", expected, val)
|
||||
}
|
||||
_, err = balance.FieldAsString([]string{"InvalidField"})
|
||||
if err == nil {
|
||||
t.Error("Expected error for invalid field path, but got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBalancesString(t *testing.T) {
|
||||
balance := &Balance{
|
||||
Uuid: "123e4567-e89b-12d3-a456-426614174000",
|
||||
}
|
||||
jsonStr := balance.String()
|
||||
var data map[string]interface{}
|
||||
err := json.Unmarshal([]byte(jsonStr), &data)
|
||||
if err != nil {
|
||||
t.Errorf("Error unmarshalling JSON string: %v", err)
|
||||
}
|
||||
expectedUuid := balance.Uuid
|
||||
if uuid, ok := data["Uuid"].(string); !ok || uuid != expectedUuid {
|
||||
t.Errorf("Expected Uuid '%s' in JSON, but got '%v'", expectedUuid, data["Uuid"])
|
||||
}
|
||||
}
|
||||
func TestBalancesHasBalanceReturn(t *testing.T) {
|
||||
balances := Balances{
|
||||
{ID: "1"},
|
||||
{ID: "2"},
|
||||
{ID: "3"},
|
||||
}
|
||||
existingBalance := &Balance{ID: "2"}
|
||||
if !balances.HasBalance(existingBalance) {
|
||||
t.Errorf("Expected balance with ID '%s' to exist, but it does not", existingBalance.ID)
|
||||
}
|
||||
nonExistingBalance := &Balance{ID: "4"}
|
||||
if balances.HasBalance(nonExistingBalance) {
|
||||
t.Errorf("Expected balance with ID '%s' to not exist, but it does", nonExistingBalance.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBalancesEqual(t *testing.T) {
|
||||
balances1 := Balances{
|
||||
{ID: "1"},
|
||||
{ID: "2"},
|
||||
{ID: "3"},
|
||||
}
|
||||
balances2 := Balances{
|
||||
{ID: "1"},
|
||||
{ID: "2"},
|
||||
{ID: "3"},
|
||||
}
|
||||
if !balances1.Equal(balances1) {
|
||||
t.Errorf("Expected balances1 to equal itself, but it does not")
|
||||
}
|
||||
if !balances1.Equal(balances2) {
|
||||
t.Errorf("Expected balances1 to equal balances2, but they are not equal")
|
||||
}
|
||||
balances3 := Balances{
|
||||
{ID: "1"},
|
||||
{ID: "2"},
|
||||
}
|
||||
if balances1.Equal(balances3) {
|
||||
t.Errorf("Expected balances1 to not equal balances3, but they are equal")
|
||||
}
|
||||
balances4 := Balances{
|
||||
{ID: "1"},
|
||||
{ID: "2"},
|
||||
{ID: "4"},
|
||||
}
|
||||
if balances1.Equal(balances4) {
|
||||
t.Errorf("Expected balances1 to not equal balances4, but they are equal")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,12 +18,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
@@ -4842,3 +4844,135 @@ func TestECfieldAsInterfaceNilECCost(t *testing.T) {
|
||||
t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventCostSet(t *testing.T) {
|
||||
ec := &EventCost{}
|
||||
testCases := []struct {
|
||||
name string
|
||||
fldPath []string
|
||||
val interface{}
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "cgrates",
|
||||
fldPath: []string{"field1"},
|
||||
val: "value1",
|
||||
wantErr: utils.ErrNotImplemented,
|
||||
},
|
||||
{
|
||||
name: "cgrates2",
|
||||
fldPath: []string{"field2", "subfield"},
|
||||
val: 123,
|
||||
wantErr: utils.ErrNotImplemented,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
||||
err := ec.Set(tc.fldPath, tc.val)
|
||||
|
||||
if err != tc.wantErr {
|
||||
t.Errorf("Set() error = %v, wantErr %v", err, tc.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventCostRemove(t *testing.T) {
|
||||
ec := &EventCost{}
|
||||
testCases := []struct {
|
||||
name string
|
||||
fldPath []string
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "cgrates",
|
||||
fldPath: []string{"field1"},
|
||||
wantErr: utils.ErrNotImplemented,
|
||||
},
|
||||
{
|
||||
name: "cgrates2",
|
||||
fldPath: []string{"field2", "subfield"},
|
||||
wantErr: utils.ErrNotImplemented,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
||||
err := ec.Remove(tc.fldPath)
|
||||
|
||||
if err != tc.wantErr {
|
||||
t.Errorf("Remove() error = %v, wantErr %v", err, tc.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventCostGetKeys(t *testing.T) {
|
||||
ec := &EventCost{}
|
||||
testCases := []struct {
|
||||
name string
|
||||
nested bool
|
||||
nestedLimit int
|
||||
prefix string
|
||||
expectedKeys []string
|
||||
}{
|
||||
{
|
||||
name: "cgrates",
|
||||
nested: true,
|
||||
nestedLimit: 3,
|
||||
prefix: "prefix",
|
||||
expectedKeys: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
||||
keys := ec.GetKeys(tc.nested, tc.nestedLimit, tc.prefix)
|
||||
|
||||
if !reflect.DeepEqual(keys, tc.expectedKeys) {
|
||||
t.Errorf("GetKeys() returned %+v, expected %+v", keys, tc.expectedKeys)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvenCostProcessEventCostField(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
fldPath []string
|
||||
cd interface{}
|
||||
event map[string]interface{}
|
||||
expectedValue interface{}
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
name: "cgrates",
|
||||
fldPath: []string{"field1"},
|
||||
cd: nil,
|
||||
event: make(map[string]interface{}),
|
||||
expectedValue: nil,
|
||||
expectedErr: errors.New("unsupported field prefix: <field1>"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
||||
val, err := processEventCostField(tc.fldPath, tc.cd, tc.event)
|
||||
|
||||
if !reflect.DeepEqual(val, tc.expectedValue) {
|
||||
t.Errorf("ProcessEventCostField() returned %+v, expected %+v", val, tc.expectedValue)
|
||||
}
|
||||
|
||||
if (err == nil && tc.expectedErr != nil) || (err != nil && tc.expectedErr == nil) || (err != nil && tc.expectedErr != nil && err.Error() != tc.expectedErr.Error()) {
|
||||
t.Errorf("ProcessEventCostField() error = %v, expected error = %v", err, tc.expectedErr)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -667,3 +667,11 @@ func TestExportRequestSetFields2(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportRequestString(t *testing.T) {
|
||||
eeR := &ExportRequest{}
|
||||
jsonStr := eeR.String()
|
||||
if jsonStr == "" {
|
||||
t.Errorf("Expected non-empty JSON string, but got empty")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package engine
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -47,3 +48,11 @@ func TestSetDataStorage(t *testing.T) {
|
||||
}
|
||||
dm = tmp
|
||||
}
|
||||
|
||||
func TestGlobalvarsGetHTTPPstrTransport(t *testing.T) {
|
||||
httpPstrTransport = &http.Transport{}
|
||||
transport := GetHTTPPstrTransport()
|
||||
if transport == nil {
|
||||
t.Error("Expected transport to be initialized, but got nil")
|
||||
}
|
||||
}
|
||||
|
||||
110
engine/libchargers_test.go
Normal file
110
engine/libchargers_test.go
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
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 engine
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLibChargersSort(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input ChargerProfiles
|
||||
output ChargerProfiles
|
||||
}{
|
||||
{
|
||||
"AlreadySorted",
|
||||
ChargerProfiles{
|
||||
{Weight: 5},
|
||||
{Weight: 3},
|
||||
{Weight: 1},
|
||||
},
|
||||
ChargerProfiles{
|
||||
{Weight: 5},
|
||||
{Weight: 3},
|
||||
{Weight: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
"Unsorted",
|
||||
ChargerProfiles{
|
||||
{Weight: 1},
|
||||
{Weight: 5},
|
||||
{Weight: 3},
|
||||
},
|
||||
ChargerProfiles{
|
||||
{Weight: 5},
|
||||
{Weight: 3},
|
||||
{Weight: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
"AllSameWeight",
|
||||
ChargerProfiles{
|
||||
{Weight: 2},
|
||||
{Weight: 2},
|
||||
{Weight: 2},
|
||||
},
|
||||
ChargerProfiles{
|
||||
{Weight: 2},
|
||||
{Weight: 2},
|
||||
{Weight: 2},
|
||||
},
|
||||
},
|
||||
{
|
||||
"SingleElement",
|
||||
ChargerProfiles{
|
||||
{Weight: 4},
|
||||
},
|
||||
ChargerProfiles{
|
||||
{Weight: 4},
|
||||
},
|
||||
},
|
||||
{
|
||||
"Empty",
|
||||
ChargerProfiles{},
|
||||
ChargerProfiles{},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.input.Sort()
|
||||
if len(tt.input) != len(tt.output) {
|
||||
t.Errorf("expected length %d, got %d", len(tt.output), len(tt.input))
|
||||
}
|
||||
for i := range tt.input {
|
||||
if tt.input[i].Weight != tt.output[i].Weight {
|
||||
t.Errorf("at index %d, expected Weight %f, got %f", i, tt.output[i].Weight, tt.input[i].Weight)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibChargersTenantID(t *testing.T) {
|
||||
cp := &ChargerProfile{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "2012",
|
||||
}
|
||||
result := cp.TenantID()
|
||||
expected := "cgrates.org:2012"
|
||||
if result != expected {
|
||||
t.Errorf("TenantID() = %v, want %v", result, expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user