Adding test for insufficient balance

This commit is contained in:
DanB
2013-12-05 10:44:36 +01:00
parent e90960e7fc
commit f0fbe79fca
2 changed files with 28 additions and 5 deletions

View File

@@ -21,7 +21,7 @@ package engine
import (
"errors"
"fmt"
//"encoding/json"
"encoding/json"
"github.com/cgrates/cgrates/cache2go"
"github.com/cgrates/cgrates/history"
"github.com/cgrates/cgrates/utils"
@@ -494,10 +494,10 @@ func (cd *CallDescriptor) Debit() (cc *CallCost, err error) {
} else {
Logger.Debug(fmt.Sprintf("<Rater> Attempting to debit from %v, value: %v", cd.GetUserBalanceKey(), cc.Cost+cc.ConnectFee))
defer storageGetter.SetUserBalance(userBalance)
//ub, _ := json.Marshal(userBalance)
//Logger.Debug(fmt.Sprintf("UserBalance: %s", ub))
//cCost,_ := json.Marshal(cc)
//Logger.Debug(fmt.Sprintf("CallCost: %s", cCost))
ub, _ := json.Marshal(userBalance)
Logger.Debug(fmt.Sprintf("UserBalance: %s", ub))
cCost,_ := json.Marshal(cc)
Logger.Debug(fmt.Sprintf("CallCost: %s", cCost))
if cc.Cost != 0 || cc.ConnectFee != 0 {
userBalance.debitCreditBalance(cc, true)
}

View File

@@ -45,3 +45,26 @@ func TestDebitBalanceForCall1(t *testing.T) {
t.Error("Error debiting from balance: ", b1.BalanceMap[CREDIT+OUTBOUND][0])
}
}
var balanceInsufficient = `{"Id":"*out:192.168.56.66:dan","Type":"*prepaid","BalanceMap":{"*monetary*out":[{"Uuid":"de49cb1a40b2740a8087a1d28112e11c","Value":1,"ExpirationDate":"0001-01-01T00:00:00Z","Weight":10,"GroupIds":null,"DestinationId":"*any","RateSubject":""}]},"UnitCounters":[{"Direction":"*out","BalanceId":"*monetary","Balances":[{"Uuid":"","Value":4,"ExpirationDate":"0001-01-01T00:00:00Z","Weight":0,"GroupIds":null,"DestinationId":"","RateSubject":""}]}],"ActionTriggers":[{"Id":"a33f036e402c2b47801f1380c5c82564","BalanceId":"*monetary","Direction":"*out","ThresholdType":"*min_balance","ThresholdValue":2,"DestinationId":"","Weight":10,"ActionsId":"LOG_BALANCE","Executed":true},{"Id":"91776f2d40496c03806cdbc73ae6b5f9","BalanceId":"*monetary","Direction":"*out","ThresholdType":"*max_balance","ThresholdValue":20,"DestinationId":"","Weight":10,"ActionsId":"LOG_BALANCE","Executed":false},{"Id":"e85e933a4045f77c80ef647295ae209b","BalanceId":"*monetary","Direction":"*out","ThresholdType":"*max_counter","ThresholdValue":15,"DestinationId":"FS_USERS","Weight":10,"ActionsId":"LOG_BALANCE","Executed":false},{"Id":"89c178b440a640b6802334b17b63cd4e","BalanceId":"*monetary","Direction":"*out","ThresholdType":"*min_balance","ThresholdValue":2.1,"DestinationId":"","Weight":10,"ActionsId":"WARN_HTTP","Executed":true}],"Groups":null,"UserIds":null}`
var costInsufficient = `{"Direction":"*out","TOR":"call","Tenant":"192.168.56.66","Subject":"dan","Account":"dan","Destination":"+4986517174963","Cost":1,"ConnectFee":3,"Timespans":[{"TimeStart":"2013-12-05T09:52:17+01:00","TimeEnd":"2013-12-05T09:53:17+01:00","Cost":1,"RateInterval":{"Timing":{"Years":[],"Months":[],"MonthDays":[],"WeekDays":[],"StartTime":"00:00:00","EndTime":""},"Rating":{"ConnectFee":3,"Rates":[{"GroupIntervalStart":0,"Value":1,"RateIncrement":60000000000,"RateUnit":60000000000}],"RoundingMethod":"*up","RoundingDecimals":2},"Weight":10},"CallDuration":60000000000,"Increments":null,"MatchedSubject":"*out:192.168.56.66:call:*any","MatchedPrefix":"+49"}]}`
func SomeTestDebitInsufficientBalance(t *testing.T) {
b1 := new(UserBalance)
if err := json.Unmarshal([]byte(balanceInsufficient), b1); err != nil {
t.Error("Error restoring balance1: ", err)
}
cc1 := new(CallCost)
if err := json.Unmarshal([]byte(costInsufficient), cc1); err != nil {
t.Error("Error restoring callCost1: ", err)
}
err := b1.debitCreditBalance(cc1, false)
if err != nil {
t.Error("Error debiting balance: ", err)
}
if b1.BalanceMap[CREDIT+OUTBOUND][0].Value != -3 {
t.Error("Error debiting from balance: ", b1.BalanceMap[CREDIT+OUTBOUND][0])
}
}