Updated engine tests after replacing MapStorage

This commit is contained in:
adragusin
2019-11-20 16:17:20 +02:00
committed by Dan Christian Bogos
parent 6d5e7d125d
commit d3522ef2e7
10 changed files with 258 additions and 25 deletions

View File

@@ -834,17 +834,22 @@ func (account *Account) GetUniqueSharedGroupMembers(cd *CallDescriptor) (utils.S
func (acc *Account) Clone() *Account {
newAcc := &Account{
ID: acc.ID,
UnitCounters: nil, // not used when cloned (dryRun)
ActionTriggers: nil, // not used when cloned (dryRun)
AllowNegative: acc.AllowNegative,
Disabled: acc.Disabled,
ID: acc.ID,
UnitCounters: acc.UnitCounters.Clone(), // not used when cloned (dryRun)
AllowNegative: acc.AllowNegative,
Disabled: acc.Disabled,
}
if acc.BalanceMap != nil {
newAcc.BalanceMap = make(map[string]Balances, len(acc.BalanceMap))
for key, balanceChain := range acc.BalanceMap {
newAcc.BalanceMap[key] = balanceChain.Clone()
}
}
for key, balanceChain := range acc.BalanceMap {
newAcc.BalanceMap[key] = balanceChain.Clone()
if acc.ActionTriggers != nil {
newAcc.ActionTriggers = make(ActionTriggers, len(acc.ActionTriggers))
for key, actionTrigger := range acc.ActionTriggers {
newAcc.ActionTriggers[key] = actionTrigger.Clone()
}
}
return newAcc
}

View File

@@ -2328,13 +2328,11 @@ func TestAccountAsNavigableMap(t *testing.T) {
}
func TestAccountClone(t *testing.T) {
//empty check
account := &Account{}
eOut := &Account{}
if rcv := account.Clone(); !reflect.DeepEqual(eOut, rcv) {
if rcv := account.Clone(); !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv))
}
//normal check
account = &Account{
ID: "testID",
BalanceMap: map[string]Balances{
@@ -2347,9 +2345,8 @@ func TestAccountClone(t *testing.T) {
ID: "ActionTriggerID2",
},
},
AllowNegative: true,
Disabled: true,
executingTriggers: true,
AllowNegative: true,
Disabled: true,
}
eOut = &Account{
ID: "testID",
@@ -2363,11 +2360,10 @@ func TestAccountClone(t *testing.T) {
ID: "ActionTriggerID2",
},
},
AllowNegative: true,
Disabled: true,
executingTriggers: true,
AllowNegative: true,
Disabled: true,
}
if rcv := account.Clone(); !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv))
}

View File

@@ -152,9 +152,25 @@ func (at *ActionTrigger) CreateBalance() *Balance {
// makes a shallow copy of the receiver
func (at *ActionTrigger) Clone() *ActionTrigger {
clone := new(ActionTrigger)
*clone = *at
return clone
if at == nil {
return nil
}
return &ActionTrigger{
ID: at.ID,
UniqueID: at.UniqueID,
ThresholdType: at.ThresholdType,
ThresholdValue: at.ThresholdValue,
Recurrent: at.Recurrent,
MinSleep: at.MinSleep,
ExpirationDate: at.ExpirationDate,
ActivationDate: at.ActivationDate,
Weight: at.Weight,
ActionsID: at.ActionsID,
MinQueuedItems: at.MinQueuedItems,
Executed: at.Executed,
LastExecutionTime: at.LastExecutionTime,
Balance: at.Balance.Clone(),
}
}
func (at *ActionTrigger) Equals(oat *ActionTrigger) bool {
@@ -189,3 +205,14 @@ func (atpl ActionTriggers) Less(j, i int) bool {
func (atpl ActionTriggers) Sort() {
sort.Sort(atpl)
}
func (atpl ActionTriggers) Clone() ActionTriggers {
if atpl == nil {
return nil
}
clone := make(ActionTriggers, len(atpl))
for i, at := range atpl {
clone[i] = at.Clone()
}
return clone
}

View File

@@ -0,0 +1,118 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package engine
import (
"reflect"
"testing"
"time"
"github.com/cgrates/cgrates/utils"
)
func TestActionTriggerClone(t *testing.T) {
var at *ActionTrigger
if rcv := at.Clone(); at != nil {
t.Errorf("Expecting : nil, received: %s", utils.ToJSON(rcv))
}
at = &ActionTrigger{}
eOut := &ActionTrigger{}
if rcv := at.Clone(); !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting : %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv))
}
ttime := time.Now()
at = &ActionTrigger{
ID: "testID",
UniqueID: "testUniqueID",
ThresholdType: "testThresholdType",
ThresholdValue: 0.1,
Recurrent: true,
MinSleep: time.Duration(10),
ExpirationDate: ttime,
ActivationDate: ttime.Add(5),
Weight: 0.1,
ActionsID: "testActionsID",
MinQueuedItems: 7,
Executed: true,
LastExecutionTime: ttime.Add(1),
Balance: &BalanceFilter{
Uuid: utils.StringPointer("testUuid"),
},
}
eOut = &ActionTrigger{
ID: "testID",
UniqueID: "testUniqueID",
ThresholdType: "testThresholdType",
ThresholdValue: 0.1,
Recurrent: true,
MinSleep: time.Duration(10),
ExpirationDate: ttime,
ActivationDate: ttime.Add(5),
Weight: 0.1,
ActionsID: "testActionsID",
MinQueuedItems: 7,
Executed: true,
LastExecutionTime: ttime.Add(1),
Balance: &BalanceFilter{
Uuid: utils.StringPointer("testUuid"),
},
}
rcv := at.Clone()
if !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting : %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv))
}
rcv.Balance.Uuid = utils.StringPointer("modified")
if at.Balance.Uuid == utils.StringPointer("modified") {
t.Errorf("Expedcting: modified, received %s", utils.ToJSON(at.Balance.Uuid))
}
}
func TestActionTriggersClone(t *testing.T) {
var atpl ActionTriggers
if rcv := atpl.Clone(); rcv != nil {
t.Errorf("Expecting : nil, received: %s", utils.ToJSON(rcv))
}
atpl = make(ActionTriggers, 0)
eOut := make(ActionTriggers, 0)
if rcv := atpl.Clone(); !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting : %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv))
}
atpl = []*ActionTrigger{
&ActionTrigger{
ID: "test1",
},
&ActionTrigger{
ID: "test2",
},
}
eOut = []*ActionTrigger{
&ActionTrigger{
ID: "test1",
},
&ActionTrigger{
ID: "test2",
},
}
if rcv := atpl.Clone(); !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting : %s, received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv))
}
}

View File

@@ -64,6 +64,9 @@ func (bp *BalanceFilter) CreateBalance() *Balance {
}
func (bf *BalanceFilter) Clone() *BalanceFilter {
if bf == nil {
return nil
}
result := &BalanceFilter{}
if bf.Uuid != nil {
result.Uuid = new(string)

View File

@@ -882,7 +882,7 @@ func TestLoadActionTriggers(t *testing.T) {
Executed: false,
}
if !reflect.DeepEqual(atr, expected) {
t.Errorf("Error loading action trigger: %+v", utils.ToIJSON(atr.Balance))
t.Errorf("Expected: %+v, received %+v", utils.ToJSON(expected), utils.ToJSON(atr))
}
atr = csvr.actionsTriggers["STANDARD_TRIGGER"][1]
expected = &ActionTrigger{

View File

@@ -486,7 +486,7 @@ func (iDB *InternalDB) GetActionTriggersDrv(id string) (at ActionTriggers, err e
if !ok || x == nil {
return nil, utils.ErrNotFound
}
return x.(ActionTriggers), nil
return x.(ActionTriggers).Clone(), nil
}
func (iDB *InternalDB) SetActionTriggersDrv(id string, at ActionTriggers) (err error) {

View File

@@ -44,6 +44,48 @@ func (cfs CounterFilters) HasCounter(cf *CounterFilter) bool {
return false
}
// Clone clones *UnitCounter
func (uc *UnitCounter) Clone() (newUnit *UnitCounter) {
if uc == nil {
return
}
newUnit = &UnitCounter{
CounterType: uc.CounterType,
Counters: make(CounterFilters, len(uc.Counters)),
}
for i, counter := range uc.Counters {
newUnit.Counters[i] = counter.Clone()
}
return newUnit
}
// Clone clones *CounterFilter
func (cfs *CounterFilter) Clone() *CounterFilter {
if cfs == nil {
return nil
}
return &CounterFilter{
Value: cfs.Value,
Filter: cfs.Filter.Clone(),
}
}
// Clone clones *UnitCounters
func (ucs UnitCounters) Clone() UnitCounters {
if ucs == nil {
return nil
}
newUnitCounters := make(UnitCounters)
for key, unitCounter := range ucs {
newal := make([]*UnitCounter, len(unitCounter))
for i, uc := range unitCounter {
newal[i] = uc.Clone()
}
newUnitCounters[key] = newal
}
return newUnitCounters
}
// Returns true if the counters were of the same type
// Copies the value from old balances
func (uc *UnitCounter) CopyCounterValues(oldUc *UnitCounter) bool {

View File

@@ -118,6 +118,7 @@ func FirstNonEmpty(vals ...string) string {
return EmptyString
}
// Sha1 generate the SHA1 hash from any string
func Sha1(attrs ...string) string {
hasher := sha1.New()
for _, attr := range attrs {
@@ -126,9 +127,9 @@ func Sha1(attrs ...string) string {
return fmt.Sprintf("%x", hasher.Sum(nil))
}
func NewTPid() string {
return Sha1(GenUUID())
}
// func NewTPid() string {
// return Sha1(GenUUID())
// }
// helper function for uuid generation
func GenUUID() string {

View File

@@ -51,11 +51,52 @@ func TestFirstNonEmpty(t *testing.T) {
}
}
func TestSha1(t *testing.T) {
//empty check
rcv := Sha1(" ")
eOut := "b858cb282617fb0956d960215c8e84d1ccf909c6"
if !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %s, received: %s", eOut, rcv)
}
//normal check
rcv = Sha1("teststring")
eOut = "b8473b86d4c2072ca9b08bd28e373e8253e865c4"
if !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %s, received: %s", eOut, rcv)
}
rcv = Sha1("test1", "test2")
eOut = "dff964f6e3c1761b6288f5c75c319d36fb09b2b9"
if !reflect.DeepEqual(eOut, rcv) {
t.Errorf("Expecting: %s, received: %s", eOut, rcv)
}
}
func TestUUID(t *testing.T) {
uuid := GenUUID()
if len(uuid) == 0 {
t.Fatalf("GenUUID error %s", uuid)
}
uuid2 := GenUUID()
if len(uuid2) == 0 {
t.Fatalf("GenUUID error %s", uuid)
}
if uuid == uuid2 {
t.Error("GenUUID error.")
}
}
func TestUUIDSha1Prefix(t *testing.T) {
rcv := UUIDSha1Prefix()
if len(rcv) != 7 {
t.Errorf("Expected len: 7, received %d", len(rcv))
}
rcv2 := UUIDSha1Prefix()
if len(rcv2) != 7 {
t.Errorf("Expected len: 7, received %d", len(rcv))
}
if rcv == rcv2 {
t.Error("UUIDSha1Prefix error")
}
}
func TestRoundByMethodUp1(t *testing.T) {