diff --git a/engine/account.go b/engine/account.go index 3ff8d206c..69598ed6e 100644 --- a/engine/account.go +++ b/engine/account.go @@ -302,21 +302,30 @@ func (ub *Account) getBalancesForPrefix(prefix, category, direction, tor string, continue } b.account = ub + if len(b.DestinationIDs) > 0 && b.DestinationIDs[utils.ANY] == false { for _, p := range utils.SplitPrefix(prefix, MIN_PREFIX_MATCH) { if x, err := cache2go.Get(utils.DESTINATION_PREFIX + p); err == nil { destIds := x.(map[interface{}]struct{}) foundResult := false - includeDest := true // wheter it is excluded or included + allInclude := true // wheter it is excluded or included for dId, _ := range destIds { inclDest, found := b.DestinationIDs[dId.(string)] if found { foundResult = true - includeDest = includeDest && inclDest + allInclude = allInclude && inclDest } } - if foundResult { - if includeDest { + // check wheter all destination ids in the balance were exclusions + allExclude := true + for _, inclDest := range b.DestinationIDs { + if inclDest { + allExclude = false + break + } + } + if foundResult || allExclude { + if allInclude { b.precision = len(p) usefulBalances = append(usefulBalances, b) } else { diff --git a/engine/account_test.go b/engine/account_test.go index 8a7c56bac..6cedc5995 100644 --- a/engine/account_test.go +++ b/engine/account_test.go @@ -1721,6 +1721,75 @@ func TestAccountDoubleInitCounters(t *testing.T) { } } +func TestAccountGetBalancesForPrefixMixed(t *testing.T) { + acc := &Account{ + BalanceMap: map[string]Balances{ + utils.MONETARY: Balances{ + &Balance{ + Value: 10, + DestinationIDs: utils.StringMap{"NAT": true, "RET": false}, + }, + }, + }, + } + bcs := acc.getBalancesForPrefix("999123", "", utils.OUT, utils.MONETARY, "") + if len(bcs) != 0 { + t.Error("error excluding on mixed balances") + } +} + +func TestAccountGetBalancesForPrefixAllExcl(t *testing.T) { + acc := &Account{ + BalanceMap: map[string]Balances{ + utils.MONETARY: Balances{ + &Balance{ + Value: 10, + DestinationIDs: utils.StringMap{"NAT": false, "RET": false}, + }, + }, + }, + } + bcs := acc.getBalancesForPrefix("999123", "", utils.OUT, utils.MONETARY, "") + if len(bcs) == 0 { + t.Error("error finding balance on all excluded") + } +} + +func TestAccountGetBalancesForPrefixMixedGood(t *testing.T) { + acc := &Account{ + BalanceMap: map[string]Balances{ + utils.MONETARY: Balances{ + &Balance{ + Value: 10, + DestinationIDs: utils.StringMap{"NAT": true, "RET": false, "EXOTIC": true}, + }, + }, + }, + } + + bcs := acc.getBalancesForPrefix("999123", "", utils.OUT, utils.MONETARY, "") + if len(bcs) == 0 { + t.Error("error finding on mixed balances good") + } +} + +func TestAccountGetBalancesForPrefixMixedBad(t *testing.T) { + acc := &Account{ + BalanceMap: map[string]Balances{ + utils.MONETARY: Balances{ + &Balance{ + Value: 10, + DestinationIDs: utils.StringMap{"NAT": true, "RET": false, "EXOTIC": false}, + }, + }, + }, + } + bcs := acc.getBalancesForPrefix("999123", "", utils.OUT, utils.MONETARY, "") + if len(bcs) != 0 { + t.Error("error excluding on mixed balances bad") + } +} + /*********************************** Benchmarks *******************************/ func BenchmarkGetSecondForPrefix(b *testing.B) { diff --git a/engine/history_test.go b/engine/history_test.go index e3d797e4b..a15be8d4e 100644 --- a/engine/history_test.go +++ b/engine/history_test.go @@ -39,6 +39,7 @@ func TestHistoryDestinations(t *testing.T) { expected := `{"Id":"ALL","Prefixes":["49","41","43"]}, {"Id":"DST_UK_Mobile_BIG5","Prefixes":["447956"]}, {"Id":"EU_LANDLINE","Prefixes":["444"]}, +{"Id":"EXOTIC","Prefixes":["999"]}, {"Id":"GERMANY","Prefixes":["49"]}, {"Id":"GERMANY_O2","Prefixes":["41"]}, {"Id":"GERMANY_PREMIUM","Prefixes":["43"]}, diff --git a/engine/loader_csv_test.go b/engine/loader_csv_test.go index fbcf9e8f2..c26038562 100644 --- a/engine/loader_csv_test.go +++ b/engine/loader_csv_test.go @@ -49,6 +49,7 @@ PSTN_70,+4970 DST_UK_Mobile_BIG5,447956 URG,112 EU_LANDLINE,444 +EXOTIC,999 ` timings = ` WORKDAYS_00,*any,*any,*any,1;2;3;4;5,00:00:00 @@ -322,7 +323,7 @@ func init() { } func TestLoadDestinations(t *testing.T) { - if len(csvr.destinations) != 13 { + if len(csvr.destinations) != 14 { t.Error("Failed to load destinations: ", len(csvr.destinations)) } for _, d := range csvr.destinations {