From b2ce0ce16718b065e0155a4c54bbb9034e7f77ce Mon Sep 17 00:00:00 2001 From: NikolasPetriti Date: Thu, 17 Aug 2023 16:24:58 +0200 Subject: [PATCH] Add coverage tests for engine --- engine/balances_test.go | 2 +- engine/callcost_test.go | 2 +- engine/cdr_test.go | 2 +- engine/model_helpers_test.go | 4 +- engine/ratingplan_test.go | 46 +++--- engine/statmetrics_test.go | 96 ++++++------- engine/storage_interface_test.go | 239 +++++++++++++++++++++++++++++++ engine/suretax_test.go | 15 ++ engine/timespans_test.go | 10 ++ 9 files changed, 340 insertions(+), 76 deletions(-) create mode 100644 engine/storage_interface_test.go diff --git a/engine/balances_test.go b/engine/balances_test.go index d2d795ab7..c27492873 100644 --- a/engine/balances_test.go +++ b/engine/balances_test.go @@ -667,4 +667,4 @@ func TestBalanceCloneNil(t *testing.T) { if rcv != nil { t.Error(rcv) } -} \ No newline at end of file +} diff --git a/engine/callcost_test.go b/engine/callcost_test.go index 69e385ec5..c38355f21 100644 --- a/engine/callcost_test.go +++ b/engine/callcost_test.go @@ -326,4 +326,4 @@ func TestCallCostUpdateRatedUsage(t *testing.T) { if rcv != 0 { t.Error(rcv) } -} \ No newline at end of file +} diff --git a/engine/cdr_test.go b/engine/cdr_test.go index 12ec84966..71f71742a 100644 --- a/engine/cdr_test.go +++ b/engine/cdr_test.go @@ -1636,4 +1636,4 @@ func TestCDRCloneNil(t *testing.T) { if rcv != nil { t.Error(rcv) } -} \ No newline at end of file +} diff --git a/engine/model_helpers_test.go b/engine/model_helpers_test.go index 7713f38cd..270dc5df6 100644 --- a/engine/model_helpers_test.go +++ b/engine/model_helpers_test.go @@ -3434,12 +3434,12 @@ func TestModelHelperscsvLoadErrors(t *testing.T) { { name: "index tag error", args: args{tst, []string{"test"}}, - err: "invalid Test.Fl index a", + err: "invalid Test.Fl index a", }, { name: "regex tag error", args: args{tst2, []string{"123"}}, - err: "invalid Test2.Field value 123", + err: "invalid Test2.Field value 123", }, } diff --git a/engine/ratingplan_test.go b/engine/ratingplan_test.go index 00498dddc..37d8971ab 100644 --- a/engine/ratingplan_test.go +++ b/engine/ratingplan_test.go @@ -454,14 +454,14 @@ func BenchmarkRatingPlanRestore(b *testing.B) { func TestRatingPlanEqual(t *testing.T) { rt := &RITiming{ - Years: utils.Years{}, - Months: utils.Months{}, - MonthDays: utils.MonthDays{}, - WeekDays: utils.WeekDays{}, - StartTime: "00:00:00", - EndTime: "00:00:00", - cronString: str, - tag: str, + Years: utils.Years{}, + Months: utils.Months{}, + MonthDays: utils.MonthDays{}, + WeekDays: utils.WeekDays{}, + StartTime: "00:00:00", + EndTime: "00:00:00", + cronString: str, + tag: str, } rr := &RIRate{ @@ -470,13 +470,13 @@ func TestRatingPlanEqual(t *testing.T) { RoundingDecimals: nm, MaxCost: fl, MaxCostStrategy: str, - Rates: RateGroups{{ + Rates: RateGroups{{ GroupIntervalStart: 1 * time.Second, Value: fl, RateIncrement: 1 * time.Second, RateUnit: 1 * time.Second, }}, - tag: str, + tag: str, } rl := RPRateList{{ @@ -486,21 +486,21 @@ func TestRatingPlanEqual(t *testing.T) { }} rp := &RatingPlan{ - Id: "test", + Id: "test", Timings: map[string]*RITiming{str: rt}, Ratings: map[string]*RIRate{str: rr}, DestinationRates: map[string]RPRateList{str: rl}, } rt2 := &RITiming{ - Years: utils.Years{}, - Months: utils.Months{}, - MonthDays: utils.MonthDays{}, - WeekDays: utils.WeekDays{}, - StartTime: "00:00:00", - EndTime: "00:00:00", - cronString: "val1", - tag: str, + Years: utils.Years{}, + Months: utils.Months{}, + MonthDays: utils.MonthDays{}, + WeekDays: utils.WeekDays{}, + StartTime: "00:00:00", + EndTime: "00:00:00", + cronString: "val1", + tag: str, } rr2 := &RIRate{ @@ -509,13 +509,13 @@ func TestRatingPlanEqual(t *testing.T) { RoundingDecimals: nm, MaxCost: fl, MaxCostStrategy: str, - Rates: RateGroups{{ + Rates: RateGroups{{ GroupIntervalStart: 1 * time.Second, Value: fl, RateIncrement: 1 * time.Second, RateUnit: 1 * time.Second, }}, - tag: str, + tag: str, } rl2 := RPRateList{{ @@ -525,7 +525,7 @@ func TestRatingPlanEqual(t *testing.T) { }} o := &RatingPlan{ - Id: "test2", + Id: "test2", Timings: map[string]*RITiming{str: rt2}, Ratings: map[string]*RIRate{str: rr2}, DestinationRates: map[string]RPRateList{str: rl2}, @@ -536,4 +536,4 @@ func TestRatingPlanEqual(t *testing.T) { if rcv != false { t.Error(rcv) } -} \ No newline at end of file +} diff --git a/engine/statmetrics_test.go b/engine/statmetrics_test.go index 1dc5517c3..11059678f 100644 --- a/engine/statmetrics_test.go +++ b/engine/statmetrics_test.go @@ -3059,14 +3059,14 @@ func TestStatMetricsGetFilterIDs(t *testing.T) { FilterIDs: slc, Sum: 1 * time.Second, Count: 1, - Events: map[string]*DurationWithCompress{ + Events: map[string]*DurationWithCompress{ str: { - Duration: 1 * time.Second, + Duration: 1 * time.Second, CompressFactor: 1, }, }, - MinItems: 1, - val: &tm, + MinItems: 1, + val: &tm, } exp := slc @@ -3084,14 +3084,14 @@ func TestStatMetricsGetFilterIDsStatTCD(t *testing.T) { FilterIDs: slc, Sum: 1 * time.Second, Count: 1, - Events: map[string]*DurationWithCompress{ + Events: map[string]*DurationWithCompress{ str: { - Duration: 1 * time.Second, + Duration: 1 * time.Second, CompressFactor: 1, }, }, - MinItems: 1, - val: &tm, + MinItems: 1, + val: &tm, } exp := slc @@ -3109,14 +3109,14 @@ func TestStatMetricsGetFloat64ValueStatACC(t *testing.T) { FilterIDs: slc, Sum: 1.2, Count: 1, - Events: map[string]*StatWithCompress{ + Events: map[string]*StatWithCompress{ "test": { Stat: fl, CompressFactor: 1, }, }, - MinItems: 1, - val: &fl, + MinItems: 1, + val: &fl, } exp := fl @@ -3134,14 +3134,14 @@ func TestStatMetricsGetFilterIDsStatACC(t *testing.T) { FilterIDs: slc, Sum: 1.2, Count: 1, - Events: map[string]*StatWithCompress{ + Events: map[string]*StatWithCompress{ "test": { Stat: fl, CompressFactor: 1, }, }, - MinItems: 1, - val: &fl, + MinItems: 1, + val: &fl, } exp := slc @@ -3159,14 +3159,14 @@ func TestStatMetricsGetFloat64ValueStatTCC(t *testing.T) { FilterIDs: slc, Sum: 1.2, Count: 1, - Events: map[string]*StatWithCompress{ + Events: map[string]*StatWithCompress{ "test": { Stat: fl, CompressFactor: 1, }, }, - MinItems: 1, - val: &fl, + MinItems: 1, + val: &fl, } exp := fl @@ -3184,14 +3184,14 @@ func TestStatMetricsGetFilterIDsStatTCC(t *testing.T) { FilterIDs: slc, Sum: 1.2, Count: 1, - Events: map[string]*StatWithCompress{ + Events: map[string]*StatWithCompress{ "test": { Stat: fl, CompressFactor: 1, }, }, - MinItems: 1, - val: &fl, + MinItems: 1, + val: &fl, } exp := slc @@ -3209,14 +3209,14 @@ func TestStatMetricsGetFilterIDsStatPDD(t *testing.T) { FilterIDs: slc, Sum: 1 * time.Second, Count: 1, - Events: map[string]*DurationWithCompress{ + Events: map[string]*DurationWithCompress{ str: { - Duration: 1 * time.Second, + Duration: 1 * time.Second, CompressFactor: 1, }, }, - MinItems: 1, - val: &tm, + MinItems: 1, + val: &tm, } exp := slc @@ -3230,9 +3230,9 @@ func TestStatMetricsGetFilterIDsStatPDD(t *testing.T) { func TestStatMetricsGetFilterIDsStatDDC(t *testing.T) { slc := []string{"test"} acd := &StatDDC{ - FilterIDs: slc, - MinItems: 1, - Count: 1, + FilterIDs: slc, + MinItems: 1, + Count: 1, } rcv := acd.GetValue() @@ -3245,9 +3245,9 @@ func TestStatMetricsGetFilterIDsStatDDC(t *testing.T) { func TestStatMetricsGetFilterIDsDDC(t *testing.T) { slc := []string{"test"} acd := &StatDDC{ - FilterIDs: slc, - MinItems: 1, - Count: 1, + FilterIDs: slc, + MinItems: 1, + Count: 1, } rcv := acd.GetFilterIDs() @@ -3263,14 +3263,14 @@ func TestStatMetricsGetValueSum(t *testing.T) { FilterIDs: slc, Sum: 1.2, Count: 1, - Events: map[string]*StatWithCompress{ + Events: map[string]*StatWithCompress{ "test": { Stat: fl, CompressFactor: 1, }, }, - MinItems: 1, - val: &fl, + MinItems: 1, + val: &fl, } rcv := acd.GetValue() @@ -3286,14 +3286,14 @@ func TestStatMetricsGetFilterIDsSum(t *testing.T) { FilterIDs: slc, Sum: 1.2, Count: 1, - Events: map[string]*StatWithCompress{ + Events: map[string]*StatWithCompress{ "test": { Stat: fl, CompressFactor: 1, }, }, - MinItems: 1, - val: &fl, + MinItems: 1, + val: &fl, } rcv := acd.GetFilterIDs() @@ -3309,14 +3309,14 @@ func TestStatMetricsGetValueAvarage(t *testing.T) { FilterIDs: slc, Sum: 1.2, Count: 1, - Events: map[string]*StatWithCompress{ + Events: map[string]*StatWithCompress{ "test": { Stat: fl, CompressFactor: 1, }, }, - MinItems: 1, - val: &fl, + MinItems: 1, + val: &fl, } rcv := acd.GetValue() @@ -3332,14 +3332,14 @@ func TestStatMetricsGetFilterIDsAvarage(t *testing.T) { FilterIDs: slc, Sum: 1.2, Count: 1, - Events: map[string]*StatWithCompress{ + Events: map[string]*StatWithCompress{ "test": { Stat: fl, CompressFactor: 1, }, }, - MinItems: 1, - val: &fl, + MinItems: 1, + val: &fl, } rcv := acd.GetFilterIDs() @@ -3352,9 +3352,9 @@ func TestStatMetricsGetFilterIDsAvarage(t *testing.T) { func TestStatMetricsGetValueDistinct(t *testing.T) { slc := []string{"test"} acd := &StatDistinct{ - FilterIDs: slc, - MinItems: 1, - Count: 1, + FilterIDs: slc, + MinItems: 1, + Count: 1, } rcv := acd.GetValue() @@ -3367,9 +3367,9 @@ func TestStatMetricsGetValueDistinct(t *testing.T) { func TestStatMetricsGetFilterIDsDistinct(t *testing.T) { slc := []string{"test"} acd := &StatDistinct{ - FilterIDs: slc, - MinItems: 1, - Count: 1, + FilterIDs: slc, + MinItems: 1, + Count: 1, } rcv := acd.GetFilterIDs() diff --git a/engine/storage_interface_test.go b/engine/storage_interface_test.go new file mode 100644 index 000000000..bc58f7cd3 --- /dev/null +++ b/engine/storage_interface_test.go @@ -0,0 +1,239 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package engine + +import ( + "reflect" + "testing" + + "github.com/ugorji/go/codec" +) + +func TestStorageInterfaceNewMarshaler(t *testing.T) { + str := "json" + + rcv, err := NewMarshaler(str) + + exp := new(JSONMarshaler) + if err != nil { + t.Error(err) + } + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", exp, rcv) + } +} + +func TestSorageInterfaceMarshal(t *testing.T) { + type Test struct { + Field string + } + arg := Test{ + Field: "test", + } + jm := BSONMarshaler{} + + rcv, err := jm.Marshal(&arg) + if err != nil { + t.Error(err) + } + + exp := []byte{21, 0, 0, 0, 2, 102, 105, 101, 108, 100, 0, 5, 0, 0, 0, 116, 101, 115, 116, 0, 0} + + if !reflect.DeepEqual(exp, rcv) { + t.Errorf("expected %v, received %v", exp, rcv) + } +} + +func TestStorageInterfaceUnmarshal(t *testing.T) { + type Test struct { + Field string + } + arg := Test{ + Field: "test", + } + jm := BSONMarshaler{} + + rcv, err := jm.Marshal(&arg) + if err != nil { + t.Error(err) + } + + var um Test + + err = jm.Unmarshal(rcv, &um) + if err != nil { + t.Error(err) + } + + if !reflect.DeepEqual(um, arg) { + t.Errorf("expected %v, received %v", arg, um) + } +} + +func TestStorageInterfaceMarshalBufMarshaler(t *testing.T) { + type Test struct { + Field string + } + arg := Test{ + Field: "test", + } + jbm := JSONBufMarshaler{} + + rcv, err := jbm.Marshal(arg) + if err != nil { + t.Error(err) + } + + exp := []byte{123, 34, 70, 105, 101, 108, 100, 34, 58, 34, 116, 101, 115, 116, 34, 125, 10} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expeted %v, received %v", exp, rcv) + } +} + +func TestStorageInterfaceUnmarshalBufMarshaler(t *testing.T) { + type Test struct { + Field string + } + arg := Test{ + Field: "test", + } + jbm := JSONBufMarshaler{} + + rcv, err := jbm.Marshal(arg) + if err != nil { + t.Error(err) + } + + var um Test + + err = jbm.Unmarshal(rcv, &um) + if err != nil { + t.Error(err) + } + + if !reflect.DeepEqual(arg, um) { + t.Errorf("expeted %v, received %v", arg, um) + } +} + +func TestStorageInterfaceNewBincMarshaler(t *testing.T) { + rcv := NewBincMarshaler() + + exp := &BincMarshaler{new(codec.BincHandle)} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expeted %v, received %v", exp, rcv) + } +} + +func TestStorageInterfaceMarshalBinc(t *testing.T) { + type Test struct { + Field string + } + arg := Test{ + Field: "test", + } + bm := NewBincMarshaler() + + rcv, err := bm.Marshal(arg) + if err != nil { + t.Error(err) + } + + exp := []byte{117, 73, 70, 105, 101, 108, 100, 72, 116, 101, 115, 116} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expeted %v, received %v", exp, rcv) + } +} + +func TestStorageInterfaceUnmarshalBinc(t *testing.T) { + type Test struct { + Field string + } + arg := Test{ + Field: "test", + } + bm := NewBincMarshaler() + + rcv, err := bm.Marshal(arg) + if err != nil { + t.Error(err) + } + + var um Test + + err = bm.Unmarshal(rcv, &um) + if err != nil { + t.Error(err) + } + + if !reflect.DeepEqual(arg, um) { + t.Errorf("expeted %v, received %v", arg, um) + } +} + +func TestStorageInterfaceMarshalGOB(t *testing.T) { + type Test struct { + Field string + } + arg := Test{ + Field: "test", + } + bm := GOBMarshaler{} + + rcv, err := bm.Marshal(arg) + if err != nil { + t.Error(err) + } + + exp := []byte{28, 255, 185, 3, 1, 1, 4, 84, 101, 115, 116, 1, 255, 186, 0, 1, 1, 1, 5, 70, 105, 101, 108, 100, 1, 12, 0, 0, 0, 9, 255, 186, 1, 4, 116, 101, 115, 116, 0} + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expeted %v, received %v", exp, rcv) + } +} + +func TestStorageInterfaceUnmarshalGOB(t *testing.T) { + type Test struct { + Field string + } + arg := Test{ + Field: "test", + } + bm := GOBMarshaler{} + + rcv, err := bm.Marshal(arg) + if err != nil { + t.Error(err) + } + + var um Test + + err = bm.Unmarshal(rcv, &um) + if err != nil { + t.Error(err) + } + + if !reflect.DeepEqual(arg, um) { + t.Errorf("expeted %v, received %v", arg, um) + } +} diff --git a/engine/suretax_test.go b/engine/suretax_test.go index 5972c6cbe..51ed72ba3 100644 --- a/engine/suretax_test.go +++ b/engine/suretax_test.go @@ -84,3 +84,18 @@ func TestNewSureTaxRequest(t *testing.T) { t.Errorf("Expecting:\n%s\nReceived:\n%s", string(eSureTaxRequest.Request), string(stReq.Request)) } } + +func TestSureTaxNewSureTaxRequest(t *testing.T) { + cdr := &CDR{} + rcv, err := NewSureTaxRequest(cdr, nil) + + if err != nil { + if err.Error() != "invalid SureTax config" { + t.Fatal(err) + } + } + + if rcv != nil { + t.Error(rcv) + } +} diff --git a/engine/timespans_test.go b/engine/timespans_test.go index d986a3022..d8284e066 100644 --- a/engine/timespans_test.go +++ b/engine/timespans_test.go @@ -1993,3 +1993,13 @@ func TestIncrementClone(t *testing.T) { t.Errorf("Expecting %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(clone)) } } + +func TestTimeSpansClone(t *testing.T) { + var incs Increments + + rcv := incs.Clone() + + if rcv != nil { + t.Error(rcv) + } +}