mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Added tests for coverage in utils package
This commit is contained in:
committed by
Dan Christian Bogos
parent
077d916a1d
commit
f87a342b2c
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
39
utils/concureqs_test.go
Normal file
39
utils/concureqs_test.go
Normal file
@@ -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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
70
utils/pathitemlist_test.go
Normal file
70
utils/pathitemlist_test.go
Normal file
@@ -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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
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())
|
||||
|
||||
}
|
||||
*/
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user