mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-15 13:19:53 +05:00
Add coverage tests for engine
This commit is contained in:
committed by
Dan Christian Bogos
parent
f0a2695350
commit
0cd4ee5ca8
@@ -22,6 +22,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/birpc"
|
||||
"github.com/cgrates/birpc/context"
|
||||
"github.com/cgrates/cgrates/config"
|
||||
|
||||
@@ -1374,3 +1375,199 @@ func TestRouteSV1GetRoutesListErr(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestRouteSMatchingRouteProfilesForEventGetRouteProfileErr1(t *testing.T) {
|
||||
|
||||
defer func() {
|
||||
Cache = NewCacheS(config.CgrConfig(), nil, nil, nil)
|
||||
}()
|
||||
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
||||
cM := NewConnManager(cfg)
|
||||
dm := NewDataManager(data, config.CgrConfig().CacheCfg(), cM)
|
||||
fltrS := NewFilterS(cfg, cM, dm)
|
||||
rpS := NewRouteService(dm, fltrS, cfg, cM)
|
||||
|
||||
ev := []*utils.CGREvent{
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "utils.CGREvent1",
|
||||
Event: map[string]interface{}{
|
||||
"Route": "RouteProfile1",
|
||||
utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC),
|
||||
"UsageInterval": "1s",
|
||||
"PddInterval": "1s",
|
||||
utils.Weight: "20.0",
|
||||
},
|
||||
APIOpts: map[string]interface{}{
|
||||
utils.OptsRoutesProfilesCount: 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
prepareRoutesData(t, dm)
|
||||
|
||||
if err := Cache.Set(context.Background(), utils.CacheRouteProfiles, "cgrates.org:RouteProfile1", nil, []string{}, true, utils.NonTransactional); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if _, err := rpS.matchingRouteProfilesForEvent(context.Background(), "cgrates.org", ev[0]); err != utils.ErrNotFound {
|
||||
t.Errorf("Expected error <%v>, Received error <%v>", utils.ErrNotFound, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestRouteSMatchingRouteProfilesForEventGetRouteProfileErr2(t *testing.T) {
|
||||
|
||||
cfgtmp := config.CgrConfig()
|
||||
defer func() {
|
||||
Cache = NewCacheS(config.CgrConfig(), nil, nil, nil)
|
||||
config.SetCgrConfig(cfgtmp)
|
||||
}()
|
||||
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
cfg.CacheCfg().ReplicationConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaReplicator)}
|
||||
cfg.CacheCfg().Partitions[utils.CacheRouteProfiles].Replicate = true
|
||||
config.SetCgrConfig(cfg)
|
||||
|
||||
data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
||||
|
||||
cc := make(chan birpc.ClientConnector, 1)
|
||||
cc <- &ccMock{
|
||||
|
||||
calls: map[string]func(ctx *context.Context, args interface{}, reply interface{}) error{
|
||||
utils.CacheSv1ReplicateSet: func(ctx *context.Context, args, reply interface{}) error { return utils.ErrNotImplemented },
|
||||
},
|
||||
}
|
||||
|
||||
cM := NewConnManager(cfg)
|
||||
cM.AddInternalConn(utils.ConcatenatedKey(utils.MetaInternal, utils.MetaReplicator), utils.CacheSv1, cc)
|
||||
|
||||
dm := NewDataManager(data, config.CgrConfig().CacheCfg(), cM)
|
||||
fltrS := NewFilterS(cfg, cM, dm)
|
||||
rpS := NewRouteService(dm, fltrS, cfg, cM)
|
||||
|
||||
ev := []*utils.CGREvent{
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "utils.CGREvent1",
|
||||
Event: map[string]interface{}{
|
||||
"Route": "RouteProfile1",
|
||||
utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC),
|
||||
"UsageInterval": "1s",
|
||||
"PddInterval": "1s",
|
||||
utils.Weight: "20.0",
|
||||
},
|
||||
APIOpts: map[string]interface{}{
|
||||
utils.OptsRoutesProfilesCount: 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
Cache = NewCacheS(cfg, dm, cM, nil)
|
||||
|
||||
if err := dm.SetFilter(context.Background(), &Filter{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "FLTR_RPP_1",
|
||||
Rules: []*FilterRule{
|
||||
{
|
||||
Type: utils.MetaString,
|
||||
Element: "~*req.Route",
|
||||
Values: []string{"RouteProfile1"},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: "~*req.UsageInterval",
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Weight,
|
||||
Values: []string{"9.0"},
|
||||
},
|
||||
},
|
||||
}, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := dm.SetRouteProfile(context.Background(), testRoutesPrfs[0], true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := rpS.matchingRouteProfilesForEvent(context.Background(), "cgrates.org", ev[0]); err != utils.ErrNotImplemented {
|
||||
t.Errorf("Expected error <%v>, Received error <%v>", utils.ErrNotImplemented, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestRouteSMatchingRouteProfilesForEventPassErr(t *testing.T) {
|
||||
|
||||
defer func() {
|
||||
Cache = NewCacheS(config.CgrConfig(), nil, nil, nil)
|
||||
}()
|
||||
|
||||
Cache = NewCacheS(config.CgrConfig(), nil, nil, nil)
|
||||
cfg := config.NewDefaultCGRConfig()
|
||||
data := NewInternalDB(nil, nil, cfg.DataDbCfg().Items)
|
||||
cM := NewConnManager(cfg)
|
||||
dm := NewDataManager(data, config.CgrConfig().CacheCfg(), cM)
|
||||
fltrS := NewFilterS(cfg, cM, dm)
|
||||
rpS := NewRouteService(dm, fltrS, cfg, cM)
|
||||
|
||||
ev := []*utils.CGREvent{
|
||||
{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "utils.CGREvent1",
|
||||
Event: map[string]interface{}{
|
||||
"Route": "RouteProfile1",
|
||||
utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC),
|
||||
"UsageInterval": "1s",
|
||||
"PddInterval": "1s",
|
||||
utils.Weight: "20.0",
|
||||
},
|
||||
APIOpts: map[string]interface{}{
|
||||
utils.OptsRoutesProfilesCount: 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if err := dm.SetFilter(context.Background(), &Filter{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "FLTR_RPP_1",
|
||||
Rules: []*FilterRule{
|
||||
{
|
||||
Type: "bad input",
|
||||
Element: "bad input",
|
||||
Values: []string{"bad input"},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaString,
|
||||
Element: "~*req.Route",
|
||||
Values: []string{"RouteProfile1"},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: "~*req.UsageInterval",
|
||||
Values: []string{(time.Second).String()},
|
||||
},
|
||||
{
|
||||
Type: utils.MetaGreaterOrEqual,
|
||||
Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Weight,
|
||||
Values: []string{"9.0"},
|
||||
},
|
||||
},
|
||||
}, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := dm.SetRouteProfile(context.Background(), testRoutesPrfs[0], true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expErr := "NOT_IMPLEMENTED:bad input"
|
||||
if _, err := rpS.matchingRouteProfilesForEvent(context.Background(), "cgrates.org", ev[0]); err == nil || err.Error() != expErr {
|
||||
t.Errorf("Expected error <%v>, received <%v>", expErr, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3838,3 +3838,144 @@ func TestStatDistinctClone(t *testing.T) {
|
||||
t.Errorf("Expecting <%+v>,\n Recevied <%+v>", dst, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestACCAddEventErr(t *testing.T) {
|
||||
acc := NewACC(2, "", nil)
|
||||
ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
APIOpts: map[string]interface{}{
|
||||
utils.MetaStartTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
utils.MetaCost: "wrong input"}}
|
||||
|
||||
expErr := "can't convert <wrong input> to decimal"
|
||||
if err := acc.AddEvent(ev.ID, utils.MapStorage{utils.MetaOpts: ev.APIOpts}); err == nil || err.Error() != expErr {
|
||||
t.Errorf("Expected error: <%v> received <%v>", expErr, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestTCCAddEventErr(t *testing.T) {
|
||||
tcc := NewTCC(2, "", nil)
|
||||
ev := &utils.CGREvent{Tenant: "cgrates.org", ID: "EVENT_1",
|
||||
APIOpts: map[string]interface{}{
|
||||
utils.MetaStartTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
utils.MetaCost: "wrong input"}}
|
||||
|
||||
expErr := "can't convert <wrong input> to decimal"
|
||||
if err := tcc.AddEvent(ev.ID, utils.MapStorage{utils.MetaOpts: ev.APIOpts}); err == nil || err.Error() != expErr {
|
||||
t.Errorf("Expected error: <%v> received <%v>", expErr, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestDDCGetFilterIDs(t *testing.T) {
|
||||
|
||||
ddc := NewDDC(2, "", []string{"flt1", "flt2"})
|
||||
|
||||
exp := &StatDDC{
|
||||
FilterIDs: []string{"flt1", "flt2"},
|
||||
}
|
||||
|
||||
if rcv := ddc.GetFilterIDs(); !reflect.DeepEqual(rcv, exp.FilterIDs) {
|
||||
t.Errorf("Expecting <%+v>,\n Recevied <%+v>", utils.ToJSON(exp.FilterIDs), utils.ToJSON(rcv))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMetricClone(t *testing.T) {
|
||||
|
||||
sum := &Metric{
|
||||
Value: utils.NewDecimal(2, 0),
|
||||
Events: map[string]*DecimalWithCompress{
|
||||
"Event1": {
|
||||
Stat: utils.NewDecimal(int64(time.Second), 0),
|
||||
CompressFactor: 200000000,
|
||||
},
|
||||
},
|
||||
MinItems: 3,
|
||||
Count: 3,
|
||||
}
|
||||
|
||||
if rcv := sum.Clone(); !reflect.DeepEqual(rcv, sum) {
|
||||
t.Errorf("Expecting <%+v>,\n Recevied <%+v>", sum, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetricEqualFalse(t *testing.T) {
|
||||
|
||||
sum := &Metric{
|
||||
Value: utils.NewDecimal(2, 0),
|
||||
Events: map[string]*DecimalWithCompress{
|
||||
"Event1": {
|
||||
Stat: utils.NewDecimal(int64(time.Second), 0),
|
||||
CompressFactor: 200000000,
|
||||
},
|
||||
},
|
||||
MinItems: 3,
|
||||
Count: 3,
|
||||
}
|
||||
|
||||
sum2 := &Metric{
|
||||
Value: utils.NewDecimal(2, 0),
|
||||
Events: map[string]*DecimalWithCompress{
|
||||
"Event1": {
|
||||
Stat: utils.NewDecimal(int64(time.Second), 0),
|
||||
CompressFactor: 200000000,
|
||||
},
|
||||
"Event2": {
|
||||
Stat: utils.NewDecimal(int64(time.Second), 0),
|
||||
CompressFactor: 200000000,
|
||||
},
|
||||
},
|
||||
MinItems: 3,
|
||||
Count: 3,
|
||||
}
|
||||
|
||||
if rcv := sum.Equal(sum2); rcv {
|
||||
t.Errorf("Expecting to not be equal, Recevied equal <%v>", rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetricEqualEventFalse(t *testing.T) {
|
||||
|
||||
sum := &Metric{
|
||||
Value: utils.NewDecimal(2, 0),
|
||||
Events: map[string]*DecimalWithCompress{
|
||||
"even1": {
|
||||
Stat: utils.NewDecimal(int64(time.Second), 0),
|
||||
CompressFactor: 200000000,
|
||||
},
|
||||
},
|
||||
MinItems: 3,
|
||||
Count: 3,
|
||||
}
|
||||
|
||||
sum2 := &Metric{
|
||||
Value: utils.NewDecimal(2, 0),
|
||||
Events: map[string]*DecimalWithCompress{
|
||||
"even1": {
|
||||
Stat: utils.NewDecimal(int64(time.Second), 0),
|
||||
CompressFactor: 1,
|
||||
},
|
||||
},
|
||||
MinItems: 3,
|
||||
Count: 3,
|
||||
}
|
||||
|
||||
if rcv := sum.Equal(sum2); rcv {
|
||||
t.Errorf("Expecting to not be equal, Recevied equal <%v>", rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatDistinctGetFilterIDs(t *testing.T) {
|
||||
|
||||
dst := NewStatDistinct(2, "", []string{"flt1", "flt2"})
|
||||
|
||||
exp := &StatDistinct{
|
||||
FilterIDs: []string{"flt1", "flt2"},
|
||||
}
|
||||
|
||||
if rcv := dst.GetFilterIDs(); !reflect.DeepEqual(rcv, exp.FilterIDs) {
|
||||
t.Errorf("Expecting <%+v>,\n Recevied <%+v>", utils.ToJSON(exp.FilterIDs), utils.ToJSON(rcv))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user