mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-23 16:18:44 +05:00
Revise unit tests in engine
This commit is contained in:
committed by
Dan Christian Bogos
parent
70f4f44421
commit
58dc0f0ae2
@@ -3335,10 +3335,91 @@ func TestEngineToStringJSON(t *testing.T) {
|
||||
}
|
||||
want, err := json.Marshal(acc)
|
||||
if err != nil {
|
||||
t.Errorf("Error marshalling Account to JSON: %v", err)
|
||||
t.Error(err)
|
||||
}
|
||||
got := acc.String()
|
||||
if got != string(want) {
|
||||
t.Errorf("Expected JSON: %s, got: %s", want, got)
|
||||
t.Errorf("acc.String()=%s, want%s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEngineGetID(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
accID string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Valid ID format",
|
||||
accID: "prefix" + utils.ConcatenatedKeySep + "suffix",
|
||||
want: "suffix",
|
||||
},
|
||||
{
|
||||
name: "Invalid ID format (missing separator)",
|
||||
accID: "invalidID",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "Invalid ID format (too many parts)",
|
||||
accID: "prefix" + utils.ConcatenatedKeySep + "suffix" + utils.ConcatenatedKeySep + "extra",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "Empty ID",
|
||||
accID: "",
|
||||
want: "",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
acc := &Account{ID: tt.accID}
|
||||
got := acc.GetID()
|
||||
if got != tt.want {
|
||||
t.Errorf("Expected ID: %s, Got: %s", tt.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEngineNewAccountSummaryFromJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
jsonStr string
|
||||
want *AccountSummary
|
||||
}{
|
||||
{
|
||||
name: "Valid JSON",
|
||||
jsonStr: `{"tenant": "cgrates.org", "id": "1234", "balanceSummaries": [], "allowNegative": false, "disabled": true}`,
|
||||
want: &AccountSummary{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "1234",
|
||||
BalanceSummaries: BalanceSummaries{},
|
||||
AllowNegative: false,
|
||||
Disabled: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Empty JSON",
|
||||
jsonStr: "",
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "Null JSON",
|
||||
jsonStr: "null",
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "Invalid JSON",
|
||||
jsonStr: "{invalid: json}",
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, _ := NewAccountSummaryFromJSON(tt.jsonStr)
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("NewAccountSummaryFromJSON(%q) got = %v, want %v", tt.jsonStr, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,12 +18,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package engine
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func TestGetRatingProfileForPrefix(t *testing.T) {
|
||||
@@ -479,14 +479,13 @@ func TestEngineSwapRpasIndex(t *testing.T) {
|
||||
FallbackKeys: []string{"key3"},
|
||||
}
|
||||
initialRPAs := RatingPlanActivations{plan1, plan2}
|
||||
index1 := 0
|
||||
index2 := 1
|
||||
want := RatingPlanActivations{plan2, plan1}
|
||||
rpas := make(RatingPlanActivations, len(initialRPAs))
|
||||
copy(rpas, initialRPAs)
|
||||
rpas.Swap(index1, index2)
|
||||
if !reflect.DeepEqual(rpas, want) {
|
||||
t.Errorf("Expected RatingPlanActivations after swap: %v, got: %v", want, rpas)
|
||||
rpas.Swap(0, 1)
|
||||
diff := cmp.Diff(rpas, want)
|
||||
if diff != "" {
|
||||
t.Errorf("Expected RatingPlanActivations after swap to be equal to want: (-got, +want)\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -957,7 +957,7 @@ func TestEngineCounterFilterString(t *testing.T) {
|
||||
}
|
||||
want, err := json.Marshal(testFilter)
|
||||
if err != nil {
|
||||
t.Errorf("Error marshalling CounterFilter to JSON: %v", err)
|
||||
t.Error(err)
|
||||
}
|
||||
got := testFilter.String()
|
||||
if got != string(want) {
|
||||
@@ -971,7 +971,7 @@ func TestEngineUnitCounterString(t *testing.T) {
|
||||
}
|
||||
want, err := json.Marshal(testCounter)
|
||||
if err != nil {
|
||||
t.Errorf("Error marshalling UnitCounter to JSON: %v", err)
|
||||
t.Error(err)
|
||||
}
|
||||
got := testCounter.String()
|
||||
if got != string(want) {
|
||||
|
||||
Reference in New Issue
Block a user