Skip to content

Commit

Permalink
nil balance map fix
Browse files Browse the repository at this point in the history
fix for adding default balance on uninitialized account
  • Loading branch information
rif committed Jan 6, 2015
1 parent 6881d84 commit ef101ce
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions engine/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ func (ub *Account) GetDefaultMoneyBalance(direction string) *Balance {
}
// create default balance
defaultBalance := &Balance{Weight: 0} // minimum weight
if ub.BalanceMap == nil {
ub.BalanceMap = make(map[string]BalanceChain)
}
ub.BalanceMap[CREDIT+direction] = append(ub.BalanceMap[CREDIT+direction], defaultBalance)
return defaultBalance
}
Expand Down
19 changes: 19 additions & 0 deletions engine/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,25 @@ func TestDebitDataMoney(t *testing.T) {
}
}

func TestAccountGetDefaultMoneyBalanceEmpty(t *testing.T) {
acc := &Account{}
defBal := acc.GetDefaultMoneyBalance(OUTBOUND)
if defBal == nil || len(acc.BalanceMap) != 1 || !defBal.IsDefault() {
t.Errorf("Bad default money balance: %+v", defBal)
}
}

func TestAccountGetDefaultMoneyBalance(t *testing.T) {
acc := &Account{}
acc.BalanceMap = make(map[string]BalanceChain)
tag := CREDIT + OUTBOUND
acc.BalanceMap[tag] = append(acc.BalanceMap[tag], &Balance{Weight: 10})
defBal := acc.GetDefaultMoneyBalance(OUTBOUND)
if defBal == nil || len(acc.BalanceMap[tag]) != 2 || !defBal.IsDefault() {
t.Errorf("Bad default money balance: %+v", defBal)
}
}

/*********************************** Benchmarks *******************************/

func BenchmarkGetSecondForPrefix(b *testing.B) {
Expand Down
3 changes: 2 additions & 1 deletion engine/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func (b *Balance) IsDefault() bool {
b.RatingSubject == "" &&
b.Category == "" &&
b.ExpirationDate.IsZero() &&
b.SharedGroup == ""
b.SharedGroup == "" &&
b.Weight == 0
}

func (b *Balance) IsExpired() bool {
Expand Down
7 changes: 7 additions & 0 deletions engine/balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,10 @@ func TestBalanceMatchActionTriggerSharedGroup(t *testing.T) {
t.Errorf("Error matching action trigger: %+v %+v", b, at)
}
}

func TestBalanceIsDefault(t *testing.T) {
b := &Balance{Weight: 0}
if !b.IsDefault() {
t.Errorf("Balance should be default: +v", b)
}
}

0 comments on commit ef101ce

Please sign in to comment.