Improving coverage at utils

This commit is contained in:
arberkatellari
2022-11-14 10:16:38 -05:00
committed by Dan Christian Bogos
parent ce6129c118
commit 9cf7e38eb6
4 changed files with 117 additions and 0 deletions

View File

@@ -113,3 +113,24 @@ func TestBlockersClone(t *testing.T) {
}
}
func TestBlockersString(t *testing.T) {
blkrs := DynamicBlockers{}
exp := ""
if rcv := blkrs.String("", ""); !reflect.DeepEqual(rcv, exp) {
t.Errorf("Expected %v \n received %v, length of blksr is %v", ToJSON(exp), ToJSON(rcv), len(blkrs))
}
blkrsTrue := DynamicBlocker{
Blocker: true,
}
expTrue := "true"
if rcvTrue := blkrsTrue.String("", ""); !reflect.DeepEqual(rcvTrue, expTrue) {
t.Errorf("Expected %v \n received %v", ToJSON(expTrue), ToJSON(rcvTrue))
}
}

View File

@@ -542,6 +542,11 @@ func TestParseTimeDetectLayout(t *testing.T) {
if err != nil || !date.UTC().Equal(expected.UTC()) {
t.Errorf("Expecting: %v, received: %v", expected.UTC(), date.UTC())
}
if date, err := ParseTimeDetectLayout("2014-11-25T00:00:00+01:00", "65"); err == nil {
t.Error("Expecting error 'timezone: invalid timezone', received: nil")
} else if date != nilTime {
t.Errorf("Expecting nilTime, received: %+v", date)
}
}
@@ -1475,6 +1480,9 @@ func TestFibDuration(t *testing.T) {
if tmp := FibDuration(2*time.Second, 0); tmp() != 2*time.Second {
t.Error("Expecting: 2, received ", tmp())
}
if tmp := FibDuration(2*time.Second, 4); tmp() != 4*time.Nanosecond {
t.Error("Expecting: 4, received ", tmp())
}
}
func TestCoreUtilsPaginate(t *testing.T) {
@@ -1686,3 +1694,46 @@ func TestCoreUtilsFibDurationSeqNrOverflow(t *testing.T) {
}
}
}
func TestUnzip(t *testing.T) {
if rcv := Unzip("Test error for unzip", "Test dest"); rcv == nil {
t.Error(rcv)
}
}
func TestToUnescapedJSON(t *testing.T) {
type testStruc struct {
testInt int
}
a := func(a int, b int) int {
return a + b
}
r := testStruc{testInt: 999}
if rcv, err := ToUnescapedJSON(a); err == nil {
t.Error(err)
t.Errorf("Expected nil, received %v", rcv)
}
exp := []byte{123, 125, 10}
if rcv, err := ToUnescapedJSON(r); err != nil {
t.Error(err)
t.Errorf("Expected %v, received %v", ToJSON(exp), ToJSON(rcv))
}
}
func TestCloneOfObject(t *testing.T) {
pgnt := Paginator{
MaxItems: IntPointer(7),
}
exp := Paginator{
MaxItems: IntPointer(7),
}
if rcv := pgnt.Clone(); !reflect.DeepEqual(exp, rcv) {
t.Errorf("Expected %v, received %v", ToJSON(exp), ToJSON(rcv))
}
}

View File

@@ -444,3 +444,27 @@ func TestMarshalUnmarshalNA(t *testing.T) {
t.Errorf("%v and %v are different", dec2, DecimalNaN)
}
}
func TestNewRoundingMode(t *testing.T) {
var tests = []struct {
rnd string
exp decimal.RoundingMode
}{
{"*toNearestEven", 0},
{"*toNearestAway", 1},
{"*toZero", 2},
{"*awayFromZero", 3},
{"*toNegativeInf", 4},
{"*toPositiveInf", 5},
{"*toNearestTowardZero", 6},
}
for _, e := range tests {
if rcv, err := NewRoundingMode(e.rnd); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(rcv, e.exp) {
t.Errorf("expected: <%v>, received: <%v>", e.exp, rcv)
}
}
}

View File

@@ -90,6 +90,27 @@ func TestNewDynamicWeightsFromString(t *testing.T) {
if _, err := NewDynamicWeightsFromString(dwsStr, ";", "&"); err == nil || err.Error() != expected {
t.Errorf("expecting: %+v, received: %+v", expected, err)
}
exp := DynamicWeights{{}}
if rcv, err := NewDynamicWeightsFromString("", "", ""); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(exp, rcv) {
t.Errorf("Expected %v, Received %v", ToJSON(exp), ToJSON(rcv))
}
exps := DynamicWeights{
{
FilterIDs: nil,
Weight: 5,
},
}
if rcv, err := NewDynamicWeightsFromString(";5", ";", "&"); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(exps, rcv) {
t.Errorf("expecting: %+v, received: %+v", ToJSON(exps), ToJSON(rcv))
} else if _, err := NewDynamicWeightsFromString(";5;", ";", "&"); err == nil {
t.Error(err)
}
}
func TestDynamicWeightString(t *testing.T) {