Skip to content

Commit

Permalink
Added coverage tests for fwvdp.go, objdp.go and removed error variabl…
Browse files Browse the repository at this point in the history
…es from source code
  • Loading branch information
NikolasPetriti authored and danbogos committed Jun 23, 2023
1 parent 86a1fbd commit 65cf729
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 79 deletions.
34 changes: 29 additions & 5 deletions config/fwvdp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/cgrates/cgrates/utils"
)

func TestNewFWVProvider(t *testing.T) {
func TestFVWDPNewFWVProvider(t *testing.T) {

dP := NewFWVProvider("test")

Expand All @@ -45,9 +45,9 @@ func TestFwvdpString(t *testing.T) {
}
}

func TestFWVFieldAsInterface(t *testing.T) {
func TestFWVDPFieldAsInterface(t *testing.T) {

dP := FWVProvider{req: "test", cache: utils.MapStorage{"0-4": "test"}}
dP := FWVProvider{req: "test", cache: utils.MapStorage{"test": "test"}}

tests := []struct{
name string
Expand All @@ -61,6 +61,30 @@ func TestFWVFieldAsInterface(t *testing.T) {
exp: nil,
err: false,
},
{
name: "empty field path",
arg: []string{"a-b"},
exp: nil,
err: true,
},
{
name: "empty field path",
arg: []string{"5-6"},
exp: "",
err: true,
},
{
name: "empty field path",
arg: []string{"0-a"},
exp: nil,
err: true,
},
{
name: "empty field path",
arg: []string{"0-6"},
exp: "",
err: true,
},
}

for _, tt := range tests {
Expand All @@ -85,7 +109,7 @@ func TestFWVFieldAsInterface(t *testing.T) {
}
}

func TestFWVFieldAsString(t *testing.T) {
func TestFVWDPFieldAsString(t *testing.T) {

dP := NewFWVProvider("test")

Expand Down Expand Up @@ -133,7 +157,7 @@ func TestFWVFieldAsString(t *testing.T) {
}
}

func TestFWVRemoteHost(t *testing.T) {
func TestFVWDPRemoteHost(t *testing.T) {

dP := NewFWVProvider("test")

Expand Down
264 changes: 264 additions & 0 deletions config/objdp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
/*
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 TestObjDPNewObjectDP(t *testing.T) {

type args struct {
obj any
prfxSlc []string
}

tests := []struct {
name string
args args
exp utils.DataProvider
}{
{
name: "new objectDP",
args: args{obj: "test", prfxSlc: []string{}},
exp: &ObjectDP{obj: "test", cache: make(map[string]any), prfxSls: []string{}},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

rcv := NewObjectDP(tt.args.obj, tt.args.prfxSlc)

if rcv.String() != tt.exp.String() {
t.Errorf("recived %+s, expected %+s", rcv, tt.exp.String())
}
})
}
}

func TestObjDPSetCache(t *testing.T) {

type args struct {
path string
val any
}

tests := []struct{
name string
args args
}{
{
name: "set cache",
args: args{"test", "val1"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

odp := ObjectDP{"test", map[string]any{}, []string{}}

odp.setCache(tt.args.path, tt.args.val)

if odp.cache["test"] != "val1" {
t.Error("didn't set cache")
}
})
}
}

func TestObjDPGetCache(t *testing.T) {

type exp struct {
val any
has bool
}

tests := []struct{
name string
arg string
exp exp
}{
{
name: "get cache",
arg: "test",
exp: exp{val: "val1", has: true},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

odp := ObjectDP{"test", map[string]any{}, []string{}}

odp.setCache(tt.arg, tt.exp.val)
rcv, has := odp.getCache(tt.arg)

if !has {
t.Error("didn't get cache")
}

if rcv != tt.exp.val {
t.Errorf("recived %s, expected %s", rcv, tt.exp.val)
}

})
}
}

func TestObjDPFieldAsInterface(t *testing.T) {

odp := ObjectDP{"test", map[string]any{}, []string{}}

odp.setCache("test", "val1")

type exp struct {
data any
err bool
}

tests := []struct{
name string
arg []string
exp exp
slcPrfx []string
}{
{
name: "found in cache",
arg: []string{"test"},
exp: exp{data: "val1", err: false},
slcPrfx: []string{"!", "."},
},
{
name: "object has prefix slice and length of field pat his smaller than lenght of prefix slice",
arg: []string{"test1"},
exp: exp{data: nil, err: true},
slcPrfx: []string{"!", "."},
},
{
name: "has slice prefix different from field path",
arg: []string{"test1", "test2"},
exp: exp{data: nil, err: true},
slcPrfx: []string{"!", "."},
},
{
name: "has slice prefix",
arg: []string{"!", "."},
exp: exp{data: nil, err: false},
slcPrfx: []string{"!", "."},
},
{
name: "has selector",
arg: []string{"test[0", "."},
exp: exp{data: nil, err: true},
slcPrfx: []string{},
},
{
name: "has selector",
arg: []string{"test[0]", "."},
exp: exp{data: nil, err: true},
slcPrfx: []string{},
},
}

for _, tt := range tests {

odp.prfxSls = tt.slcPrfx

data, err := odp.FieldAsInterface(tt.arg)

if !tt.exp.err {
if err != nil {
t.Fatal("was not expecting an error")
}
} else {
if err == nil {
t.Fatal("was expecting an error")
}
}

if !reflect.DeepEqual(data, tt.exp.data) {
t.Errorf("recived %v, expected %v", data, tt.exp.data)
}

}
}

func TestObjDPFieldAsString(t *testing.T) {

type exp struct {
data string
err bool
}

tests := []struct{
name string
args []string
exp exp
}{
{
name: "error",
args: []string{"123"},
exp: exp{data: "", err: true},
},
{
name: "error",
args: []string{},
exp: exp{data: "", err: false},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

odp := ObjectDP{"test", map[string]any{}, []string{}}

data, err := odp.FieldAsString(tt.args)

if tt.exp.err {
if err == nil {
t.Error("was expecting an error")
}
} else {
if err != nil {
t.Error("was not expecting an error")
}
}

if data != tt.exp.data {
t.Errorf("recived %s, expected %s", data, tt.exp.data)
}
})
}
}

func TestObjDPRemoteHost(t *testing.T) {

odp := ObjectDP{"test", map[string]any{}, []string{}}

rcv := odp.RemoteHost()

if rcv.String() != "local" {
t.Errorf("recived %s", rcv.String())
}
}

0 comments on commit 65cf729

Please sign in to comment.