From bcfd9cb1f94cafa9cbd7658816ae49249bac60ba Mon Sep 17 00:00:00 2001 From: NikolasPetriti Date: Tue, 25 Jul 2023 16:52:30 +0200 Subject: [PATCH] Add coverage tests for engine --- engine/action_trigger_test.go | 228 ++++++++++++++++++++++++++++++++++ engine/caches_test.go | 85 ++++++++++++- 2 files changed, 308 insertions(+), 5 deletions(-) diff --git a/engine/action_trigger_test.go b/engine/action_trigger_test.go index b2c6bf21d..0022e6e1c 100644 --- a/engine/action_trigger_test.go +++ b/engine/action_trigger_test.go @@ -19,6 +19,7 @@ along with this program. If not, see package engine import ( + "encoding/json" "reflect" "testing" "time" @@ -116,3 +117,230 @@ func TestActionTriggersClone(t *testing.T) { } } + +func TestActionTriggerExecute(t *testing.T) { + at := ActionTrigger{ + Recurrent: true, + LastExecutionTime: time.Now(), + MinSleep: 1 * time.Hour, + } + at2 := ActionTrigger{} + + tests := []struct { + name string + arg *Account + err string + }{ + { + name: "min sleep time", + arg: &Account{}, + err: "", + }, + { + name: "account disabled", + arg: &Account{ + Disabled: true, + }, + err: "User is disabled and there are triggers in action!", + }, + } + + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var err error + if i == 0 { + err = at.Execute(tt.arg) + } else if i == 1 { + err = at2.Execute(tt.arg) + } + + if err != nil { + if err.Error() != tt.err { + t.Error(err) + } + } + }) + } +} + +func TestActionCreateBalance(t *testing.T) { + str := "test" + fl := 1.4 + tm := time.Date(2021, 8, 15, 14, 30, 45, 100, time.Local) + sm := utils.StringMap{"test": true} + rt := RITiming{ + Years: utils.Years{2019}, + Months: utils.Months{1}, + MonthDays: utils.MonthDays{6}, + WeekDays: utils.WeekDays{5}, + StartTime: str, + EndTime: str, + cronString: str, + tag: str, + } + bl := true + + at := ActionTrigger{ + UniqueID: "test", + Balance: &BalanceFilter{ + Uuid: &str, + ID: &str, + Type: &str, + Value: &utils.ValueFormula{ + Method: str, + Params: map[string]any{"test": 1}, + Static: fl, + }, + ExpirationDate: &tm, + Weight: &fl, + DestinationIDs: &sm, + RatingSubject: &str, + Categories: &sm, + SharedGroups: &sm, + TimingIDs: &sm, + Timings: []*RITiming{&rt}, + Disabled: &bl, + Factor: &ValueFactor{}, + Blocker: &bl, + }, + } + + rcv := at.CreateBalance() + + exp := &Balance{ + Uuid: str, + ID: str, + Value: 0.0, + ExpirationDate: tm, + Weight: fl, + DestinationIDs: sm, + RatingSubject: str, + Categories: sm, + SharedGroups: sm, + Timings: []*RITiming{&rt}, + TimingIDs: sm, + Disabled: bl, + Factor: nil, + Blocker: bl, + } + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", utils.ToJSON(rcv), utils.ToJSON(exp)) + } +} + +func TestActionTriggerEquals(t *testing.T) { + str := "test" + fl := 1.4 + tm := time.Date(2021, 8, 15, 14, 30, 45, 100, time.Local) + sm := utils.StringMap{"test": true} + rt := RITiming{ + Years: utils.Years{2019}, + Months: utils.Months{1}, + MonthDays: utils.MonthDays{6}, + WeekDays: utils.WeekDays{5}, + StartTime: str, + EndTime: str, + cronString: str, + tag: str, + } + bl := true + + at := ActionTrigger{ + ID: "test", + UniqueID: "test", + Balance: &BalanceFilter{ + Uuid: &str, + ID: &str, + Type: &str, + Value: &utils.ValueFormula{ + Method: str, + Params: map[string]any{"test": 1}, + Static: fl, + }, + ExpirationDate: &tm, + Weight: &fl, + DestinationIDs: &sm, + RatingSubject: &str, + Categories: &sm, + SharedGroups: &sm, + TimingIDs: &sm, + Timings: []*RITiming{&rt}, + Disabled: &bl, + Factor: &ValueFactor{}, + Blocker: &bl, + }, + } + + arg := ActionTrigger{ + ID: "test1", + UniqueID: "test1", + Balance: &BalanceFilter{ + Uuid: &str, + ID: &str, + Type: &str, + Value: &utils.ValueFormula{ + Method: str, + Params: map[string]any{"test": 1}, + Static: fl, + }, + ExpirationDate: &tm, + Weight: &fl, + DestinationIDs: &sm, + RatingSubject: &str, + Categories: &sm, + SharedGroups: &sm, + TimingIDs: &sm, + Timings: []*RITiming{&rt}, + Disabled: &bl, + Factor: &ValueFactor{}, + Blocker: &bl, + }, + } + + rcv := at.Equals(&arg) + + if rcv != false { + t.Error(rcv) + } +} + +func TestActionTriggerMatch(t *testing.T) { + strT := "*string" + str := "test" + + at := ActionTrigger{ + UniqueID: "test", + Balance: &BalanceFilter{ + Type: &strT, + }, + } + + tj := struct { + GroupID string + UniqueID string + ThresholdType string + }{ + GroupID: "", + UniqueID: str, + ThresholdType: str, + } + + path, err := json.Marshal(tj) + if err != nil { + t.Error(err) + } + + a := Action{ + Balance: &BalanceFilter{ + Type: &strT, + }, + ExtraParameters: string(path), + } + + rcv := at.Match(&a) + + if rcv != true { + t.Error(rcv) + } +} \ No newline at end of file diff --git a/engine/caches_test.go b/engine/caches_test.go index 95bbb091c..b9d9f8c05 100644 --- a/engine/caches_test.go +++ b/engine/caches_test.go @@ -454,16 +454,19 @@ func TestCachetoStringSlice(t *testing.T) { func TestCacheSV1ReloadCacheErrors(t *testing.T) { str := "test" + slc := []string{str} type args struct { attrs utils.AttrReloadCacheWithArgDispatcher reply *string } + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + chS := CacheS{ cfg: &config.CGRConfig{}, - dm: &DataManager{ - cacheCfg: config.CacheCfg{}, - }, + dm: dm, } tests := []struct { @@ -483,12 +486,36 @@ func TestCacheSV1ReloadCacheErrors(t *testing.T) { err: "", }, { - name: "REload destination error", + name: "Reload destination error", args: args{attrs: utils.AttrReloadCacheWithArgDispatcher{ &utils.ArgDispatcher{}, utils.TenantArg{}, utils.AttrReloadCache{ - FlushAll: true, + utils.ArgsCache{ + DestinationIDs: slc, + ReverseDestinationIDs: slc, + RatingPlanIDs: slc, + RatingProfileIDs: slc, + ActionIDs: slc, + ActionPlanIDs: slc, + AccountActionPlanIDs: slc, + ActionTriggerIDs: slc, + SharedGroupIDs: slc, + ResourceProfileIDs: slc, + ResourceIDs: slc, + StatsQueueIDs: slc, + StatsQueueProfileIDs: slc, + ThresholdIDs: slc, + ThresholdProfileIDs: slc, + FilterIDs: slc, + SupplierProfileIDs: slc, + AttributeProfileIDs: slc, + ChargerProfileIDs: slc, + DispatcherProfileIDs: slc, + DispatcherHostIDs: slc, + DispatcherRoutesIDs: slc, + }, + false, }, }, reply: &str}, err: "err", @@ -507,3 +534,51 @@ func TestCacheSV1ReloadCacheErrors(t *testing.T) { }) } } + +func TestCacheSV1LoadCache(t *testing.T) { + str := "test" + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + + chS := CacheS{ + cfg: &config.CGRConfig{}, + dm: dm, + } + + err := chS.V1LoadCache(utils.AttrReloadCacheWithArgDispatcher{ + &utils.ArgDispatcher{}, + utils.TenantArg{}, + utils.AttrReloadCache{ + FlushAll: true, + }, + }, &str) + + if err != nil { + t.Error(err) + } +} + +func TestCacheSV1FlushCacheFlushAll(t *testing.T) { + str := "test" + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + + chS := CacheS{ + cfg: &config.CGRConfig{}, + dm: dm, + } + + err := chS.V1FlushCache(utils.AttrReloadCacheWithArgDispatcher{ + &utils.ArgDispatcher{}, + utils.TenantArg{}, + utils.AttrReloadCache{ + FlushAll: true, + }, + }, &str) + + if err != nil { + t.Error(err) + } +}