Improving coverage of unit tests

This commit is contained in:
gezimbll
2023-04-06 10:53:33 -04:00
committed by Dan Christian Bogos
parent ae037931d0
commit ff8b23e93f
4 changed files with 262 additions and 3 deletions

View File

@@ -142,6 +142,7 @@ func TestDmMatchFilterIndexFromKey(t *testing.T) {
cfg2, _ := config.NewDefaultCGRConfig()
config.SetCgrConfig(cfg2)
}()
Cache.Clear(nil)
db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
cfg.DataDbCfg().Items[utils.MetaFilterIndexes].Remote = true
cfg.DataDbCfg().RmtConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)}
@@ -184,10 +185,9 @@ func TestDmMatchFilterIndexFromKey(t *testing.T) {
t.Error(err)
}
config.SetCgrConfig(cfg)
if err := dm.MatchFilterIndexFromKey(utils.CacheResourceFilterIndexes, "cgrates.org:*string:Account:1002"); err == nil {
if err := dm.MatchFilterIndexFromKey(utils.CacheResourceFilterIndexes, "cgrates.org:*string:Account:1002"); err != nil {
t.Error(err)
}
//unifinished
}
func TestCacheDataFromDB(t *testing.T) {
@@ -218,6 +218,12 @@ func TestCacheDataFromDB(t *testing.T) {
Weight: 20,
},
}
dest := &Destination{
Id: "DEST", Prefixes: []string{"1004", "1002", "1002"},
}
dm.SetDestination(dest, "")
dm.SetReverseDestination(dest, "")
for _, chg := range chgS {
if err := dm.SetChargerProfile(chg, true); err != nil {
t.Error(err)
@@ -226,6 +232,14 @@ func TestCacheDataFromDB(t *testing.T) {
if err := dm.CacheDataFromDB(utils.ChargerProfilePrefix, nil, false); err != nil {
t.Error(err)
}
if err := dm.CacheDataFromDB(utils.DESTINATION_PREFIX, nil, false); err != nil {
t.Error(err)
}
if err := dm.CacheDataFromDB(utils.REVERSE_DESTINATION_PREFIX, nil, false); err != nil {
t.Error(err)
}
}
func TestCacheDataFromDBFilterIndexes(t *testing.T) {
@@ -947,3 +961,59 @@ func TestThresholdProfileSetWithIndex(t *testing.T) {
t.Error(err)
}
}
func TestDmAllActionPlans(t *testing.T) {
cfg, _ := config.NewDefaultCGRConfig()
db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
dm := NewDataManager(db, cfg.CacheCfg(), nil)
apS := []*ActionPlan{
{
Id: "AP1",
AccountIDs: utils.StringMap{"cgrates.org:1001": true},
ActionTimings: []*ActionTiming{
{
Uuid: utils.GenUUID(),
Timing: &RateInterval{
Timing: &RITiming{
Years: utils.Years{2022},
Months: utils.Months{},
MonthDays: utils.MonthDays{},
WeekDays: utils.WeekDays{},
StartTime: utils.ASAP,
},
},
Weight: 10,
ActionsID: "ACT_1",
},
},
},
{
Id: "AP2",
AccountIDs: utils.StringMap{"cgrates.org:1001": true},
ActionTimings: []*ActionTiming{{
Uuid: utils.GenUUID(),
Timing: &RateInterval{
Timing: &RITiming{
Years: utils.Years{2022},
Months: utils.Months{},
MonthDays: utils.MonthDays{},
WeekDays: utils.WeekDays{},
StartTime: utils.ASAP,
},
},
Weight: 10,
ActionsID: "ACT_2",
},
},
}}
expMap := make(map[string]*ActionPlan)
for _, ap := range apS {
dm.SetActionPlan(ap.Id, ap, true, utils.NonTransactional)
expMap[ap.Id] = ap
}
if rpl, err := dm.GetAllActionPlans(); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expMap, rpl) {
t.Errorf("Expected %+v,Received %+v", utils.ToJSON(expMap), utils.ToJSON(rpl))
}
}

View File

@@ -1129,5 +1129,4 @@ func TestResponderDebitCached(t *testing.T) {
if err := rsponder.Debit(cd, &reply); err == nil {
t.Error(err)
}
}

View File

@@ -632,3 +632,93 @@ func TestStatSGetQueueIDs(t *testing.T) {
t.Errorf("Expected %v,Received %v", expqIds, qIDs)
}
}
func TestStatSReloadRunBackUp(t *testing.T) {
cfg, _ := config.NewDefaultCGRConfig()
cfg.StatSCfg().StoreInterval = 1
Cache.Clear(nil)
db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
dm := NewDataManager(db, cfg.CacheCfg(), nil)
sts, err := NewStatService(dm, cfg, nil, nil)
sts.storedStatQueues["SQ1"] = true
if err != nil {
t.Error(err)
}
go func() {
time.Sleep(2 * time.Millisecond)
sts.loopStoped <- struct{}{}
}()
Cache.Set(utils.CacheStatQueues, "SQ1", &StatQueue{
Tenant: "cgrates.org",
ID: "SQ1",
SQMetrics: map[string]StatMetric{
utils.MetaASR: &StatASR{
Answered: 1,
Count: 2,
Events: map[string]*StatWithCompress{
"cgrates.org:TestRemEventWithID_1": {Stat: 1, CompressFactor: 1},
},
},
}}, []string{}, true, utils.NonTransactional)
sts.Reload()
}
func TestStatProcessEvent2(t *testing.T) {
cfg, _ := config.NewDefaultCGRConfig()
tpDm := dm
defer func() {
dm = tpDm
}()
db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
dm := NewDataManager(db, cfg.CacheCfg(), nil)
sts, err := NewStatService(dm, cfg, nil, nil)
if err != nil {
t.Error(err)
}
args := &StatsArgsProcessEvent{
CGREvent: &utils.CGREvent{
Tenant: "cgrates.org",
ID: "event1",
Event: map[string]interface{}{
utils.Account: "1001",
utils.AnswerTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
utils.Usage: time.Duration(135 * time.Second),
utils.COST: 123.0,
utils.PDD: time.Duration(12 * time.Second),
},
},
ArgDispatcher: &utils.ArgDispatcher{
APIKey: utils.StringPointer("12345"),
},
}
fltr := &Filter{
Tenant: "cgrates.org",
ID: "FLTR_TH_Stats1",
Rules: []*FilterRule{
{
Type: "*lt",
Element: "~*req.Cost",
Values: []string{"120.0"},
},
},
}
dm.SetFilter(fltr)
dm.SetStatQueueProfile(&StatQueueProfile{
Tenant: "cgrates.org",
ID: "STS_PoccessCDR",
FilterIDs: []string{"FLTR_TH_Stats1"},
Metrics: []*MetricWithFilters{{
MetricID: "*sum:~*req.Usage",
}},
ThresholdIDs: []string{utils.META_NONE},
Blocker: true,
Stored: true,
Weight: 20,
MinItems: 0,
}, true)
SetDataStorage(dm)
if _, err := sts.processEvent(args); err == nil {
t.Error(err)
}
//unfinished
}

View File

@@ -1163,6 +1163,15 @@ func TestTpRLoadAll(t *testing.T) {
WeekDays: "1;2;3;4;5",
Time: "19:00:00",
},
{
TPid: tpId,
ID: "ASAP",
Years: "*any",
Months: "*any",
MonthDays: "*any",
WeekDays: "1;2;3;4;5",
Time: "08:00:00",
},
}
ratingPlans := []*utils.TPRatingPlan{
@@ -1179,6 +1188,78 @@ func TestTpRLoadAll(t *testing.T) {
},
}
ratingProfiles := []*utils.TPRatingProfile{
{
TPid: tpId,
LoadId: "TEST_LOADID",
Tenant: "cgrates.org",
Category: "call",
Subject: "*any",
RatingPlanActivations: []*utils.TPRatingActivation{
{
ActivationTime: "2022-01-14T00:00:00Z",
RatingPlanId: "RP_1"},
},
},
}
actions := []*utils.TPActions{
{TPid: tpId,
ID: "TOPUP_RST_10",
Actions: []*utils.TPAction{
{
Identifier: "*topup_reset",
BalanceType: "*monetary",
Units: "5.0",
ExpiryTime: "*never",
DestinationIds: "*any",
Categories: "call",
BalanceWeight: "10.0",
Weight: 10.0},
},
},
}
actionPlans := []*utils.TPActionPlan{
{
TPid: tpId,
ID: "PACKAGE_10",
ActionPlan: []*utils.TPActionTiming{
{
ActionsId: "TOPUP_RST_10",
TimingId: "ASAP",
Weight: 10.0},
},
},
}
actionTriggers := []*utils.TPActionTriggers{
{
TPid: tpId,
ID: "STANDARD_TRIGGERS",
ActionTriggers: []*utils.TPActionTrigger{
{
Id: "STANDARD_TRIGGERS",
UniqueID: "1",
ThresholdType: "*min_balance",
ThresholdValue: 2.0,
Recurrent: false,
Weight: 10},
},
},
}
accounts := []*utils.TPAccountActions{
{
TPid: tpId,
LoadId: "TEST_LOADID",
Tenant: "cgrates.org",
Account: "1001",
ActionPlanId: "PACKAGE_10",
ActionTriggersId: "STANDARD_TRIGGERS",
},
}
if err := storDb.SetTPDestinations(dests); err != nil {
t.Error(err)
}
@@ -1201,6 +1282,25 @@ func TestTpRLoadAll(t *testing.T) {
if err := storDb.SetTPRatingPlans(ratingPlans); err != nil {
t.Error(err)
}
if err := storDb.SetTPRatingProfiles(ratingProfiles); err != nil {
t.Error(err)
}
if err := storDb.SetTPActions(actions); err != nil {
t.Error(err)
}
if err := storDb.SetTPActionPlans(actionPlans); err != nil {
t.Error(err)
}
if err := storDb.SetTPActionTriggers(actionTriggers); err != nil {
t.Error(err)
}
if err := storDb.SetTPAccountActions(accounts); err != nil {
t.Error(err)
}
if err := tpr.LoadAll(); err != nil {
t.Error(err)