Coverage tests for fwvdp.go and slicedp.go

This commit is contained in:
NikolasPetriti
2023-06-21 16:26:26 +02:00
committed by Dan Christian Bogos
parent 40d0cbbca7
commit 86a1fbd531
6 changed files with 339 additions and 4 deletions

145
config/fwvdp_test.go Normal file
View File

@@ -0,0 +1,145 @@
/*
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 config
import (
"reflect"
"testing"
"github.com/cgrates/cgrates/utils"
)
func TestNewFWVProvider(t *testing.T) {
dP := NewFWVProvider("test")
if dP == nil {
t.Error("didn't recive the FWVProvider", dP)
}
}
func TestFwvdpString(t *testing.T) {
dP := NewFWVProvider("test")
rcv := dP.String()
if rcv != "{}" {
t.Errorf("recived %s, expected {}", rcv)
}
}
func TestFWVFieldAsInterface(t *testing.T) {
dP := FWVProvider{req: "test", cache: utils.MapStorage{"0-4": "test"}}
tests := []struct{
name string
arg []string
exp any
err bool
}{
{
name: "empty field path",
arg: []string{},
exp: nil,
err: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := dP.FieldAsInterface(tt.arg)
if tt.err {
if err == nil {
t.Fatal("was expecting an error")
}
} else {
if err != nil {
t.Fatal("was not expecting an error")
}
}
if !reflect.DeepEqual(data, tt.exp) {
t.Errorf("recived %v, expected %v", data, tt.exp)
}
})
}
}
func TestFWVFieldAsString(t *testing.T) {
dP := NewFWVProvider("test")
type exp struct {
data string
err error
}
tests := []struct {
name string
arg []string
exp exp
}{
{
name: "no err",
arg: []string{"0-1"},
exp: exp{"t", nil},
},
{
name: "err",
arg: []string{"test"},
exp: exp{"", nil},
},
}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := dP.FieldAsString(tt.arg)
if i < 1 {
if err != tt.exp.err {
t.Fatalf("recived %s, expected %s", err, tt.exp.err)
}
} else {
if err == nil {
t.Fatalf("was expecting an error")
}
}
if data != tt.exp.data {
t.Errorf("recived %s, expected %s", data, tt.exp.data)
}
})
}
}
func TestFWVRemoteHost(t *testing.T) {
dP := NewFWVProvider("test")
rcv := dP.RemoteHost()
if rcv == nil {
t.Error("didn't recive")
}
}

189
config/slicedp_test.go Normal file
View File

@@ -0,0 +1,189 @@
/*
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 config
import (
"fmt"
"reflect"
"testing"
"github.com/cgrates/cgrates/utils"
)
func TestSliceDPNewSliceDP(t *testing.T) {
slc := []string{"test", "test2"}
rcv := NewSliceDP(slc)
exp := &SliceDP{req: slc, cache: utils.MapStorage{}}
if !reflect.DeepEqual(rcv, exp) {
t.Errorf("recived %v, expected %v", rcv, exp)
}
}
func TestSliceDPString(t *testing.T) {
slc := []string{"test", "test2"}
cP := NewSliceDP(slc)
rcv := cP.String()
exp := "{}"
if rcv != exp {
t.Errorf("recived %v, expected %v", rcv, exp)
}
}
func TestSliceDPFieldAsInterface(t *testing.T) {
slc := []string{"0", "1", "2"}
slc2 := []string{"0"}
slc3 := []string{"test"}
slc4 := []string{"4"}
cp := SliceDP{req:slc2, cache: utils.MapStorage{"0": "val1"}}
type exp struct {
data any
err error
}
tests := []struct {
name string
arg []string
exp exp
}{
{
name: "empty field path",
arg: []string{},
exp: exp{data: nil, err: nil},
},
{
name: "invalid field path",
arg: slc,
exp: exp{data: nil, err: fmt.Errorf("Invalid fieldPath %+v", slc)},
},
{
name: "item found in cache",
arg: slc2,
exp: exp{data: "val1", err: nil},
},
{
name: "strings.Atoi error",
arg: slc3,
exp: exp{data: nil},
},
{
name: "error not found",
arg: slc4,
exp: exp{data: nil, err: utils.ErrNotFound},
},
}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := cp.FieldAsInterface(tt.arg)
if i == 3 {
if err == nil {
t.Fatal("was expecting an error")
}
} else {
if !reflect.DeepEqual(err, tt.exp.err) {
t.Fatalf("recived %s, expected %s", err, tt.exp.err)
}
}
if !reflect.DeepEqual(data, tt.exp.data) {
t.Errorf("recived %v, expected %v", data, tt.exp.data)
}
})
}
t.Run("no error", func(t *testing.T) {
slcRec := []string{"0", "1"}
slcArg := []string{"0"}
cp := SliceDP{req:slcRec, cache: utils.MapStorage{"test": "val1"}}
data, err := cp.FieldAsInterface(slcArg)
if err != nil {
t.Fatal("was not expecting an error:", err)
}
if !reflect.DeepEqual(data, "0") {
t.Errorf("recived %v, expected %v", data, "0")
}
})
}
func TestSliceDPFieldAsString(t *testing.T) {
slc := []string{"0", "1"}
cP := NewSliceDP(slc)
type exp struct {
data string
err error
}
tests := []struct{
name string
arg []string
exp exp
}{
{
name: "err",
arg: []string{"2"},
exp: exp{"", utils.ErrNotFound},
},
{
name: "no err",
arg: []string{"1"},
exp: exp{"1", nil},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := cP.FieldAsString(tt.arg)
if err != tt.exp.err {
t.Fatalf("recived %s, expected %s", err, tt.exp.err)
}
if !reflect.DeepEqual(data, tt.exp.data) {
t.Errorf("recived %v, expected %v", data, tt.exp.data)
}
})
}
}
func TestSliceDPRemoteHost(t *testing.T) {
slc := []string{"test", "test2"}
cP := NewSliceDP(slc)
rcv := cP.RemoteHost()
exp := utils.LocalAddr()
if rcv.String() != exp.String() {
t.Errorf("recived %s, expected %s", rcv, exp)
}
}

View File

@@ -287,7 +287,7 @@ func getPathFromValue(in reflect.Value, prefix string) (out []string) {
out = append(out, getPathFromValue(in.Index(i), pref+NestingSep)...)
}
case reflect.Map:
iter := reflect.ValueOf(in).MapRange()
iter := in.MapRange()
for iter.Next() {
pref := prefix + iter.Key().String()
// out = append(out, pref)

View File

@@ -493,11 +493,11 @@ func TestGetPathFromValue(t *testing.T) {
args: args{reflect.ValueOf(&[]string{"test"}), "test"},
exp: []string{"test[0]"},
},
/*{
{
name: "map",
args: args{reflect.ValueOf(map[string]string{"test": "test"}), "test"},
exp: []string{"testtest"},
},*/
},
{
name: "struct",
args: args{reflect.ValueOf(struct{ test string }{"test"}), "test"},

View File

@@ -33,7 +33,6 @@ func fieldByIndexIsEmpty(v reflect.Value, index []int) bool {
for i, x := range index {
if i > 0 {
if v.Kind() == reflect.Ptr && v.Type().Elem().Kind() == reflect.Struct {
//impossible to get here
if v.IsNil() {
return true
}

View File

@@ -114,6 +114,7 @@ func TestNonemptyStructFields(t *testing.T) {
Account string
Type string
ActionTimingsId string
//Amount int
}{"bevoip.eu", true, "testaccount", META_PREPAID, ""}
mapStruct := NonemptyStructFields(&attr)
expMapStruct := map[string]any{
@@ -121,6 +122,7 @@ func TestNonemptyStructFields(t *testing.T) {
"Direction": true,
"Account": "testaccount",
"Type": META_PREPAID,
//"Amount": 10,
}
if !reflect.DeepEqual(expMapStruct, mapStruct) {
t.Errorf("expecting: %+v, received: %+v", expMapStruct, mapStruct)