From bcf26daf5ac0a519e39ca2563d26e83ce931b5dd Mon Sep 17 00:00:00 2001 From: NikolasPetriti Date: Mon, 21 Aug 2023 16:20:33 +0200 Subject: [PATCH] Add coverage tests for engine --- engine/statmetrics_test.go | 108 +++++++++++++++++++ engine/timespans_test.go | 199 +++++++++++++++++++++++++++++++++++ engine/units_counter_test.go | 53 ++++++++++ 3 files changed, 360 insertions(+) diff --git a/engine/statmetrics_test.go b/engine/statmetrics_test.go index 11059678f..d4f6468cd 100644 --- a/engine/statmetrics_test.go +++ b/engine/statmetrics_test.go @@ -3378,3 +3378,111 @@ func TestStatMetricsGetFilterIDsDistinct(t *testing.T) { t.Errorf("expected %v, received %v", slc, rcv) } } + +func TestStatMetricsGetCompressedFactorStatACD(t *testing.T) { + tm := 1 * time.Second + acd := &StatACD{ + FilterIDs: []string{"test"}, + Sum: tm, + Count: 1, + Events: map[string]*DurationWithCompress{"test": { + Duration: 1 * time.Second, + CompressFactor: 2, + }}, + MinItems: 1, + val: &tm, + } + + events := map[string]int{"test": 1} + exp := map[string]int{"test": 2} + + rcv := acd.GetCompressFactor(events) + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %s, received %s", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + +func TestStatMetricsGetCompressedFactorStatTCD(t *testing.T) { + tm := 1 * time.Second + acd := &StatTCD{ + FilterIDs: []string{"test"}, + Sum: tm, + Count: 1, + Events: map[string]*DurationWithCompress{"test": { + Duration: 1 * time.Second, + CompressFactor: 2, + }}, + MinItems: 1, + val: &tm, + } + + events := map[string]int{"test": 1} + exp := map[string]int{"test": 2} + + rcv := acd.GetCompressFactor(events) + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %s, received %s", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + +func TestStatMetricsGetCompressedFactorStatPDD(t *testing.T) { + tm := 1 * time.Second + acd := &StatPDD{ + FilterIDs: []string{"test"}, + Sum: tm, + Count: 1, + Events: map[string]*DurationWithCompress{"test": { + Duration: 1 * time.Second, + CompressFactor: 2, + }}, + MinItems: 1, + val: &tm, + } + + events := map[string]int{"test": 1} + exp := map[string]int{"test": 2} + + rcv := acd.GetCompressFactor(events) + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %s, received %s", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + +func TestStatMetricsGetCompressedFactorStatDDC(t *testing.T) { + acd := &StatDDC{ + FilterIDs: []string{"test"}, + Count: 1, + Events: map[string]map[string]int64{"test": {"test": 2}}, + MinItems: 1, + } + + events := map[string]int{"test": 1} + exp := map[string]int{"test": 2} + + rcv := acd.GetCompressFactor(events) + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %s, received %s", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + +func TestStatMetricsGetCompressedFactorStatDistinct(t *testing.T) { + acd := &StatDistinct{ + FilterIDs: []string{"test"}, + Count: 1, + Events: map[string]map[string]int64{"test": {"test": 2}}, + MinItems: 1, + } + + events := map[string]int{"test": 1} + exp := map[string]int{"test": 2} + + rcv := acd.GetCompressFactor(events) + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %s, received %s", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} diff --git a/engine/timespans_test.go b/engine/timespans_test.go index eccc36985..5af08f779 100644 --- a/engine/timespans_test.go +++ b/engine/timespans_test.go @@ -2159,3 +2159,202 @@ func TestTimespansSharingSignatureFalse(t *testing.T) { t.Error(rcv) } } + +func TestTimeSpansIsPaid(t *testing.T) { + ts := TimeSpan{ + TimeStart: time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC), + TimeEnd: time.Date(2010, 11, 17, 20, 34, 58, 651387237, time.UTC), + Cost: 1.2, + RateInterval: &RateInterval{ + Timing: &RITiming{ + Years: utils.Years{2022}, + Months: utils.Months{8}, + MonthDays: utils.MonthDays{9}, + WeekDays: utils.WeekDays{2}, + StartTime: "00:00:00", + EndTime: "00:00:00", + cronString: "test", + tag: "test", + }, + Rating: &RIRate{ + ConnectFee: 1.2, + RoundingMethod: "test", + RoundingDecimals: 1, + MaxCost: 1.2, + MaxCostStrategy: "test", + Rates: RateGroups{{ + GroupIntervalStart: 1 * time.Second, + Value: 1.2, + RateIncrement: 1 * time.Second, + RateUnit: 1 * time.Second, + }}, + tag: "test", + }, + Weight: 1.2, + }, + Increments: Increments{{ + Duration: 1 * time.Second, + Cost: 1.2, + BalanceInfo: &DebitInfo{}, + CompressFactor: 1, + paid: false, + }}, + RoundIncrement: &Increment{ + Duration: 1 * time.Second, + Cost: 1.2, + BalanceInfo: &DebitInfo{}, + CompressFactor: 1, + paid: false, + }, + MatchedSubject: "test", + MatchedPrefix: "test", + MatchedDestId: "test", + RatingPlanId: "test", + CompressFactor: 1, + } + + bl, nm := ts.IsPaid() + + if bl != false { + t.Fatal(bl) + } + + if nm != 0 { + t.Error(nm) + } + + ts.Increments[0].paid = true + bl, nm = ts.IsPaid() + + if bl != true { + t.Fatal(bl) + } + + if nm != 1 { + t.Error(nm) + } +} + +func TestTimeSpansSplitByDuration(t *testing.T) { + ts := TimeSpan{} + + rcv := ts.SplitByDuration(0 * time.Second) + + if rcv != nil { + t.Error(rcv) + } +} + +func TestTimeSpansAddIncrement(t *testing.T) { + ts := TimeSpan{ + TimeStart: time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC), + TimeEnd: time.Date(2010, 11, 17, 20, 34, 58, 651387237, time.UTC), + Cost: 1.2, + RateInterval: &RateInterval{ + Timing: &RITiming{ + Years: utils.Years{2022}, + Months: utils.Months{8}, + MonthDays: utils.MonthDays{9}, + WeekDays: utils.WeekDays{2}, + StartTime: "00:00:00", + EndTime: "00:00:00", + cronString: "test", + tag: "test", + }, + Rating: &RIRate{ + ConnectFee: 1.2, + RoundingMethod: "test", + RoundingDecimals: 1, + MaxCost: 1.2, + MaxCostStrategy: "test", + Rates: RateGroups{{ + GroupIntervalStart: 1 * time.Second, + Value: 1.2, + RateIncrement: 1 * time.Second, + RateUnit: 1 * time.Second, + }}, + tag: "test", + }, + Weight: 1.2, + }, + Increments: Increments{{ + Duration: 1 * time.Second, + Cost: 1.2, + BalanceInfo: &DebitInfo{}, + CompressFactor: 1, + paid: false, + }}, + RoundIncrement: &Increment{ + Duration: 1 * time.Second, + Cost: 1.2, + BalanceInfo: &DebitInfo{}, + CompressFactor: 1, + paid: false, + }, + MatchedSubject: "test", + MatchedPrefix: "test", + MatchedDestId: "test", + RatingPlanId: "test", + CompressFactor: 1, + } + inc := &Increment{ + Duration: 1 * time.Second, + Cost: 1.2, + BalanceInfo: &DebitInfo{}, + CompressFactor: 1, + paid: false, + } + + ts.AddIncrement(inc) + + exp := Increments{{ + Duration: 1 * time.Second, + Cost: 1.2, + BalanceInfo: &DebitInfo{}, + CompressFactor: 1, + paid: false, + }, + { + Duration: 1 * time.Second, + Cost: 1.2, + BalanceInfo: &DebitInfo{}, + CompressFactor: 1, + paid: false, + }} + + if !reflect.DeepEqual(ts.Increments, exp) { + t.Errorf("expeced %v, received %v", ts.Increments, exp) + } + + if ts.TimeEnd != time.Date(2010, 11, 17, 20, 34, 58, 651387237, time.UTC) { + t.Error(ts.TimeEnd) + } +} + +func TestTimeSpansMerge(t *testing.T) { + ts := TimeSpan{ + TimeStart: time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC), + TimeEnd: time.Date(2010, 11, 17, 20, 34, 58, 651387237, time.UTC), + } + + other := TimeSpan{ + TimeStart: time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC), + TimeEnd: time.Date(2010, 11, 17, 20, 34, 58, 651387237, time.UTC), + } + + rcv := ts.Merge(&other) + + if rcv != false { + t.Error(rcv) + } +} + +func TestTimeSpansSetRateInterval(t *testing.T) { + ts := TimeSpan{} + + ts.SetRateInterval(nil) + + if ts.RateInterval != nil { + t.Error("didn't return") + } +} diff --git a/engine/units_counter_test.go b/engine/units_counter_test.go index dc45b5323..5cdfc76dc 100644 --- a/engine/units_counter_test.go +++ b/engine/units_counter_test.go @@ -20,6 +20,7 @@ package engine import ( "reflect" "testing" + "time" "github.com/cgrates/cgrates/utils" ) @@ -597,3 +598,55 @@ func TestUnitCountersResetCounterById(t *testing.T) { t.Errorf("Error Initializing adding unit counters: %v", len(a.UnitCounters)) } } + +func TestUnitsCounter(t *testing.T) { + str := "test" + fl := 1.2 + bl := true + tm := time.Date(2010, 11, 17, 20, 34, 58, 651387237, time.UTC) + vf := utils.ValueFormula{ + Method: str, + Params: map[string]any{str: fl}, + Static: 1.2, + } + va := ValueFactor{str: fl} + rt := []*RITiming{{ + Years: utils.Years{2022}, + Months: utils.Months{8}, + MonthDays: utils.MonthDays{9}, + WeekDays: utils.WeekDays{2}, + StartTime: "00:00:00", + EndTime: "00:00:00", + cronString: "test", + tag: "test", + }} + + cf := CounterFilter{ + Value: 1.2, + Filter: &BalanceFilter{ + Uuid: &str, + ID: &str, + Type: &str, + Value: &vf, + ExpirationDate: &tm, + Weight: &fl, + DestinationIDs: &sm, + RatingSubject: &str, + Categories: &sm, + SharedGroups: &sm, + TimingIDs: &sm, + Timings: rt, + Disabled: &bl, + Factor: &va, + Blocker: &bl, + }, + } + + cfg := CounterFilters{&cf} + + rcv := cfg.HasCounter(&cf) + + if rcv != true { + t.Error(rcv) + } +}