Skip to content

Commit

Permalink
Coverage tests for fwvdp.go and slicedp.go
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolasPetriti authored and danbogos committed Jun 22, 2023
1 parent 40d0cbb commit 86a1fbd
Show file tree
Hide file tree
Showing 6 changed files with 339 additions and 4 deletions.
145 changes: 145 additions & 0 deletions config/fwvdp_test.go
Original file line number Diff line number Diff line change
@@ -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 changes: 189 additions & 0 deletions config/slicedp_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion utils/mapstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions utils/mapstorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
1 change: 0 additions & 1 deletion utils/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions utils/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ 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{
"Tenant": "bevoip.eu",
"Direction": true,
"Account": "testaccount",
"Type": META_PREPAID,
//"Amount": 10,
}
if !reflect.DeepEqual(expMapStruct, mapStruct) {
t.Errorf("expecting: %+v, received: %+v", expMapStruct, mapStruct)
Expand Down

0 comments on commit 86a1fbd

Please sign in to comment.