diff --git a/utils/accountprofile_test.go b/utils/accountprofile_test.go index ccb023c2b..5017beb8e 100644 --- a/utils/accountprofile_test.go +++ b/utils/accountprofile_test.go @@ -710,3 +710,39 @@ func TestAPBalances(t *testing.T) { t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", ToJSON(expected), ToJSON(received)) } } + +func TestEqualsUnitFactor(t *testing.T) { + uf1 := &UnitFactor{ + FilterIDs: []string{"*string:~*req.Account:1003"}, + Factor: NewDecimal(10, 0), + } + uf2 := &UnitFactor{ + FilterIDs: []string{"*string:~*req.Account:1003"}, + Factor: NewDecimal(10, 0), + } + if !uf1.Equals(uf2) { + t.Errorf("Unexpected equal result") + } + + uf1.FilterIDs = []string{"*string:~*req.Account:1004"} + if uf1.Equals(uf2) { + t.Errorf("Unexpected equal result") + } + uf1.FilterIDs = nil + + if uf1.Equals(uf2) { + t.Errorf("Unexpected equal result") + } + uf1.FilterIDs = []string{"*string:~*req.Account:1003"} + + uf1.Factor = NewDecimal(100, 0) + if uf1.Equals(uf2) { + t.Errorf("Unexpected equal result") + } + + uf1.Factor = nil + uf2.Factor = nil + if !uf1.Equals(uf2) { + t.Errorf("Unexpected equal result") + } +} diff --git a/utils/eventcharges_test.go b/utils/eventcharges_test.go index 639bcdf4e..13ecd5bd9 100644 --- a/utils/eventcharges_test.go +++ b/utils/eventcharges_test.go @@ -168,3 +168,200 @@ func TestECAsExtEventChargesFailConcretes(t *testing.T) { t.Errorf("\nExpected: %v,\nReceived: %v", expected, err) } } + +func TestEqualsAccountCharge(t *testing.T) { + accCharge1 := &AccountCharge{ + AccountID: "AccountID1", + BalanceID: "BalanceID1", + Units: NewDecimal(20, 0), + BalanceLimit: NewDecimal(40, 0), + UnitFactorID: "UF1", + AttributeIDs: []string{"ID1", "ID2"}, + RatingID: "RatingID1", + JoinedChargeIDs: []string{"chID1"}, + } + accCharge2 := &AccountCharge{ + AccountID: "AccountID1", + BalanceID: "BalanceID1", + Units: NewDecimal(20, 0), + BalanceLimit: NewDecimal(40, 0), + UnitFactorID: "UF1", + AttributeIDs: []string{"ID1", "ID2"}, + RatingID: "RatingID1", + JoinedChargeIDs: []string{"chID1"}, + } + if !accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are not equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + + // not equal for AccountID + accCharge1.AccountID = "test" + if accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + accCharge1.AccountID = "AccountID1" + + accCharge2.AccountID = "test" + if accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + accCharge2.AccountID = "AccountID1" + + // not equal for BalanceID + accCharge1.BalanceID = "test" + if accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + accCharge1.BalanceID = "AccountID1" + + accCharge2.BalanceID = "test" + if accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + accCharge2.BalanceID = "AccountID1" + + // not equal for BalanceLimit + accCharge1.BalanceLimit = NewDecimal(35, 0) + if accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + accCharge1.BalanceLimit = NewDecimal(40, 0) + + accCharge2.BalanceLimit = NewDecimal(35, 0) + if accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + accCharge2.BalanceLimit = NewDecimal(40, 0) + + // not equal for Units + accCharge1.Units = NewDecimal(35, 0) + if accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + accCharge1.Units = NewDecimal(20, 0) + + accCharge2.Units = NewDecimal(35, 0) + if accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + accCharge2.Units = NewDecimal(20, 0) + + // not equal for AttributeIDs + accCharge1.AttributeIDs = []string{"ID1"} + if accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + accCharge1.AttributeIDs = []string{"ID1", "ID2"} + + accCharge2.AttributeIDs = []string{"ID1"} + if accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + accCharge2.AttributeIDs = []string{"ID1", "ID2"} + + // not equal for JoinedChargeIDs + accCharge1.JoinedChargeIDs = []string{} + if accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + accCharge1.JoinedChargeIDs = []string{"chID1"} + + accCharge2.JoinedChargeIDs = []string{} + if accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + accCharge2.JoinedChargeIDs = []string{"chID1"} + + //both units and BalanceLimit are nil will be equal + accCharge1.Units = nil + accCharge2.Units = nil + if !accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are not equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } + + accCharge1.BalanceLimit = nil + accCharge2.BalanceLimit = nil + if !accCharge1.Equals(accCharge2) { + t.Errorf("Charge %+v and %+v are not equal", ToJSON(accCharge1), ToJSON(accCharge2)) + } +} + +func TestCompressEqualsChargingInterval(t *testing.T) { + chIn1 := &ChargingInterval{ + Increments: []*ChargingIncrement{ + { + Units: NewDecimal(10, 0), + AccountChargeID: "CHARGER1", + CompressFactor: 1, + }, + }, + CompressFactor: 2, + } + chIn2 := &ChargingInterval{ + Increments: []*ChargingIncrement{ + { + Units: NewDecimal(10, 0), + AccountChargeID: "CHARGER1", + CompressFactor: 1, + }, + }, + CompressFactor: 4, + } + + // compressEquals is not looking for compress factor + if !chIn1.CompressEquals(chIn2) { + t.Errorf("Intervals %+v and %+v are not equal", ToJSON(chIn1), ToJSON(chIn2)) + } + + //same thing in ChargingIncrements + chIn1.Increments[0].CompressFactor = 2 + if !chIn1.CompressEquals(chIn2) { + t.Errorf("Intervals %+v and %+v are not equal", ToJSON(chIn1), ToJSON(chIn2)) + } + + //not equals for AccountChargeID + chIn1.Increments[0].AccountChargeID = "Changed_Charger" + if chIn1.CompressEquals(chIn2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(chIn1), ToJSON(chIn2)) + } + chIn1.Increments[0].AccountChargeID = "CHARGER1" + + chIn2.Increments[0].AccountChargeID = "Changed_Charger" + if chIn1.CompressEquals(chIn2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(chIn1), ToJSON(chIn2)) + } + chIn2.Increments[0].AccountChargeID = "CHARGER1" + + //not equals for Units + chIn1.Increments[0].Units = NewDecimal(30, 0) + if chIn1.CompressEquals(chIn2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(chIn1), ToJSON(chIn2)) + } + chIn1.Increments[0].Units = NewDecimal(10, 0) + + chIn2.Increments[0].Units = NewDecimal(30, 0) + if chIn1.CompressEquals(chIn2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(chIn1), ToJSON(chIn2)) + } + chIn2.Increments[0].Units = NewDecimal(10, 0) + + //not equals by the length of increments + chIn1 = &ChargingInterval{ + Increments: []*ChargingIncrement{ + { + Units: NewDecimal(10, 0), + AccountChargeID: "CHARGER1", + CompressFactor: 1, + }, + { + Units: NewDecimal(12, 0), + AccountChargeID: "CHARGER2", + CompressFactor: 6, + }, + }, + CompressFactor: 0, + } + if chIn1.CompressEquals(chIn2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(chIn1), ToJSON(chIn2)) + } +} diff --git a/utils/librates_test.go b/utils/librates_test.go index 9c024b223..0967a1fad 100644 --- a/utils/librates_test.go +++ b/utils/librates_test.go @@ -1543,3 +1543,181 @@ func TestLibratesAsRateProfileNon0Len(t *testing.T) { t.Errorf("\nExpected: <%v>, \nReceived: <%v>", expected, received) } } + +func TestRatesIntervalEquals(t *testing.T) { + rtInt1 := &RateSInterval{ + IntervalStart: NewDecimal(int64(10*time.Second), 0), + Increments: []*RateSIncrement{ + { + IncrementStart: NewDecimal(int64(time.Second), 0), + IntervalRateIndex: 1, + Rate: &Rate{ + uID: "newID", + }, + CompressFactor: 2, + Usage: NewDecimal(int64(5*time.Second), 0), + }, + }, + CompressFactor: 2, + } + rtInt2 := &RateSInterval{ + IntervalStart: NewDecimal(int64(10*time.Second), 0), + Increments: []*RateSIncrement{ + { + IncrementStart: NewDecimal(int64(time.Second), 0), + IntervalRateIndex: 1, + Rate: &Rate{ + uID: "newID", + }, + CompressFactor: 2, + Usage: NewDecimal(int64(5*time.Second), 0), + }, + }, + CompressFactor: 2, + } + + // equals is looking for compressFactor + if !rtInt1.Equals(rtInt2) { + t.Errorf("Intervals %+v and %+v are not equal", ToJSON(rtInt1), ToJSON(rtInt2)) + } + + // not equals for IntervalStart + rtInt1.IntervalStart = NewDecimal(int64(20*time.Second), 0) + if rtInt1.Equals(rtInt2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(rtInt1), ToJSON(rtInt2)) + } + rtInt1.IntervalStart = NewDecimal(int64(10*time.Second), 0) + + rtInt2.IntervalStart = NewDecimal(int64(20*time.Second), 0) + if rtInt1.Equals(rtInt2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(rtInt1), ToJSON(rtInt2)) + } + rtInt2.IntervalStart = NewDecimal(int64(10*time.Second), 0) + + // not equals for CompressFactor + rtInt1.CompressFactor = 5 + if rtInt1.Equals(rtInt2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(rtInt1), ToJSON(rtInt2)) + } + rtInt1.CompressFactor = 2 + + rtInt2.CompressFactor = 8 + if rtInt1.Equals(rtInt2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(rtInt1), ToJSON(rtInt2)) + } + rtInt2.CompressFactor = 2 + + //not equals for Increments and their length + rtInt1.Increments[0].Usage = NewDecimal(int64(90*time.Second), 0) + if rtInt1.Equals(rtInt2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(rtInt1), ToJSON(rtInt2)) + } + rtInt1.Increments[0].Usage = NewDecimal(int64(5*time.Second), 0) + + rtInt2.Increments[0].Usage = NewDecimal(int64(80*time.Second), 0) + if rtInt1.Equals(rtInt2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(rtInt1), ToJSON(rtInt2)) + } + rtInt2.Increments[0].Usage = NewDecimal(int64(5*time.Second), 0) + + rtInt1 = &RateSInterval{ + IntervalStart: NewDecimal(int64(10*time.Second), 0), + Increments: []*RateSIncrement{ + { + IncrementStart: NewDecimal(int64(time.Second), 0), + IntervalRateIndex: 1, + Rate: &Rate{ + uID: "newID", + }, + CompressFactor: 2, + Usage: NewDecimal(int64(5*time.Second), 0), + }, + { + IncrementStart: NewDecimal(int64(time.Second), 0), + Usage: NewDecimal(int64(5*time.Second), 0), + }, + }, + CompressFactor: 2, + } + if rtInt1.Equals(rtInt2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(rtInt1), ToJSON(rtInt2)) + } +} + +func TestRatesIncrementEquals(t *testing.T) { + incr1 := &RateSIncrement{ + IncrementStart: NewDecimal(int64(time.Second), 0), + IntervalRateIndex: 1, + Rate: &Rate{ + uID: "newID", + }, + CompressFactor: 2, + Usage: NewDecimal(int64(5*time.Second), 0), + } + incr2 := &RateSIncrement{ + IncrementStart: NewDecimal(int64(time.Second), 0), + IntervalRateIndex: 1, + Rate: &Rate{ + uID: "newID", + }, + CompressFactor: 2, + Usage: NewDecimal(int64(5*time.Second), 0), + } + + // equals is not looking for compress factor + if !incr1.Equals(incr2) { + t.Errorf("Intervals %+v and %+v are not equal", ToJSON(incr1), ToJSON(incr2)) + } + + // not equals by IncrementStart + incr1.IncrementStart = NewDecimal(int64(10*time.Second), 0) + if incr1.Equals(incr2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(incr1), ToJSON(incr2)) + } + incr1.IncrementStart = NewDecimal(int64(time.Second), 0) + + incr2.IncrementStart = NewDecimal(int64(10*time.Second), 0) + if incr1.Equals(incr2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(incr1), ToJSON(incr2)) + } + incr2.IncrementStart = NewDecimal(int64(time.Second), 0) + + // not equals by IntervalRateIndex + incr1.IntervalRateIndex = 5 + if incr1.Equals(incr2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(incr1), ToJSON(incr2)) + } + incr1.IntervalRateIndex = 1 + + incr2.IntervalRateIndex = 5 + if incr1.Equals(incr2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(incr1), ToJSON(incr2)) + } + incr2.IntervalRateIndex = 1 + + //not equals by RateUID + incr1.Rate.uID = "changed_uID" + if incr1.Equals(incr2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(incr1), ToJSON(incr2)) + } + incr1.Rate.uID = "newID" + + incr2.Rate.uID = "changed_uID" + if incr1.Equals(incr2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(incr1), ToJSON(incr2)) + } + incr2.Rate.uID = "newID" + + // not equals by CompressFactor + incr1.CompressFactor = 0 + if incr1.Equals(incr2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(incr1), ToJSON(incr2)) + } + incr1.CompressFactor = 2 + + incr2.CompressFactor = 9 + if incr1.Equals(incr2) { + t.Errorf("Intervals %+v and %+v are equal", ToJSON(incr1), ToJSON(incr2)) + } + incr2.CompressFactor = 2 +}