From 515684ea388688a798a9cd6e0924980d3f382efa Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 11 Nov 2015 19:34:26 +0200 Subject: [PATCH] action transaction tests fixes #284 --- engine/account.go | 3 ++ engine/action.go | 3 +- engine/action_plan.go | 2 - engine/actions_test.go | 85 ++++++++++++++++++++++++++++++++++++++++-- engine/balances.go | 14 +++++-- 5 files changed, 97 insertions(+), 10 deletions(-) diff --git a/engine/account.go b/engine/account.go index 3f8f22811..c7d1a8879 100644 --- a/engine/account.go +++ b/engine/account.go @@ -94,6 +94,9 @@ func (ub *Account) debitBalanceAction(a *Action, reset bool) error { return errors.New("nil action") } bClone := a.Balance.Clone() + if bClone == nil { + return errors.New("nil balance") + } if ub.BalanceMap == nil { ub.BalanceMap = make(map[string]BalanceChain, 1) diff --git a/engine/action.go b/engine/action.go index ac1d1fe38..f9ef1680b 100644 --- a/engine/action.go +++ b/engine/action.go @@ -377,8 +377,7 @@ func genericDebit(ub *Account, a *Action, reset bool) (err error) { if ub.BalanceMap == nil { ub.BalanceMap = make(map[string]BalanceChain) } - ub.debitBalanceAction(a, reset) - return + return ub.debitBalanceAction(a, reset) } func enableUserAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) (err error) { diff --git a/engine/action_plan.go b/engine/action_plan.go index fcc2b30b2..c2dffa2b9 100644 --- a/engine/action_plan.go +++ b/engine/action_plan.go @@ -300,7 +300,6 @@ func (at *ActionPlan) Execute() (err error) { // TODO: maybe we should break here as the account is gone // will leave continue for now as the next action can create another acount } - actionFunction, exists := getActionFunc(a.ActionType) if !exists { // do not allow the action plan to be rescheduled @@ -309,7 +308,6 @@ func (at *ActionPlan) Execute() (err error) { transactionFailed = true break } - if err := actionFunction(ub, nil, a, aac); err != nil { utils.Logger.Err(fmt.Sprintf("Error executing action %s: %v!", a.ActionType, err)) transactionFailed = true diff --git a/engine/actions_test.go b/engine/actions_test.go index 5bcab6241..40fd36390 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -21,6 +21,7 @@ package engine import ( "encoding/json" "fmt" + "log" "reflect" "testing" "time" @@ -418,13 +419,13 @@ func TestActionPlanFunctionNotAvailable(t *testing.T) { Balance: &Balance{Value: 1.1}, } at := &ActionPlan{ - AccountIds: []string{"one", "two", "three"}, + AccountIds: []string{"cgrates.org:dy"}, Timing: &RateInterval{}, actions: []*Action{a}, } err := at.Execute() - if at.Timing != nil { - t.Logf("Faild to detect wrong function type: %v", err) + if err != nil { + t.Errorf("Faild to detect wrong function type: %v", err) } } @@ -1274,6 +1275,84 @@ func TestActionSetDDestination(t *testing.T) { } } +func TestActionTransactionFuncType(t *testing.T) { + err := accountingStorage.SetAccount(&Account{ + Id: "cgrates.org:trans", + BalanceMap: map[string]BalanceChain{ + utils.MONETARY: BalanceChain{&Balance{ + Value: 10, + }}, + }, + }) + if err != nil { + t.Error("Error setting account: ", err) + } + at := &ActionPlan{ + AccountIds: []string{"cgrates.org:trans"}, + Timing: &RateInterval{}, + actions: []*Action{ + &Action{ + ActionType: TOPUP, + BalanceType: utils.MONETARY, + Balance: &Balance{Value: 1.1}, + }, + &Action{ + ActionType: "VALID_FUNCTION_TYPE", + BalanceType: "test", + Balance: &Balance{Value: 1.1}, + }, + }, + } + log.Print("=========") + err = at.Execute() + log.Print("=========") + acc, err := accountingStorage.GetAccount("cgrates.org:trans") + if err != nil || acc == nil { + t.Error("Error getting account: ", acc, err) + } + if acc.BalanceMap[utils.MONETARY][0].Value != 10 { + t.Errorf("Transaction didn't work: %v", acc.BalanceMap[utils.MONETARY][0].Value) + } +} + +func TestActionTransactionBalanceType(t *testing.T) { + err := accountingStorage.SetAccount(&Account{ + Id: "cgrates.org:trans", + BalanceMap: map[string]BalanceChain{ + utils.MONETARY: BalanceChain{&Balance{ + Value: 10, + }}, + }, + }) + if err != nil { + t.Error("Error setting account: ", err) + } + at := &ActionPlan{ + AccountIds: []string{"cgrates.org:trans"}, + Timing: &RateInterval{}, + actions: []*Action{ + &Action{ + ActionType: TOPUP, + BalanceType: utils.MONETARY, + Balance: &Balance{Value: 1.1}, + }, + &Action{ + ActionType: TOPUP, + BalanceType: "test", + Balance: nil, + }, + }, + } + err = at.Execute() + acc, err := accountingStorage.GetAccount("cgrates.org:trans") + if err != nil || acc == nil { + t.Error("Error getting account: ", acc, err) + } + if acc.BalanceMap[utils.MONETARY][0].Value != 10 { + t.Errorf("Transaction didn't work: %v", acc.BalanceMap[utils.MONETARY][0].Value) + } +} + /**************** Benchmarks ********************************/ func BenchmarkUUID(b *testing.B) { diff --git a/engine/balances.go b/engine/balances.go index 1468e1a6b..36c7f566f 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -217,12 +217,13 @@ func (b *Balance) MatchActionTrigger(at *ActionTrigger) bool { } func (b *Balance) Clone() *Balance { - return &Balance{ + if b == nil { + return nil + } + n := &Balance{ Uuid: b.Uuid, Id: b.Id, Value: b.Value, // this value is in seconds - DestinationIds: b.DestinationIds.Clone(), - Directions: b.Directions.Clone(), ExpirationDate: b.ExpirationDate, Weight: b.Weight, RatingSubject: b.RatingSubject, @@ -233,6 +234,13 @@ func (b *Balance) Clone() *Balance { Disabled: b.Disabled, dirty: b.dirty, } + if b.DestinationIds != nil { + n.DestinationIds = b.DestinationIds.Clone() + } + if b.Directions != nil { + n.Directions = b.Directions.Clone() + } + return n } func (b *Balance) getMatchingPrefixAndDestId(dest string) (prefix, destId string) {