mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add coverage tests for fctemplate.go
This commit is contained in:
committed by
Dan Christian Bogos
parent
023cf8f5f2
commit
7042048771
@@ -18,32 +18,134 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestNewFCTemplateFromFCTemplateJsonCfg(t *testing.T) {
|
||||
jsonCfg := &FcTemplateJsonCfg{
|
||||
Tag: utils.StringPointer("Tenant"),
|
||||
Type: utils.StringPointer("*composed"),
|
||||
Path: utils.StringPointer("Tenant"),
|
||||
Filters: &[]string{"Filter1", "Filter2"},
|
||||
Value: utils.StringPointer("cgrates.org"),
|
||||
|
||||
tag := "tagTest"
|
||||
typ := "typeTest"
|
||||
path := "pathTest"
|
||||
att := "attTest"
|
||||
slc := []string{"val1", "val2"}
|
||||
val := "valTest"
|
||||
width := 10
|
||||
strip := "stripTest"
|
||||
pad := "padTest"
|
||||
mand := false
|
||||
newBr := false
|
||||
time := "timeTest"
|
||||
block := false
|
||||
brk := false
|
||||
lay := "layTest"
|
||||
cost := 50
|
||||
round := 1
|
||||
maskD := "maskDTest"
|
||||
maskL := 5
|
||||
|
||||
fcJs := FcTemplateJsonCfg{
|
||||
Tag: &tag,
|
||||
Type: &typ,
|
||||
Path: &path,
|
||||
Attribute_id: &att,
|
||||
Filters: &slc,
|
||||
Value: &val,
|
||||
Width: &width,
|
||||
Strip: &strip,
|
||||
Padding: &pad,
|
||||
Mandatory: &mand,
|
||||
New_branch: &newBr,
|
||||
Timezone: &time,
|
||||
Blocker: &block,
|
||||
Break_on_success: &brk,
|
||||
Layout: &lay,
|
||||
Cost_shift_digits: &cost,
|
||||
Rounding_decimals: &round,
|
||||
Mask_destinationd_id: &maskD,
|
||||
Mask_length: &maskL,
|
||||
}
|
||||
expected := &FCTemplate{
|
||||
Tag: "Tenant",
|
||||
Type: "*composed",
|
||||
Path: "Tenant",
|
||||
Filters: []string{"Filter1", "Filter2"},
|
||||
Value: NewRSRParsersMustCompile("cgrates.org", true, utils.INFIELD_SEP),
|
||||
|
||||
valRp, _ := NewRSRParsers(val, true, "")
|
||||
pSlc := strings.Split(path, utils.NestingSep)
|
||||
pItm := utils.NewPathItems(pSlc)
|
||||
|
||||
fcT := FCTemplate{
|
||||
Tag: tag,
|
||||
Type: typ, // Type of field
|
||||
Path: path, // Field identifier
|
||||
Filters: slc, // list of filter profiles
|
||||
Value: valRp,
|
||||
Width: width,
|
||||
Strip: strip,
|
||||
Padding: pad,
|
||||
Mandatory: mand,
|
||||
AttributeID: att, // Used by NavigableMap when creating CGREvent/XMLElements
|
||||
NewBranch: newBr, // Used by NavigableMap when creating XMLElements
|
||||
Timezone: time,
|
||||
Blocker: block,
|
||||
BreakOnSuccess: brk,
|
||||
Layout: lay, // time format
|
||||
CostShiftDigits: cost, // Used for CDR
|
||||
RoundingDecimals: &round,
|
||||
MaskDestID: maskD,
|
||||
MaskLen: maskL,
|
||||
pathItems: pItm, // Field identifier
|
||||
pathSlice: pSlc,
|
||||
}
|
||||
expected.ComputePath()
|
||||
if rcv, err := NewFCTemplateFromFCTemplateJsonCfg(jsonCfg, utils.INFIELD_SEP); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rcv) {
|
||||
t.Errorf("expected: %s ,received: %s", utils.ToJSON(expected), utils.ToJSON(rcv))
|
||||
|
||||
valErr := "test`test"
|
||||
|
||||
fcJs2 := FcTemplateJsonCfg{
|
||||
Value: &valErr,
|
||||
}
|
||||
|
||||
type args struct {
|
||||
jsnCfg *FcTemplateJsonCfg
|
||||
separator string
|
||||
}
|
||||
|
||||
type exp struct {
|
||||
val *FCTemplate
|
||||
err error
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
exp exp
|
||||
}{
|
||||
{
|
||||
name: "cover most if statements",
|
||||
args: args{jsnCfg: &fcJs, separator: ""},
|
||||
exp: exp{val: &fcT, err: nil},
|
||||
},
|
||||
{
|
||||
name: "cover most if statements",
|
||||
args: args{jsnCfg: &fcJs2, separator: ""},
|
||||
exp: exp{val: nil, err: fmt.Errorf("Unclosed unspilit syntax")},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
rcv, err := NewFCTemplateFromFCTemplateJsonCfg(tt.args.jsnCfg, tt.args.separator)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != tt.exp.err.Error() {
|
||||
t.Fatalf("expected %s, recived %s", tt.exp.err, err)
|
||||
}
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(rcv, tt.exp.val) {
|
||||
t.Errorf("expected %v, recived %v", tt.exp.val, rcv)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,9 +190,26 @@ func TestFCTemplatesFromFCTemplatesJsonCfg(t *testing.T) {
|
||||
} else if !reflect.DeepEqual(expected, rcv) {
|
||||
t.Errorf("expected: %s ,received: %s", utils.ToJSON(expected), utils.ToJSON(rcv))
|
||||
}
|
||||
|
||||
t.Run("recive error", func(t *testing.T) {
|
||||
|
||||
fcJs2 := FcTemplateJsonCfg{
|
||||
Value: utils.StringPointer("test`test"),
|
||||
}
|
||||
|
||||
slcFc := []*FcTemplateJsonCfg{
|
||||
&fcJs2,
|
||||
}
|
||||
|
||||
_, err := FCTemplatesFromFCTemplatesJsonCfg(slcFc, "")
|
||||
|
||||
if err.Error() != fmt.Errorf("Unclosed unspilit syntax").Error() {
|
||||
t.Error("didn't recive an error or wrong error message")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestFCTemplateInflate1(t *testing.T) {
|
||||
func TestFCTemplateInflateTemplates(t *testing.T) {
|
||||
fcTmp1 := []*FCTemplate{
|
||||
{
|
||||
Tag: "Tenant",
|
||||
@@ -302,6 +421,74 @@ func TestFCTemplateInflate3(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFCTemplateInflate4(t *testing.T) {
|
||||
|
||||
fcT := FCTemplate{
|
||||
Tag: "TmpMap",
|
||||
Type: "*template",
|
||||
Filters: []string{"Filter1_1", "Filter2_2"},
|
||||
Value: NewRSRParsersMustCompile("TmpMap", true, utils.INFIELD_SEP),
|
||||
}
|
||||
fcT2 := FCTemplate{
|
||||
Tag: "TmpMap2",
|
||||
Type: "*template",
|
||||
Filters: []string{"Filter1_1", "Filter2_2"},
|
||||
Value: NewRSRParsersMustCompile("TmpMap", true, utils.INFIELD_SEP),
|
||||
}
|
||||
fcT3 := FCTemplate{
|
||||
Tag: "TmpMap3",
|
||||
Type: "*template",
|
||||
Filters: []string{"Filter1_1", "Filter2_2"},
|
||||
Value: NewRSRParsersMustCompile("TmpMap", true, utils.INFIELD_SEP),
|
||||
}
|
||||
|
||||
fcTm := FCTemplate{}
|
||||
|
||||
type args struct {
|
||||
fcts []*FCTemplate
|
||||
msgTpls map[string][]*FCTemplate
|
||||
}
|
||||
|
||||
type exp struct {
|
||||
out []*FCTemplate
|
||||
err error
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
exp exp
|
||||
}{
|
||||
{
|
||||
name: "does not have templates",
|
||||
args: args{[]*FCTemplate{}, map[string][]*FCTemplate{}},
|
||||
exp: exp{out: nil, err: nil},
|
||||
},
|
||||
{
|
||||
name: "expecting error",
|
||||
args: args{fcts: []*FCTemplate{&fcT, &fcT2, &fcT3}, msgTpls: map[string][]*FCTemplate{"TmpMap": {&fcTm}}},
|
||||
exp: exp{out: []*FCTemplate{&fcTm, &fcTm, &fcTm}, err: nil},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
rcv, err := InflateTemplates(tt.args.fcts, tt.args.msgTpls)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != tt.exp.err.Error() {
|
||||
t.Error("wrong error message:", err)
|
||||
}
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(rcv, tt.exp.out) {
|
||||
t.Errorf("expecting %v, recived %v", tt.exp.out, rcv)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFCTemplateClone(t *testing.T) {
|
||||
smpl := &FCTemplate{
|
||||
Tag: "Tenant",
|
||||
@@ -332,13 +519,13 @@ func TestFCTemplateClone(t *testing.T) {
|
||||
|
||||
func TestFCTemplateGetPathSlice(t *testing.T) {
|
||||
fc := FCTemplate{
|
||||
Tag: "Elem1",
|
||||
Type: "*composed",
|
||||
Path: "Elem1",
|
||||
Filters: []string{"Filter1", "Filter2"},
|
||||
Value: NewRSRParsersMustCompile("Elem1", true, utils.INFIELD_SEP),
|
||||
pathItems: utils.PathItems{utils.PathItem{Field: "test"}},
|
||||
pathSlice: []string{"val1", "val2"},
|
||||
Tag: "Elem1",
|
||||
Type: "*composed",
|
||||
Path: "Elem1",
|
||||
Filters: []string{"Filter1", "Filter2"},
|
||||
Value: NewRSRParsersMustCompile("Elem1", true, utils.INFIELD_SEP),
|
||||
pathItems: utils.PathItems{utils.PathItem{Field: "test"}},
|
||||
pathSlice: []string{"val1", "val2"},
|
||||
}
|
||||
|
||||
rcv := fc.GetPathSlice()
|
||||
@@ -352,13 +539,13 @@ func TestFCTemplateGetPathSlice(t *testing.T) {
|
||||
|
||||
func TestFCTemplateGetPathItems(t *testing.T) {
|
||||
fc := FCTemplate{
|
||||
Tag: "Elem1",
|
||||
Type: "*composed",
|
||||
Path: "Elem1",
|
||||
Filters: []string{"Filter1", "Filter2"},
|
||||
Value: NewRSRParsersMustCompile("Elem1", true, utils.INFIELD_SEP),
|
||||
pathItems: utils.PathItems{utils.PathItem{Field: "test"}},
|
||||
pathSlice: []string{"val1", "val2"},
|
||||
Tag: "Elem1",
|
||||
Type: "*composed",
|
||||
Path: "Elem1",
|
||||
Filters: []string{"Filter1", "Filter2"},
|
||||
Value: NewRSRParsersMustCompile("Elem1", true, utils.INFIELD_SEP),
|
||||
pathItems: utils.PathItems{utils.PathItem{Field: "test"}},
|
||||
pathSlice: []string{"val1", "val2"},
|
||||
}
|
||||
|
||||
rcv := fc.GetPathItems()
|
||||
@@ -369,3 +556,99 @@ func TestFCTemplateGetPathItems(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFCTemplateAsMapInterface(t *testing.T) {
|
||||
|
||||
tag := "tagTest"
|
||||
typ := "typeTest"
|
||||
path := "pathTest"
|
||||
att := "attTest"
|
||||
slc := []string{"val1", "val2"}
|
||||
val := "valTest"
|
||||
width := 10
|
||||
strip := "stripTest"
|
||||
pad := "padTest"
|
||||
mand := true
|
||||
newBr := true
|
||||
time := "timeTest"
|
||||
block := true
|
||||
brk := true
|
||||
lay := "layTest"
|
||||
cost := 50
|
||||
round := 1
|
||||
maskD := "maskDTest"
|
||||
maskL := 5
|
||||
|
||||
valRp, _ := NewRSRParsers(val, true, "")
|
||||
pSlc := strings.Split(path, utils.NestingSep)
|
||||
pItm := utils.NewPathItems(pSlc)
|
||||
|
||||
fcT := FCTemplate{
|
||||
Tag: tag,
|
||||
Type: typ, // Type of field
|
||||
Path: path, // Field identifier
|
||||
Filters: slc, // list of filter profiles
|
||||
Value: valRp,
|
||||
Width: width,
|
||||
Strip: strip,
|
||||
Padding: pad,
|
||||
Mandatory: mand,
|
||||
AttributeID: att, // Used by NavigableMap when creating CGREvent/XMLElements
|
||||
NewBranch: newBr, // Used by NavigableMap when creating XMLElements
|
||||
Timezone: time,
|
||||
Blocker: block,
|
||||
BreakOnSuccess: brk,
|
||||
Layout: lay, // time format
|
||||
CostShiftDigits: cost, // Used for CDR
|
||||
RoundingDecimals: &round,
|
||||
MaskDestID: maskD,
|
||||
MaskLen: maskL,
|
||||
pathItems: pItm, // Field identifier
|
||||
pathSlice: pSlc,
|
||||
}
|
||||
|
||||
fcMp := map[string]any{
|
||||
utils.TagCfg: tag,
|
||||
utils.TypeCf: typ,
|
||||
utils.PathCfg: path,
|
||||
utils.FiltersCfg: slc,
|
||||
utils.ValueCfg: val,
|
||||
utils.WidthCfg: width,
|
||||
utils.StripCfg: strip,
|
||||
utils.PaddingCfg: pad,
|
||||
utils.MandatoryCfg: mand,
|
||||
utils.AttributeIDCfg: att,
|
||||
utils.NewBranchCfg: newBr,
|
||||
utils.TimezoneCfg: time,
|
||||
utils.BlockerCfg: block,
|
||||
utils.BreakOnSuccessCfg: brk,
|
||||
utils.LayoutCfg: lay,
|
||||
utils.CostShiftDigitsCfg: cost,
|
||||
utils.RoundingDecimalsCfg: round,
|
||||
utils.MaskDestIDCfg: maskD,
|
||||
utils.MaskLenCfg: maskL,
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
arg string
|
||||
exp map[string]any
|
||||
}{
|
||||
{
|
||||
name: "cover most returns",
|
||||
arg: "",
|
||||
exp: fcMp,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
rcv := fcT.AsMapInterface(tt.arg)
|
||||
|
||||
if !reflect.DeepEqual(rcv, tt.exp) {
|
||||
t.Errorf("recived %v, expected %v", rcv, tt.exp)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user