coverage tests at engine

This commit is contained in:
gezimbll
2022-11-01 11:19:07 -04:00
committed by Dan Christian Bogos
parent 8848bb6898
commit d795283c20
3 changed files with 430 additions and 2 deletions

View File

@@ -20,6 +20,7 @@ package engine
import (
"encoding/json"
"reflect"
"sort"
"testing"
"time"
@@ -2600,7 +2601,6 @@ func TestAccountFieldAsInterface(t *testing.T) {
} else if _, err = acc.FieldAsInterface([]string{"ActionTriggers[0]", "test"}); err == nil {
t.Error(err)
}
if _, err := acc.FieldAsInterface([]string{"ID"}); err != nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{"ID", "test"}); err == nil {
@@ -2610,5 +2610,123 @@ func TestAccountFieldAsInterface(t *testing.T) {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{"BalanceMap", "first[0]"}); err != nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{"BalanceMap", "first[3]"}); err == nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{"BalanceMap", "first[first]"}); err == nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{utils.UnitCounters}); err != nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{utils.UnitCounters, "first"}); err != nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{utils.UnitCounters, "first", "sec"}); err == nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{utils.UnitCounters, "first", "2"}); err == nil {
t.Error(err)
} else if _, err := acc.FieldAsInterface([]string{utils.UnitCounters, "first", "0"}); err == nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{utils.UnitCounters, "sec"}); err == nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{utils.ActionTriggers}); err != nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{utils.ActionTriggers, "val"}); err == nil {
t.Error(err)
} else if val, err := acc.FieldAsInterface([]string{utils.ActionTriggers, "id"}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(val, acc.ActionTriggers[0]) {
t.Errorf("expected %v ,received %v", utils.ToJSON(val), utils.ToJSON(acc.ActionTriggers[0]))
} else if _, err = acc.FieldAsInterface([]string{utils.AllowNegative}); err != nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{utils.AllowNegative, "test"}); err == nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{utils.Disabled}); err != nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{utils.Disabled, "test"}); err == nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{utils.UpdateTime}); err != nil {
t.Error(err)
} else if _, err = acc.FieldAsInterface([]string{utils.UpdateTime, "test"}); err == nil {
t.Error(err)
}
}
func TestAccountFieldAsString(t *testing.T) {
acc := &Account{
ID: "id",
BalanceMap: map[string]Balances{
"first": {
&Balance{
Uuid: "uuid1",
ID: "id",
Value: 20.44,
},
&Balance{
Uuid: "uuid2",
ID: "id2",
Value: 12.2,
},
}},
UnitCounters: UnitCounters{
"first": []*UnitCounter{
{
CounterType: "balance",
Counters: CounterFilters{
{
Value: 20.44,
Filter: &BalanceFilter{
Uuid: utils.StringPointer("filterUuid"),
ID: utils.StringPointer("filterId"),
Type: utils.StringPointer("type"),
},
},
},
}}},
ActionTriggers: ActionTriggers{
{
UniqueID: "uniId",
ID: "id",
ThresholdType: "*min_event_counter",
ThresholdValue: 20.55,
},
{
UniqueID: "uniId2",
ID: "id2",
ThresholdType: "*max_event_counter",
ThresholdValue: 19.22,
Recurrent: true,
},
},
}
if _, err := acc.FieldAsString([]string{"val"}); err == nil {
t.Error(err)
} else if _, err := acc.FieldAsString([]string{"ID"}); err != nil {
t.Error(err)
}
}
func TestGetSharedGroups(t *testing.T) {
acc := &Account{
BalanceMap: map[string]Balances{
"first": {
{SharedGroups: utils.StringMap{
"firstval": true,
"firstval2": true,
}}},
"second": {
{
SharedGroups: utils.StringMap{
"secondval": true,
"secondval2": false,
}}},
},
}
exp := []string{"firstval", "firstval2", "secondval", "secondval2"}
val := acc.GetSharedGroups()
sort.Slice(val, func(i, j int) bool {
return val[i] < val[j]
})
if !reflect.DeepEqual(val, exp) {
t.Errorf("expected %v ,received %v", utils.ToJSON(val), utils.ToJSON(exp))
}
}

View File

@@ -174,3 +174,310 @@ func TestBalanceFilterClone(t *testing.T) {
t.Errorf("Expecting: 0.7, received: %+v", *bf.Weight)
}
}
func TestBalanceLoadFromBalance(t *testing.T) {
bf := &BalanceFilter{
Uuid: utils.StringPointer(""),
ID: utils.StringPointer(""),
Value: &utils.ValueFormula{
Static: 0},
ExpirationDate: utils.TimePointer(time.Date(2022, 12, 21, 20, 0, 0, 0, time.UTC)),
Weight: utils.Float64Pointer(0),
DestinationIDs: &utils.StringMap{},
RatingSubject: utils.StringPointer(""),
Categories: &utils.StringMap{},
SharedGroups: &utils.StringMap{},
Timings: []*RITiming{},
TimingIDs: &utils.StringMap{},
Factor: &ValueFactor{},
Disabled: utils.BoolPointer(false),
Blocker: utils.BoolPointer(false),
}
b := &Balance{
Uuid: "uuid",
ID: "id",
Value: 20.4,
ExpirationDate: time.Date(2022, 12, 21, 20, 0, 0, 0, time.UTC),
Weight: 533.43,
DestinationIDs: utils.StringMap{
"key": true,
},
RatingSubject: "rate",
Categories: utils.StringMap{
"category": true,
},
SharedGroups: utils.StringMap{
"sharedgroup": true,
},
Timings: []*RITiming{
{ID: "id",
Years: utils.Years([]int{2, 2}),
Months: utils.Months([]time.Month{2, 2}),
MonthDays: utils.MonthDays([]int{2, 22, 11}),
WeekDays: utils.WeekDays([]time.Weekday{0}),
},
{
ID: "id2",
Years: utils.Years([]int{1, 3, 2}),
Months: utils.Months([]time.Month{2, 5, 6}),
MonthDays: utils.MonthDays([]int{2, 22, 11, 6, 4}),
WeekDays: utils.WeekDays([]time.Weekday{0, 2}),
}},
TimingIDs: utils.StringMap{
"timing": true,
},
Factor: ValueFactor{
"valfac": 22,
},
Disabled: false,
Blocker: false,
}
eOut := &BalanceFilter{
Uuid: utils.StringPointer("uuid"),
ID: utils.StringPointer("id"),
Value: &utils.ValueFormula{
Static: 20.4,
},
ExpirationDate: utils.TimePointer(time.Date(2022, 12, 21, 20, 0, 0, 0, time.UTC)),
Weight: utils.Float64Pointer(533.43),
DestinationIDs: &utils.StringMap{
"key": true,
},
RatingSubject: utils.StringPointer("rate"),
Categories: &utils.StringMap{
"category": true,
},
SharedGroups: &utils.StringMap{
"sharedgroup": true,
},
Timings: []*RITiming{
{ID: "id",
Years: utils.Years([]int{2, 2}),
Months: utils.Months([]time.Month{2, 2}),
MonthDays: utils.MonthDays([]int{2, 22, 11}),
WeekDays: utils.WeekDays([]time.Weekday{0}),
},
{
ID: "id2",
Years: utils.Years([]int{1, 3, 2}),
Months: utils.Months([]time.Month{2, 5, 6}),
MonthDays: utils.MonthDays([]int{2, 22, 11, 6, 4}),
WeekDays: utils.WeekDays([]time.Weekday{0, 2}),
},
},
TimingIDs: &utils.StringMap{
"timing": true,
},
Factor: &ValueFactor{
"valfac": 22,
},
Disabled: utils.BoolPointer(false),
Blocker: utils.BoolPointer(false),
}
if val := bf.LoadFromBalance(b); !reflect.DeepEqual(val, eOut) {
t.Errorf("expected %v ,received %v", utils.ToJSON(eOut), utils.ToJSON(val))
}
}
func TestBalanceFilterFieldAsInterface(t *testing.T) {
bp := &BalanceFilter{}
if _, err := bp.FieldAsInterface([]string{}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"DestinationIDs[key]"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Categories[indx]"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"SharedGroups[indx]"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"TimingIDs[index]"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Timings[indx]"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Factor[indx]"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Uuid"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"ID"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Type"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"ExpirationDate"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Weight"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"RatingSubject"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Disabled"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Blocker"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"DestinationIDs"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Categories"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"SharedGroups"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Timings"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"TimingIDs"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Factor"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Value"}); err != nil {
t.Error(err)
}
bp = &BalanceFilter{
Uuid: utils.StringPointer("uuid"),
ID: utils.StringPointer("id"),
Value: &utils.ValueFormula{
Static: 20.4,
},
ExpirationDate: utils.TimePointer(time.Date(2022, 12, 21, 20, 0, 0, 0, time.UTC)),
Weight: utils.Float64Pointer(533.43),
DestinationIDs: &utils.StringMap{
"key": true,
},
RatingSubject: utils.StringPointer("rate"),
Categories: &utils.StringMap{
"category": true,
},
SharedGroups: &utils.StringMap{
"sharedgroup": true,
},
Timings: []*RITiming{
{ID: "id",
Years: utils.Years([]int{2, 2}),
Months: utils.Months([]time.Month{2, 2}),
MonthDays: utils.MonthDays([]int{2, 22, 11}),
WeekDays: utils.WeekDays([]time.Weekday{0}),
},
{
ID: "id2",
Years: utils.Years([]int{1, 3, 2}),
Months: utils.Months([]time.Month{2, 5, 6}),
MonthDays: utils.MonthDays([]int{2, 22, 11, 6, 4}),
WeekDays: utils.WeekDays([]time.Weekday{0, 2}),
},
},
TimingIDs: &utils.StringMap{
"timing": true,
},
Factor: &ValueFactor{
"valfac": 22,
},
Disabled: utils.BoolPointer(false),
Blocker: utils.BoolPointer(false),
}
if _, err := bp.FieldAsInterface([]string{"DestinationIDs[indx]"}); err == nil {
t.Error(err)
} else if _, err := bp.FieldAsInterface([]string{"Categories[indx]"}); err == nil {
t.Error(err)
} else if _, err := bp.FieldAsInterface([]string{"SharedGroups[indx]"}); err == nil {
t.Error(err)
} else if _, err := bp.FieldAsInterface([]string{"TimingIDs[indx]"}); err == nil {
t.Error(err)
} else if _, err := bp.FieldAsInterface([]string{"Factor[indx]"}); err == nil {
t.Error(err)
}
if _, err := bp.FieldAsInterface([]string{"DestinationIDs[key]"}); err != nil {
t.Error(err)
} else if _, err := bp.FieldAsInterface([]string{"Categories[category]"}); err != nil {
t.Error(err)
} else if _, err := bp.FieldAsInterface([]string{"SharedGroups[sharedgroup]"}); err != nil {
t.Error(err)
} else if _, err := bp.FieldAsInterface([]string{"TimingIDs[timing]"}); err != nil {
t.Error(err)
} else if _, err := bp.FieldAsInterface([]string{"Factor[valfac]"}); err != nil {
t.Error(err)
}
if _, err = bp.FieldAsInterface([]string{"Timings[three]"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Timings[3]"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Timings[0]"}); err != nil {
t.Error(err)
}
if _, err := bp.FieldAsInterface([]string{"Uuid"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Uuid", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"ID"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"ID", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Type"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Type", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"ExpirationDate"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"ExpirationDate", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Weight"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Weight", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Type"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Type", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"RatingSubject"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"RatingSubject", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"RatingSubject"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"RatingSubject", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Disabled"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Disabled", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"RatingSubject"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"RatingSubject", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Blocker"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Blocker", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"DestinationIDs"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"DestinationIDs", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Categories"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Categories", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"SharedGroups"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"SharedGroups", "test"}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Timings"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Timings", "id2"}); err != nil {
t.Error(err)
} else if _, err = bp.FieldAsInterface([]string{"Timings", "test"}); err == nil {
t.Error(err)
}
}
func TestBalanceFilterFieldAsString(t *testing.T) {
bp := &BalanceFilter{
Uuid: utils.StringPointer("uuid"),
ID: utils.StringPointer("id"),
}
if _, err := bp.FieldAsString([]string{}); err == nil {
t.Error(err)
} else if _, err = bp.FieldAsString([]string{"Uuid"}); err != nil {
t.Error(err)
}
}

View File

@@ -749,7 +749,10 @@ func TestCDRNewCDRFromSQL(t *testing.T) {
} else if !reflect.DeepEqual(cdr, eCDR) {
t.Errorf("Expecting: %+v, received: %+v", cdr, eCDR)
}
cdrSQL.ExtraFields = "extrafield"
if _, err := NewCDRFromSQL(cdrSQL); err == nil {
t.Error(err)
}
}
func TestCDRAsCGREvent(t *testing.T) {