From ce8f7bf927c576152e0ebabc6deb7dffe0ca4e48 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 4 Mar 2016 16:10:29 +0200 Subject: [PATCH] rounding unit tests --- engine/calldesc.go | 8 ++------ engine/calldesc_test.go | 37 ++++++++++++++++++++++++++++++++++++- engine/timespans.go | 2 +- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/engine/calldesc.go b/engine/calldesc.go index 12dd637cc..c70f8b4eb 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -777,7 +777,6 @@ func (cd *CallDescriptor) RefundIncrements() error { for _, increment := range cd.Increments { accMap[increment.BalanceInfo.AccountId] = true } - accountIDs := accMap.Slice() // start increment refunding loop _, err := Guardian.Guard(func() (interface{}, error) { accountsCache := make(map[string]*Account) @@ -816,7 +815,7 @@ func (cd *CallDescriptor) RefundIncrements() error { } } return 0, nil - }, 0, accountIDs...) + }, 0, accMap.Slice()...) return err } @@ -827,7 +826,6 @@ func (cd *CallDescriptor) RefundRounding() error { for _, inc := range cd.Increments { accMap[inc.BalanceInfo.AccountId] = true } - accountIDs := accMap.Slice() // start increment refunding loop _, err := Guardian.Guard(func() (interface{}, error) { accountsCache := make(map[string]*Account) @@ -851,14 +849,12 @@ func (cd *CallDescriptor) RefundRounding() error { if balance = account.BalanceMap[utils.MONETARY].GetBalance(increment.BalanceInfo.MoneyBalanceUuid); balance == nil { return 0, nil } - //log.Print("BEFORE: ", balance.GetValue(), increment.Cost) balance.AddValue(-increment.Cost) - //log.Print("AFTER: ", balance.GetValue(), increment.Cost) account.countUnits(increment.Cost, utils.MONETARY, cc, balance) } } return 0, nil - }, 0, accountIDs...) + }, 0, accMap.Slice()...) return err } diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index 0ac51f88f..49b6cc26a 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -745,7 +745,7 @@ func TestMaxDebitUnknowDest(t *testing.T) { } } -func TestGetCostMaxDebitRoundingIssue(t *testing.T) { +func TestMaxDebitRoundingIssue(t *testing.T) { ap, _ := ratingStorage.GetActionPlan("TOPUP10_AT", false) for _, at := range ap.ActionTimings { at.accountIDs = ap.AccountIDs @@ -780,6 +780,41 @@ func TestGetCostMaxDebitRoundingIssue(t *testing.T) { } } +func TestDebitRoundingRefund(t *testing.T) { + ap, _ := ratingStorage.GetActionPlan("TOPUP10_AT", false) + for _, at := range ap.ActionTimings { + at.accountIDs = ap.AccountIDs + at.Execute() + } + cd := &CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "dy", + Account: "dy", + Destination: "0723123113", + TimeStart: time.Date(2016, 3, 4, 13, 50, 00, 0, time.UTC), + TimeEnd: time.Date(2016, 3, 4, 13, 53, 00, 0, time.UTC), + MaxCostSoFar: 0, + PerformRounding: true, + } + acc, err := accountingStorage.GetAccount("cgrates.org:dy") + if err != nil || acc.BalanceMap[utils.MONETARY][0].Value != 1 { + t.Errorf("Error getting account: %+v (%v)", utils.ToIJSON(acc), err) + } + + cc, err := cd.Debit() + expected := 0.3 + if cc.Cost != expected || err != nil { + t.Log(utils.ToIJSON(cc)) + t.Errorf("Expected %v was %+v (%v)", expected, cc, err) + } + acc, err = accountingStorage.GetAccount("cgrates.org:dy") + if err != nil || acc.BalanceMap[utils.MONETARY][0].Value != 1-expected { + t.Errorf("Error getting account: %+v (%v)", utils.ToIJSON(acc), err) + } +} + func TestMaxSessionTimeWithMaxCostFree(t *testing.T) { ap, _ := ratingStorage.GetActionPlan("TOPUP10_AT", false) for _, at := range ap.ActionTimings { diff --git a/engine/timespans.go b/engine/timespans.go index 4f0a74de5..dea4af215 100644 --- a/engine/timespans.go +++ b/engine/timespans.go @@ -33,13 +33,13 @@ A unit in which a call will be split that has a specific price related interval type TimeSpan struct { TimeStart, TimeEnd time.Time Cost float64 - ratingInfo *RatingInfo RateInterval *RateInterval DurationIndex time.Duration // the call duration so far till TimeEnd Increments Increments RoundIncrements Increments MatchedSubject, MatchedPrefix, MatchedDestId, RatingPlanId string CompressFactor int + ratingInfo *RatingInfo } type Increment struct {