engine&utils:improving coverage tests

This commit is contained in:
gezimbll
2022-12-01 10:59:07 -05:00
committed by Dan Christian Bogos
parent 258492fcf3
commit fd23a70bd1
7 changed files with 261 additions and 11 deletions

View File

@@ -3344,7 +3344,6 @@ func TestResetAccountCDRSuccesful(t *testing.T) {
}
/*
func TestRemoveSessionCost(t *testing.T) {
Cache.Clear(nil)
cfg := config.NewDefaultCGRConfig()
@@ -3378,7 +3377,6 @@ func TestRemoveSessionCost(t *testing.T) {
dm.SetFilter(fl, true)
Cache = NewCacheS(cfg, dm, nil)
SetDataStorage(dm)
action := &Action{
ExtraParameters: "BalanceID;~*acnt.BalanceID;ActionID;~*act.ActionID;BalanceValue;~*acnt.BalanceValue",
}
@@ -3386,4 +3384,3 @@ func TestRemoveSessionCost(t *testing.T) {
t.Error(err)
}
}
*/

View File

@@ -21,8 +21,11 @@ import (
"encoding/json"
"reflect"
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
"github.com/cgrates/rpcclient"
"github.com/nyaruka/phonenumbers"
)
@@ -193,12 +196,11 @@ func TestDPNewLibNumber(t *testing.T) {
}
/*
func TestDMSetDestination(t *testing.T) {
Cache.Clear(nil)
cfg := config.NewDefaultCGRConfig()
cfg.DataDbCfg().Items = map[string]*config.ItemOpt{
utils.MetaDestinations: &config.ItemOpt{
utils.MetaDestinations: {
Replicate: true,
},
}
@@ -235,11 +237,67 @@ func TestDMSetDestination(t *testing.T) {
Prefixes: []string{},
}
dm := NewDataManager(db, cfg.CacheCfg(), connMngr)
config.SetCgrConfig(cfg)
Cache = NewCacheS(cfg, dm, nil)
if err := dm.SetDestination(dest, utils.NonTransactional); err != nil {
t.Error(err)
}
}
*/
func TestDMSetAccount(t *testing.T) {
Cache.Clear(nil)
cfg := config.NewDefaultCGRConfig()
cfg.DataDbCfg().RplConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg)}
cfg.DataDbCfg().RplFiltered = false
cfg.DataDbCfg().Items = map[string]*config.ItemOpt{
utils.MetaAccounts: {
Replicate: true,
},
}
clientConn := make(chan rpcclient.ClientConnector, 1)
clientConn <- &ccMock{
calls: map[string]func(args interface{}, reply interface{}) error{
utils.ReplicatorSv1SetAccount: func(args, reply interface{}) error {
*reply.(*string) = "reply"
return nil
},
},
}
connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{
utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg): clientConn,
})
db := NewInternalDB(nil, nil, true, map[string]*config.ItemOpt{
utils.CacheAccounts: {
Limit: 3,
TTL: 2 * time.Minute,
StaticTTL: true,
Remote: true,
Replicate: true,
},
})
dm := NewDataManager(db, cfg.CacheCfg(), connMgr)
acc := &Account{
ID: "id",
BalanceMap: map[string]Balances{
"bal": {
{
Uuid: "uuid",
ID: "id",
Value: 21.3,
},
},
},
UnitCounters: UnitCounters{},
ActionTriggers: ActionTriggers{},
AllowNegative: true,
Disabled: false,
}
Cache = NewCacheS(cfg, dm, nil)
if err := dm.SetAccount(acc); err != nil {
t.Error(err)
}
}

View File

@@ -1023,7 +1023,7 @@ func TestRouteServiceV1GetRoutes(t *testing.T) {
t.Error(err)
}
if err := rpS.V1GetRoutesList(args, &[]string{}); err == nil {
if err := rpS.V1GetRoutesList(args, &[]string{}); err == nil || err != utils.ErrNotFound {
t.Error(err)
}
@@ -1733,7 +1733,8 @@ func TestRouteServicePopulateSortingData(t *testing.T) {
}
extraOpts := &optsGetRoutes{
sortingStrategy: utils.MetaLoad,
sortingStrategy: utils.MetaLoad,
sortingParameters: []string{"sort1"},
}
exp := &SortedRoute{
RouteID: "id",
@@ -1759,4 +1760,10 @@ func TestRouteServicePopulateSortingData(t *testing.T) {
} else if !reflect.DeepEqual(exp.SortingData, sroutes.SortingData) {
t.Errorf("expected %+v,received %+v", utils.ToJSON(exp), utils.ToJSON(sroutes))
}
extraOpts.sortingStrategy = "other"
if _, pass, err := routeService.populateSortingData(ev, route, extraOpts); err != nil || !pass {
t.Error(err)
}
}

View File

@@ -21,6 +21,7 @@ import (
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
@@ -361,3 +362,42 @@ func BenchmarkMarshallerBincStoreRestore(b *testing.B) {
ms.Unmarshal(result, ub1)
}
}
func TestIDBRemoveIndexesDrv(t *testing.T) {
idb := NewInternalDB(nil, nil, true, map[string]*config.ItemOpt{
"chID": {
Limit: 3,
TTL: 4 * time.Minute,
StaticTTL: false,
Remote: true,
Replicate: true,
RouteID: "route",
APIKey: "api",
},
"chID2": {
Limit: 3,
TTL: 4 * time.Minute,
StaticTTL: false,
Remote: true,
Replicate: true,
RouteID: "route",
APIKey: "api",
},
},
)
idb.db.Set("chID", "itmID", true, []string{utils.EmptyString}, true, "trID")
idb.db.Set("chID2", "itmIDv", true, []string{"grpID"}, true, "trID")
if err := idb.RemoveIndexesDrv("chID", utils.EmptyString, utils.EmptyString); err != nil {
t.Error(err)
}
if err := idb.RemoveIndexesDrv("chID2", "itmID", "v"); err != nil {
t.Error(err)
}
if has := idb.db.HasGroup("chID", utils.EmptyString); has {
t.Error("group should be removed")
}
}

View File

@@ -1154,3 +1154,42 @@ func TestTpReaderIsValid(t *testing.T) {
}
}
func TestTpReaderLoadAccountActions(t *testing.T) {
cfg := config.NewDefaultCGRConfig()
tscache := ltcache.NewTransCache(
map[string]*ltcache.CacheConfig{
utils.CacheTBLTPAccountActions: {
MaxItems: 3,
TTL: time.Minute * 30,
StaticTTL: false,
OnEvicted: func(itmID string, value interface{}) {
},
}},
)
tscache.Set(utils.CacheTBLTPAccountActions, "*prfitemId", &utils.TPAccountActions{
TPid: "tp_acc1",
Account: "acc1",
Tenant: "tn1",
}, []string{"groupId"}, true, "tId")
tscache.Set(utils.CacheTBLTPAccountActions, "*prfitemId2", &utils.TPAccountActions{
TPid: "tp_acc2",
Account: "acc2",
Tenant: "tn2",
ActionPlanId: "actionplans",
}, []string{"groupId"}, true, "tId")
db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
db.db = tscache
tpr, err := NewTpReader(db, db, "*prf", "local", nil, nil, true)
if err != nil {
t.Error(err)
}
if err := tpr.LoadAccountActions(); err == nil || err.Error() != fmt.Sprintf("could not get action plan for tag %q", "actionplans") {
t.Error(err)
}
tpr.dm = nil
if err := tpr.LoadAccountActions(); err == nil {
t.Error(err)
}
}

View File

@@ -393,7 +393,7 @@ func (sm *SecureMapStorage) Remove(fldPath []string) (err error) {
func (sm *SecureMapStorage) Clone() (smClone *SecureMapStorage) {
sm.RLock()
smClone = new(SecureMapStorage)
smClone.ms = smClone.ms.Clone()
smClone.ms = sm.ms.Clone()
sm.RUnlock()
return
}

View File

@@ -916,3 +916,112 @@ func TestMSgetPathFromValue(t *testing.T) {
)
}
}
func TestSecureMapStorageString(t *testing.T) {
sm := &SecureMapStorage{ms: MapStorage{
"field1": 23,
"field2": []string{"ms1", "ms2"},
}}
exp := "{\"field1\":23,\"field2\":[\"ms1\",\"ms2\"]}"
if val := sm.String(); val != exp {
t.Errorf("expected %+s,received %+s", exp, val)
}
}
func TestSecureMapStorageFieldAsInterface(t *testing.T) {
sm := &SecureMapStorage{ms: MapStorage{
"field1": []string{"val1", "val2"},
}}
if val, err := sm.FieldAsInterface([]string{"field1[0]"}); err != nil {
t.Error(err)
} else if val.(string) != sm.ms["field1"].([]string)[0] {
t.Errorf("expected %s,received %v", val, sm.ms["field1"].([]string)[0])
}
}
func TestSecureMapStorageFieldAsString(t *testing.T) {
sm := &SecureMapStorage{ms: MapStorage{
"field1": []string{"val1", "val2"},
}}
if val, err := sm.FieldAsString([]string{"field1[0]"}); err != nil {
t.Error(err)
} else if val != sm.ms["field1"].([]string)[0] {
t.Errorf("expected %s,received %v", val, sm.ms["field1"].([]string)[0])
}
}
func TestSecureMapStorageSet(t *testing.T) {
sm := &SecureMapStorage{ms: MapStorage{
"field": map[string]interface{}{},
}}
exp := MapStorage{
"test": "val",
}
if err := sm.Set([]string{"field2"}, MapStorage{"test": "val"}); err != nil {
t.Error(err)
}
if v, has := sm.ms["field2"]; !has {
t.Error("expected")
} else if !reflect.DeepEqual(v, exp) {
t.Errorf("expected %v,reeived %v", ToJSON(exp), ToJSON(v))
}
}
func TestSecureMapStorageGetKeys(t *testing.T) {
sm := &SecureMapStorage{
ms: MapStorage{
"field1": map[string]interface{}{
"subfield1": []string{"subkey"},
},
"field2": MapStorage{
"mapfield": map[string]interface{}{
"submapfield2": []string{"subkey"},
},
},
},
}
exp := []string{"*pre.field1.subfield1[0]", "*pre.field2.mapfield.submapfield2[0]"}
val := sm.GetKeys(true, 3, "*pre")
sort.Slice(val, func(i, j int) bool {
return val[i] < val[j]
})
if !reflect.DeepEqual(val, exp) {
t.Errorf("expected %+v,received %+v", ToJSON(exp), ToJSON(val))
}
}
func TestSecureMapStorageRemove(t *testing.T) {
sm := &SecureMapStorage{
ms: MapStorage{
"field": map[string]interface{}{
"subfield": uint16(3),
},
},
}
if err := sm.Remove([]string{"field", "subfield"}); err != nil {
t.Error(err)
}
if _, has := sm.ms["field"].(map[string]interface{})["subfield"]; has {
t.Error("should been removed")
}
}
func TestSecureMapStorageClone(t *testing.T) {
sm := &SecureMapStorage{
ms: MapStorage{
"field1": []string{"val1", "val2"},
"field2": "val",
},
}
if val := sm.Clone(); !reflect.DeepEqual(val, sm) {
t.Errorf("expected %+v,received %+v", ToJSON(sm.ms), ToJSON(val.ms))
}
}
func TestNewSecureMapStorage(t *testing.T) {
if sm := NewSecureMapStorage(); reflect.DeepEqual(sm, nil) {
t.Error("should receive new secure map")
}
}