test for GetCost with maxcost enabled

This commit is contained in:
Radu Ioan Fericean
2015-05-26 12:29:53 +03:00
parent 636574cc5a
commit 97025381b3
3 changed files with 56 additions and 8 deletions

View File

@@ -417,20 +417,22 @@ func (cd *CallDescriptor) GetCost() (*CallCost, error) {
if cd.LoopIndex == 0 && i == 0 && ts.RateInterval != nil {
cost += ts.RateInterval.Rating.ConnectFee
}
//log.Printf("TS: %+v", ts)
// handle max cost
maxCost, strategy := ts.RateInterval.GetMaxCost()
cost += ts.getCost()
cd.MaxCostSoFar += cost
//log.Print("Before: ", cost)
if strategy != "" && maxCost > 0 {
//log.Print("HERE: ", strategy, maxCost)
if strategy == utils.MAX_COST_FREE && cd.MaxCostSoFar >= maxCost {
cost = maxCost
cd.MaxCostSoFar = maxCost
} else {
cost += ts.getCost()
cd.MaxCostSoFar += cost
}
} else {
cost += ts.getCost()
}
//log.Print("Cost: ", cost)
}
cc.Cost = cost
// global rounding
@@ -615,7 +617,7 @@ func (cd *CallDescriptor) Debit() (cc *CallCost, err error) {
// Interface method used to add/substract an amount of cents or bonus seconds (as returned by GetCost method)
// from user's money balance.
// This methods combines the Debit and GetMaxSessionDuration and will debit the max available time as returned
// by the GetMaxSessionTime method. The amount filed has to be filled in call descriptor.
// by the GetMaxSessionDuration method. The amount filed has to be filled in call descriptor.
func (cd *CallDescriptor) MaxDebit() (cc *CallCost, err error) {
if account, err := cd.getAccount(); err != nil || account == nil {
Logger.Err(fmt.Sprintf("Could not get user balance for <%s>: %s.", cd.GetAccountKey(), err.Error()))

View File

@@ -512,6 +512,29 @@ func TestMaxSessionTimeWithMaxCost(t *testing.T) {
}
}
func TestGetCostWithMaxCost(t *testing.T) {
ap, _ := accountingStorage.GetActionTimings("TOPUP10_AT")
for _, at := range ap {
at.Execute()
}
cd := &CallDescriptor{
Direction: "*out",
Category: "call",
Tenant: "cgrates.org",
Subject: "max",
Account: "max",
Destination: "0723123113",
TimeStart: time.Date(2015, 3, 23, 6, 0, 0, 0, time.UTC),
TimeEnd: time.Date(2015, 3, 23, 6, 30, 0, 0, time.UTC),
MaxCostSoFar: 0,
}
cc, err := cd.GetCost()
expected := 1800.0
if cc.Cost != expected || err != nil {
t.Errorf("Expected %v was %v", expected, cc.Cost)
}
}
func TestMaxSessionTimeWithMaxCostFree(t *testing.T) {
ap, _ := accountingStorage.GetActionTimings("TOPUP10_AT")
for _, at := range ap {
@@ -535,6 +558,30 @@ func TestMaxSessionTimeWithMaxCostFree(t *testing.T) {
}
}
func TestGetCostWithMaxCostFree(t *testing.T) {
ap, _ := accountingStorage.GetActionTimings("TOPUP10_AT")
for _, at := range ap {
at.Execute()
}
cd := &CallDescriptor{
Direction: "*out",
Category: "call",
Tenant: "cgrates.org",
Subject: "max",
Account: "max",
Destination: "0723123113",
TimeStart: time.Date(2015, 3, 23, 19, 0, 0, 0, time.UTC),
TimeEnd: time.Date(2015, 3, 23, 19, 30, 0, 0, time.UTC),
MaxCostSoFar: 0,
}
cc, err := cd.GetCost()
expected := 10.0
if cc.Cost != expected || err != nil {
t.Errorf("Expected %v was %v", expected, cc.Cost)
}
}
func TestMaxSessionTimeWithAccountAlias(t *testing.T) {
cd := &CallDescriptor{
TimeStart: time.Date(2013, 10, 21, 18, 34, 0, 0, time.UTC),

View File

@@ -1,5 +1,5 @@
#! /usr/bin/env sh
./build.sh
./test.sh
gen=$?
echo 'go test github.com/cgrates/cgrates/apier/v1 -local'
@@ -28,4 +28,3 @@ gnr=$?
exit $gen && $ap1 && $ap2 && $en && $cdrc && $cfg && $gnr