From d03ff0a5cd106598c50ff9471319d42d6e3dfdb9 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 15 Oct 2013 14:56:39 +0300 Subject: [PATCH] newsubject tests and fixes --- engine/calldesc.go | 2 +- engine/userbalance.go | 24 ++++++-- engine/userbalance_test.go | 116 +++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 5 deletions(-) diff --git a/engine/calldesc.go b/engine/calldesc.go index 6f14f3378..f2de51b64 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -41,7 +41,7 @@ func init() { m, _ := NewMapStorage() //m, _ := NewMongoStorage(db_server, "27017", "cgrates_test", "", "") //m, _ := NewRedisStorage(db_server+":6379", 11, "", utils.MSGPACK) - //m, _ := NewRedigoStorage(db_server+":6379", 11, "") + //fm, _ := NewRedigoStorage(db_server+":6379", 11, "") //m, _ := NewRadixStorage(db_server+":6379", 11, "") storageGetter, _ = m.(DataStorage) diff --git a/engine/userbalance.go b/engine/userbalance.go index 76dacf37d..f5c521824 100644 --- a/engine/userbalance.go +++ b/engine/userbalance.go @@ -188,7 +188,6 @@ func (ub *UserBalance) debitCreditBalance(cc *CallCost, count bool) error { } paid := false for _, b := range usefulMinuteBalances { - // check standard subject tags if b.RateSubject == ZEROSECOND || b.RateSubject == "" { amount := increment.Duration.Seconds() @@ -263,6 +262,7 @@ func (ub *UserBalance) debitCreditBalance(cc *CallCost, count bool) error { // get the new rate cd := cc.CreateCallDescriptor() + cd.Subject = b.RateSubject cd.TimeStart = ts.GetTimeStartForIncrement(incrementIndex) cd.TimeEnd = cc.Timespans[len(cc.Timespans)-1].TimeEnd cd.CallDuration = cc.Timespans[len(cc.Timespans)-1].CallDuration @@ -274,6 +274,7 @@ func (ub *UserBalance) debitCreditBalance(cc *CallCost, count bool) error { //debit new callcost var paidTs []*TimeSpan for _, nts := range newCC.Timespans { + nts.createIncrementsSlice() paidTs = append(paidTs, nts) for nIdx, nInc := range nts.Increments { // debit minutes and money @@ -311,8 +312,14 @@ func (ub *UserBalance) debitCreditBalance(cc *CallCost, count bool) error { if paidDuration > 0 { // split from current increment newTs := ts.SplitByIncrement(incrementIndex) - remainingTs := []*TimeSpan{newTs} - + var remainingTs []*TimeSpan + if newTs != nil { + remainingTs = append(remainingTs, newTs) + } else { + // nothing was paied form current ts so remove it + cc.Timespans = append(cc.Timespans[:tsIndex], cc.Timespans[tsIndex+1:]...) + tsIndex-- + } for tsi := tsIndex + 1; tsi < len(cc.Timespans); tsi++ { remainingTs = append(remainingTs, cc.Timespans[tsi]) } @@ -376,6 +383,7 @@ func (ub *UserBalance) debitCreditBalance(cc *CallCost, count bool) error { } else { // get the new rate cd := cc.CreateCallDescriptor() + cd.Subject = b.RateSubject cd.TimeStart = ts.GetTimeStartForIncrement(incrementIndex) cd.TimeEnd = cc.Timespans[len(cc.Timespans)-1].TimeEnd cd.CallDuration = cc.Timespans[len(cc.Timespans)-1].CallDuration @@ -387,6 +395,7 @@ func (ub *UserBalance) debitCreditBalance(cc *CallCost, count bool) error { //debit new callcost var paidTs []*TimeSpan for _, nts := range newCC.Timespans { + nts.createIncrementsSlice() paidTs = append(paidTs, nts) for nIdx, nInc := range nts.Increments { // debit money @@ -410,7 +419,14 @@ func (ub *UserBalance) debitCreditBalance(cc *CallCost, count bool) error { if paidDuration > 0 { // split from current increment newTs := ts.SplitByIncrement(incrementIndex) - remainingTs := []*TimeSpan{newTs} + var remainingTs []*TimeSpan + if newTs != nil { + remainingTs = append(remainingTs, newTs) + } else { + // nothing was paied form current ts so remove it + cc.Timespans = append(cc.Timespans[:tsIndex], cc.Timespans[tsIndex+1:]...) + tsIndex-- + } for tsi := tsIndex + 1; tsi < len(cc.Timespans); tsi++ { remainingTs = append(remainingTs, cc.Timespans[tsi]) diff --git a/engine/userbalance_test.go b/engine/userbalance_test.go index 9af25ce87..27265389f 100644 --- a/engine/userbalance_test.go +++ b/engine/userbalance_test.go @@ -572,6 +572,122 @@ func TestDebitCreditMoneyOnly(t *testing.T) { } } +func TestDebitCreditSubjectMinutes(t *testing.T) { + b1 := &Balance{Uuid: "testb", Value: 250, Weight: 10, DestinationId: "NAT", RateSubject: "minu"} + cc := &CallCost{ + Tenant: "vdf", + TOR: "0", + Direction: OUTBOUND, + Destination: "0723045326", + Timespans: []*TimeSpan{ + &TimeSpan{ + TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), + TimeEnd: time.Date(2013, 9, 24, 10, 49, 10, 0, time.UTC), + CallDuration: 0, + RateInterval: &RateInterval{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 100, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}, + }, + }, + } + rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{ + MINUTES + OUTBOUND: BalanceChain{b1}, + CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 350}}, + }} + err := rifsBalance.debitCreditBalance(cc, false) + if err != nil { + t.Error("Error debiting balance: ", err) + } + if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" || + cc.Timespans[0].Increments[0].BalanceUuids[1] != "moneya" || + cc.Timespans[0].Increments[0].Duration != time.Second { + t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0]) + } + if rifsBalance.BalanceMap[MINUTES+OUTBOUND][0].Value != 180 || + rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 280 { + t.Errorf("Error extracting minutes from balance: %+v, %+v", + rifsBalance.BalanceMap[MINUTES+OUTBOUND][0].Value, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value) + } + if len(cc.Timespans) != 1 || cc.Timespans[0].GetDuration() != 70*time.Second { + t.Error("Error truncating extra timespans: ", cc.Timespans) + } +} + +func TestDebitCreditSubjectMoney(t *testing.T) { + cc := &CallCost{ + Tenant: "vdf", + TOR: "0", + Direction: OUTBOUND, + Destination: "0723045326", + Timespans: []*TimeSpan{ + &TimeSpan{ + TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), + TimeEnd: time.Date(2013, 9, 24, 10, 49, 10, 0, time.UTC), + CallDuration: 0, + RateInterval: &RateInterval{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 100, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}, + }, + }, + } + rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{ + CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 75, DestinationId: "NAT", RateSubject: "minu"}}, + }} + err := rifsBalance.debitCreditBalance(cc, false) + if err != nil { + t.Error("Error debiting balance: ", err) + } + if cc.Timespans[0].Increments[0].BalanceUuids[0] != "moneya" || + cc.Timespans[0].Increments[0].Duration != time.Second { + t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0]) + } + if rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 5 { + t.Errorf("Error extracting minutes from balance: %+v", + rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value) + } + if len(cc.Timespans) != 1 || cc.Timespans[0].GetDuration() != 70*time.Second { + t.Error("Error truncating extra timespans: ", cc.Timespans) + } +} + +func TestDebitCreditSubjectMixed(t *testing.T) { + b1 := &Balance{Uuid: "testb", Value: 70, Weight: 10, DestinationId: "NAT", RateSubject: ZEROMINUTE} + cc := &CallCost{ + Direction: OUTBOUND, + Destination: "0723045326", + Timespans: []*TimeSpan{ + &TimeSpan{ + TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), + TimeEnd: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), + CallDuration: 0, + RateInterval: &RateInterval{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 100, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}, + }, + &TimeSpan{ + TimeStart: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), + TimeEnd: time.Date(2013, 9, 24, 10, 49, 20, 0, time.UTC), + CallDuration: 10 * time.Second, + RateInterval: &RateInterval{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 1, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}, + }, + }, + } + rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{ + MINUTES + OUTBOUND: BalanceChain{b1}, + CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 50}}, + }} + err := rifsBalance.debitCreditBalance(cc, false) + if err != nil { + t.Error("Error debiting balance: ", err) + } + if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" || + cc.Timespans[0].Increments[0].Duration != time.Minute { + t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0]) + } + if rifsBalance.BalanceMap[MINUTES+OUTBOUND][0].Value != 10 || + rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 30 { + t.Errorf("Error extracting minutes from balance: %+v, %+v", + rifsBalance.BalanceMap[MINUTES+OUTBOUND][0].Value, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value) + } + if len(cc.Timespans) != 2 || cc.Timespans[0].GetDuration() != time.Minute || cc.Timespans[1].GetDuration() != 20*time.Second { + t.Error("Error truncating extra timespans: ", cc.Timespans) + } +} + /* func TestDebitMinuteBalance(t *testing.T) { b1 := &Balance{Value: 10, Weight: 10, DestinationId: "NAT"}