Add test for EventCost as DataProvider using ObjectDP

This commit is contained in:
TeoV
2019-09-16 04:32:02 -04:00
committed by Dan Christian Bogos
parent 4ed47daabc
commit ae7785910f
2 changed files with 30 additions and 6 deletions

View File

@@ -54,6 +54,7 @@ func (objDP *ObjectDP) String() string {
// FieldAsInterface is part of engine.DataProvider interface
func (objDP *ObjectDP) FieldAsInterface(fldPath []string) (data interface{}, err error) {
obj := objDP.obj
// []string{ BalanceMap *monetary[0] Value }
var has bool
if data, has = objDP.getCache(strings.Join(fldPath, utils.NestingSep)); has {
@@ -75,10 +76,9 @@ func (objDP *ObjectDP) FieldAsInterface(fldPath []string) (data interface{}, err
} else {
prevFld += utils.NestingSep + fld
}
// check if we take the current path from cache
if data, has = objDP.getCache(prevFld); !has {
if data, err = utils.ReflectFieldMethodInterface(objDP.obj, fld); err != nil { // take the object the field for current path
if data, err = utils.ReflectFieldMethodInterface(obj, fld); err != nil { // take the object the field for current path
// in case of error set nil for the current path and return err
objDP.setCache(prevFld, nil)
return nil, err
@@ -86,14 +86,13 @@ func (objDP *ObjectDP) FieldAsInterface(fldPath []string) (data interface{}, err
// add the current field in prevFld so we can set in cache the full path with it's data
objDP.setCache(prevFld, data)
}
// change the obj to be the current data and continue the processing
objDP.obj = data
obj = data
if slctrStr != utils.EmptyString { //we have selector so we need to do an aditional get
prevFld += utils.IdxStart + slctrStr + utils.IdxEnd
// check if we take the current path from cache
if data, has = objDP.getCache(prevFld); !has {
if data, err = utils.ReflectFieldMethodInterface(objDP.obj, slctrStr); err != nil { // take the object the field for current path
if data, err = utils.ReflectFieldMethodInterface(obj, slctrStr); err != nil { // take the object the field for current path
// in case of error set nil for the current path and return err
objDP.setCache(prevFld, nil)
return nil, err
@@ -102,7 +101,7 @@ func (objDP *ObjectDP) FieldAsInterface(fldPath []string) (data interface{}, err
objDP.setCache(prevFld, data)
}
// change the obj to be the current data and continue the processing
objDP.obj = data
obj = data
}
}

View File

@@ -23,6 +23,7 @@ import (
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
@@ -2281,3 +2282,27 @@ func TestECSyncKeys(t *testing.T) {
utils.ToIJSON(eEC), utils.ToIJSON(ec))
}
}
func TestECAsDataProvider(t *testing.T) {
ecDP := config.NewObjectDP(testEC)
if data, err := ecDP.FieldAsInterface([]string{"RunID"}); err != nil {
t.Error(err)
} else if data != utils.META_DEFAULT {
t.Errorf("Expecting: <%s> \nreceived: <%s>", utils.META_DEFAULT, data)
}
if data, err := ecDP.FieldAsInterface([]string{"AccountSummary", "ID"}); err != nil {
t.Error(err)
} else if data != "dan" {
t.Errorf("Expecting: <%s> \nreceived: <%s>", "data", data)
}
if data, err := ecDP.FieldAsInterface([]string{"AccountSummary", "BalanceSummaries[1]", "ID"}); err != nil {
t.Error(err)
} else if data != "4b8b53d7-c1a1-4159-b845-4623a00a0165" {
t.Errorf("Expecting: <%s> \nreceived: <%s>", "4b8b53d7-c1a1-4159-b845-4623a00a0165", data)
}
if data, err := ecDP.FieldAsInterface([]string{"AccountSummary", "BalanceSummaries[2]", "Type"}); err != nil {
t.Error(err)
} else if data != "*voice" {
t.Errorf("Expecting: <%s> \nreceived: <%s>", "*voice", data)
}
}