From 86a1fbd5311cd8f8c81ffe280b7e34ea7686f65b Mon Sep 17 00:00:00 2001 From: NikolasPetriti Date: Wed, 21 Jun 2023 16:26:26 +0200 Subject: [PATCH] Coverage tests for fwvdp.go and slicedp.go --- config/fwvdp_test.go | 145 ++++++++++++++++++++++++++++++ config/slicedp_test.go | 189 +++++++++++++++++++++++++++++++++++++++ utils/mapstorage.go | 2 +- utils/mapstorage_test.go | 4 +- utils/struct.go | 1 - utils/struct_test.go | 2 + 6 files changed, 339 insertions(+), 4 deletions(-) create mode 100644 config/fwvdp_test.go create mode 100644 config/slicedp_test.go diff --git a/config/fwvdp_test.go b/config/fwvdp_test.go new file mode 100644 index 000000000..e7b776021 --- /dev/null +++ b/config/fwvdp_test.go @@ -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 +*/ + +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") + } +} diff --git a/config/slicedp_test.go b/config/slicedp_test.go new file mode 100644 index 000000000..7cbf90cd6 --- /dev/null +++ b/config/slicedp_test.go @@ -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 +*/ + +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) + } +} diff --git a/utils/mapstorage.go b/utils/mapstorage.go index 4c881555c..ed09767b5 100644 --- a/utils/mapstorage.go +++ b/utils/mapstorage.go @@ -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) diff --git a/utils/mapstorage_test.go b/utils/mapstorage_test.go index f3e3b42f0..a3f57d1c4 100644 --- a/utils/mapstorage_test.go +++ b/utils/mapstorage_test.go @@ -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"}, diff --git a/utils/struct.go b/utils/struct.go index 7505117bf..0afd1205b 100644 --- a/utils/struct.go +++ b/utils/struct.go @@ -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 } diff --git a/utils/struct_test.go b/utils/struct_test.go index 8f44dcae3..70f6cc5e3 100644 --- a/utils/struct_test.go +++ b/utils/struct_test.go @@ -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)