From bb4a944135e7b90fa63bbf2616ccd31f048ecc12 Mon Sep 17 00:00:00 2001 From: NikolasPetriti Date: Mon, 31 Jul 2023 16:26:07 +0200 Subject: [PATCH] Add coverage tests for engine --- engine/action_plan_test.go | 88 +++++++++++++++++ engine/action_test.go | 183 +++++++++++++++++++++++++++++++++-- engine/mapevent_test.go | 23 +++++ engine/model_helpers_test.go | 75 ++++++++++++++ 4 files changed, 359 insertions(+), 10 deletions(-) diff --git a/engine/action_plan_test.go b/engine/action_plan_test.go index d1ebb7c5d..5bba6ee04 100644 --- a/engine/action_plan_test.go +++ b/engine/action_plan_test.go @@ -376,3 +376,91 @@ func TestGetNextStartTimeOld(t *testing.T) { }) } } + +func TestActionPlanCloneNilReturn(t *testing.T) { + var at *ActionTiming + + rcv := at.Clone() + + if rcv != nil { + t.Error(rcv) + } +} + +func TestActionPlanGetNextStartTimeOld(t *testing.T) { + zr := time.Date(0, 0, 0, 0, 0, 0, 0, time.Local) + at := ActionTiming{ + stCache: zr, + } + var zr2 time.Time + at2 := ActionTiming{ + stCache: zr2, + Timing: nil, + } + var zr3 time.Time + at3 := ActionTiming{ + stCache: zr3, + Timing: &RateInterval{ + Timing: &RITiming{ + StartTime: "test", + }, + }, + } + var zr4 time.Time + at4 := ActionTiming{ + stCache: zr4, + Timing: &RateInterval{ + Timing: &RITiming{ + StartTime: "", + Years: utils.Years{2021}, + Months: utils.Months{2}, + MonthDays: utils.MonthDays{2}, + WeekDays: utils.WeekDays{2}, + EndTime: "08:09:23", + cronString: "test", + tag: "test", + }, + }, + } + + tests := []struct { + name string + exp time.Time + }{ + { + name: "stCache is not zero", + exp: zr, + }, + { + name: "timing nil", + exp: zr2, + }, + { + name: "cannot parse start time", + exp: zr3, + }, + { + name: "empty timings", + exp: at4.GetNextStartTimeOld(time.Now()), + }, + } + + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var rcv time.Time + if i == 0 { + rcv = at.GetNextStartTimeOld(time.Now()) + } else if i == 1 { + rcv = at2.GetNextStartTimeOld(time.Now()) + } else if i == 2 { + rcv = at3.GetNextStartTimeOld(time.Now()) + } else if i == 3 { + rcv = at4.GetNextStartTimeOld(time.Now()) + } + + if rcv != tt.exp { + t.Error("received", rcv, "expected", tt.exp) + } + }) + } +} diff --git a/engine/action_test.go b/engine/action_test.go index ca61a3768..a3b067b7c 100644 --- a/engine/action_test.go +++ b/engine/action_test.go @@ -196,7 +196,7 @@ var ac Account = Account{ UpdateTime: tm2, executingTriggers: bl, } -var a Action = Action{ +var a *Action = &Action{ Id: str2, ActionType: str2, ExtraParameters: str2, @@ -241,7 +241,7 @@ func TestActionresetAction(t *testing.T) { acs := Actions{} - err := debitResetAction(&ac, &a, acs, nil) + err := debitResetAction(&ac, a, acs, nil) if err != nil { if err.Error() != "" { t.Error(err) @@ -287,12 +287,6 @@ func TestActiongetOneData(t *testing.T) { } func TestAccountsendErrors(t *testing.T) { - type args struct { - ub *Account - a *Action - acs Actions - extraData any - } tests := []struct { name string rcv error @@ -333,6 +327,11 @@ func TestAccountsendErrors(t *testing.T) { rcv: callURLAsync(nil, nil, nil, make(chan int)), exp: "json: unsupported type: chan int", }, + { + name: "postEvent error check", + rcv: postEvent(nil, nil, nil, make(chan int)), + exp: "json: unsupported type: chan int", + }, } for _, tt := range tests { @@ -457,7 +456,7 @@ func TestActionCloneNil(t *testing.T) { var cdrP cdrLogProvider = cdrLogProvider{ acnt: &ac, - action: &a, + action: a, cache: utils.MapStorage{}, } @@ -473,7 +472,7 @@ func TestActionString(t *testing.T) { func TestActionFieldAsInterface(t *testing.T) { cdrP := cdrLogProvider{ acnt: &ac, - action: &a, + action: a, cache: utils.MapStorage{}, } @@ -598,4 +597,168 @@ func TestActionRemoteHost(t *testing.T) { if rcv.String() != "local" { t.Error(rcv) } +} + +func TestActionresetAccountErrors(t *testing.T) { + type args struct { + ub *Account + action *Action + acts Actions + } + tests := []struct{ + name string + args args + err string + }{ + { + name: "nil account", + args: args{ub: nil, action: nil, acts: nil}, + err: "nil account", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := resetAccount(tt.args.ub, tt.args.action, tt.args.acts, nil) + + if err != nil { + if err.Error() != tt.err { + t.Error(err) + } + } + }) + } +} + +func TestActionremoveExpired(t *testing.T) { + sm := utils.StringMap{ + "test1": bl, + } + rtm := RITiming{ + Years: utils.Years{2021}, + Months: utils.Months{8}, + MonthDays: utils.MonthDays{28}, + WeekDays: utils.WeekDays{5}, + StartTime: str, + EndTime: str, + cronString: str, + tag: str, + } + blc := Balance{ + Uuid: str2, + ID: str2, + Value: fl2, + ExpirationDate: tm2, + Weight: fl2, + DestinationIDs: sm, + RatingSubject: str2, + Categories: sm, + SharedGroups: sm, + Timings: []*RITiming{&rtm}, + TimingIDs: sm, + Disabled: bl, + Factor: ValueFactor{str2: fl2}, + Blocker: bl, + precision: nm2, + dirty: bl, + } + acc := &Account{ + BalanceMap: map[string]Balances{"test1": {&blc}}, + } + + type args struct { + ub *Account + action *Action + acts Actions + extra any + } + tests := []struct{ + name string + args args + err string + }{ + { + name: "nil account", + args: args{nil, nil, nil, nil}, + err: "nil account for null action", + }, + { + name: "nil account", + args: args{acc, a, nil, nil}, + err: "NOT_FOUND", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := removeExpired(tt.args.ub, tt.args.action, tt.args.acts, nil) + + if err != nil { + if err.Error() != tt.err { + t.Error(err) + } + } + }) + } +} + +func TestActiontopupZeroNegativeAction(t *testing.T) { + ub := &Account{} + + topupZeroNegativeAction(ub, nil, nil, nil) + + if ub.BalanceMap == nil { + t.Error("didn't update") + } +} + +func TestActionremoveAccountAction(t *testing.T) { + ub := &Account{ + ID: "", + } + actn := &Action{ + ExtraParameters: str2, + } + actn2 := &Action{ + ExtraParameters: "", + } + type args struct { + ub *Account + action *Action + acts Actions + extra any + } + tests := []struct{ + name string + args args + err string + }{ + { + name: "error json unmarshal", + args: args{nil, actn, nil, nil}, + err: "invalid character 'e' in literal true (expecting 'r')", + }, + { + name: "concatenate key", + args: args{nil, actn2, nil, nil}, + err: "", + }, + { + name: "accID empty string", + args: args{ub, actn2, nil, nil}, + err: "INVALID_KEY", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := removeAccountAction(tt.args.ub, tt.args.action, tt.args.acts, tt.args.extra) + + if err != nil { + if err.Error() != tt.err { + t.Error(err) + } + } + }) + } } \ No newline at end of file diff --git a/engine/mapevent_test.go b/engine/mapevent_test.go index 0004483c4..199c46e0d 100644 --- a/engine/mapevent_test.go +++ b/engine/mapevent_test.go @@ -748,3 +748,26 @@ func TestMapEventGetDurationPtrOrDefault(t *testing.T) { t.Errorf("Expected: %+v, received: %+v", newVal, ptr) } } + +func TestMapEventCloneNil(t *testing.T) { + var me MapEvent + + rcv := me.Clone() + + if rcv != nil { + t.Error(rcv) + } +} + +func TestMapEventData(t *testing.T) { + me := MapEvent{ + "test": "val1", + } + + rcv := me.Data() + exp := map[string]any{"test": "val1"} + + if !reflect.DeepEqual(rcv, exp) { + t.Error(rcv) + } +} \ No newline at end of file diff --git a/engine/model_helpers_test.go b/engine/model_helpers_test.go index bb1cb1104..6e0199078 100644 --- a/engine/model_helpers_test.go +++ b/engine/model_helpers_test.go @@ -1899,3 +1899,78 @@ func TestTPSuppliersAsTPSupplierProfiles(t *testing.T) { t.Errorf("Expecting: %+v,\nReceived: %+v", utils.ToJSON(expPrfRev), utils.ToJSON(rcvRev)) } } + +func TestModelHelpersAsMapDestinations(t *testing.T) { + tp := TpDestination{ + Id: 1, + Tpid: "test", + Tag: "test", + Prefix: "test", + CreatedAt: time.Now(), + } + tps := TpDestinations{tp} + + d := &Destination{ + Id: "test", + Prefixes: []string{"test"}, + } + exp := map[string]*Destination{"test": d} + + rcv, err := tps.AsMapDestinations() + if err != nil { + t.Error(err) + } + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", exp, rcv) + } +} + +func TestModelHelpersAPItoModelDestination(t *testing.T) { + d := &utils.TPDestination{ + TPid: "test", + ID: "test", + } + + tp := TpDestination{ + Tpid: "test", + Tag: "test", + } + exp := TpDestinations{tp} + + rcv := APItoModelDestination(d) + + if !reflect.DeepEqual(exp, rcv) { + t.Errorf("expected %v, received %v", exp, rcv) + } +} + +func TestModelHelpersAPItoModelTimings(t *testing.T) { + ts := []*utils.ApierTPTiming{{ + TPid: str, + ID: str, + Years: str, + Months: str, + MonthDays: str, + WeekDays: str, + Time: str, + }} + + tp := TpTiming{ + Id: 0, + Tpid: str, + Tag: str, + Years: str, + Months: str, + MonthDays: str, + WeekDays: str, + Time: str, + } + exp := TpTimings{tp} + + rcv := APItoModelTimings(ts) + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", exp, rcv) + } +}