mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-16 13:49:53 +05:00
Add coverage tests for utils
This commit is contained in:
committed by
Dan Christian Bogos
parent
7050622bc3
commit
16c0360ff4
@@ -1334,3 +1334,72 @@ func TestRandomInteger(t *testing.T) {
|
||||
t.Errorf("one of the numbers are below min limit")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoreutilFibDuraton(t *testing.T) {
|
||||
type args struct {
|
||||
durationUnit time.Duration
|
||||
maxDuration time.Duration
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
exp time.Duration
|
||||
}{
|
||||
{
|
||||
name: "max duration",
|
||||
args: args{1 * time.Millisecond, 1 * time.Millisecond},
|
||||
exp: 1 * time.Millisecond,
|
||||
},
|
||||
{
|
||||
name: "max duration",
|
||||
args: args{1 * time.Nanosecond, 1 * time.Millisecond},
|
||||
exp: 1 * time.Nanosecond,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rcv := FibDuration(tt.args.durationUnit, tt.args.maxDuration)
|
||||
rcv2 := rcv()
|
||||
|
||||
if rcv2 != tt.exp {
|
||||
t.Errorf("expected %v, received %v", tt.exp, rcv2)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoreUtilsClone(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
a, b any
|
||||
err string
|
||||
}{
|
||||
{
|
||||
name: "Encode error",
|
||||
a: nil,
|
||||
b: nil,
|
||||
err: "gob: cannot encode nil value",
|
||||
},
|
||||
{
|
||||
name: "Decode error",
|
||||
a: "test",
|
||||
b: &[]string{},
|
||||
err: "gob: decoding into local type *[]string, received remote type string",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := Clone(tt.a, tt.b)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != tt.err {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -452,3 +452,13 @@ func TestPathItemListMoveAfter(t *testing.T) {
|
||||
t.Error("moved after")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathItemListmove(t *testing.T) {
|
||||
l := PathItemList{}
|
||||
|
||||
rcv := l.move(nil, nil)
|
||||
|
||||
if rcv != nil {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -778,3 +778,184 @@ func TestRSRFieldNewRSRFilterdMustCompile(t *testing.T) {
|
||||
t.Error("didn't create RSRFilter", rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRSRFieldCompile(t *testing.T) {
|
||||
str := "test)"
|
||||
rsrf := &RSRField{
|
||||
Id: str,
|
||||
Rules: str,
|
||||
staticValue: str,
|
||||
RSRules: []*ReSearchReplace{},
|
||||
filters: []*RSRFilter{},
|
||||
converters: DataConverters{},
|
||||
}
|
||||
|
||||
err := rsrf.Compile()
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != "Invalid FilterStartValue in string: test)" {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRSRFieldRegexMatched(t *testing.T) {
|
||||
str := "test)"
|
||||
rsrf := &RSRField{
|
||||
Id: str,
|
||||
Rules: str,
|
||||
staticValue: str,
|
||||
RSRules: []*ReSearchReplace{
|
||||
{
|
||||
SearchRegexp: nil,
|
||||
ReplaceTemplate: str,
|
||||
Matched: false,
|
||||
},
|
||||
},
|
||||
filters: []*RSRFilter{},
|
||||
converters: DataConverters{},
|
||||
}
|
||||
|
||||
rcv := rsrf.RegexpMatched()
|
||||
|
||||
if rcv != false {
|
||||
t.Error(rcv)
|
||||
}
|
||||
|
||||
rsrf2 := &RSRField{
|
||||
Id: str,
|
||||
Rules: str,
|
||||
staticValue: str,
|
||||
RSRules: []*ReSearchReplace{
|
||||
{
|
||||
SearchRegexp: nil,
|
||||
ReplaceTemplate: str,
|
||||
Matched: true,
|
||||
},
|
||||
},
|
||||
filters: []*RSRFilter{},
|
||||
converters: DataConverters{},
|
||||
}
|
||||
|
||||
rcv = rsrf2.RegexpMatched()
|
||||
|
||||
if rcv != true {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRSRFieldFilterRule(t *testing.T) {
|
||||
str := "test"
|
||||
rsrFltr := RSRFilter{
|
||||
filterRule: str,
|
||||
fltrRgxp: nil,
|
||||
negative: true,
|
||||
}
|
||||
|
||||
rcv := rsrFltr.FilterRule()
|
||||
|
||||
if rcv != str {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRSRFieldParseRSRFilters(t *testing.T) {
|
||||
rcv, err := ParseRSRFilters("", "")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if rcv != nil {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRSRFieldFilterRules(t *testing.T) {
|
||||
str := "test"
|
||||
rsrFltr := RSRFilters{
|
||||
{
|
||||
filterRule: str,
|
||||
fltrRgxp: nil,
|
||||
negative: true,
|
||||
},
|
||||
}
|
||||
|
||||
rcv := rsrFltr.FilterRules()
|
||||
|
||||
if rcv != str {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRSRFieldPass(t *testing.T) {
|
||||
rsrFltr := RSRFilters{}
|
||||
|
||||
rcv := rsrFltr.Pass("", true)
|
||||
|
||||
if rcv != true {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRSRFieldParseRSRFieldsMustCompile(t *testing.T) {
|
||||
rcv := ParseRSRFieldsMustCompile("test)", "test)")
|
||||
|
||||
if rcv != nil {
|
||||
t.Error(rcv)
|
||||
}
|
||||
|
||||
rcv = ParseRSRFieldsMustCompile("test", ":")
|
||||
r := &RSRField{
|
||||
Id: "test",
|
||||
Rules: "test",
|
||||
}
|
||||
exp := RSRFields{r}
|
||||
|
||||
if !reflect.DeepEqual(rcv, exp) {
|
||||
t.Errorf("expected %s, received %s", ToJSON(exp), ToJSON(rcv))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRSRFieldsCompile(t *testing.T) {
|
||||
r := &RSRField{
|
||||
Id: "test)",
|
||||
Rules: "test)",
|
||||
}
|
||||
rsr := RSRFields{r}
|
||||
|
||||
err := rsr.Compile()
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != "Invalid FilterStartValue in string: test)" {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRSRFieldParseRSRFieldFromSlice(t *testing.T) {
|
||||
rcv, err := ParseRSRFieldsFromSlice([]string{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if rcv != nil {
|
||||
t.Error(rcv)
|
||||
}
|
||||
|
||||
rcv, err = ParseRSRFieldsFromSlice([]string{"test)"})
|
||||
if err != nil {
|
||||
if err.Error() != "Invalid FilterStartValue in string: test)" {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
|
||||
if rcv != nil {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -407,3 +407,77 @@ func TestStructUpdateStructWithStrMap(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStructUpdateStructWithIfaceMap(t *testing.T) {
|
||||
type Test struct {
|
||||
Bl bool
|
||||
Nm int
|
||||
Fl float64
|
||||
Df []byte
|
||||
}
|
||||
test := Test{Bl: true}
|
||||
test2 := Test{Nm: 1}
|
||||
test3 := Test{Fl: 1.2}
|
||||
test4 := Test{Df: []byte{}}
|
||||
type args struct {
|
||||
s any
|
||||
mp map[string]any
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
err string
|
||||
}{
|
||||
{
|
||||
name: "auto populate bool",
|
||||
args: args{&test, map[string]any{"Bl": ""}},
|
||||
err: "",
|
||||
},
|
||||
{
|
||||
name: "interface as bool error",
|
||||
args: args{&test, map[string]any{"Bl": []byte{}}},
|
||||
err: "cannot convert field: [] to bool",
|
||||
},
|
||||
{
|
||||
name: "auto populate int",
|
||||
args: args{&test2, map[string]any{"Nm": ""}},
|
||||
err: "",
|
||||
},
|
||||
{
|
||||
name: "interface as int error",
|
||||
args: args{&test2, map[string]any{"Nm": []byte{}}},
|
||||
err: "cannot convert field: [] to int",
|
||||
},
|
||||
{
|
||||
name: "auto populate float64",
|
||||
args: args{&test3, map[string]any{"Fl": ""}},
|
||||
err: "",
|
||||
},
|
||||
{
|
||||
name: "interface as flaot64 error",
|
||||
args: args{&test3, map[string]any{"Fl": []byte{}}},
|
||||
err: "cannot convert field: [] to float64",
|
||||
},
|
||||
{
|
||||
name: "default",
|
||||
args: args{&test4, map[string]any{"Df": ""}},
|
||||
err: "cannot update unsupported struct field: []",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := UpdateStructWithIfaceMap(tt.args.s, tt.args.mp)
|
||||
|
||||
if tt.err != "" {
|
||||
if err != nil {
|
||||
if err.Error() != tt.err {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user