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
f84e7c29a3
commit
8edc239bd1
75
engine/libchargers_test.go
Normal file
75
engine/libchargers_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
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 (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestChargerProfileTenantID(t *testing.T) {
|
||||
profile1 := ChargerProfile{
|
||||
Tenant: "cgrates1",
|
||||
ID: "2012",
|
||||
}
|
||||
expectedID1 := "cgrates1:2012"
|
||||
got1 := profile1.TenantID()
|
||||
if got1 != expectedID1 {
|
||||
t.Errorf("TenantID() = %v, want %v", got1, expectedID1)
|
||||
}
|
||||
profile2 := ChargerProfile{
|
||||
Tenant: "cgrates2",
|
||||
ID: "2012",
|
||||
}
|
||||
expectedID2 := "cgrates2:2012"
|
||||
got2 := profile2.TenantID()
|
||||
if got2 != expectedID2 {
|
||||
t.Errorf("TenantID() = %v, want %v", got2, expectedID2)
|
||||
}
|
||||
}
|
||||
func TestChargerProfilesSort(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
profiles ChargerProfiles
|
||||
want ChargerProfiles
|
||||
}{
|
||||
{
|
||||
name: "Sort descending",
|
||||
profiles: ChargerProfiles{
|
||||
{Weight: 5},
|
||||
{Weight: 10},
|
||||
{Weight: 1},
|
||||
},
|
||||
want: ChargerProfiles{
|
||||
{Weight: 10},
|
||||
{Weight: 5},
|
||||
{Weight: 1},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.profiles.Sort()
|
||||
if !reflect.DeepEqual(tt.profiles, tt.want) {
|
||||
t.Errorf("Sort() = %v, want %v", tt.profiles, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -240,3 +240,39 @@ func TestVersionCompare2(t *testing.T) {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetRoundingDecimals(t *testing.T) {
|
||||
initialRoundingDecimals := globalRoundingDecimals
|
||||
testCases := []struct {
|
||||
name string
|
||||
roundingDec int
|
||||
expected int
|
||||
}{
|
||||
{name: "Positive rounding decimals", roundingDec: 2., expected: 2},
|
||||
{name: "Zero rounding decimals", roundingDec: 0, expected: 0},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
SetRoundingDecimals(tc.roundingDec)
|
||||
if globalRoundingDecimals != tc.expected {
|
||||
t.Errorf("Expected rounding decimals to be %d, got %d", tc.expected, globalRoundingDecimals)
|
||||
}
|
||||
globalRoundingDecimals = initialRoundingDecimals
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallDescriptor_AddRatingInfo(t *testing.T) {
|
||||
initialRatingInfos := []*RatingInfo{}
|
||||
cd := &CallDescriptor{RatingInfos: initialRatingInfos}
|
||||
ratingInfo1 := &RatingInfo{}
|
||||
ratingInfo2 := &RatingInfo{}
|
||||
cd.AddRatingInfo(ratingInfo1, ratingInfo2)
|
||||
expectedLength := len(initialRatingInfos) + 2
|
||||
if len(cd.RatingInfos) != expectedLength {
|
||||
t.Errorf("Expected RatingInfos length to be %d, got %d", expectedLength, len(cd.RatingInfos))
|
||||
}
|
||||
if cd.RatingInfos[0] != ratingInfo1 || cd.RatingInfos[1] != ratingInfo2 {
|
||||
t.Errorf("Added RatingInfo objects don't match expectations")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -481,3 +481,26 @@ func TestStructUpdateStructWithIfaceMap(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToMapMapStringInterface(t *testing.T) {
|
||||
|
||||
type TestStruct struct {
|
||||
Field1 string
|
||||
Field2 int
|
||||
Field3 bool
|
||||
}
|
||||
input := TestStruct{
|
||||
Field1: "value1",
|
||||
Field2: 42,
|
||||
Field3: true,
|
||||
}
|
||||
expected := map[string]any{
|
||||
"Field1": "value1",
|
||||
"Field2": 42,
|
||||
"Field3": true,
|
||||
}
|
||||
output := ToMapMapStringInterface(input)
|
||||
if !reflect.DeepEqual(output, expected) {
|
||||
t.Errorf("expected %v, got %v", expected, output)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user