diff --git a/engine/balances_test.go b/engine/balances_test.go index 99112ec22..995ba9c9a 100644 --- a/engine/balances_test.go +++ b/engine/balances_test.go @@ -26,6 +26,7 @@ import ( "time" "github.com/cgrates/cgrates/utils" + "github.com/google/go-cmp/cmp" ) func TestBalanceSortPrecision(t *testing.T) { @@ -583,3 +584,50 @@ func TestBalancesMatchDestination(t *testing.T) { t.Errorf("MatchDestination(anydestination): expected true, got false for empty DestinationIDs map") } } + +func TestBalancesSetInitialValue(t *testing.T) { + t.Run("OldNotNull", func(t *testing.T) { + as := &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "1", Value: 100, Initial: 0}, + {UUID: "2", Value: 200, Initial: 0}, + }, + } + old := &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "1", Value: 50, Initial: 0}, + {UUID: "3", Value: 300, Initial: 0}, + }, + } + as.SetInitialValue(old) + expected := &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "1", Value: 100, Initial: 50}, + {UUID: "2", Value: 200, Initial: 0}, + {UUID: "3", Value: 0, Initial: 0}, + }, + } + if diff := cmp.Diff(as, expected); diff != "" { + t.Errorf("Unexpected result (-got +want):\n%s", diff) + } + }) + t.Run("OldNull", func(t *testing.T) { + as := &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "1", Value: 100, Initial: 0}, + {UUID: "2", Value: 200, Initial: 0}, + }, + } + as.SetInitialValue(nil) + expected := &AccountSummary{ + BalanceSummaries: BalanceSummaries{ + {UUID: "1", Value: 100, Initial: 0}, + {UUID: "2", Value: 200, Initial: 0}, + }, + } + + if diff := cmp.Diff(as, expected); diff != "" { + t.Errorf("Unexpected result (-got +want):\n%s", diff) + } + }) +} diff --git a/engine/libindex_test.go b/engine/libindex_test.go index 01d2f0e7e..84e29413b 100644 --- a/engine/libindex_test.go +++ b/engine/libindex_test.go @@ -18,7 +18,13 @@ along with this program. If not, see package engine -import "testing" +import ( + "errors" + "testing" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" +) func TestLibIndexIsDynamicDPPath(t *testing.T) { tests := []struct { @@ -38,3 +44,68 @@ func TestLibIndexIsDynamicDPPath(t *testing.T) { }) } } + +func TestLibIndexRemoveFilterIndexesForFilterErrnotFound(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + dataDB := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) + tntCtx := "cgrates.org:*sessions" + test := struct { + name string + idx map[string]utils.StringSet + keys []string + itemIDs utils.StringSet + }{ + name: "testing for ErrNotFound", + idx: map[string]utils.StringSet{ + "*string:~*req.Account:1001": utils.NewStringSet([]string{"AP1", "AP2"}), + "*string:~*req.Account:1002": utils.NewStringSet([]string{"AP1", "AP2"}), + }, + keys: []string{"*string:~*req.Account:1001"}, + itemIDs: utils.NewStringSet([]string{"AP1", "AP2"}), + } + t.Run(test.name, func(t *testing.T) { + t.Cleanup(func() { + if err := dataDB.Flush(""); err != nil { + t.Logf("failed to flush dataDB: %v", err) + } + }) + if err := dm.SetIndexes(utils.CacheAttributeFilterIndexes, tntCtx, test.idx, true, ""); err != nil { + t.Fatalf("dm.SetIndexes() returned unexpected error: %v", err) + } + err := removeFilterIndexesForFilter(dm, utils.CacheAttributeFilterIndexes, tntCtx, test.keys, test.itemIDs) + if err != nil && !errors.Is(err, utils.ErrNotFound) { + t.Fatalf("Expected error %v, got %v", utils.ErrNotFound, err) + } + }) +} + +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", + }, + { + name: "another invalid index key format with less than 4 parts", + tntCtxIdxKey: "another:invalid:key", + wantErr: "WRONG_IDX_KEY_FORMAT", + }, + } + 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()) + } + }) + } +}