mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Added all tests for mapevent.go.
This commit is contained in:
committed by
Dan Christian Bogos
parent
8f3a96e3e4
commit
0345819718
@@ -102,25 +102,25 @@ type CDR struct {
|
||||
|
||||
// AddDefaults will add missing information based on other fields
|
||||
func (cdr *CDR) AddDefaults(cfg *config.CGRConfig) {
|
||||
if cdr.CGRID == "" {
|
||||
if cdr.CGRID == utils.EmptyString {
|
||||
cdr.ComputeCGRID()
|
||||
}
|
||||
if cdr.RunID == "" {
|
||||
if cdr.RunID == utils.EmptyString {
|
||||
cdr.RunID = utils.MetaRaw
|
||||
}
|
||||
if cdr.ToR == "" {
|
||||
if cdr.ToR == utils.EmptyString {
|
||||
cdr.ToR = utils.VOICE
|
||||
}
|
||||
if cdr.RequestType == "" {
|
||||
if cdr.RequestType == utils.EmptyString {
|
||||
cdr.RequestType = cfg.DefaultReqType
|
||||
}
|
||||
if cdr.Tenant == "" {
|
||||
if cdr.Tenant == utils.EmptyString {
|
||||
cdr.Tenant = cfg.DefaultTenant
|
||||
}
|
||||
if cdr.Category == "" {
|
||||
if cdr.Category == utils.EmptyString {
|
||||
cdr.Category = cfg.DefaultCategory
|
||||
}
|
||||
if cdr.Subject == "" {
|
||||
if cdr.Subject == utils.EmptyString {
|
||||
cdr.Subject = cdr.Account
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ func (me MapEvent) AsMapString(ignoredFlds utils.StringMap) (mp map[string]strin
|
||||
if out, err = utils.IfaceAsString(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
me[k] = out
|
||||
mp[k] = out
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -122,7 +122,7 @@ func (me MapEvent) AsMapStringIgnoreErrors(ignoredFlds utils.StringMap) (mp map[
|
||||
continue
|
||||
}
|
||||
if out, can := utils.CastFieldIfToString(v); can {
|
||||
me[k] = out
|
||||
mp[k] = out
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -132,7 +132,9 @@ func (me MapEvent) AsMapStringIgnoreErrors(ignoredFlds utils.StringMap) (mp map[
|
||||
func (me MapEvent) AsCDR(cfg *config.CGRConfig, tmz string) (cdr *CDR, err error) {
|
||||
cdr = &CDR{Cost: -1.0, ExtraFields: make(map[string]string)}
|
||||
for k, v := range me {
|
||||
if !utils.IsSliceMember(utils.PrimaryCdrFields, k) { // not primary field, populate extra ones
|
||||
if !utils.IsSliceMember(
|
||||
append(utils.PrimaryCdrFields, utils.PreRated, utils.CostSource),
|
||||
k) { // not primary field, populate extra ones
|
||||
if cdr.ExtraFields[k], err = utils.IfaceAsString(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -21,8 +21,9 @@ package engine
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
//"time"
|
||||
// "github.com/cgrates/cgrates/config"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
@@ -32,12 +33,14 @@ var mapEv = MapEvent(map[string]interface{}{
|
||||
"test3": 42.3,
|
||||
"test4": true,
|
||||
"test5": "test",
|
||||
"test6": time.Duration(10 * time.Second),
|
||||
"test7": "42s",
|
||||
"test8": time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC),
|
||||
"test9": "2009-11-10T23:00:00Z",
|
||||
})
|
||||
|
||||
func TestMapEventNewMapEvent(t *testing.T) {
|
||||
expected := MapEvent(make(map[string]interface{}))
|
||||
rply := NewMapEvent(nil)
|
||||
if !reflect.DeepEqual(expected, rply) {
|
||||
if rply, expected := NewMapEvent(nil), MapEvent(make(map[string]interface{})); !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
mp := map[string]interface{}{
|
||||
@@ -47,70 +50,331 @@ func TestMapEventNewMapEvent(t *testing.T) {
|
||||
"test4": true,
|
||||
"test5": "test",
|
||||
}
|
||||
expected = MapEvent(mp)
|
||||
rply = NewMapEvent(mp)
|
||||
if !reflect.DeepEqual(expected, rply) {
|
||||
if rply, expected := NewMapEvent(mp), MapEvent(mp); !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEventString(t *testing.T) {
|
||||
me := NewMapEvent(nil)
|
||||
expected := utils.ToJSON(me)
|
||||
rply := me.String()
|
||||
if !reflect.DeepEqual(expected, rply) {
|
||||
if rply, expected := me.String(), utils.ToJSON(me); !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
expected = utils.ToJSON(mapEv)
|
||||
rply = mapEv.String()
|
||||
if !reflect.DeepEqual(expected, rply) {
|
||||
if rply, expected := mapEv.String(), utils.ToJSON(mapEv); !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEventHasField(t *testing.T) {
|
||||
me := NewMapEvent(nil)
|
||||
expected := false
|
||||
rply := me.HasField("test1")
|
||||
if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
if rply := me.HasField("test1"); rply {
|
||||
t.Errorf("Expecting false, received: %+v", rply)
|
||||
}
|
||||
expected = true
|
||||
rply = mapEv.HasField("test2")
|
||||
if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
if rply := mapEv.HasField("test2"); !rply {
|
||||
t.Errorf("Expecting true, received: %+v", rply)
|
||||
}
|
||||
expected = false
|
||||
rply = mapEv.HasField("test7")
|
||||
if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
if rply := mapEv.HasField("test"); rply {
|
||||
t.Errorf("Expecting false, received: %+v", rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEventGetString(t *testing.T) {
|
||||
if _, err := mapEv.GetString("test"); err != utils.ErrNotFound {
|
||||
if rply, err := mapEv.GetString("test"); err != utils.ErrNotFound {
|
||||
t.Errorf("Expected: %+v, received: %+v", utils.ErrNotFound, err)
|
||||
} else if rply != utils.EmptyString {
|
||||
t.Errorf("Expected error: %+v , received string: %+v", utils.ErrNotFound, rply)
|
||||
}
|
||||
strI, err := mapEv.GetString("test2")
|
||||
if strI != "42" || err != nil {
|
||||
t.Errorf("Expecting %+v, received: %+v", "42", strI)
|
||||
if rply, err := mapEv.GetString("test2"); err != nil {
|
||||
t.Error(err)
|
||||
} else if rply != "42" {
|
||||
t.Errorf("Expecting %+v, received: %+v", "42", rply)
|
||||
}
|
||||
strI, err = mapEv.GetString("test1")
|
||||
if strI != "null" || err != nil {
|
||||
t.Errorf("Expecting %+v, received: %+v", "null", strI)
|
||||
if rply, err := mapEv.GetString("test1"); err != nil {
|
||||
t.Error(err)
|
||||
} else if rply != utils.EmptyString {
|
||||
t.Errorf("Expecting , received: %+v", rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEventGetStringIgnoreErrors(t *testing.T) {
|
||||
if s := mapEv.GetStringIgnoreErrors("test"); s != "" {
|
||||
t.Errorf("Expected: %+v, received: %+v", "", s)
|
||||
if rply := mapEv.GetStringIgnoreErrors("test"); rply != utils.EmptyString {
|
||||
t.Errorf("Expected: , received: %+v", rply)
|
||||
}
|
||||
strI := mapEv.GetStringIgnoreErrors("test2")
|
||||
if strI != "42" {
|
||||
t.Errorf("Expecting %+v, received: %+v", "42", strI)
|
||||
if rply := mapEv.GetStringIgnoreErrors("test2"); rply != "42" {
|
||||
t.Errorf("Expecting 42, received: %+v", rply)
|
||||
}
|
||||
strI = mapEv.GetStringIgnoreErrors("test1")
|
||||
if strI != "null" {
|
||||
t.Errorf("Expecting %+v, received: %+v", "null", strI)
|
||||
if rply := mapEv.GetStringIgnoreErrors("test1"); rply != utils.EmptyString {
|
||||
t.Errorf("Expecting , received: %+v", rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEventGetDuration(t *testing.T) {
|
||||
if rply, err := mapEv.GetDuration("test"); err != utils.ErrNotFound {
|
||||
t.Errorf("Expected: %+v, received: %+v", utils.ErrNotFound, err)
|
||||
} else if rply != time.Duration(0) {
|
||||
t.Errorf("Expected: %+v , received duration: %+v", time.Duration(0), rply)
|
||||
}
|
||||
expected := time.Duration(10 * time.Second)
|
||||
if rply, err := mapEv.GetDuration("test6"); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
expected = time.Duration(42 * time.Second)
|
||||
if rply, err := mapEv.GetDuration("test7"); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
expected = time.Duration(42)
|
||||
if rply, err := mapEv.GetDuration("test2"); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEventGetDurationIgnoreErrors(t *testing.T) {
|
||||
if rply := mapEv.GetDurationIgnoreErrors("test"); rply != time.Duration(0) {
|
||||
t.Errorf("Expected: %+v, received: %+v", time.Duration(0), rply)
|
||||
}
|
||||
expected := time.Duration(10 * time.Second)
|
||||
if rply := mapEv.GetDurationIgnoreErrors("test6"); rply != expected {
|
||||
t.Errorf("Expected: %+v, received: %+v", expected, rply)
|
||||
}
|
||||
expected = time.Duration(42 * time.Second)
|
||||
if rply := mapEv.GetDurationIgnoreErrors("test7"); rply != expected {
|
||||
t.Errorf("Expected: %+v, received: %+v", expected, rply)
|
||||
}
|
||||
expected = time.Duration(42)
|
||||
if rply := mapEv.GetDurationIgnoreErrors("test2"); rply != expected {
|
||||
t.Errorf("Expected: %+v, received: %+v", expected, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEventGetTime(t *testing.T) {
|
||||
if rply, err := mapEv.GetTime("test", utils.EmptyString); err != utils.ErrNotFound {
|
||||
t.Errorf("Expected: %+v, received: %+v", utils.ErrNotFound, err)
|
||||
} else if !rply.IsZero() {
|
||||
t.Errorf("Expected: January 1, year 1, 00:00:00.000000000 UTC, received: %+v", rply)
|
||||
}
|
||||
expected := time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)
|
||||
if rply, err := mapEv.GetTime("test8", utils.EmptyString); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
if rply, err := mapEv.GetTime("test9", utils.EmptyString); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEventGetTimeIgnoreErrors(t *testing.T) {
|
||||
if rply := mapEv.GetTimeIgnoreErrors("test", utils.EmptyString); !rply.IsZero() {
|
||||
t.Errorf("Expected: January 1, year 1, 00:00:00.000000000 UTC, received: %+v", rply)
|
||||
}
|
||||
expected := time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)
|
||||
if rply := mapEv.GetTimeIgnoreErrors("test8", utils.EmptyString); rply != expected {
|
||||
t.Errorf("Expected: %+v, received: %+v", expected, rply)
|
||||
}
|
||||
if rply := mapEv.GetTimeIgnoreErrors("test9", utils.EmptyString); rply != expected {
|
||||
t.Errorf("Expected: %+v, received: %+v", expected, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEventClone(t *testing.T) {
|
||||
rply := mapEv.Clone()
|
||||
if !reflect.DeepEqual(mapEv, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", mapEv, rply)
|
||||
}
|
||||
rply["test1"] = "testTest"
|
||||
if reflect.DeepEqual(mapEv, rply) {
|
||||
t.Errorf("Expecting different from: %+v, received: %+v", mapEv, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEventAsMapString(t *testing.T) {
|
||||
expected := map[string]string{
|
||||
"test1": utils.EmptyString,
|
||||
"test2": "42",
|
||||
"test3": "42.3",
|
||||
"test4": "true",
|
||||
"test5": "test",
|
||||
}
|
||||
mpIgnore := utils.StringMap{
|
||||
"test6": true,
|
||||
"test7": false,
|
||||
"test8": true,
|
||||
"test9": false,
|
||||
}
|
||||
if rply, err := mapEv.AsMapString(mpIgnore); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
var mp MapEvent
|
||||
mp = nil
|
||||
if rply, err := mp.AsMapString(nil); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(map[string]string{}, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", map[string]string{}, rply)
|
||||
}
|
||||
if rply, err := mp.AsMapString(mpIgnore); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(map[string]string{}, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", map[string]string{}, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEventAsMapStringIgnoreErrors(t *testing.T) {
|
||||
expected := map[string]string{
|
||||
"test1": utils.EmptyString,
|
||||
"test2": "42",
|
||||
"test3": "42.3",
|
||||
"test4": "true",
|
||||
"test5": "test",
|
||||
}
|
||||
mpIgnore := utils.StringMap{
|
||||
"test6": true,
|
||||
"test7": true,
|
||||
"test8": true,
|
||||
"test9": true,
|
||||
}
|
||||
if rply := mapEv.AsMapStringIgnoreErrors(mpIgnore); !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
var mp MapEvent
|
||||
mp = nil
|
||||
if rply := mp.AsMapStringIgnoreErrors(nil); !reflect.DeepEqual(map[string]string{}, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", map[string]string{}, rply)
|
||||
}
|
||||
if rply := mp.AsMapStringIgnoreErrors(mpIgnore); !reflect.DeepEqual(map[string]string{}, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", map[string]string{}, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEventAsCDR(t *testing.T) {
|
||||
me := NewMapEvent(nil)
|
||||
expected := &CDR{Cost: -1.0, ExtraFields: make(map[string]string)}
|
||||
if rply, err := me.AsCDR(nil, utils.EmptyString); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
cfg, err := config.NewDefaultCGRConfig()
|
||||
if err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
expected = &CDR{
|
||||
CGRID: "da39a3ee5e6b4b0d3255bfef95601890afd80709",
|
||||
Cost: -1.0,
|
||||
RunID: utils.MetaRaw,
|
||||
ToR: utils.VOICE,
|
||||
RequestType: cfg.DefaultReqType,
|
||||
Tenant: cfg.DefaultTenant,
|
||||
Category: cfg.DefaultCategory,
|
||||
ExtraFields: make(map[string]string),
|
||||
}
|
||||
if rply, err := me.AsCDR(cfg, utils.EmptyString); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
me = MapEvent{"SetupTime": "clearly not time string"}
|
||||
if _, err := me.AsCDR(nil, utils.EmptyString); err == nil {
|
||||
t.Errorf("Expecting not null error, received: null error")
|
||||
}
|
||||
me = MapEvent{"AnswerTime": "clearly not time string"}
|
||||
if _, err := me.AsCDR(nil, utils.EmptyString); err == nil {
|
||||
t.Errorf("Expecting not null error, received: null error")
|
||||
}
|
||||
me = MapEvent{"Usage": "clearly not duration string"}
|
||||
if _, err := me.AsCDR(nil, utils.EmptyString); err == nil {
|
||||
t.Errorf("Expecting not null error, received: null error")
|
||||
}
|
||||
me = MapEvent{"Partial": "clearly not bool string"}
|
||||
if _, err := me.AsCDR(nil, utils.EmptyString); err == nil {
|
||||
t.Errorf("Expecting not null error, received: null error")
|
||||
}
|
||||
me = MapEvent{"PreRated": "clearly not bool string"}
|
||||
if _, err := me.AsCDR(nil, utils.EmptyString); err == nil {
|
||||
t.Errorf("Expecting not null error, received: null error")
|
||||
}
|
||||
me = MapEvent{"Cost": "clearly not float64 string"}
|
||||
if _, err := me.AsCDR(nil, utils.EmptyString); err == nil {
|
||||
t.Errorf("Expecting not null error, received: null error")
|
||||
}
|
||||
me = MapEvent{"ExtraField1": 5, "ExtraField2": "extra"}
|
||||
expected = &CDR{
|
||||
Cost: -1.0,
|
||||
ExtraFields: map[string]string{
|
||||
"ExtraField1": "5",
|
||||
"ExtraField2": "extra",
|
||||
}}
|
||||
if rply, err := me.AsCDR(nil, utils.EmptyString); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
me = MapEvent{
|
||||
"ExtraField1": 5,
|
||||
"Source": 1001,
|
||||
"CostSource": "1002",
|
||||
"ExtraField2": "extra",
|
||||
}
|
||||
expected = &CDR{
|
||||
CGRID: "da39a3ee5e6b4b0d3255bfef95601890afd80709",
|
||||
Cost: -1.0,
|
||||
Source: "1001",
|
||||
CostSource: "1002",
|
||||
ExtraFields: map[string]string{
|
||||
"ExtraField1": "5",
|
||||
"ExtraField2": "extra",
|
||||
},
|
||||
RunID: utils.MetaRaw,
|
||||
ToR: utils.VOICE,
|
||||
RequestType: cfg.DefaultReqType,
|
||||
Tenant: cfg.DefaultTenant,
|
||||
Category: cfg.DefaultCategory,
|
||||
}
|
||||
if rply, err := me.AsCDR(cfg, utils.EmptyString); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
me = MapEvent{
|
||||
"ExtraField1": 5,
|
||||
"Source": 1001,
|
||||
"CostSource": "1002",
|
||||
"ExtraField2": "extra",
|
||||
"SetupTime": "2009-11-10T23:00:00Z",
|
||||
"Usage": "42s",
|
||||
"PreRated": "True",
|
||||
"Cost": "42.3",
|
||||
}
|
||||
expected = &CDR{
|
||||
CGRID: "da39a3ee5e6b4b0d3255bfef95601890afd80709",
|
||||
Cost: 42.3,
|
||||
Source: "1001",
|
||||
CostSource: "1002",
|
||||
PreRated: true,
|
||||
Usage: time.Duration(42 * time.Second),
|
||||
SetupTime: time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC),
|
||||
ExtraFields: map[string]string{
|
||||
"ExtraField1": "5",
|
||||
"ExtraField2": "extra",
|
||||
},
|
||||
RunID: utils.MetaRaw,
|
||||
ToR: utils.VOICE,
|
||||
RequestType: cfg.DefaultReqType,
|
||||
Tenant: cfg.DefaultTenant,
|
||||
Category: cfg.DefaultCategory,
|
||||
}
|
||||
if rply, err := me.AsCDR(cfg, utils.EmptyString); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rply) {
|
||||
t.Errorf("Expecting %+v, received: %+v", expected, rply)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,6 +236,8 @@ func IfaceAsBool(itm interface{}) (b bool, err error) {
|
||||
|
||||
func IfaceAsString(fld interface{}) (out string, err error) {
|
||||
switch fld.(type) {
|
||||
case nil:
|
||||
return
|
||||
case int:
|
||||
return strconv.Itoa(fld.(int)), nil
|
||||
case int64:
|
||||
|
||||
Reference in New Issue
Block a user