mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-17 14:19:54 +05:00
Add coverage tests for config and engine
This commit is contained in:
committed by
Dan Christian Bogos
parent
4d246d7697
commit
fe2f034a7a
@@ -18,6 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package config
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
@@ -350,3 +355,229 @@ func TestGetErrorLine2(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type reader struct {
|
||||
}
|
||||
|
||||
func (r *reader) Read(p []byte) (n int, err error) {
|
||||
return 0, fmt.Errorf("test error: %v", n)
|
||||
}
|
||||
|
||||
func TestRjReaderNewRjReader(t *testing.T) {
|
||||
r := &reader{}
|
||||
|
||||
rcv, err := NewRjReader(r)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != "test error: 0" {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
|
||||
if rcv != nil {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRjReaderUnreadByte(t *testing.T) {
|
||||
val := "val1"
|
||||
mrsh, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
rjr := &rjReader{
|
||||
buf: mrsh,
|
||||
isInString: true,
|
||||
indx: 0,
|
||||
}
|
||||
|
||||
err = rjr.UnreadByte()
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != bufio.ErrInvalidUnreadByte.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRjReaderconsumeComent(t *testing.T) {
|
||||
val := "1"
|
||||
mrsh, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
rjr := &rjReader{
|
||||
buf: mrsh,
|
||||
isInString: true,
|
||||
indx: 20,
|
||||
}
|
||||
|
||||
rcv, err := rjr.consumeComent('*')
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != utils.ErrJsonIncompleteComment.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
|
||||
if rcv != true {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRjReaderPeekByteWC(t *testing.T) {
|
||||
val := "1"
|
||||
mrsh, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
rjr := &rjReader{
|
||||
buf: mrsh,
|
||||
isInString: true,
|
||||
indx: 20,
|
||||
}
|
||||
|
||||
rcv, err := rjr.PeekByteWC()
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != io.EOF.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
|
||||
if rcv != 0 {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRjReaderreadEnvName(t *testing.T) {
|
||||
val := "val1"
|
||||
mrsh, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
rjr := &rjReader{
|
||||
buf: mrsh,
|
||||
isInString: true,
|
||||
indx: 0,
|
||||
}
|
||||
|
||||
rcv1, rcv2, err := rjr.readEnvName(20)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != io.EOF.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
|
||||
if rcv1 != nil {
|
||||
t.Error(rcv1)
|
||||
}
|
||||
|
||||
if rcv2 != 20 {
|
||||
t.Error(rcv2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRjReaderReplaceEnv(t *testing.T) {
|
||||
val := "val1"
|
||||
mrsh, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
rjr := &rjReader{
|
||||
buf: mrsh,
|
||||
isInString: true,
|
||||
indx: 0,
|
||||
}
|
||||
|
||||
err = rjr.replaceEnv(20)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != "NOT_FOUND:ENV_VAR:" {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRjReaderHandleJSONError(t *testing.T) {
|
||||
err1 := &json.InvalidUTF8Error{
|
||||
S: "test err1",
|
||||
}
|
||||
err2 := &json.InvalidUnmarshalError{
|
||||
Type: reflect.TypeOf(fmt.Errorf("error %s", "test")),
|
||||
}
|
||||
err3 := &json.UnmarshalTypeError{
|
||||
Type: reflect.TypeOf(fmt.Errorf("error %s", "test")),
|
||||
}
|
||||
val := "val1"
|
||||
mrsh, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
rjr := &rjReader{
|
||||
buf: mrsh,
|
||||
isInString: true,
|
||||
indx: 0,
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
arg error
|
||||
exp string
|
||||
}{
|
||||
{
|
||||
name: "case nil",
|
||||
arg: nil,
|
||||
exp: "",
|
||||
},
|
||||
{
|
||||
name: "case *json.InvalidUTF8Error, *json.UnmarshalFieldError",
|
||||
arg: err1,
|
||||
exp: `json: invalid UTF-8 in string: "test err1"`,
|
||||
},
|
||||
{
|
||||
name: "case *json.InvalidUnmarshalError",
|
||||
arg: err2,
|
||||
exp: `json: Unmarshal(nil *errors.errorString)`,
|
||||
},
|
||||
{
|
||||
name: "case *json.UnmarshalTypeError",
|
||||
arg: err3,
|
||||
exp: `json: cannot unmarshal into Go value of type *errors.errorString at line 0 around position 0`,
|
||||
},
|
||||
{
|
||||
name: "case default",
|
||||
arg: errors.New("test error"),
|
||||
exp: `test error`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := rjr.HandleJSONError(tt.arg)
|
||||
|
||||
if tt.exp != "" {
|
||||
if err != nil {
|
||||
if err.Error() != tt.exp {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
55
engine/cgrsafev_test.go
Normal file
55
engine/cgrsafev_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
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 engine
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestCGRSafEventNewCGRSafEventFromCGREventAsCGREvent(t *testing.T) {
|
||||
tm := time.Date(2021, 8, 15, 14, 30, 45, 100, time.Local)
|
||||
cgrEv := &utils.CGREvent{
|
||||
Tenant: "test",
|
||||
ID: "test",
|
||||
Time: &tm,
|
||||
Event: map[string]any{"test": 1},
|
||||
}
|
||||
|
||||
exp := &CGRSafEvent{
|
||||
Tenant: cgrEv.Tenant,
|
||||
ID: cgrEv.ID,
|
||||
Time: cgrEv.Time,
|
||||
Event: NewSafEvent(cgrEv.Event),
|
||||
}
|
||||
rcv := NewCGRSafEventFromCGREvent(cgrEv)
|
||||
|
||||
if !reflect.DeepEqual(exp, rcv) {
|
||||
t.Errorf("\nexpected: %s\nreceived: %s\n", utils.ToJSON(exp), utils.ToJSON(rcv))
|
||||
}
|
||||
|
||||
rcv2 := rcv.AsCGREvent()
|
||||
|
||||
if !reflect.DeepEqual(cgrEv, rcv2) {
|
||||
t.Errorf("\nexpected: %s\nreceived: %s\n", utils.ToJSON(cgrEv), utils.ToJSON(rcv2))
|
||||
}
|
||||
}
|
||||
@@ -356,6 +356,21 @@ func TestChargerV1ProcessEvent(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestChargersV1ProcessEvent(t *testing.T) {
|
||||
cS := &ChargerService{}
|
||||
args := &utils.CGREventWithArgDispatcher{}
|
||||
|
||||
err := cS.V1ProcessEvent(args, nil)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != utils.NewErrMandatoryIeMissing("Event").Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
}
|
||||
|
||||
// func TestChSListenAndServe(t *testing.T) {
|
||||
// cfg, _ := config.NewDefaultCGRConfig()
|
||||
// db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
|
||||
|
||||
@@ -3679,3 +3679,80 @@ func TestEventCostnewChargingIncrement(t *testing.T) {
|
||||
t.Errorf("expected %v, received %v", exp, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventCostAsRefundIncrements(t *testing.T) {
|
||||
str := "test"
|
||||
td := 1 * time.Second
|
||||
ec := &EventCost{
|
||||
CGRID: str,
|
||||
RunID: str,
|
||||
StartTime: time.Date(2021, 8, 15, 14, 30, 45, 100, time.Local),
|
||||
Usage: &td,
|
||||
}
|
||||
|
||||
rcv := ec.AsRefundIncrements(str)
|
||||
exp := &CallDescriptor{
|
||||
CgrID: ec.CGRID,
|
||||
RunID: ec.RunID,
|
||||
ToR: str,
|
||||
TimeStart: ec.StartTime,
|
||||
TimeEnd: ec.StartTime.Add(ec.GetUsage()),
|
||||
DurationIndex: ec.GetUsage(),
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(exp, rcv) {
|
||||
t.Errorf("\nexpected: %s\nreceived: %s\n", utils.ToJSON(exp), utils.ToJSON(rcv))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventCostappendChargingIntervalFromEventCost(t *testing.T) {
|
||||
str := "test"
|
||||
td := 1 * time.Second
|
||||
fl := 1.2
|
||||
str2 := "test2"
|
||||
td2 := 2 * time.Millisecond
|
||||
ec := &EventCost{
|
||||
CGRID: str,
|
||||
RunID: str,
|
||||
StartTime: time.Date(2021, 8, 15, 14, 30, 45, 100, time.Local),
|
||||
Usage: &td,
|
||||
Charges: []*ChargingInterval{{
|
||||
RatingID: str,
|
||||
Increments: []*ChargingIncrement{{
|
||||
Usage: td,
|
||||
Cost: fl,
|
||||
AccountingID: str,
|
||||
CompressFactor: 1,
|
||||
}},
|
||||
CompressFactor: 1,
|
||||
usage: &td,
|
||||
ecUsageIdx: &td,
|
||||
cost: &fl,
|
||||
},
|
||||
}}
|
||||
oEC := &EventCost{
|
||||
CGRID: str2,
|
||||
RunID: str2,
|
||||
StartTime: time.Date(2022, 8, 15, 14, 30, 45, 100, time.Local),
|
||||
Usage: &td2,
|
||||
Charges: []*ChargingInterval{{
|
||||
RatingID: str,
|
||||
Increments: []*ChargingIncrement{{
|
||||
Usage: td,
|
||||
Cost: fl,
|
||||
AccountingID: str,
|
||||
CompressFactor: 1,
|
||||
}},
|
||||
CompressFactor: 1,
|
||||
usage: &td,
|
||||
ecUsageIdx: &td,
|
||||
cost: &fl,
|
||||
}},
|
||||
}
|
||||
|
||||
ec.appendChargingIntervalFromEventCost(oEC, 0)
|
||||
|
||||
if ec.Charges[0].CompressFactor != 2 {
|
||||
t.Error("didn't append charging interval from event cost")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user