From 97025381b32cc392ef2e0b07df69edd076f97a0b Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 26 May 2015 12:29:53 +0300 Subject: [PATCH] test for GetCost with maxcost enabled --- engine/calldesc.go | 14 ++++++------ engine/calldesc_test.go | 47 +++++++++++++++++++++++++++++++++++++++++ local_test.sh | 3 +-- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/engine/calldesc.go b/engine/calldesc.go index 047c86dd1..2a69a8830 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -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())) diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index cd0642a3b..56d4cb2a3 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -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), diff --git a/local_test.sh b/local_test.sh index b102597e5..f111919d6 100755 --- a/local_test.sh +++ b/local_test.sh @@ -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 -