Updated Equals method + tests

This commit is contained in:
porosnicuadrian
2021-07-26 15:24:41 +03:00
committed by Dan Christian Bogos
parent a278780686
commit 2dccdb2162
7 changed files with 114 additions and 141 deletions

View File

@@ -372,30 +372,32 @@ func TestAccountsDebitGetUsage(t *testing.T) {
evChExp := &utils.EventCharges{
Abstracts: utils.NewDecimal(89, 0),
Concretes: utils.NewDecimal(1484,1),
Concretes: utils.NewDecimal(1484, 1),
Charges: []*utils.ChargeEntry{
{
ChargingID: "CHARGING1",
ChargingID: "CHARGING1",
CompressFactor: 1,
},
},
Accounting: map[string]*utils.AccountCharge{
"CHARGING1": {
AccountID: "TestAccountsDebitGetUsage",
BalanceID: "*transabstract",
Units: utils.NewDecimal(89,0),
RatingID: "RATING1",
AccountID: "TestAccountsDebitGetUsage",
BalanceID: "*transabstract",
Units: utils.NewDecimal(89, 0),
RatingID: "RATING1",
JoinedChargeIDs: []string{"JND_CHRG1", "JND_CHRG2"},
},
"JND_CHRG1": {
AccountID: "TestAccountsDebitGetUsage",
BalanceID: "ConcreteBal1",
Units: utils.NewDecimal(592,1),
BalanceLimit: utils.NewDecimal(0, 0),
Units: utils.NewDecimal(592, 1),
},
"JND_CHRG2": {
AccountID: "TestAccountsDebitGetUsage",
BalanceID: "ConcreteBal1",
Units: utils.NewDecimal(892,1),
BalanceLimit: utils.NewDecimal(0, 0),
Units: utils.NewDecimal(892, 1),
},
},
Rating: map[string]*utils.RateSInterval{
@@ -411,7 +413,7 @@ func TestAccountsDebitGetUsage(t *testing.T) {
},
},
UnitFactors: map[string]*utils.UnitFactor{},
Rates: map[string]*utils.IntervalRate{},
Rates: map[string]*utils.IntervalRate{},
Accounts: map[string]*utils.Account{
"TestAccountsDebitGetUsage": accntsPrf[0].Account,
},
@@ -425,30 +427,30 @@ func TestAccountsDebitGetUsage(t *testing.T) {
utils.Destination: "+445643",
},
APIOpts: map[string]interface{}{
utils.OptsRatesUsage: "2s",
utils.OptsRatesUsage: "2s",
},
}
if rcv, err := accnts.accountsDebit(context.Background(), accntsPrf,
cgrEvent, false, false); err != nil {
t.Error(err)
} else if rcv.Equals(evChExp) {
} else if !rcv.Equals(evChExp) {
t.Errorf("Expected %v, \n received %v", utils.ToJSON(evChExp), utils.ToJSON(rcv))
}
/*
// get usage from *usage
delete(cgrEvent.APIOpts, utils.OptsRatesUsage)
cgrEvent.APIOpts = map[string]interface{}{
utils.MetaUsage: "2s",
}
if rcv, err := accnts.accountsDebit(context.Background(), accntsPrf,
cgrEvent, false, false); err != nil {
t.Error(err)
} else if rcv.Equals(evChExp) {
t.Errorf("Expected %v, \n received %v", utils.ToJSON(evChExp), utils.ToJSON(rcv))
}
// get usage from *usage
delete(cgrEvent.APIOpts, utils.OptsRatesUsage)
cgrEvent.APIOpts = map[string]interface{}{
utils.MetaUsage: "2s",
}
if rcv, err := accnts.accountsDebit(context.Background(), accntsPrf,
cgrEvent, false, false); err != nil {
t.Error(err)
} else if rcv.Equals(evChExp) {
t.Errorf("Expected %v, \n received %v", utils.ToJSON(evChExp), utils.ToJSON(rcv))
}
*/
*/
}
func TestAccountsDebit(t *testing.T) {
@@ -505,7 +507,7 @@ func TestAccountsDebit(t *testing.T) {
utils.AccountField: "1004",
},
APIOpts: map[string]interface{}{
utils.OptsRatesUsage: "not_time_format",
utils.OptsRatesUsage: "not_time_format",
},
}

View File

@@ -522,6 +522,10 @@ func (dbM *dataDBMockError) SetActionProfileDrv(*context.Context, *engine.Action
return utils.ErrNoDatabaseConn
}
func (dbM *dataDBMockError) GetIndexesDrv(ctx *context.Context, idxItmType, tntCtx, idxKey string) (indexes map[string]utils.StringSet, err error) {
return nil, nil
}
func TestLogActionExecute(t *testing.T) {
evNM := utils.MapStorage{
utils.MetaReq: map[string]interface{}{

View File

@@ -1973,7 +1973,6 @@ func TestComputeRateSIntervals(t *testing.T) {
for idx, val := range rtIvls {
if !val.Equals(eRtIvls[idx], cstRts, expCstRts) {
t.Errorf("expecting: %+v \n,received: %+v", utils.ToJSON(expCstRts), utils.ToJSON(cstRts))
t.Fatalf("expecting: %+v \n,received: %+v", utils.ToJSON(eRtIvls), utils.ToJSON(rtIvls))
}
}
}

View File

@@ -360,7 +360,7 @@ func TestCGREventOptAsDuration(t *testing.T) {
if err != nil {
t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", nil, err)
}
expected := decimal.New(int64(30 * time.Nanosecond), 0)
expected := decimal.New(int64(30*time.Nanosecond), 0)
if !reflect.DeepEqual(received, expected) {
t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, received)

View File

@@ -101,12 +101,6 @@ func (cE *ChargeEntry) CompressEquals(chEn *ChargeEntry) bool {
return cE.ChargingID == chEn.ChargingID
}
// Equals return the equality between two ChargeEntry
func (cE *ChargeEntry) Equals(chEn *ChargeEntry) (eq bool) {
return cE.ChargingID == chEn.ChargingID &&
cE.CompressFactor == chEn.CompressFactor
}
// SyncIDs will repopulate Accounting, UnitFactors and Rating IDs if they equal the references in ec
func (ec *EventCharges) SyncIDs(eCs ...*EventCharges) {
for _, nEc := range eCs {
@@ -258,16 +252,19 @@ func (eEc *ExtEventCharges) Equals(exCh *ExtEventCharges) (eq bool) {
len(eEc.Accounts) != len(exCh.Accounts)) {
return
}
for idx, val := range exCh.Charges {
if ok := val.Equals(exCh.Charges[idx]); !ok {
return
/*
for idx, val := range exCh.Charges {
if ok := val.Equals(exCh.Charges[idx], eEc.Accounting, exCh.Accounting); !ok {
return
}
}
}
for key, val := range eEc.Accounting {
if ok := val.Equals(exCh.Accounting[key]); !ok {
return
for key, val := range eEc.Accounting {
if ok := val.Equals(exCh.Accounting[key]); !ok {
return
}
}
}
*/
for key, val := range eEc.UnitFactors {
if ok := val.Equals(exCh.UnitFactors[key]); !ok {
return
@@ -316,23 +313,11 @@ func (eC *EventCharges) Equals(evCh *EventCharges) (eq bool) {
len(eC.Accounts) != len(evCh.Accounts)) {
return
}
for idx, val := range eC.Charges {
if ok := val.Equals(evCh.Charges[idx]); !ok {
return
}
}
for key, val := range eC.Accounting {
if ok := val.Equals(evCh.Accounting[key]); !ok {
return
}
}
for key, val := range eC.UnitFactors {
if ok := val.Equals(evCh.UnitFactors[key]); !ok {
return
}
}
for key, val := range eC.Rating {
if ok := val.Equals(evCh.Rating[key], eC.Rates, evCh.Rates); !ok {
for idx, ch1 := range eC.Charges {
if ch2 := evCh.Charges[idx]; ch1.CompressFactor != ch2.CompressFactor ||
!equalsAccounting(eC.Accounting[ch1.ChargingID], evCh.Accounting[ch2.ChargingID],
eC.Accounting, evCh.Accounting, eC.UnitFactors, evCh.UnitFactors,
eC.Rating, evCh.Rating, eC.Rates, evCh.Rates) {
return
}
}
@@ -344,6 +329,28 @@ func (eC *EventCharges) Equals(evCh *EventCharges) (eq bool) {
return true
}
func equalsAccounting(acc1, acc2 *AccountCharge,
accM1, accM2 map[string]*AccountCharge,
uf1, uf2 map[string]*UnitFactor,
rat1, rat2 map[string]*RateSInterval,
rts1, rts2 map[string]*IntervalRate) (_ bool) {
if !acc1.equals(acc2) ||
(uf1 != nil && uf2 != nil &&
acc1.UnitFactorID != EmptyString && acc2.UnitFactorID != EmptyString &&
!uf1[acc1.UnitFactorID].Equals(uf2[acc2.UnitFactorID])) ||
!rat1[acc1.RatingID].Equals(rat2[acc2.RatingID], rts1, rts2) {
return
}
for idx, jc1 := range acc1.JoinedChargeIDs {
jc2 := acc2.JoinedChargeIDs[idx]
if !equalsAccounting(accM1[jc1], accM2[jc2], accM1, accM2,
uf1, uf2, rat1, rat2, rts1, rts2) {
return
}
}
return true
}
// unitFactorID returns the ID of the matching UnitFactor within ec.UnitFactors
func (ec *EventCharges) unitFactorID(uF *UnitFactor) (ufID string) {
for ecUfID, ecUf := range ec.UnitFactors {
@@ -367,7 +374,7 @@ func (ec *EventCharges) ratingID(rIl *RateSInterval, nIrRef map[string]*Interval
// accountChargeID returns the ID of the matching AccountCharge within ec.Accounting
func (ec *EventCharges) accountChargeID(ac *AccountCharge) (acID string) {
for ecID, ecAc := range ec.Accounting {
if ecAc.Equals(ac) {
if ecAc.equals(ac) {
return ecID
}
}
@@ -486,7 +493,10 @@ func (eAc *ExtAccountCharge) Equals(extAc *ExtAccountCharge) (eq bool) {
}
// Equals compares two AccountCharges
func (ac *AccountCharge) Equals(nAc *AccountCharge) (eq bool) {
func (ac *AccountCharge) equals(nAc *AccountCharge) (eq bool) {
if ac == nil && nAc == nil {
return true
}
if (ac.AttributeIDs == nil && nAc.AttributeIDs != nil ||
ac.AttributeIDs != nil && nAc.AttributeIDs == nil ||
len(ac.AttributeIDs) != len(nAc.AttributeIDs)) ||
@@ -494,9 +504,9 @@ func (ac *AccountCharge) Equals(nAc *AccountCharge) (eq bool) {
ac.JoinedChargeIDs != nil && nAc.JoinedChargeIDs == nil ||
len(ac.JoinedChargeIDs) != len(nAc.JoinedChargeIDs) ||
(ac.AccountID != nAc.AccountID ||
ac.BalanceID != nAc.BalanceID ||
ac.UnitFactorID != nAc.UnitFactorID ||
ac.RatingID != nAc.RatingID) ||
ac.BalanceID != nAc.BalanceID) ||
((len(ac.UnitFactorID) == 0) != (len(nAc.UnitFactorID) == 0)) ||
((len(ac.RatingID) == 0) != (len(nAc.RatingID) == 0)) ||
(ac.Units == nil && nAc.Units != nil ||
ac.Units != nil && nAc.Units == nil ||
(ac.Units != nil && nAc.Units != nil &&
@@ -512,10 +522,5 @@ func (ac *AccountCharge) Equals(nAc *AccountCharge) (eq bool) {
return
}
}
for i := range ac.JoinedChargeIDs {
if ac.JoinedChargeIDs[i] != nAc.JoinedChargeIDs[i] {
return
}
}
return true
}

View File

@@ -27,12 +27,13 @@ import (
"github.com/ericlagergren/decimal"
)
/*
func TestECNewEventCharges(t *testing.T) {
expected := &EventCharges{
Accounting: make(map[string]*AccountCharge),
UnitFactors: make(map[string]*UnitFactor),
Rating: make(map[string]*RateSInterval),
Rates: make(map[string]*IntervalRate),
Accounts: make(map[string]*Account),
}
received := NewEventCharges()
@@ -40,7 +41,6 @@ func TestECNewEventCharges(t *testing.T) {
t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, received)
}
}
*/
func TestECMergeAbstractsEmpty(t *testing.T) {
ec1 := &EventCharges{
@@ -602,14 +602,14 @@ func TestEventChargesEquals(t *testing.T) {
},
},
Accounting: map[string]*AccountCharge{
"GENUUID_GHOST1": {
"THIS_GENUUID1": {
AccountID: "TestEventChargesEquals",
BalanceID: "CONCRETE1",
Units: NewDecimal(8, 1),
BalanceLimit: NewDecimal(200, 0),
UnitFactorID: "GENUUID_FACTOR1",
},
"GENUUID3": {
"GENUUID1": {
AccountID: "TestEventChargesEquals",
BalanceID: "ABSTRACT2",
BalanceLimit: NewDecimal(0, 0),
@@ -779,14 +779,14 @@ func TestEventChargesEquals(t *testing.T) {
},
},
Accounting: map[string]*AccountCharge{
"GENUUID_GHOST1": {
"THIS_GENUUID1": {
AccountID: "TestEventChargesEquals",
BalanceID: "CONCRETE1",
Units: NewDecimal(8, 1),
BalanceLimit: NewDecimal(200, 0),
UnitFactorID: "GENUUID_FACTOR1",
},
"GENUUID3": {
"GENUUID1": {
AccountID: "TestEventChargesEquals",
BalanceID: "ABSTRACT2",
BalanceLimit: NewDecimal(0, 0),
@@ -962,14 +962,14 @@ func TestEqualsExtEventCharges(t *testing.T) {
},
},
Accounting: map[string]*AccountCharge{
"GENUUID_GHOST1": {
"THIS_GENUUID1": {
AccountID: "TestEventChargesEquals",
BalanceID: "CONCRETE1",
Units: NewDecimal(8, 1),
BalanceLimit: NewDecimal(200, 0),
UnitFactorID: "GENUUID_FACTOR1",
},
"GENUUID3": {
"GENUUID1": {
AccountID: "TestEventChargesEquals",
BalanceID: "ABSTRACT2",
BalanceLimit: NewDecimal(0, 0),
@@ -1145,14 +1145,14 @@ func TestEqualsExtEventCharges(t *testing.T) {
},
},
Accounting: map[string]*ExtAccountCharge{
"GENUUID_GHOST1": {
"THIS_GENUUID1": {
AccountID: "TestEventChargesEquals",
BalanceID: "CONCRETE1",
Units: Float64Pointer(0.8),
BalanceLimit: Float64Pointer(200),
UnitFactorID: "GENUUID_FACTOR1",
},
"GENUUID3": {
"GENUUID1": {
AccountID: "TestEventChargesEquals",
BalanceID: "ABSTRACT2",
BalanceLimit: Float64Pointer(0),

View File

@@ -116,43 +116,6 @@ func (eRt *ExtRate) Equals(extRT *ExtRate) (eq bool) {
return true
}
// Equals returns the equality between two Rate
func (rT *Rate) Equals(rte *Rate) (eq bool) {
if (rT.ID != rte.ID ||
rT.ActivationTimes != rte.ActivationTimes ||
rT.Blocker != rte.Blocker) ||
(rT.FilterIDs == nil && rte.FilterIDs != nil ||
rT.FilterIDs != nil && rte.FilterIDs == nil) ||
(rT.Weights == nil && rte.Weights != nil ||
rT.Weights != nil && rte.Weights == nil ||
len(rT.Weights) != len(rte.Weights)) ||
(rT.IntervalRates == nil && rte.IntervalRates != nil ||
rT.IntervalRates != nil && rte.IntervalRates == nil ||
len(rT.IntervalRates) != len(rte.IntervalRates)) {
return
}
for idx, val := range rT.FilterIDs {
if val != rte.FilterIDs[idx] {
return
}
}
if rT.Weights != nil && rte.Weights != nil {
for idx, val := range rT.Weights {
if ok := val.Equals(rte.Weights[idx]); !ok {
return
}
}
}
if rT.IntervalRates != nil && rte.IntervalRates != nil {
for idx, val := range rT.IntervalRates {
if ok := val.Equals(rte.IntervalRates[idx]); !ok {
return
}
}
}
return true
}
// AsExtRate converts Rate to ExtRate
func (rT *Rate) AsExtRate() (eRt *ExtRate, err error) {
eRt = &ExtRate{
@@ -265,8 +228,16 @@ func (eIr *ExtIntervalRate) Equals(extIr *ExtIntervalRate) (eq bool) {
// Equals returns the equality between two IntervalRate
func (iR *IntervalRate) Equals(inRt *IntervalRate) (eq bool) {
if ((iR != nil || inRt != nil) &&
(iR == nil || inRt == nil)) ||
if iR == nil && inRt == nil {
return true
}
if iR == nil && inRt != nil ||
iR != nil && inRt == nil {
return
}
return !(//((iR != nil) != (inRt != nil)) ||
// (iR == nil || inRt == nil) ||
(iR.RecurrentFee == nil && inRt.RecurrentFee != nil) ||
(iR.RecurrentFee != nil && inRt.RecurrentFee == nil) ||
(iR.FixedFee == nil && inRt.FixedFee != nil) ||
@@ -276,30 +247,17 @@ func (iR *IntervalRate) Equals(inRt *IntervalRate) (eq bool) {
(iR.Unit == nil && inRt.Unit != nil) ||
(iR.Unit != nil && inRt.Unit == nil) ||
(iR.IntervalStart == nil && inRt.IntervalStart != nil) ||
(iR.IntervalStart != nil && inRt.IntervalStart == nil) {
return
}
if iR.RecurrentFee != nil && inRt.RecurrentFee != nil &&
iR.RecurrentFee.Compare(inRt.RecurrentFee) != 0 {
return
}
if iR.FixedFee != nil && inRt.FixedFee != nil &&
iR.FixedFee.Compare(inRt.FixedFee) != 0 {
return
}
if iR.Increment != nil && inRt.Increment != nil &&
iR.Increment.Compare(inRt.Increment) != 0 {
return
}
if iR.Unit != nil && inRt.Unit != nil &&
iR.Unit.Compare(inRt.Unit) != 0 {
return
}
if iR.IntervalStart != nil && inRt.IntervalStart != nil &&
iR.IntervalStart.Compare(inRt.IntervalStart) != 0 {
return
}
return true
(iR.IntervalStart != nil && inRt.IntervalStart == nil) ||
(iR.RecurrentFee != nil && inRt.RecurrentFee != nil &&
iR.RecurrentFee.Compare(inRt.RecurrentFee) != 0) ||
(iR.FixedFee != nil && inRt.FixedFee != nil &&
iR.FixedFee.Compare(inRt.FixedFee) != 0) ||
(iR.Increment != nil && inRt.Increment != nil &&
iR.Increment.Compare(inRt.Increment) != 0) ||
(iR.Unit != nil && inRt.Unit != nil &&
iR.Unit.Compare(inRt.Unit) != 0) ||
(iR.IntervalStart != nil && inRt.IntervalStart != nil &&
iR.IntervalStart.Compare(inRt.IntervalStart) != 0))
}
func (rt *Rate) Compile() (err error) {
@@ -492,6 +450,9 @@ func (eRI *ExtRateSIncrement) Equals(extRI *ExtRateSIncrement, exInRt, exInRtRef
// Equals compares two RateSIntervals
func (rIl *RateSInterval) Equals(nRil *RateSInterval, rIlRef, nRilRef map[string]*IntervalRate) (eq bool) {
if rIl == nil && nRil == nil {
return true
}
if rIl.IntervalStart == nil && nRil.IntervalStart != nil ||
rIl.IntervalStart != nil && nRil.IntervalStart == nil ||
(rIl.IntervalStart != nil && nRil.IntervalStart != nil &&
@@ -524,7 +485,9 @@ func (rI *RateSIncrement) Equals(rtIn *RateSIncrement, rIRef, rtInRef map[string
rI.IncrementStart.Compare(rtIn.IncrementStart) != 0)) ||
rI.CompressFactor != rtIn.CompressFactor ||
rI.RateIntervalIndex != rtIn.RateIntervalIndex ||
!rIRef[rI.RateID].Equals(rtInRef[rtIn.RateID]))
(rIRef != nil && rtInRef != nil &&
rI.RateID != EmptyString && rtIn.RateID != EmptyString &&
!rIRef[rI.RateID].Equals(rtInRef[rtIn.RateID])))
}
// RateProfileCost is the cost returned by RateS at cost queries