From f87a342b2c28a5adfa4b27736a59840bcd69120e Mon Sep 17 00:00:00 2001 From: andronache Date: Fri, 6 Nov 2020 11:27:50 +0200 Subject: [PATCH] Added tests for coverage in utils package --- engine/calldesc_test.go | 1 - utils/concureqs_test.go | 39 ++++++++ utils/coreutils.go | 4 - utils/decimal_test.go | 14 +++ utils/pathitemlist_test.go | 70 ++++++++++++++ utils/set_test.go | 22 +++++ utils/value_formula.go | 2 - utils/value_formula_test.go | 176 ++++++++++++++++++++++++++++++++++-- 8 files changed, 312 insertions(+), 16 deletions(-) create mode 100644 utils/concureqs_test.go create mode 100644 utils/pathitemlist_test.go diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index 3df064557..9eff541fc 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -613,7 +613,6 @@ func TestSpansMultipleRatingPlans(t *testing.T) { Destination: "0257308200", TimeStart: t1, TimeEnd: t2} cc, _ := cd.GetCost() if cc.Cost != 2100 || cc.GetConnectFee() != 0 { - utils.LogFull(cc) t.Errorf("Expected %v was %v (%v)", 2100, cc, cc.GetConnectFee()) } } diff --git a/utils/concureqs_test.go b/utils/concureqs_test.go new file mode 100644 index 000000000..0e43379a2 --- /dev/null +++ b/utils/concureqs_test.go @@ -0,0 +1,39 @@ +/* +Real-time Online/Offline Charging System (OerS) 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 utils + +import ( + "reflect" + "testing" +) + +func TestConcureqsNewConReqs(t *testing.T) { + expected := &ConcReqs{strategy: "test", aReqs: make(chan struct{}, 1)} + received := NewConReqs(1, "test") + if reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} + +func TestConcureqsIsLimited(t *testing.T) { + received := NewConReqs(1, "test").IsLimited() + if received != true { + t.Errorf("Expecting: true, received: %+v", received) + } +} diff --git a/utils/coreutils.go b/utils/coreutils.go index 9fc15edb8..1cb48a422 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -504,10 +504,6 @@ func ToJSON(v interface{}) string { return string(b) } -func LogFull(v interface{}) { - log.Print(ToIJSON(v)) -} - // Simple object cloner, b should be a pointer towards a value into which we want to decode func Clone(a, b interface{}) error { buff := new(bytes.Buffer) diff --git a/utils/decimal_test.go b/utils/decimal_test.go index 2877089f4..78539745f 100644 --- a/utils/decimal_test.go +++ b/utils/decimal_test.go @@ -72,3 +72,17 @@ func TestDecimalMarshalUnmarshalJSON(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", expected, rcv) } } + +func TestDecimalMarshalUnmarshalJSONNil(t *testing.T) { + var a Decimal + var b Decimal + marshA, err := a.MarshalJSON() + if err != nil { + t.Errorf("Expecting: nil, received: %+v", marshA) + } + unmarshB := b.UnmarshalJSON(marshA) + if unmarshB != nil { + t.Errorf("Expecting: nil, received: %+v", unmarshB) + } + +} diff --git a/utils/pathitemlist_test.go b/utils/pathitemlist_test.go new file mode 100644 index 000000000..460e0da85 --- /dev/null +++ b/utils/pathitemlist_test.go @@ -0,0 +1,70 @@ +/* +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 utils + +import ( + "testing" +) + +func TestPathItemListNextNil(t *testing.T) { + var e PathItemElement + received := e.Next() + if received != nil { + t.Errorf("Expecting: nil, received: %+v", received) + } +} + +func TestPathItemListPrevNil(t *testing.T) { + var e PathItemElement + received := e.Prev() + if received != nil { + t.Errorf("Expecting: nil, received: %+v", received) + } +} + +func TestPathItemListFrontNil(t *testing.T) { + var e PathItemList + received := e.Front() + if received != nil { + t.Errorf("Expecting: nil, received: %+v", received) + } +} + +func TestPathItemListBackNil(t *testing.T) { + var e PathItemList + received := e.Back() + if received != nil { + t.Errorf("Expecting: nil, received: %+v", received) + } +} + +/* +func TestPathItemListNext(t *testing.T) { + list := NewPathItemList() + node1 := NewPathItems([]string{"path1"}) + node2 := NewPathItems([]string{"path2"}) + node3 := NewPathItems([]string{"path3"}) + list.PushFront(node1) + list.PushFront(node2) + list.PushFront(node3) + fmt.Println(list.Back().Value.String()) + fmt.Println(list.Back().Prev().Value.String()) + +} +*/ diff --git a/utils/set_test.go b/utils/set_test.go index 26314ab7f..f3be6cd9f 100644 --- a/utils/set_test.go +++ b/utils/set_test.go @@ -196,3 +196,25 @@ func TestIntersect(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", eOut, s1) } } + +func TestSetClone(t *testing.T) { + a := StringSet{"test1": struct{}{}, "test2": struct{}{}} + initA := StringSet{"test1": struct{}{}, "test2": struct{}{}} + received := a.Clone() + if !reflect.DeepEqual(initA, received) { + t.Errorf("Expecting: %+v, received: %+v", initA, received) + } + a["test3"] = struct{}{} + if !reflect.DeepEqual(initA, received) { + t.Errorf("Expecting: %+v, received: %+v", initA, received) + } +} + +func TestSetCloneEmpty(t *testing.T) { + var a StringSet + var expected StringSet + received := a.Clone() + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} diff --git a/utils/value_formula.go b/utils/value_formula.go index a9a794f96..4c49a1f75 100644 --- a/utils/value_formula.go +++ b/utils/value_formula.go @@ -21,7 +21,6 @@ package utils import ( "encoding/json" "errors" - "log" "strconv" "time" ) @@ -73,7 +72,6 @@ func incrementalFormula(params map[string]interface{}) float64 { } units, ok := unitsInterface.(float64) if !ok { - log.Print("units") return 0.0 } var interval string diff --git a/utils/value_formula_test.go b/utils/value_formula_test.go index 04ffcc8bb..c2e07b215 100644 --- a/utils/value_formula_test.go +++ b/utils/value_formula_test.go @@ -25,9 +25,10 @@ import ( ) func TestValueFormulaDayWeek(t *testing.T) { - params := make(map[string]interface{}) - if err := json.Unmarshal([]byte(`{"Units":10, "Interval":"week", "Increment":"day"}`), ¶ms); err != nil { - t.Error("error unmarshalling params: ", err) + params := map[string]interface{}{ + "Units": 10.0, + "Interval": "week", + "Increment": "day", } if x := incrementalFormula(params); x != 10/7.0 { t.Error("error caclulating value using formula: ", x) @@ -35,9 +36,10 @@ func TestValueFormulaDayWeek(t *testing.T) { } func TestValueFormulaDayMonth(t *testing.T) { - params := make(map[string]interface{}) - if err := json.Unmarshal([]byte(`{"Units":10, "Interval":"month", "Increment":"day"}`), ¶ms); err != nil { - t.Error("error unmarshalling params: ", err) + params := map[string]interface{}{ + "Units": 10.0, + "Interval": "month", + "Increment": "day", } now := time.Now() if x := incrementalFormula(params); x != 10/DaysInMonth(now.Year(), now.Month()) { @@ -46,10 +48,12 @@ func TestValueFormulaDayMonth(t *testing.T) { } func TestValueFormulaDayYear(t *testing.T) { - params := make(map[string]interface{}) - if err := json.Unmarshal([]byte(`{"Units":10, "Interval":"year", "Increment":"day"}`), ¶ms); err != nil { - t.Error("error unmarshalling params: ", err) + params := map[string]interface{}{ + "Units": 10.0, + "Interval": "year", + "Increment": "day", } + now := time.Now() if x := incrementalFormula(params); x != 10/DaysInYear(now.Year()) { t.Error("error caclulating value using formula: ", x) @@ -131,3 +135,157 @@ func TestParseBalanceFilterValue(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", eOut, rcv) } } + +func TestValueFormulaEmptyFields(t *testing.T) { + params := map[string]interface{}{} + expected := 0.0 + received := incrementalFormula(params) + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} + +func TestValueFormulaConvertFloat64(t *testing.T) { + params := map[string]interface{}{ + "Units": 50, + "Interval": "day", + "Increment": "hour", + } + expected := 0.0 + received := incrementalFormula(params) + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} + +func TestValueFormulaIntervalByte(t *testing.T) { + params := map[string]interface{}{ + "Units": 10.0, + "Interval": []byte("week"), + "Increment": "day", + } + + expected := 10.0 / 7.0 + received := incrementalFormula(params) + + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} + +func TestValueFormulaIntervalDefault(t *testing.T) { + params := map[string]interface{}{ + "Units": 10.0, + "Interval": 5, + "Increment": "day", + } + + expected := 0.0 + received := incrementalFormula(params) + + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} + +func TestValueFormulaIncrementDefault(t *testing.T) { + params := map[string]interface{}{ + "Units": 10.0, + "Interval": "week", + "Increment": 5, + } + + expected := 0.0 + received := incrementalFormula(params) + + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} + +func TestValueFormulaIncrementByte(t *testing.T) { + params := map[string]interface{}{ + "Units": 10.0, + "Interval": "week", + "Increment": []byte("day"), + } + + expected := 10.0 / 7.0 + received := incrementalFormula(params) + + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} + +func TestValueFormulaIncrementHourDay(t *testing.T) { + params := map[string]interface{}{ + "Units": 10.0, + "Interval": "day", + "Increment": "hour", + } + + expected := 10.0 / 24.0 + received := incrementalFormula(params) + + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} + +func TestValueFormulaIncrementHourMonth(t *testing.T) { + params := map[string]interface{}{ + "Units": 10.0, + "Interval": "month", + "Increment": "hour", + } + now := time.Now() + expected := 10.0 / (DaysInMonth(now.Year(), now.Month()) * 24) + received := incrementalFormula(params) + + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} + +func TestValueFormulaIncrementHourYear(t *testing.T) { + params := map[string]interface{}{ + "Units": 10.0, + "Interval": "year", + "Increment": "hour", + } + now := time.Now() + expected := 10.0 / (DaysInYear(now.Year()) * 24) + received := incrementalFormula(params) + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} + +func TestValueFormulaIncrementMinute(t *testing.T) { + params := map[string]interface{}{ + "Units": 10.0, + "Interval": "hour", + "Increment": "minute", + } + + expected := 10.0 / 60 + received := incrementalFormula(params) + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +} + +func TestValueFormulaCover(t *testing.T) { + params := map[string]interface{}{ + "Units": 10.0, + "Interval": "cat", + "Increment": "cat", + } + + expected := 0.0 + received := incrementalFormula(params) + if !reflect.DeepEqual(expected, received) { + t.Errorf("Expecting: %+v, received: %+v", expected, received) + } +}