From 3e7394de757c09565c62a596dd45cb95f7c45053 Mon Sep 17 00:00:00 2001 From: NikolasPetriti Date: Tue, 22 Aug 2023 16:46:13 +0200 Subject: [PATCH] Add coverage tests for engine --- engine/account_test.go | 24 ++++ engine/sharedgroup_test.go | 43 +++++++ engine/statmetrics_test.go | 244 +++++++++++++++++++++++++++++++++++++ 3 files changed, 311 insertions(+) diff --git a/engine/account_test.go b/engine/account_test.go index 9ada37235..38db386bb 100644 --- a/engine/account_test.go +++ b/engine/account_test.go @@ -3407,3 +3407,27 @@ func TestAccountGetID(t *testing.T) { t.Error(rcv) } } + +func TestAccountmatchActionFilter(t *testing.T) { + acc := &Account{ + ID: str2, + UnitCounters: UnitCounters{str: {&uc}}, + ActionTriggers: ActionTriggers{&at}, + AllowNegative: bl, + Disabled: bl, + UpdateTime: tm2, + executingTriggers: bl, + } + + rcv, err := acc.matchActionFilter("test") + + if err != nil { + if err.Error() != "invalid character 'e' in literal true (expecting 'r')" { + t.Error(err) + } + } + + if rcv != false { + t.Error(rcv) + } +} diff --git a/engine/sharedgroup_test.go b/engine/sharedgroup_test.go index d939c4184..3b01c10dd 100644 --- a/engine/sharedgroup_test.go +++ b/engine/sharedgroup_test.go @@ -184,6 +184,49 @@ func TestSharedPopBalanceByStrategyMineHigh(t *testing.T) { } } +func TestSharedGroupSortBalanceByStrategy(t *testing.T) { + str := "test" + sg := &SharedGroup{ + Id: str, + AccountParameters: map[string]*SharingParameters{ + utils.ANY: { + Strategy: str, + RatingSubject: str, + }, + }, + MemberIds: utils.StringMap{str: true}, + } + + myBalance := &Balance{ + Uuid: str, + ID: str, + Value: fl, + ExpirationDate: tm, + Weight: fl, + DestinationIDs: sm, + RatingSubject: str, + Categories: sm, + SharedGroups: sm, + Timings: []*RITiming{&rtm}, + TimingIDs: sm, + Disabled: bl, + Factor: ValueFactor{str: fl}, + Blocker: bl, + precision: nm, + dirty: bl, + account: &Account{ID: ""}, + } + + bc := Balances{myBalance} + + rcv := sg.SortBalancesByStrategy(myBalance, bc) + exp := Balances{myBalance} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %s, receives %s", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + /*func TestSharedPopBalanceByStrategyRandomHigh(t *testing.T) { bc := Balances{ &Balance{Uuid: "uuuu", Value: 2.0, account: &Account{Id: "test"}}, diff --git a/engine/statmetrics_test.go b/engine/statmetrics_test.go index d4f6468cd..4487068cd 100644 --- a/engine/statmetrics_test.go +++ b/engine/statmetrics_test.go @@ -3486,3 +3486,247 @@ func TestStatMetricsGetCompressedFactorStatDistinct(t *testing.T) { t.Errorf("expected %s, received %s", utils.ToJSON(exp), utils.ToJSON(rcv)) } } + +func TestStatMetricsAddEvent(t *testing.T) { + fl := 1.2 + str := "test" + tm := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) + asr := StatASR{ + FilterIDs: []string{str}, + Answered: fl, + Count: 1, + Events: map[string]*StatWithCompress{str: { + Stat: 1.2, + CompressFactor: 1, + }}, + MinItems: 1, + val: &fl, + } + ev := &utils.CGREvent{ + Tenant: str, + ID: str, + Time: &tm, + Event: map[string]any{"AnswerTime": fl}, + } + + err := asr.AddEvent(ev) + + if err != nil { + if err.Error() != "cannot convert field: 1.2 to time.Time" { + t.Error(err) + } + } else { + t.Error("was expecting an error") + } +} + +func TestStatMetricsRemEvent(t *testing.T) { + ddc := StatDDC{ + FilterIDs: []string{"test"}, + Events: map[string]map[string]int64{"test": {}}, + MinItems: 1, + Count: 1, + } + + err := ddc.RemEvent("test") + + if err != nil { + if err.Error() != "NOT_FOUND" { + t.Error(err) + } + } else { + t.Error("was expecting an error") + } + + if _, has := ddc.Events["test"]; has { + t.Error("didn't delete event") + } + + ddc2 := StatDDC{ + FilterIDs: []string{"test"}, + Events: map[string]map[string]int64{"test": {"test": 2}}, + MinItems: 1, + Count: 1, + } + + err = ddc2.RemEvent("test") + + if err != nil { + t.Error(err) + } + + if _, has := ddc2.Events["test"]; !has { + t.Error("event deleted") + } + + ddc3 := StatDDC{ + FilterIDs: []string{"test"}, + FieldValues: map[string]map[string]struct{}{"test1": {"test1": {}}}, + Events: map[string]map[string]int64{"test": {"test": 1}}, + MinItems: 1, + Count: 1, + } + + err = ddc3.RemEvent("test") + + if err != nil { + t.Error(err) + } + + if _, has := ddc3.Events["test"]; !has { + t.Error("event deleted") + } +} + +func TestStatMetricsSumAddEvent(t *testing.T) { + str := "test" + fl := 1.2 + sum := StatSum{ + FilterIDs: []string{str}, + Sum: fl, + Count: 1, + Events: map[string]*StatWithCompress{str: { + Stat: fl, + CompressFactor: 1, + }}, + MinItems: 1, + FieldName: str, + val: &fl, + } + + ev := &utils.CGREvent{ + Tenant: str, + ID: str, + Time: &tm, + Event: map[string]any{"AnswerTime": fl}, + } + + err := sum.AddEvent(ev) + + if err != nil { + if err.Error() != `strconv.ParseFloat: parsing "test": invalid syntax` { + t.Error(err) + } + } else { + t.Error("was expecting an error") + } +} + +func TestStatMetricsAverageAddEvent(t *testing.T) { + str := "test" + fl := 1.2 + sum := StatAverage{ + FilterIDs: []string{str}, + Sum: fl, + Count: 1, + Events: map[string]*StatWithCompress{str: { + Stat: fl, + CompressFactor: 1, + }}, + MinItems: 1, + FieldName: str, + val: &fl, + } + + ev := &utils.CGREvent{ + Tenant: str, + ID: str, + Time: &tm, + Event: map[string]any{"AnswerTime": fl}, + } + + err := sum.AddEvent(ev) + + if err != nil { + if err.Error() != `strconv.ParseFloat: parsing "test": invalid syntax` { + t.Error(err) + } + } else { + t.Error("was expecting an error") + } +} + +func TestStatMetricsDistinctRemEvent(t *testing.T) { + ddc := StatDistinct{ + FilterIDs: []string{"test"}, + Events: map[string]map[string]int64{"test": {}}, + MinItems: 1, + Count: 1, + } + + err := ddc.RemEvent("test") + + if err != nil { + if err.Error() != "NOT_FOUND" { + t.Error(err) + } + } else { + t.Error("was expecting an error") + } + + if _, has := ddc.Events["test"]; has { + t.Error("didn't delete event") + } + + ddc2 := StatDistinct{ + FilterIDs: []string{"test"}, + Events: map[string]map[string]int64{"test": {"test": 2}}, + MinItems: 1, + Count: 1, + } + + err = ddc2.RemEvent("test") + + if err != nil { + t.Error(err) + } + + if _, has := ddc2.Events["test"]; !has { + t.Error("event deleted") + } + + ddc3 := StatDistinct{ + FilterIDs: []string{"test"}, + FieldValues: map[string]map[string]struct{}{"test1": {"test1": {}}}, + Events: map[string]map[string]int64{"test": {"test": 1}}, + MinItems: 1, + Count: 1, + } + + err = ddc3.RemEvent("test") + + if err != nil { + t.Error(err) + } + + if _, has := ddc3.Events["test"]; !has { + t.Error("event deleted") + } +} + +func TestStatMetricsDistinctAddEvent(t *testing.T) { + dst := &StatDistinct{ + FilterIDs: []string{"test"}, + FieldValues: map[string]map[string]struct{}{"test1": {"test1": {}}}, + Events: map[string]map[string]int64{"test": {"test": 1}}, + MinItems: 1, + Count: 1, + } + + ev := &utils.CGREvent{ + Tenant: str, + ID: str, + Time: &tm, + Event: map[string]any{"AnswerTime": fl}, + } + + err := dst.AddEvent(ev) + + if err != nil { + if err.Error() != "Invalid format for field <>" { + t.Error(err) + } + } else { + t.Error("was expecting an error") + } +}