Added unit tests for CallCache

This commit is contained in:
Trial97
2020-08-31 10:48:28 +03:00
committed by Dan Christian Bogos
parent ee78db2617
commit 0d5c619f47

View File

@@ -22,12 +22,14 @@ import (
"reflect"
"testing"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
"github.com/cgrates/rpcclient"
)
func TestComposeArgsReload(t *testing.T) {
apv1 := &APIerSv1{DataManager: &engine.DataManager{}}
apv1 := &APIerSv1{}
expArgs := utils.AttrReloadCacheWithOpts{
Opts: make(map[string]interface{}),
TenantArg: utils.TenantArg{Tenant: "cgrates.org"},
@@ -58,9 +60,156 @@ func TestComposeArgsReload(t *testing.T) {
}
if rply, err := apv1.composeArgsReload("cgrates.org", utils.CacheAttributeProfiles,
"cgrates.org:ATTR1", &[]string{"*string:~*req.Account:1001;~req.Subject", "*prefix:1001:~*req.Destination"}, []string{utils.MetaCDRs}, make(map[string]interface{})); err != nil {
"cgrates.org:ATTR1", &[]string{"*string:~*req.Account:1001;~req.Subject", "*prefix:1001:~*req.Destination", "*gt:~req.Usage:0"}, []string{utils.MetaCDRs}, make(map[string]interface{})); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(expArgs, rply) {
t.Errorf("Expected %s ,received: %s", utils.ToJSON(expArgs), utils.ToJSON(rply))
}
expArgs = utils.AttrReloadCacheWithOpts{
Opts: make(map[string]interface{}),
TenantArg: utils.TenantArg{Tenant: "cgrates.org"},
ArgsCache: map[string][]string{
utils.StatsQueueProfileIDs: {"cgrates.org:Stat2"},
utils.StatFilterIndexIDs: {
"cgrates.org:*string:*req.Account:1001",
"cgrates.org:*prefix:*req.Destination:1001",
},
},
}
if rply, err := apv1.composeArgsReload("cgrates.org", utils.CacheStatQueueProfiles,
"cgrates.org:Stat2", &[]string{"*string:~*req.Account:1001;~req.Subject", "*prefix:1001:~*req.Destination"}, nil, make(map[string]interface{})); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(expArgs, rply) {
t.Errorf("Expected %s ,received: %s", utils.ToJSON(expArgs), utils.ToJSON(rply))
}
expArgs.ArgsCache[utils.StatFilterIndexIDs] = []string{"cgrates.org:*none:*any:*any"}
if rply, err := apv1.composeArgsReload("cgrates.org", utils.CacheStatQueueProfiles,
"cgrates.org:Stat2", &[]string{}, []string{utils.MetaCDRs}, make(map[string]interface{})); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(expArgs, rply) {
t.Errorf("Expected %s ,received: %s", utils.ToJSON(expArgs), utils.ToJSON(rply))
}
if _, err := apv1.composeArgsReload("cgrates.org", utils.CacheStatQueueProfiles,
"cgrates.org:Stat2", &[]string{"FLTR1"}, []string{utils.MetaCDRs}, make(map[string]interface{})); err != utils.ErrNoDatabaseConn {
t.Fatal(err)
}
}
type rpcRequest struct {
Method string
Params interface{}
}
type rpcMock chan *rpcRequest
func (r rpcMock) Call(method string, args, _ interface{}) error {
r <- &rpcRequest{
Method: method,
Params: args,
}
return nil
}
func TestCallCache(t *testing.T) {
cache := make(rpcMock, 1)
ch := make(chan rpcclient.ClientConnector, 1)
ch <- cache
cn := engine.NewConnManager(config.CgrConfig(), map[string]chan rpcclient.ClientConnector{
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches): ch,
})
apv1 := &APIerSv1{
ConnMgr: cn,
Config: config.CgrConfig(),
}
if err := apv1.CallCache(utils.StringPointer(utils.META_NONE), "", "", "", nil, nil, nil); err != nil {
t.Fatal(err)
} else if len(cache) != 0 {
t.Fatal("Expected call cache to not be called")
}
exp := &rpcRequest{
Method: utils.CacheSv1Clear,
Params: &utils.AttrCacheIDsWithOpts{
TenantArg: utils.TenantArg{Tenant: "cgrates.org"},
CacheIDs: []string{utils.CacheStatQueueProfiles, utils.CacheStatFilterIndexes},
Opts: make(map[string]interface{}),
},
}
if err := apv1.CallCache(utils.StringPointer(utils.MetaClear), "cgrates.org", utils.CacheStatQueueProfiles, "", nil, nil, make(map[string]interface{})); err != nil {
t.Fatal(err)
} else if len(cache) != 1 {
t.Fatal("Expected call cache to be called")
} else if rply := <-cache; !reflect.DeepEqual(exp, rply) {
t.Errorf("Expected %s ,received: %s", utils.ToJSON(exp), utils.ToJSON(rply))
}
exp = &rpcRequest{
Method: utils.CacheSv1ReloadCache,
Params: utils.AttrReloadCacheWithOpts{
Opts: make(map[string]interface{}),
TenantArg: utils.TenantArg{Tenant: "cgrates.org"},
ArgsCache: map[string][]string{
utils.StatsQueueProfileIDs: {"cgrates.org:Stat2"},
utils.StatFilterIndexIDs: {
"cgrates.org:*string:*req.Account:1001",
"cgrates.org:*prefix:*req.Destination:1001",
},
},
},
}
if err := apv1.CallCache(utils.StringPointer(utils.MetaReload), "cgrates.org", utils.CacheStatQueueProfiles,
"cgrates.org:Stat2", &[]string{"*string:~*req.Account:1001;~req.Subject", "*prefix:1001:~*req.Destination"},
nil, make(map[string]interface{})); err != nil {
t.Fatal(err)
} else if len(cache) != 1 {
t.Fatal("Expected call cache to be called")
} else if rply := <-cache; !reflect.DeepEqual(exp, rply) {
t.Errorf("Expected %s ,received: %s", utils.ToJSON(exp), utils.ToJSON(rply))
}
exp.Method = utils.CacheSv1LoadCache
if err := apv1.CallCache(utils.StringPointer(utils.MetaLoad), "cgrates.org", utils.CacheStatQueueProfiles,
"cgrates.org:Stat2", &[]string{"*string:~*req.Account:1001;~req.Subject", "*prefix:1001:~*req.Destination"},
nil, make(map[string]interface{})); err != nil {
t.Fatal(err)
} else if len(cache) != 1 {
t.Fatal("Expected call cache to be called")
} else if rply := <-cache; !reflect.DeepEqual(exp, rply) {
t.Errorf("Expected %s ,received: %s", utils.ToJSON(exp), utils.ToJSON(rply))
}
exp.Method = utils.CacheSv1RemoveItems
if err := apv1.CallCache(utils.StringPointer(utils.MetaRemove), "cgrates.org", utils.CacheStatQueueProfiles,
"cgrates.org:Stat2", &[]string{"*string:~*req.Account:1001;~req.Subject", "*prefix:1001:~*req.Destination"},
nil, make(map[string]interface{})); err != nil {
t.Fatal(err)
} else if len(cache) != 1 {
t.Fatal("Expected call cache to be called")
} else if rply := <-cache; !reflect.DeepEqual(exp, rply) {
t.Errorf("Expected %s ,received: %s", utils.ToJSON(exp), utils.ToJSON(rply))
}
if err := apv1.CallCache(utils.StringPointer(utils.MetaLoad), "cgrates.org", utils.CacheStatQueueProfiles,
"cgrates.org:Stat2", &[]string{"FLTR1", "*prefix:1001:~*req.Destination"},
nil, make(map[string]interface{})); err != utils.ErrNoDatabaseConn {
t.Fatal(err)
} else if len(cache) != 0 {
t.Fatal("Expected call cache to not be called")
}
if err := apv1.CallCache(utils.StringPointer(utils.MetaRemove), "cgrates.org", utils.CacheStatQueueProfiles,
"cgrates.org:Stat2", &[]string{"FLTR1", "*prefix:1001:~*req.Destination"},
nil, make(map[string]interface{})); err != utils.ErrNoDatabaseConn {
t.Fatal(err)
} else if len(cache) != 0 {
t.Fatal("Expected call cache to not be called")
}
if err := apv1.CallCache(utils.StringPointer(utils.MetaReload), "cgrates.org", utils.CacheStatQueueProfiles,
"cgrates.org:Stat2", &[]string{"FLTR1", "*prefix:1001:~*req.Destination"},
nil, make(map[string]interface{})); err != utils.ErrNoDatabaseConn {
t.Fatal(err)
} else if len(cache) != 0 {
t.Fatal("Expected call cache to not be called")
}
}