mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Added unit tests in utils/engine
This commit is contained in:
committed by
Dan Christian Bogos
parent
5b045d8196
commit
fd1c0c2223
@@ -874,7 +874,7 @@ func (ec *EventCost) FieldAsInterface(fldPath []string) (val interface{}, err er
|
||||
return
|
||||
}
|
||||
|
||||
// FieldAsInterface the implementation of FieldAsInterface
|
||||
// fieldAsInterface the implementation of FieldAsInterface
|
||||
func (ec *EventCost) fieldAsInterface(fldPath []string) (val interface{}, err error) {
|
||||
switch fldPath[0] {
|
||||
default: // "Charges[1]"
|
||||
|
||||
@@ -2548,3 +2548,195 @@ func TestECAsDataProvider(t *testing.T) {
|
||||
t.Errorf("Expecting: <%s> \nreceived: <%s>", "*voice", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitCache(t *testing.T) {
|
||||
eventCost := &EventCost{}
|
||||
eventCost.initCache()
|
||||
eOut := config.NewNavigableMap(nil)
|
||||
if !reflect.DeepEqual(eOut, eventCost.cache) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(eventCost.cache))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventCostFieldAsInterface(t *testing.T) {
|
||||
eventCost := &EventCost{}
|
||||
eventCost.initCache()
|
||||
// empty check
|
||||
if rcv, err := eventCost.FieldAsInterface([]string{}); err != utils.ErrNotFound {
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ErrNotFound, err)
|
||||
} else if rcv != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", rcv)
|
||||
}
|
||||
// item found in cache
|
||||
eventCost.cache = config.NewNavigableMap(map[string]interface{}{"test": nil})
|
||||
if rcv, err := eventCost.FieldAsInterface([]string{"test"}); err == nil || err != utils.ErrNotFound {
|
||||
t.Errorf("Expecting: nil, received: %+v", err)
|
||||
} else if rcv != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", rcv)
|
||||
}
|
||||
// data found in cache
|
||||
eventCost.cache = config.NewNavigableMap(map[string]interface{}{"test": "test"})
|
||||
if rcv, err := eventCost.FieldAsInterface([]string{"test"}); err != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", err)
|
||||
} else if rcv != "test" {
|
||||
t.Errorf("Expecting: nil, received: %+v", rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventCostfieldAsInterface(t *testing.T) {
|
||||
eventCost := &EventCost{}
|
||||
// default case
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.EmptyString}); err == nil || err.Error() != "unsupported field prefix: <>" {
|
||||
t.Error(err)
|
||||
} else if rcv != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", rcv)
|
||||
}
|
||||
// case utils.Charges:
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.Charges, utils.Charges}); err == nil || err != utils.ErrNotFound {
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ErrNotFound, err)
|
||||
} else if rcv != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", rcv)
|
||||
}
|
||||
eventCost = &EventCost{
|
||||
Charges: []*ChargingInterval{
|
||||
&ChargingInterval{
|
||||
RatingID: "RatingID1",
|
||||
},
|
||||
&ChargingInterval{
|
||||
RatingID: "RatingID2",
|
||||
},
|
||||
},
|
||||
}
|
||||
expectedCharges := []*ChargingInterval{
|
||||
&ChargingInterval{
|
||||
RatingID: "RatingID1",
|
||||
},
|
||||
&ChargingInterval{
|
||||
RatingID: "RatingID2",
|
||||
},
|
||||
}
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.Charges}); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expectedCharges, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", expectedCharges, rcv)
|
||||
}
|
||||
// case utils.CGRID:
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.CGRID, utils.CGRID}); err == nil || err != utils.ErrNotFound {
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ErrNotFound, err)
|
||||
} else if rcv != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", rcv)
|
||||
}
|
||||
eventCost = &EventCost{
|
||||
CGRID: "CGRID",
|
||||
}
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.CGRID}); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual("CGRID", rcv) {
|
||||
t.Errorf("Expecting: \"CGRID\", received: %+v", rcv)
|
||||
}
|
||||
// case utils.RunID:
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.RunID, utils.RunID}); err == nil || err != utils.ErrNotFound {
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ErrNotFound, err)
|
||||
} else if rcv != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", rcv)
|
||||
}
|
||||
eventCost = &EventCost{
|
||||
RunID: "RunID",
|
||||
}
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.RunID}); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual("RunID", rcv) {
|
||||
t.Errorf("Expecting: \"RunID\", received: %+v", rcv)
|
||||
}
|
||||
// case utils.StartTime:
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.StartTime, utils.StartTime}); err == nil || err != utils.ErrNotFound {
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ErrNotFound, err)
|
||||
} else if rcv != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", rcv)
|
||||
}
|
||||
eventCost = &EventCost{
|
||||
StartTime: time.Date(2020, time.April, 18, 23, 0, 4, 0, time.UTC),
|
||||
}
|
||||
expectedStartTime := time.Date(2020, time.April, 18, 23, 0, 4, 0, time.UTC)
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.StartTime}); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expectedStartTime, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", expectedStartTime, rcv)
|
||||
}
|
||||
// case utils.Usage:
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.Usage, utils.Usage}); err == nil || err != utils.ErrNotFound {
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ErrNotFound, err)
|
||||
} else if rcv != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", rcv)
|
||||
}
|
||||
eventCost = &EventCost{
|
||||
Usage: utils.DurationPointer(time.Duration(5 * time.Minute)),
|
||||
}
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.Usage}); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(utils.DurationPointer(5*time.Minute), rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.DurationPointer(5*time.Minute), rcv)
|
||||
}
|
||||
// case utils.Cost:
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.Cost, utils.Cost}); err == nil || err != utils.ErrNotFound {
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ErrNotFound, err)
|
||||
} else if rcv != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", rcv)
|
||||
}
|
||||
eventCost = &EventCost{
|
||||
Cost: utils.Float64Pointer(0.7),
|
||||
}
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.Cost}); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(utils.Float64Pointer(0.7), rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.Float64Pointer(0.7), rcv)
|
||||
}
|
||||
// case utils.AccountSummary:
|
||||
eventCost = &EventCost{
|
||||
AccountSummary: &AccountSummary{
|
||||
ID: "IDtest",
|
||||
},
|
||||
}
|
||||
expectedAccountSummary := &AccountSummary{ID: "IDtest"}
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.AccountSummary}); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expectedAccountSummary, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", expectedAccountSummary, rcv)
|
||||
}
|
||||
eventCost = &EventCost{
|
||||
AccountSummary: &AccountSummary{
|
||||
ID: "IDtest1",
|
||||
Tenant: "Tenant",
|
||||
},
|
||||
CGRID: "test",
|
||||
}
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.AccountSummary, utils.Tenant}); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual("Tenant", rcv) {
|
||||
t.Errorf("Expecting: Tenant, received: %+v", utils.ToJSON(rcv))
|
||||
}
|
||||
// case utils.Timings:
|
||||
eventCost = &EventCost{
|
||||
Timings: ChargedTimings{
|
||||
"test1": &ChargedTiming{
|
||||
StartTime: "StartTime",
|
||||
},
|
||||
},
|
||||
}
|
||||
eTimings := ChargedTimings{"test1": &ChargedTiming{StartTime: "StartTime"}}
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.Timings}); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eTimings, rcv) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eTimings, rcv)
|
||||
}
|
||||
eChargedTiming := &ChargedTiming{
|
||||
StartTime: "StartTime",
|
||||
}
|
||||
if rcv, err := eventCost.fieldAsInterface([]string{utils.Timings, "test1"}); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(eChargedTiming, rcv) {
|
||||
fmt.Printf("%T", rcv)
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eChargedTiming), utils.ToJSON(rcv))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -52,13 +52,13 @@ func TestAddFailedPost(t *testing.T) {
|
||||
t.Error("Error when casting")
|
||||
}
|
||||
eOut := &ExportEvents{
|
||||
Path: "path1",
|
||||
Path: "path1",
|
||||
Format: "format1",
|
||||
module: "module1",
|
||||
Events: []interface{}{"1"},
|
||||
}
|
||||
if !reflect.DeepEqual(eOut, failedPost) {
|
||||
t.Errorf("Expecting: %+v, received: %+v",utils.ToJSON(eOut),utils.ToJSON(failedPost))
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(failedPost))
|
||||
}
|
||||
addFailedPost("path1", "format1", "module1", "2")
|
||||
addFailedPost("path2", "format2", "module2", "3")
|
||||
@@ -74,13 +74,13 @@ func TestAddFailedPost(t *testing.T) {
|
||||
t.Error("Error when casting")
|
||||
}
|
||||
eOut = &ExportEvents{
|
||||
Path: "path1",
|
||||
Path: "path1",
|
||||
Format: "format1",
|
||||
module: "module1",
|
||||
Events: []interface{}{"1","2"},
|
||||
Events: []interface{}{"1", "2"},
|
||||
}
|
||||
if !reflect.DeepEqual(eOut, failedPost) {
|
||||
t.Errorf("Expecting: %+v, received: %+v",utils.ToJSON(eOut),utils.ToJSON(failedPost))
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(failedPost))
|
||||
}
|
||||
x, ok = failedPostCache.Get(utils.ConcatenatedKey("path2", "format2", "module2"))
|
||||
if !ok {
|
||||
@@ -94,13 +94,13 @@ func TestAddFailedPost(t *testing.T) {
|
||||
t.Error("Error when casting")
|
||||
}
|
||||
eOut = &ExportEvents{
|
||||
Path: "path2",
|
||||
Path: "path2",
|
||||
Format: "format2",
|
||||
module: "module2",
|
||||
Events: []interface{}{"3"},
|
||||
}
|
||||
if !reflect.DeepEqual(eOut, failedPost) {
|
||||
t.Errorf("Expecting: %+v, received: %+v",utils.ToJSON(eOut),utils.ToJSON(failedPost))
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(failedPost))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1307,4 +1307,45 @@ func TestRandomInteger(t *testing.T) {
|
||||
if a == 100 || b == 100 || c == 100 {
|
||||
t.Errorf("one of the numbers equals the max limit")
|
||||
}
|
||||
if a == 0 || b == 0 || c == 0 {
|
||||
t.Errorf("one of the numbers equals the min limit")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPathIndex(t *testing.T) {
|
||||
if rcv, _ := GetPathIndex(EmptyString); rcv != EmptyString {
|
||||
t.Errorf("Expecting: \"\"(EmptyString), received: \"%+v\"", rcv)
|
||||
}
|
||||
eOut := "test"
|
||||
if rcv, _ := GetPathIndex("test"); rcv != eOut {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
}
|
||||
if rcv, index := GetPathIndex("test[10]"); rcv != eOut {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
} else if *index != 10 {
|
||||
t.Errorf("Expecting: %+v, received: %+v", 10, *index)
|
||||
}
|
||||
if rcv, index := GetPathIndex("test[0]"); rcv != eOut {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
} else if *index != 0 {
|
||||
t.Errorf("Expecting: %+v, received: %+v", 0, *index)
|
||||
}
|
||||
eOut = "test[notanumber]"
|
||||
if rcv, index := GetPathIndex("test[notanumber]"); rcv != eOut {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
} else if index != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", *index)
|
||||
}
|
||||
eOut = "test[]"
|
||||
if rcv, index := GetPathIndex("test[]"); rcv != eOut {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
} else if index != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", *index)
|
||||
}
|
||||
eOut = "[]"
|
||||
if rcv, index := GetPathIndex("[]"); rcv != eOut {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, rcv)
|
||||
} else if index != nil {
|
||||
t.Errorf("Expecting: nil, received: %+v", *index)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,43 +42,43 @@ func TestNewStringSet(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdd(t *testing.T){
|
||||
func TestAdd(t *testing.T) {
|
||||
s := &StringSet{data: map[string]struct{}{}}
|
||||
eOut := &StringSet{data: map[string]struct{}{
|
||||
"test" : struct{}{},
|
||||
"test": struct{}{},
|
||||
}}
|
||||
if reflect.DeepEqual(eOut,s){
|
||||
t.Errorf("Expecting: %+v, received: %+v",eOut,s)
|
||||
if reflect.DeepEqual(eOut, s) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, s)
|
||||
}
|
||||
s.Add("test")
|
||||
if !reflect.DeepEqual(eOut,s){
|
||||
t.Errorf("Expecting: %+v, received: %+v",eOut,s)
|
||||
if !reflect.DeepEqual(eOut, s) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemove(t *testing.T){
|
||||
func TestRemove(t *testing.T) {
|
||||
eOut := &StringSet{data: map[string]struct{}{}}
|
||||
s := &StringSet{data: map[string]struct{}{
|
||||
"test" : struct{}{},
|
||||
"test": struct{}{},
|
||||
}}
|
||||
if reflect.DeepEqual(eOut,s){
|
||||
t.Errorf("Expecting: %+v, received: %+v",eOut,s)
|
||||
if reflect.DeepEqual(eOut, s) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, s)
|
||||
}
|
||||
s.Remove("test")
|
||||
if !reflect.DeepEqual(eOut,s){
|
||||
t.Errorf("Expecting: %+v, received: %+v",eOut,s)
|
||||
if !reflect.DeepEqual(eOut, s) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHas(t *testing.T){
|
||||
func TestHas(t *testing.T) {
|
||||
s := &StringSet{}
|
||||
if s.Has("test"){
|
||||
if s.Has("test") {
|
||||
t.Error("Expecting: false, received: true")
|
||||
}
|
||||
s = &StringSet{data: map[string]struct{}{
|
||||
"test" : struct{}{},
|
||||
"test": struct{}{},
|
||||
}}
|
||||
if !s.Has("test"){
|
||||
if !s.Has("test") {
|
||||
t.Error("Expecting: true, received: false")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user