Unit tests in utils

This commit is contained in:
nickolasdaniel
2021-10-27 10:47:16 +03:00
committed by Dan Christian Bogos
parent 624ee9ef46
commit 88e07e0f5d
5 changed files with 92 additions and 2 deletions

View File

@@ -260,6 +260,21 @@ func TestAPIBalanceAsBalance(t *testing.T) {
t.Errorf("Expected %+v \n, received %+v", ToJSON(expected), ToJSON(rcv))
}
// Can't convert units
blc.Units = "can't convert"
exp := "can't convert <can't convert> to decimal"
if _, err := blc.AsBalance(); err == nil || err.Error() != exp {
t.Errorf("Expected %v \n but received \n %v", exp, err.Error())
}
blc.Units = "0"
//Can't convert increment
blc.CostIncrements[0].Increment = "error"
exp = "can't convert <error> to decimal"
if _, err := blc.AsBalance(); err == nil || err.Error() != exp {
t.Errorf("Expected %v \n but received \n %v", exp, err.Error())
}
blc.CostIncrements[0].Increment = "1"
}
func TestAccountBalancesAlteredCompareLength(t *testing.T) {

View File

@@ -1466,3 +1466,37 @@ func TestCoreUtilsSplitPath(t *testing.T) {
t.Errorf("expected: <%+v>, \nreceived: <%+v>", exp, rcv)
}
}
func TestFibDuration(t *testing.T) {
// fib := Fib()
// if tmp := fib(); tmp != 1 {
// t.Error("Expecting: 1, received ", tmp)
// }
// if tmp := fib(); tmp != 1 {
// t.Error("Expecting: 1, received ", tmp)
// }
// if tmp := fib(); tmp != 2 {
// t.Error("Expecting: 2, received ", tmp)
// }
// if tmp := fib(); tmp != 3 {
// t.Error("Expecting: 3, received ", tmp)
// }
// if tmp := fib(); tmp != 5 {
// t.Error("Expecting: 5, received ", tmp)
// }
if tmp := FibDuration(1 * time.Second); tmp() != 1*time.Second {
t.Error("Expecting: 1, received ", tmp())
}
if tmp := FibDuration(1 * time.Second); tmp() != 1*time.Second {
t.Error("Expecting: 1, received ", tmp())
}
if tmp := FibDuration(2 * time.Second); tmp() != 2*time.Second {
t.Error("Expecting: 2, received ", tmp())
}
if tmp := FibDuration(2 * time.Second); tmp() != 2*time.Second {
t.Error("Expecting: 2, received ", tmp())
}
if tmp := FibDuration(2 * time.Second); tmp() != 2*time.Second {
t.Error("Expecting: 2, received ", tmp())
}
}

View File

@@ -352,7 +352,7 @@ func TestDecimalFloat64(t *testing.T) {
}
func TestDecimalDuration(t *testing.T) {
d := NewDecimal(3, 0)
d := NewDecimal(int64(3), 0)
rcv, ok := d.Duration()
if !ok {
t.Error("Cannot convert")

View File

@@ -273,7 +273,7 @@ func (rI *RateSIncrement) AsRateSIncrementCost() (rIc *RateSIncrementCost) {
// Equals returns the equality between two RateSIntervalCost
func (rIC *RateSIntervalCost) Equals(nRIc *RateSIntervalCost, rIlRef, nRilRef map[string]*IntervalRate) (eq bool) {
if (rIC.Increments != nil && rIC.Increments == nil ||
if (rIC.Increments != nil && nRIc.Increments == nil ||
rIC.Increments == nil && nRIc.Increments != nil ||
len(rIC.Increments) != len(nRIc.Increments)) || rIC.CompressFactor != nRIc.CompressFactor {
return

View File

@@ -1628,3 +1628,44 @@ func TestAPIRateAsRateError(t *testing.T) {
t.Errorf("Expected %v \n but received \n %v", exp, err.Error())
}
}
func TestIntervalRateEqualsNilIR(t *testing.T) {
var iR *IntervalRate
iR = nil
iR.Equals(nil)
iR = &IntervalRate{
Unit: NewDecimal(int64(2), 0),
}
iR.Equals(nil)
}
func TestRateSIntervalCostEquals(t *testing.T) {
rIC := &RateSIntervalCost{
Increments: nil,
}
nRIc := &RateSIntervalCost{
Increments: []*RateSIncrementCost{
{
RateIntervalIndex: 0,
RateID: "RI1",
CompressFactor: int64(1),
Usage: NewDecimal(int64(2), 0),
},
},
}
if rIC.Equals(nRIc, nil, nil) {
t.Error("Shouldn't match")
}
rIC.Increments = []*RateSIncrementCost{
{
RateIntervalIndex: 3,
RateID: "RI2",
CompressFactor: int64(3),
Usage: NewDecimal(int64(5), 0),
},
}
if rIC.Equals(nRIc, nil, nil) {
t.Error("Shouldn't match")
}
}