From 84cb2ddd1af09bf2b9e8ac452da113d5b572bd0c Mon Sep 17 00:00:00 2001 From: NikolasPetriti Date: Tue, 18 Jul 2023 16:34:32 +0200 Subject: [PATCH] Add coverage tests for libeventcost.go --- engine/caches_test.go | 8 +- engine/dispatcherprfl_test.go | 2 +- engine/libattributes_test.go | 24 +- engine/libeventcost_test.go | 414 ++++++++++++++++++++++++++++++++++ engine/suppliers_test.go | 4 +- 5 files changed, 433 insertions(+), 19 deletions(-) diff --git a/engine/caches_test.go b/engine/caches_test.go index 583673630..21b3b824d 100644 --- a/engine/caches_test.go +++ b/engine/caches_test.go @@ -407,7 +407,7 @@ func TestCacheV1HasGroup(t *testing.T) { bl := true err := c.V1HasGroup(&utils.ArgsGetGroupWithArgDispatcher{}, &bl) - if err != nil { + if err != nil { if err.Error() != "unknown cacheID: *resources" { t.Error(err) } @@ -423,13 +423,13 @@ func TestV1GetGroupItemIDs(t *testing.T) { if err.Error() != "NOT_FOUND" { t.Error(err) } -} +} func TestV1RemoveGroup(t *testing.T) { c := CacheS{} str := "" - err := c.V1RemoveGroup(&utils.ArgsGetGroupWithArgDispatcher{}, &str) + err := c.V1RemoveGroup(&utils.ArgsGetGroupWithArgDispatcher{}, &str) if err != nil { t.Error(err) @@ -450,4 +450,4 @@ func TestCachetoStringSlice(t *testing.T) { if slc != nil { t.Error(slc) } -} \ No newline at end of file +} diff --git a/engine/dispatcherprfl_test.go b/engine/dispatcherprfl_test.go index 1f2c688b3..b9f2d33cc 100644 --- a/engine/dispatcherprfl_test.go +++ b/engine/dispatcherprfl_test.go @@ -266,4 +266,4 @@ func TestDispatcherprflClone(t *testing.T) { if !reflect.DeepEqual(dC, *rcv) { t.Errorf("expected %v, received %v", dC, rcv) } -} \ No newline at end of file +} diff --git a/engine/libattributes_test.go b/engine/libattributes_test.go index 0d3a8dcb2..c55e76c01 100644 --- a/engine/libattributes_test.go +++ b/engine/libattributes_test.go @@ -170,23 +170,23 @@ func TestLibattributesSort(t *testing.T) { } func TestLibattributesNewAttributeFromInline(t *testing.T) { - tests := []struct{ - name string - t string - in string - err string + tests := []struct { + name string + t string + in string + err string }{ { name: "split error check", - t: "", - in: "", - err: "inline parse error for string: <>", + t: "", + in: "", + err: "inline parse error for string: <>", }, { name: "NewRSRParsers error check", - t: "", - in: "test):test):test)", - err: "invalid RSRFilter start rule in string: ", + t: "", + in: "test):test):test)", + err: "invalid RSRFilter start rule in string: ", }, } @@ -201,4 +201,4 @@ func TestLibattributesNewAttributeFromInline(t *testing.T) { } }) } -} \ No newline at end of file +} diff --git a/engine/libeventcost_test.go b/engine/libeventcost_test.go index c8e9dfedf..1a1c089fc 100644 --- a/engine/libeventcost_test.go +++ b/engine/libeventcost_test.go @@ -1164,3 +1164,417 @@ func TestLibeventcostBalanceChargeFieldAsInterface(t *testing.T) { }) } } + +func TestLibeventcostRatingMatchedFiltersFieldAsInterface(t *testing.T) { + rf := RatingMatchedFilters{ + "test1": 1, + } + + tests := []struct { + name string + arg []string + val any + err string + }{ + { + name: "empty filepath", + arg: []string{}, + val: nil, + err: "NOT_FOUND", + }, + { + name: "keyword not found", + arg: []string{"test"}, + val: nil, + err: "NOT_FOUND", + }, + { + name: "found", + arg: []string{"test1"}, + val: 1, + err: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rcv, err := rf.FieldAsInterface(tt.arg) + + if err != nil { + if err.Error() != tt.err { + t.Error(err) + } + } + + if !reflect.DeepEqual(rcv, tt.val) { + t.Errorf("expected %v, received %v", tt.val, rcv) + } + }) + } +} + +func TestLibeventcostChargedTimingFieldAsInterface(t *testing.T) { + ct := ChargedTiming{ + Years: utils.Years{1999}, + Months: utils.Months{time.August}, + MonthDays: utils.MonthDays{28}, + WeekDays: utils.WeekDays{time.Friday}, + StartTime: "00:00:00", + } + + tests := []struct { + name string + arg []string + exp any + err string + }{ + { + name: "empty filepath", + arg: []string{}, + exp: nil, + err: "NOT_FOUND", + }, + { + name: "empty filepath", + arg: []string{"test"}, + exp: nil, + err: "unsupported field prefix: ", + }, + { + name: "Years case", + arg: []string{"Years"}, + exp: utils.Years{1999}, + err: "", + }, + { + name: "Months case", + arg: []string{"Months"}, + exp: utils.Months{time.August}, + err: "", + }, + { + name: "MonthDays case", + arg: []string{"MonthDays"}, + exp: utils.MonthDays{28}, + err: "", + }, + { + name: "WeekDays case", + arg: []string{"WeekDays"}, + exp: utils.WeekDays{time.Friday}, + err: "", + }, + { + name: "StartTime case", + arg: []string{"StartTime"}, + exp: "00:00:00", + err: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rcv, err := ct.FieldAsInterface(tt.arg) + + if err != nil { + if err.Error() != tt.err { + t.Error(err) + } + } + + if !reflect.DeepEqual(rcv, tt.exp) { + t.Errorf("expected %v, received %v", tt.exp, rcv) + } + }) + } +} + +func TestLibeventcostRatingUnitFieldAsInterface(t *testing.T) { + fl := 1.5 + nm := 1 + str := "test" + ct := RatingUnit{ + ConnectFee: fl, + RoundingMethod: str, + RoundingDecimals: nm, + MaxCost: fl, + MaxCostStrategy: str, + TimingID: str, + RatesID: str, + RatingFiltersID: str, + } + + tests := []struct { + name string + arg []string + exp any + err string + }{ + { + name: "empty filepath", + arg: []string{}, + exp: nil, + err: "NOT_FOUND", + }, + { + name: "empty filepath", + arg: []string{"test"}, + exp: nil, + err: "unsupported field prefix: ", + }, + { + name: "ConnectFee case", + arg: []string{"ConnectFee"}, + exp: fl, + err: "", + }, + { + name: "RoundingMethod case", + arg: []string{"RoundingMethod"}, + exp: str, + err: "", + }, + { + name: "RoundingDecimals case", + arg: []string{"RoundingDecimals"}, + exp: nm, + err: "", + }, + { + name: "MaxCost case", + arg: []string{"MaxCost"}, + exp: fl, + err: "", + }, + { + name: "MaxCostStrategy case", + arg: []string{"MaxCostStrategy"}, + exp: str, + err: "", + }, + { + name: "TimingID case", + arg: []string{"TimingID"}, + exp: str, + err: "", + }, + { + name: "RatesID case", + arg: []string{"RatesID"}, + exp: str, + err: "", + }, + { + name: "RatingFiltersID case", + arg: []string{"RatingFiltersID"}, + exp: str, + err: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rcv, err := ct.FieldAsInterface(tt.arg) + + if err != nil { + if err.Error() != tt.err { + t.Error(err) + } + } + + if rcv != tt.exp { + t.Errorf("expected %v, received %v", tt.exp, rcv) + } + }) + } +} + +func TestLibeventcostRatingFiltersFiltersFieldAsInterface(t *testing.T) { + rf := RatingFilters{ + "test1": {"test": 1}, + } + + tests := []struct { + name string + arg []string + val any + err string + }{ + { + name: "empty filepath", + arg: []string{}, + val: nil, + err: "NOT_FOUND", + }, + { + name: "keyword not found", + arg: []string{"test"}, + val: nil, + err: "NOT_FOUND", + }, + { + name: "found", + arg: []string{"test1"}, + val: RatingMatchedFilters{"test": 1}, + err: "", + }, + { + name: "found", + arg: []string{"test1", "test"}, + val: 1, + err: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rcv, err := rf.FieldAsInterface(tt.arg) + + if err != nil { + if err.Error() != tt.err { + t.Error(err) + } + } + + if !reflect.DeepEqual(rcv, tt.val) { + t.Errorf("expected %v, received %v", tt.val, rcv) + } + }) + } +} + +func TestLibeventcostRatingFiltersFieldAsInterface(t *testing.T) { + rf := Rating{ + "test1": {RatesID: "test"}, + } + + tests := []struct { + name string + arg []string + val any + err string + }{ + { + name: "empty filepath", + arg: []string{}, + val: nil, + err: "NOT_FOUND", + }, + { + name: "keyword not found", + arg: []string{"test"}, + val: nil, + err: "NOT_FOUND", + }, + { + name: "found", + arg: []string{"test1"}, + val: &RatingUnit{RatesID: "test"}, + err: "", + }, + { + name: "found", + arg: []string{"test1", "RatesID"}, + val: "test", + err: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rcv, err := rf.FieldAsInterface(tt.arg) + + if err != nil { + if err.Error() != tt.err { + t.Error(err) + } + } + + if !reflect.DeepEqual(rcv, tt.val) { + t.Errorf("expected %v, received %v", tt.val, rcv) + } + }) + } +} + +func TestLibeventcostChargedRatesFieldAsInterface(t *testing.T) { + tm := 1 * time.Millisecond + ch := ChargedRates{ + "test": {{ + GroupIntervalStart: tm, + Value: 1.2, + RateIncrement: tm, + RateUnit: tm, + }}, + } + + tests := []struct{ + name string + arg []string + val any + err string + }{ + { + name: "empty filepath", + arg: []string{}, + val: nil, + err: "NOT_FOUND", + }, + { + name: "keyword not found", + arg: []string{"test1"}, + val: nil, + err: "NOT_FOUND", + }, + { + name: "no index", + arg: []string{"test", "test"}, + val: nil, + err: "NOT_FOUND", + }, + { + name: "found", + arg: []string{"test"}, + val: RateGroups{{ + GroupIntervalStart: tm, + Value: 1.2, + RateIncrement: tm, + RateUnit: tm, + }}, + err: "", + }, + { + name: "found", + arg: []string{"test[0]"}, + val: &Rate{ + GroupIntervalStart: tm, + Value: 1.2, + RateIncrement: tm, + RateUnit: tm, + }, + err: "", + }, + { + name: "found", + arg: []string{"test[0]", "Value"}, + val: 1.2, + err: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rcv, err := ch.FieldAsInterface(tt.arg) + + if err != nil { + if err.Error() != tt.err { + t.Error(err) + } + } + + if !reflect.DeepEqual(rcv, tt.val) { + t.Errorf("expected %v, received %v", tt.val, rcv) + } + }) + } +} \ No newline at end of file diff --git a/engine/suppliers_test.go b/engine/suppliers_test.go index 24fcfd523..cafbf07bd 100644 --- a/engine/suppliers_test.go +++ b/engine/suppliers_test.go @@ -1031,7 +1031,7 @@ func TestSupplierscompileCacheParamaters(t *testing.T) { sp3 := SupplierProfile{ Sorting: "*load", - SortingParameters: []string{"*default:1",}, + SortingParameters: []string{"*default:1"}, Suppliers: []*Supplier{{}}, } @@ -1043,7 +1043,7 @@ func TestSupplierscompileCacheParamaters(t *testing.T) { sp4 := SupplierProfile{ Sorting: "*load", - SortingParameters: []string{"test:1",}, + SortingParameters: []string{"test:1"}, Suppliers: []*Supplier{{ID: "test"}}, }