Tests in console

This commit is contained in:
andronache
2021-01-28 15:13:25 +02:00
committed by Dan Christian Bogos
parent 1973d906d4
commit e5b3b2a73b
4 changed files with 147 additions and 9 deletions

View File

@@ -18,7 +18,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package console
/*
import (
"reflect"
"strings"
"testing"
v1 "github.com/cgrates/cgrates/apier/v1"
"github.com/cgrates/cgrates/utils"
)
func TestCmdComputeActionPlanIndexes(t *testing.T) {
// commands map is initiated in init function
command := commands["compute_actionplan_indexes"]
@@ -31,9 +39,10 @@ func TestCmdComputeActionPlanIndexes(t *testing.T) {
t.Fatalf("invalid number of input parameters ")
}
// verify the type of input parameter
if ok := m.Type.In(1).AssignableTo(reflect.TypeOf(command.RpcParams(true))); !ok {
t.Fatalf("cannot assign input parameter")
// for coverage purpose
result := command.RpcParams(true)
if !reflect.DeepEqual(result, new(EmptyWrapper)) {
t.Errorf("Expected <%T>, Received <%T>", new(EmptyWrapper), result)
}
// verify the type of output parameter
if ok := m.Type.In(2).AssignableTo(reflect.TypeOf(command.RpcResult())); !ok {
@@ -44,4 +53,3 @@ func TestCmdComputeActionPlanIndexes(t *testing.T) {
t.Fatal(err)
}
}
*/

132
console/ping_test.go Normal file
View File

@@ -0,0 +1,132 @@
/*
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 console
import (
"reflect"
"strings"
"testing"
v1 "github.com/cgrates/cgrates/apier/v1"
"github.com/cgrates/cgrates/utils"
)
func TestCmdPingRoutesLow(t *testing.T) {
// commands map is initiated in init function
command := commands["ping"]
castCommand, canCast := command.(*CmdApierPing)
if !canCast {
t.Fatalf("cannot cast")
}
castCommand.item = utils.RoutesLow
result2 := command.RpcMethod()
if !reflect.DeepEqual(result2, utils.RouteSv1Ping) {
t.Errorf("Expected <%+v>, Received <%+v>", utils.RouteSv1Ping, result2)
}
m, ok := reflect.TypeOf(new(v1.RouteSv1)).MethodByName(strings.Split(command.RpcMethod(), utils.NestingSep)[1])
if !ok {
t.Fatal("method not found")
}
if m.Type.NumIn() != 3 { // ApierSv1 is consider and we expect 3 inputs
t.Fatalf("invalid number of input parameters ")
}
// for coverage purpose
result := command.RpcParams(true)
if !reflect.DeepEqual(result, new(StringWrapper)) {
t.Errorf("Expected <%T>, Received <%T>", new(StringWrapper), result)
}
// verify the type of output parameter
if ok := m.Type.In(2).AssignableTo(reflect.TypeOf(command.RpcResult())); !ok {
t.Fatalf("cannot assign output parameter")
}
// for coverage purpose
if err := command.PostprocessRpcParams(); err != nil {
t.Fatal(err)
}
}
func TestCmdPingAttributesLow(t *testing.T) {
// commands map is initiated in init function
command := commands["ping"]
castCommand, canCast := command.(*CmdApierPing)
if !canCast {
t.Fatalf("cannot cast")
}
castCommand.item = utils.AttributesLow
result2 := command.RpcMethod()
if !reflect.DeepEqual(result2, utils.AttributeSv1Ping) {
t.Errorf("Expected <%+v>, Received <%+v>", utils.AttributeSv1Ping, result2)
}
m, ok := reflect.TypeOf(new(v1.AttributeSv1)).MethodByName(strings.Split(command.RpcMethod(), utils.NestingSep)[1])
if !ok {
t.Fatal("method not found")
}
if m.Type.NumIn() != 3 { // ApierSv1 is consider and we expect 3 inputs
t.Fatalf("invalid number of input parameters ")
}
// for coverage purpose
result := command.RpcParams(true)
if !reflect.DeepEqual(result, new(StringWrapper)) {
t.Errorf("Expected <%T>, Received <%T>", new(StringWrapper), result)
}
// verify the type of output parameter
if ok := m.Type.In(2).AssignableTo(reflect.TypeOf(command.RpcResult())); !ok {
t.Fatalf("cannot assign output parameter")
}
// for coverage purpose
if err := command.PostprocessRpcParams(); err != nil {
t.Fatal(err)
}
}
func TestCmdPingChargerSLow(t *testing.T) {
// commands map is initiated in init function
command := commands["ping"]
castCommand, canCast := command.(*CmdApierPing)
if !canCast {
t.Fatalf("cannot cast")
}
castCommand.item = utils.ChargerSLow
result2 := command.RpcMethod()
if !reflect.DeepEqual(result2, utils.ChargerSv1Ping) {
t.Errorf("Expected <%+v>, Received <%+v>", utils.ChargerSv1Ping, result2)
}
m, ok := reflect.TypeOf(new(v1.AttributeSv1)).MethodByName(strings.Split(command.RpcMethod(), utils.NestingSep)[1])
if !ok {
t.Fatal("method not found")
}
if m.Type.NumIn() != 3 { // ApierSv1 is consider and we expect 3 inputs
t.Fatalf("invalid number of input parameters ")
}
// for coverage purpose
result := command.RpcParams(true)
if !reflect.DeepEqual(result, new(StringWrapper)) {
t.Errorf("Expected <%T>, Received <%T>", new(StringWrapper), result)
}
// verify the type of output parameter
if ok := m.Type.In(2).AssignableTo(reflect.TypeOf(command.RpcResult())); !ok {
t.Fatalf("cannot assign output parameter")
}
// for coverage purpose
if err := command.PostprocessRpcParams(); err != nil {
t.Fatal(err)
}
}

View File

@@ -18,9 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package console
import (
"github.com/cgrates/cgrates/utils"
)
import "github.com/cgrates/cgrates/utils"
func init() {
c := &CmdSleep{

View File

@@ -64,7 +64,7 @@ func TestCmdSleepPostprocessRpcParamsCase2(t *testing.T) {
err := testStruct.PostprocessRpcParams()
if err == nil || err.Error() != "time: invalid duration \"test_item\"" {
t.Errorf("Expected <time: invalid duration \"test_item\">, Received <%T>", err)
t.Errorf("Expected <time: invalid duration \"test_item\">, Received <%+v>", err)
}
}