mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add test for Attribute Type
This commit is contained in:
committed by
Dan Christian Bogos
parent
ff49c8997e
commit
e65aa274e7
@@ -20,6 +20,8 @@ package engine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
@@ -176,6 +178,65 @@ func (alS *AttributeService) processEvent(args *AttrArgsProcessEvent) (
|
||||
substitute, err = attribute.Substitute.ParseValue(utils.EmptyString)
|
||||
case utils.MetaVariable, utils.META_COMPOSED:
|
||||
substitute, err = attribute.Substitute.ParseEvent(args.Event)
|
||||
case utils.META_USAGE_DIFFERENCE:
|
||||
if len(attribute.Substitute) != 2 {
|
||||
return nil, fmt.Errorf("invalid arguments <%s>", utils.ToJSON(attribute.Substitute))
|
||||
}
|
||||
strVal1, err := attribute.Substitute[0].ParseEvent(args.Event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
strVal2, err := attribute.Substitute[1].ParseEvent(args.Event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tEnd, err := utils.ParseTimeDetectLayout(strVal1, utils.EmptyString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tStart, err := utils.ParseTimeDetectLayout(strVal2, utils.EmptyString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
substitute = tEnd.Sub(tStart).String()
|
||||
case utils.MetaSum:
|
||||
iFaceVals := make([]interface{}, len(attribute.Substitute))
|
||||
for i, val := range attribute.Substitute {
|
||||
strVal, err := val.ParseEvent(args.Event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
iFaceVals[i] = utils.StringToInterface(strVal)
|
||||
}
|
||||
ifaceSum, err := utils.Sum(iFaceVals...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
substitute, err = utils.IfaceAsString(ifaceSum)
|
||||
case utils.MetaValueExponent:
|
||||
if len(attribute.Substitute) != 2 {
|
||||
return nil, fmt.Errorf("invalid arguments <%s> to %s",
|
||||
utils.ToJSON(attribute.Substitute), utils.MetaValueExponent)
|
||||
}
|
||||
strVal1, err := attribute.Substitute[0].ParseEvent(args.Event) // String Value
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
val, err := strconv.ParseFloat(strVal1, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid value <%s> to %s",
|
||||
strVal1, utils.MetaValueExponent)
|
||||
}
|
||||
strVal2, err := attribute.Substitute[1].ParseEvent(args.Event) // String Exponent
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
exp, err := strconv.Atoi(strVal2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
substitute = strconv.FormatFloat(utils.Round(val*math.Pow10(exp),
|
||||
config.CgrConfig().GeneralCfg().RoundingDecimals, utils.ROUNDING_MIDDLE), 'f', -1, 64)
|
||||
default: // backwards compatible in case that Type is empty
|
||||
substitute, err = attribute.Substitute.ParseEvent(args.Event)
|
||||
}
|
||||
@@ -183,7 +244,10 @@ func (alS *AttributeService) processEvent(args *AttrArgsProcessEvent) (
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rply.AlteredFields = append(rply.AlteredFields, attribute.FieldName)
|
||||
//add only once the FieldName in AlteredFields
|
||||
if !utils.IsSliceMember(rply.AlteredFields, attribute.FieldName) {
|
||||
rply.AlteredFields = append(rply.AlteredFields, attribute.FieldName)
|
||||
}
|
||||
if substitute == utils.META_NONE {
|
||||
delete(rply.CGREvent.Event, attribute.FieldName)
|
||||
continue
|
||||
|
||||
@@ -1232,3 +1232,590 @@ func TestAttributeAttributeFilterIDs(t *testing.T) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.CGREvent.Event, reply.CGREvent.Event)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttributeProcessEventConstant(t *testing.T) {
|
||||
//refresh the DM
|
||||
if err := dmAtr.DataDB().Flush(""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
Cache.Clear(nil)
|
||||
if test, err := dmAtr.DataDB().IsDBEmpty(); err != nil {
|
||||
t.Error(err)
|
||||
} else if test != true {
|
||||
t.Errorf("\nExpecting: true got :%+v", test)
|
||||
}
|
||||
attrPrf1 := &AttributeProfile{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "ATTR_1",
|
||||
Contexts: []string{utils.MetaSessionS},
|
||||
FilterIDs: []string{"*string:~Field1:Value1"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
Attributes: []*Attribute{
|
||||
{
|
||||
FieldName: "Field2",
|
||||
Type: utils.META_CONSTANT,
|
||||
Substitute: config.NewRSRParsersMustCompile("ConstVal", true, utils.INFIELD_SEP),
|
||||
},
|
||||
},
|
||||
Blocker: true,
|
||||
Weight: 10,
|
||||
}
|
||||
// Add attribute in DM
|
||||
if err := dmAtr.SetAttributeProfile(attrPrf1, true); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
attrArgs := &AttrArgsProcessEvent{
|
||||
Context: utils.StringPointer(utils.MetaSessionS),
|
||||
ProcessRuns: utils.IntPointer(1),
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
},
|
||||
},
|
||||
}
|
||||
eRply := &AttrSProcessEventReply{
|
||||
MatchedProfiles: []string{"ATTR_1"},
|
||||
AlteredFields: []string{"Field2"},
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
"Field2": "ConstVal",
|
||||
},
|
||||
},
|
||||
}
|
||||
var reply AttrSProcessEventReply
|
||||
if err := attrService.V1ProcessEvent(attrArgs, &reply); err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.MatchedProfiles, reply.MatchedProfiles) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.MatchedProfiles, reply.MatchedProfiles)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.AlteredFields, reply.AlteredFields) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.AlteredFields, reply.AlteredFields)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.CGREvent.Event, reply.CGREvent.Event) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.CGREvent.Event, reply.CGREvent.Event)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttributeProcessEventVariable(t *testing.T) {
|
||||
//refresh the DM
|
||||
if err := dmAtr.DataDB().Flush(""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
Cache.Clear(nil)
|
||||
if test, err := dmAtr.DataDB().IsDBEmpty(); err != nil {
|
||||
t.Error(err)
|
||||
} else if test != true {
|
||||
t.Errorf("\nExpecting: true got :%+v", test)
|
||||
}
|
||||
attrPrf1 := &AttributeProfile{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "ATTR_1",
|
||||
Contexts: []string{utils.MetaSessionS},
|
||||
FilterIDs: []string{"*string:~Field1:Value1"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
Attributes: []*Attribute{
|
||||
{
|
||||
FieldName: "Field2",
|
||||
Type: utils.MetaVariable,
|
||||
Substitute: config.NewRSRParsersMustCompile("~Field1", true, utils.INFIELD_SEP),
|
||||
},
|
||||
{
|
||||
FieldName: "Field2",
|
||||
Type: utils.MetaVariable,
|
||||
Substitute: config.NewRSRParsersMustCompile("~TheField", true, utils.INFIELD_SEP),
|
||||
},
|
||||
},
|
||||
Blocker: true,
|
||||
Weight: 10,
|
||||
}
|
||||
// Add attribute in DM
|
||||
if err := dmAtr.SetAttributeProfile(attrPrf1, true); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
attrArgs := &AttrArgsProcessEvent{
|
||||
Context: utils.StringPointer(utils.MetaSessionS),
|
||||
ProcessRuns: utils.IntPointer(1),
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
"TheField": "TheVal",
|
||||
},
|
||||
},
|
||||
}
|
||||
eRply := &AttrSProcessEventReply{
|
||||
MatchedProfiles: []string{"ATTR_1"},
|
||||
AlteredFields: []string{"Field2"},
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
"Field2": "TheVal",
|
||||
"TheField": "TheVal",
|
||||
},
|
||||
},
|
||||
}
|
||||
var reply AttrSProcessEventReply
|
||||
if err := attrService.V1ProcessEvent(attrArgs, &reply); err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.MatchedProfiles, reply.MatchedProfiles) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.MatchedProfiles, reply.MatchedProfiles)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.AlteredFields, reply.AlteredFields) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.AlteredFields, reply.AlteredFields)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.CGREvent.Event, reply.CGREvent.Event) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.CGREvent.Event, reply.CGREvent.Event)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttributeProcessEventComposed(t *testing.T) {
|
||||
//refresh the DM
|
||||
if err := dmAtr.DataDB().Flush(""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
Cache.Clear(nil)
|
||||
if test, err := dmAtr.DataDB().IsDBEmpty(); err != nil {
|
||||
t.Error(err)
|
||||
} else if test != true {
|
||||
t.Errorf("\nExpecting: true got :%+v", test)
|
||||
}
|
||||
attrPrf1 := &AttributeProfile{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "ATTR_1",
|
||||
Contexts: []string{utils.MetaSessionS},
|
||||
FilterIDs: []string{"*string:~Field1:Value1"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
Attributes: []*Attribute{
|
||||
{
|
||||
FieldName: "Field2",
|
||||
Type: utils.META_COMPOSED,
|
||||
Substitute: config.NewRSRParsersMustCompile("~Field1", true, utils.INFIELD_SEP),
|
||||
},
|
||||
{
|
||||
FieldName: "Field2",
|
||||
Type: utils.META_COMPOSED,
|
||||
Substitute: config.NewRSRParsersMustCompile("_", true, utils.INFIELD_SEP),
|
||||
},
|
||||
{
|
||||
FieldName: "Field2",
|
||||
Type: utils.META_COMPOSED,
|
||||
Substitute: config.NewRSRParsersMustCompile("~TheField", true, utils.INFIELD_SEP),
|
||||
},
|
||||
},
|
||||
Blocker: true,
|
||||
Weight: 10,
|
||||
}
|
||||
// Add attribute in DM
|
||||
if err := dmAtr.SetAttributeProfile(attrPrf1, true); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
attrArgs := &AttrArgsProcessEvent{
|
||||
Context: utils.StringPointer(utils.MetaSessionS),
|
||||
ProcessRuns: utils.IntPointer(1),
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
"TheField": "TheVal",
|
||||
},
|
||||
},
|
||||
}
|
||||
eRply := &AttrSProcessEventReply{
|
||||
MatchedProfiles: []string{"ATTR_1"},
|
||||
AlteredFields: []string{"Field2"},
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
"Field2": "Value1_TheVal",
|
||||
"TheField": "TheVal",
|
||||
},
|
||||
},
|
||||
}
|
||||
var reply AttrSProcessEventReply
|
||||
if err := attrService.V1ProcessEvent(attrArgs, &reply); err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.MatchedProfiles, reply.MatchedProfiles) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.MatchedProfiles, reply.MatchedProfiles)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.AlteredFields, reply.AlteredFields) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.AlteredFields, reply.AlteredFields)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.CGREvent.Event, reply.CGREvent.Event) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.CGREvent.Event, reply.CGREvent.Event)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttributeProcessEventSum(t *testing.T) {
|
||||
//refresh the DM
|
||||
if err := dmAtr.DataDB().Flush(""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
Cache.Clear(nil)
|
||||
if test, err := dmAtr.DataDB().IsDBEmpty(); err != nil {
|
||||
t.Error(err)
|
||||
} else if test != true {
|
||||
t.Errorf("\nExpecting: true got :%+v", test)
|
||||
}
|
||||
attrPrf1 := &AttributeProfile{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "ATTR_1",
|
||||
Contexts: []string{utils.MetaSessionS},
|
||||
FilterIDs: []string{"*string:~Field1:Value1"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
Attributes: []*Attribute{
|
||||
{
|
||||
FieldName: "Field2",
|
||||
Type: utils.MetaSum,
|
||||
Substitute: config.NewRSRParsersMustCompile("10;~NumField;20", true, utils.INFIELD_SEP),
|
||||
},
|
||||
},
|
||||
Blocker: true,
|
||||
Weight: 10,
|
||||
}
|
||||
// Add attribute in DM
|
||||
if err := dmAtr.SetAttributeProfile(attrPrf1, true); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
attrArgs := &AttrArgsProcessEvent{
|
||||
Context: utils.StringPointer(utils.MetaSessionS),
|
||||
ProcessRuns: utils.IntPointer(1),
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
"TheField": "TheVal",
|
||||
"NumField": "20",
|
||||
},
|
||||
},
|
||||
}
|
||||
eRply := &AttrSProcessEventReply{
|
||||
MatchedProfiles: []string{"ATTR_1"},
|
||||
AlteredFields: []string{"Field2"},
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
"TheField": "TheVal",
|
||||
"NumField": "20",
|
||||
"Field2": "50",
|
||||
},
|
||||
},
|
||||
}
|
||||
var reply AttrSProcessEventReply
|
||||
if err := attrService.V1ProcessEvent(attrArgs, &reply); err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.MatchedProfiles, reply.MatchedProfiles) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.MatchedProfiles, reply.MatchedProfiles)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.AlteredFields, reply.AlteredFields) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.AlteredFields, reply.AlteredFields)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.CGREvent.Event, reply.CGREvent.Event) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.CGREvent.Event, reply.CGREvent.Event)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttributeProcessEventUsageDifference(t *testing.T) {
|
||||
//refresh the DM
|
||||
if err := dmAtr.DataDB().Flush(""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
Cache.Clear(nil)
|
||||
if test, err := dmAtr.DataDB().IsDBEmpty(); err != nil {
|
||||
t.Error(err)
|
||||
} else if test != true {
|
||||
t.Errorf("\nExpecting: true got :%+v", test)
|
||||
}
|
||||
attrPrf1 := &AttributeProfile{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "ATTR_1",
|
||||
Contexts: []string{utils.MetaSessionS},
|
||||
FilterIDs: []string{"*string:~Field1:Value1"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
Attributes: []*Attribute{
|
||||
{
|
||||
FieldName: "Field2",
|
||||
Type: utils.META_USAGE_DIFFERENCE,
|
||||
Substitute: config.NewRSRParsersMustCompile("~UnixTimeStamp;~UnixTimeStamp2", true, utils.INFIELD_SEP),
|
||||
},
|
||||
},
|
||||
Blocker: true,
|
||||
Weight: 10,
|
||||
}
|
||||
// Add attribute in DM
|
||||
if err := dmAtr.SetAttributeProfile(attrPrf1, true); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
attrArgs := &AttrArgsProcessEvent{
|
||||
Context: utils.StringPointer(utils.MetaSessionS),
|
||||
ProcessRuns: utils.IntPointer(1),
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
"TheField": "TheVal",
|
||||
"UnixTimeStamp": "1554364297",
|
||||
"UnixTimeStamp2": "1554364287",
|
||||
},
|
||||
},
|
||||
}
|
||||
eRply := &AttrSProcessEventReply{
|
||||
MatchedProfiles: []string{"ATTR_1"},
|
||||
AlteredFields: []string{"Field2"},
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
"TheField": "TheVal",
|
||||
"UnixTimeStamp": "1554364297",
|
||||
"UnixTimeStamp2": "1554364287",
|
||||
"Field2": "10s",
|
||||
},
|
||||
},
|
||||
}
|
||||
var reply AttrSProcessEventReply
|
||||
if err := attrService.V1ProcessEvent(attrArgs, &reply); err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.MatchedProfiles, reply.MatchedProfiles) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.MatchedProfiles, reply.MatchedProfiles)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.AlteredFields, reply.AlteredFields) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.AlteredFields, reply.AlteredFields)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.CGREvent.Event, reply.CGREvent.Event) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.CGREvent.Event, reply.CGREvent.Event)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttributeProcessEventValueExponent(t *testing.T) {
|
||||
//refresh the DM
|
||||
if err := dmAtr.DataDB().Flush(""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
Cache.Clear(nil)
|
||||
if test, err := dmAtr.DataDB().IsDBEmpty(); err != nil {
|
||||
t.Error(err)
|
||||
} else if test != true {
|
||||
t.Errorf("\nExpecting: true got :%+v", test)
|
||||
}
|
||||
attrPrf1 := &AttributeProfile{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "ATTR_1",
|
||||
Contexts: []string{utils.MetaSessionS},
|
||||
FilterIDs: []string{"*string:~Field1:Value1"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
Attributes: []*Attribute{
|
||||
{
|
||||
FieldName: "Field2",
|
||||
Type: utils.MetaValueExponent,
|
||||
Substitute: config.NewRSRParsersMustCompile("~Multiplier;~Pow", true, utils.INFIELD_SEP),
|
||||
},
|
||||
},
|
||||
Blocker: true,
|
||||
Weight: 10,
|
||||
}
|
||||
// Add attribute in DM
|
||||
if err := dmAtr.SetAttributeProfile(attrPrf1, true); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
attrArgs := &AttrArgsProcessEvent{
|
||||
Context: utils.StringPointer(utils.MetaSessionS),
|
||||
ProcessRuns: utils.IntPointer(1),
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
"TheField": "TheVal",
|
||||
"Multiplier": "2",
|
||||
"Pow": "3",
|
||||
},
|
||||
},
|
||||
}
|
||||
eRply := &AttrSProcessEventReply{
|
||||
MatchedProfiles: []string{"ATTR_1"},
|
||||
AlteredFields: []string{"Field2"},
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
"TheField": "TheVal",
|
||||
"Multiplier": "2",
|
||||
"Pow": "3",
|
||||
"Field2": "2000",
|
||||
},
|
||||
},
|
||||
}
|
||||
var reply AttrSProcessEventReply
|
||||
if err := attrService.V1ProcessEvent(attrArgs, &reply); err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.MatchedProfiles, reply.MatchedProfiles) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.MatchedProfiles, reply.MatchedProfiles)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.AlteredFields, reply.AlteredFields) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.AlteredFields, reply.AlteredFields)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.CGREvent.Event, reply.CGREvent.Event) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.CGREvent.Event, reply.CGREvent.Event)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAttributeProcessEventConstant(b *testing.B) {
|
||||
data, _ := NewMapStorage()
|
||||
dmAtr = NewDataManager(data)
|
||||
defaultCfg, err := config.NewDefaultCGRConfig()
|
||||
if err != nil {
|
||||
b.Errorf("Error: %+v", err)
|
||||
}
|
||||
attrService, err = NewAttributeService(dmAtr, &FilterS{dm: dmAtr, cfg: defaultCfg}, nil, nil, 1)
|
||||
if err != nil {
|
||||
b.Errorf("Error: %+v", err)
|
||||
}
|
||||
//refresh the DM
|
||||
if err := dmAtr.DataDB().Flush(""); err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
Cache.Clear(nil)
|
||||
if test, err := dmAtr.DataDB().IsDBEmpty(); err != nil {
|
||||
b.Error(err)
|
||||
} else if test != true {
|
||||
b.Errorf("\nExpecting: true got :%+v", test)
|
||||
}
|
||||
attrPrf1 := &AttributeProfile{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "ATTR_1",
|
||||
Contexts: []string{utils.MetaSessionS},
|
||||
FilterIDs: []string{"*string:~Field1:Value1"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
Attributes: []*Attribute{
|
||||
{
|
||||
FieldName: "Field2",
|
||||
Type: utils.META_CONSTANT,
|
||||
Substitute: config.NewRSRParsersMustCompile("ConstVal", true, utils.INFIELD_SEP),
|
||||
},
|
||||
},
|
||||
Blocker: true,
|
||||
Weight: 10,
|
||||
}
|
||||
// Add attribute in DM
|
||||
if err := dmAtr.SetAttributeProfile(attrPrf1, true); err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
attrArgs := &AttrArgsProcessEvent{
|
||||
Context: utils.StringPointer(utils.MetaSessionS),
|
||||
ProcessRuns: utils.IntPointer(1),
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
},
|
||||
},
|
||||
}
|
||||
var reply AttrSProcessEventReply
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := attrService.V1ProcessEvent(attrArgs, &reply); err != nil {
|
||||
b.Errorf("Error: %+v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAttributeProcessEventVariable(b *testing.B) {
|
||||
data, _ := NewMapStorage()
|
||||
dmAtr = NewDataManager(data)
|
||||
defaultCfg, err := config.NewDefaultCGRConfig()
|
||||
if err != nil {
|
||||
b.Errorf("Error: %+v", err)
|
||||
}
|
||||
attrService, err = NewAttributeService(dmAtr, &FilterS{dm: dmAtr, cfg: defaultCfg}, nil, nil, 1)
|
||||
if err != nil {
|
||||
b.Errorf("Error: %+v", err)
|
||||
}
|
||||
//refresh the DM
|
||||
if err := dmAtr.DataDB().Flush(""); err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
Cache.Clear(nil)
|
||||
if test, err := dmAtr.DataDB().IsDBEmpty(); err != nil {
|
||||
b.Error(err)
|
||||
} else if test != true {
|
||||
b.Errorf("\nExpecting: true got :%+v", test)
|
||||
}
|
||||
attrPrf1 := &AttributeProfile{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "ATTR_1",
|
||||
Contexts: []string{utils.MetaSessionS},
|
||||
FilterIDs: []string{"*string:~Field1:Value1"},
|
||||
ActivationInterval: &utils.ActivationInterval{
|
||||
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
|
||||
},
|
||||
Attributes: []*Attribute{
|
||||
{
|
||||
FieldName: "Field2",
|
||||
Type: utils.MetaVariable,
|
||||
Substitute: config.NewRSRParsersMustCompile("~Field1", true, utils.INFIELD_SEP),
|
||||
},
|
||||
},
|
||||
Blocker: true,
|
||||
Weight: 10,
|
||||
}
|
||||
// Add attribute in DM
|
||||
if err := dmAtr.SetAttributeProfile(attrPrf1, true); err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
attrArgs := &AttrArgsProcessEvent{
|
||||
Context: utils.StringPointer(utils.MetaSessionS),
|
||||
ProcessRuns: utils.IntPointer(1),
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"Field1": "Value1",
|
||||
},
|
||||
},
|
||||
}
|
||||
var reply AttrSProcessEventReply
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := attrService.V1ProcessEvent(attrArgs, &reply); err != nil {
|
||||
b.Errorf("Error: %+v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user