more fixes and tests on balance filter matching

This commit is contained in:
Radu Ioan Fericean
2014-12-07 13:26:16 +02:00
parent 660e57f085
commit 3df2ee6d5f
3 changed files with 57 additions and 3 deletions

View File

@@ -116,13 +116,12 @@ func (ub *Account) debitBalanceAction(a *Action, reset bool) error {
if b.IsExpired() {
continue // just to be safe (cleaned expired balances above)
}
if b.Equal(a.Balance) {
if b.MatchFilter(a.Balance) {
if reset {
b.Value = 0
}
b.SubstractAmount(a.Balance.Value)
found = true
break
}
}
// if it is not found then we add it to the list

View File

@@ -763,6 +763,44 @@ func TestActionTopupResetCredit(t *testing.T) {
}
}
func TestActionTopupResetCreditId(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
BalanceMap: map[string]BalanceChain{
CREDIT + OUTBOUND: BalanceChain{
&Balance{Value: 100},
&Balance{Id: "TEST_B", Value: 15},
},
},
}
a := &Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Id: "TEST_B", Value: 10}}
topupResetAction(ub, nil, a)
if ub.AllowNegative ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 110 ||
len(ub.BalanceMap[CREDIT+OUTBOUND]) != 2 {
t.Errorf("Topup reset action failed: %+v", ub.BalanceMap[CREDIT+OUTBOUND][0])
}
}
func TestActionTopupResetCreditNoId(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
BalanceMap: map[string]BalanceChain{
CREDIT + OUTBOUND: BalanceChain{
&Balance{Value: 100},
&Balance{Id: "TEST_B", Value: 15},
},
},
}
a := &Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}}
topupResetAction(ub, nil, a)
if ub.AllowNegative ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 20 ||
len(ub.BalanceMap[CREDIT+OUTBOUND]) != 2 {
t.Errorf("Topup reset action failed: %+v", ub.BalanceMap[CREDIT+OUTBOUND][1])
}
}
func TestActionTopupResetMinutes(t *testing.T) {
ub := &Account{
Id: "TEST_UB",

View File

@@ -43,7 +43,23 @@ type Balance struct {
}
func (b *Balance) Equal(o *Balance) bool {
if b.Id != "" || o.Id != "" {
if b.DestinationId == "" {
b.DestinationId = utils.ANY
}
if o.DestinationId == "" {
o.DestinationId = utils.ANY
}
return b.Id == o.Id &&
b.ExpirationDate.Equal(o.ExpirationDate) &&
b.Weight == o.Weight &&
b.DestinationId == o.DestinationId &&
b.RatingSubject == o.RatingSubject &&
b.Category == o.Category &&
b.SharedGroup == o.SharedGroup
}
func (b *Balance) MatchFilter(o *Balance) bool {
if o.Id != "" {
return b.Id == o.Id
}
if b.DestinationId == "" {
@@ -121,6 +137,7 @@ func (b *Balance) MatchActionTrigger(at *ActionTrigger) bool {
func (b *Balance) Clone() *Balance {
return &Balance{
Uuid: b.Uuid,
Id: b.Id,
Value: b.Value, // this value is in seconds
DestinationId: b.DestinationId,
ExpirationDate: b.ExpirationDate,