From aec93f7a537426fddf923061edceec835beec26c Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 6 Jul 2012 12:21:22 +0300 Subject: [PATCH] corrected trigger tests --- timespans/action.go | 6 ++++-- timespans/actions_test.go | 19 +++++++++++++++++++ timespans/userbalance.go | 11 ++++++----- timespans/userbalance_test.go | 6 +++--- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/timespans/action.go b/timespans/action.go index 62085a6ad..100ab9a0c 100644 --- a/timespans/action.go +++ b/timespans/action.go @@ -114,14 +114,16 @@ func resetCountersAction(ub *UserBalance, a *Action) (err error) { func genericMakeNegative(a *Action) { a.Units = -a.Units - a.MinuteBucket.Seconds = -a.MinuteBucket.Seconds + if a.MinuteBucket != nil { + a.MinuteBucket.Seconds = -a.MinuteBucket.Seconds + } } func genericDebit(ub *UserBalance, a *Action) (err error) { if ub.BalanceMap == nil { ub.BalanceMap = make(map[string]float64) } - switch a.ActionType { + switch a.BalanceId { case CREDIT: ub.debitMoneyBalance(a.Units) case SMS: diff --git a/timespans/actions_test.go b/timespans/actions_test.go index 24027950f..eb360132e 100644 --- a/timespans/actions_test.go +++ b/timespans/actions_test.go @@ -24,6 +24,25 @@ import ( "time" ) +func init() { + storageGetter, _ = NewRedisStorage("tcp:127.0.0.1:6379", 10) + SetStorageGetter(storageGetter) + populateTestActions() +} + +func populateTestActions() { + /*ats := []*Action{ + &Action{ActionType: "TOPUP", BalanceId: CREDIT, Units: 10}, + &Action{ActionType: "TOPUP", BalanceId: MINUTES, MinuteBucket: &MinuteBucket{Seconds: 10, DestinationId: "NAT"}}, + } + storageGetter.SetActions("TEST_ACTIONS", ats) + ats1 := []*Action{ + &Action{ActionType: "TOPUP", BalanceId: CREDIT, Units: 10, Weight: 20}, + &Action{ActionType: "RESET_PREPAID", Weight: 10}, + } + storageGetter.SetActions("TEST_ACTIONS_ORDER", ats1)*/ +} + func TestActionTimingStoreRestore(t *testing.T) { i := &Interval{ Months: Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November, time.December}, diff --git a/timespans/userbalance.go b/timespans/userbalance.go index 57b041af4..5c1364b7e 100644 --- a/timespans/userbalance.go +++ b/timespans/userbalance.go @@ -104,7 +104,6 @@ Debits some amount of user's money credit. Returns the remaining credit in user' */ func (ub *UserBalance) debitMoneyBalance(amount float64) float64 { ub.BalanceMap[CREDIT] -= amount - return ub.BalanceMap[CREDIT] } @@ -113,7 +112,6 @@ func (ub *UserBalance) debitMinuteBucket(newMb *MinuteBucket) error { if newMb == nil { return errors.New("Nil minute bucket!") } - found := false for _, mb := range ub.MinuteBuckets { if mb.Equal(newMb) { @@ -123,7 +121,7 @@ func (ub *UserBalance) debitMinuteBucket(newMb *MinuteBucket) error { } } // if it is not found and the Seconds are negative (topup) - // then we adde it to the list + // then we add it to the list if !found && newMb.Seconds <= 0 { newMb.Seconds = -newMb.Seconds ub.MinuteBuckets = append(ub.MinuteBuckets, newMb) @@ -133,9 +131,12 @@ func (ub *UserBalance) debitMinuteBucket(newMb *MinuteBucket) error { // Adds the minutes from the received minute bucket to an existing bucket if the destination // is the same or ads the minute bucket to the list if none matches. -func (ub *UserBalance) addMinuteBucket(newMb *MinuteBucket) { +func (ub *UserBalance) addMinuteBucket(newMb *MinuteBucket) error { + if newMb == nil { + return errors.New("Nil minute bucket!") + } newMb.Seconds = -newMb.Seconds - ub.debitMinuteBucket(newMb) + return ub.debitMinuteBucket(newMb) } /* diff --git a/timespans/userbalance_test.go b/timespans/userbalance_test.go index e54cdbafd..55a9a1a30 100644 --- a/timespans/userbalance_test.go +++ b/timespans/userbalance_test.go @@ -31,13 +31,13 @@ var ( func init() { storageGetter, _ = NewRedisStorage("tcp:127.0.0.1:6379", 10) SetStorageGetter(storageGetter) - populateTestActions() + populateTestActionsForTriggers() } -func populateTestActions() { +func populateTestActionsForTriggers() { ats := []*Action{ &Action{ActionType: "TOPUP", BalanceId: CREDIT, Units: 10}, - &Action{ActionType: "TOPUP", BalanceId: MINUTES, MinuteBucket: &MinuteBucket{Seconds: 10, DestinationId: "NAT"}}, + &Action{ActionType: "TOPUP", BalanceId: MINUTES, MinuteBucket: &MinuteBucket{Weight: 20, Price: 1, Seconds: 10, DestinationId: "NAT"}}, } storageGetter.SetActions("TEST_ACTIONS", ats) ats1 := []*Action{