mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Revise and add new test case on engine
This commit is contained in:
committed by
Dan Christian Bogos
parent
336b85e5dc
commit
a4edc5dfca
@@ -31,6 +31,7 @@ import (
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -3446,3 +3447,102 @@ func TestAccountSummaryString(t *testing.T) {
|
||||
t.Errorf("Error unmarshalling result: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountProcessAccountSummaryField(t *testing.T) {
|
||||
type args struct {
|
||||
fldPath []string
|
||||
accSummary any
|
||||
event map[string]any
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want any
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Direct access for *AccountSummary (Tenant)",
|
||||
args: args{
|
||||
fldPath: []string{"Tenant"},
|
||||
accSummary: &AccountSummary{Tenant: "test_tenant", ID: "id1"},
|
||||
event: make(map[string]any),
|
||||
},
|
||||
want: "test_tenant",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Unmarshal with ValueFactors field (expecting error on missing factor)",
|
||||
args: args{
|
||||
fldPath: []string{"BalanceSummaries", "0", "Factors", "factor1"},
|
||||
accSummary: `{"BalanceSummaries": [{"UUID": "summary1", "Factors": {"factor1": 0.2}}]}`,
|
||||
event: make(map[string]any),
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Error on missing ValueFactors field",
|
||||
args: args{
|
||||
fldPath: []string{"BalanceSummaries", "0", "Factors", "missing_factor"},
|
||||
accSummary: `{"BalanceSummaries": [{"UUID": "summary1"}]}`,
|
||||
event: make(map[string]any),
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := processAccountSummaryField(tt.args.fldPath, tt.args.accSummary, tt.args.event)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("processAccountSummaryField error = %v, wantErr = %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if diff := cmp.Diff(tt.want, got); diff != "" && !tt.wantErr {
|
||||
t.Errorf("processAccountSummaryField mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessAccountSummaryFieldNonStringAccSummary(t *testing.T) {
|
||||
type args struct {
|
||||
fldPath []string
|
||||
accSummary any
|
||||
event map[string]any
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Error on non-string accSummary (marshal)",
|
||||
args: args{
|
||||
fldPath: []string{"Tenant"},
|
||||
accSummary: map[string]string{"Name": "Test Account"},
|
||||
event: make(map[string]any),
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := processAccountSummaryField(tt.args.fldPath, tt.args.accSummary, tt.args.event)
|
||||
if (err == nil) != tt.wantErr {
|
||||
t.Errorf("processAccountSummaryField error = %v, wantErr = %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountFieldAsInterfaceNilAccount(t *testing.T) {
|
||||
var acc *Account
|
||||
fldPath := []string{"ID"}
|
||||
_, err := acc.FieldAsInterface(fldPath)
|
||||
if err != utils.ErrNotFound {
|
||||
t.Errorf("Expected error %v, got %v", utils.ErrNotFound, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,31 +81,31 @@ func TestLibIndexRemoveFilterIndexesForFilterErrnotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLibIndexSplitFilterIndexErrWrongIdxKeyFormat(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tntCtxIdxKey string
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "invalid index key format with less than 4 parts",
|
||||
tntCtxIdxKey: "invalid:key",
|
||||
wantErr: "WRONG_IDX_KEY_FORMAT<invalid:key>",
|
||||
},
|
||||
{
|
||||
name: "another invalid index key format with less than 4 parts",
|
||||
tntCtxIdxKey: "another:invalid:key",
|
||||
wantErr: "WRONG_IDX_KEY_FORMAT<another:invalid:key>",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, _, err := splitFilterIndex(tt.tntCtxIdxKey)
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error but got nil")
|
||||
}
|
||||
if err.Error() != tt.wantErr {
|
||||
t.Fatalf("Expected error %v, but got %v", tt.wantErr, err.Error())
|
||||
}
|
||||
})
|
||||
tntCtxIdxKey := "invalid:key"
|
||||
expectedErr := "WRONG_IDX_KEY_FORMAT<invalid:key>"
|
||||
_, _, err := splitFilterIndex(tntCtxIdxKey)
|
||||
if err == nil || err.Error() != expectedErr {
|
||||
t.Fatalf("Expected error %v, got %v", expectedErr, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibIndexNewFilterIndexGetFilterErrNotFound(t *testing.T) {
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
dataDB := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
|
||||
dm := NewDataManager(dataDB, cfg.CacheCfg(), nil)
|
||||
tnt := "tenant"
|
||||
ctx := "context"
|
||||
itemID := "item1"
|
||||
filterIDs := []string{"nonexistent_filter"}
|
||||
idxItmType := "indexItemType"
|
||||
newFlt := &Filter{
|
||||
Tenant: tnt,
|
||||
ID: "filter1",
|
||||
Rules: []*FilterRule{},
|
||||
}
|
||||
_, err := newFilterIndex(dm, idxItmType, tnt, ctx, itemID, filterIDs, newFlt)
|
||||
expectedErr := "broken reference to filter: nonexistent_filter for itemType: indexItemType and ID: item1"
|
||||
if err == nil || err.Error() != expectedErr {
|
||||
t.Fatalf("Expected error %v, got %v", expectedErr, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user