From b62039f57f99e5f958d5877c11f6085ae4cbce52 Mon Sep 17 00:00:00 2001 From: TeoV Date: Wed, 10 Feb 2021 14:26:27 +0200 Subject: [PATCH] Active commented tests in accounts --- accounts/abstractbalance.go | 5 ++-- accounts/abstractbalance_test.go | 44 ++++++++++++++++++++------------ accounts/concretebalance_test.go | 4 +-- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/accounts/abstractbalance.go b/accounts/abstractbalance.go index cda5c6097..1730ef44f 100644 --- a/accounts/abstractbalance.go +++ b/accounts/abstractbalance.go @@ -68,7 +68,6 @@ func (aB *abstractBalance) debitUsage(usage *utils.Decimal, aB.blnCfg.Units.Big = utils.SubstractBig(aB.blnCfg.Units.Big, blncLmt.Big) hasLmt = true } - // unitFactor var uF *utils.UnitFactor if uF, err = unitFactor(aB.blnCfg.UnitFactors, aB.fltrS, cgrEv.Tenant, evNm); err != nil { @@ -87,8 +86,8 @@ func (aB *abstractBalance) debitUsage(usage *utils.Decimal, return } - // balance smaller than usage, correct usage - if aB.blnCfg.Units.Compare(usage) == -1 { + // balance smaller than usage, correct usage if the balance has limit + if aB.blnCfg.Units.Compare(usage) == -1 && blncLmt != nil { // decrease the usage to match the maximum increments // will use special rounding to 0 since otherwise we go negative (ie: 0.05 as increment) usage.Big = roundedUsageWithIncrements(aB.blnCfg.Units.Big, costIcrm.Increment.Big) diff --git a/accounts/abstractbalance_test.go b/accounts/abstractbalance_test.go index 79c4aa7b2..82677b398 100644 --- a/accounts/abstractbalance_test.go +++ b/accounts/abstractbalance_test.go @@ -229,8 +229,6 @@ func TestABCost0(t *testing.T) { } } -/* -TODO: discuss about this test try to debit only from abstract balance with cost 0 func TestABCost0WithoutConcrete(t *testing.T) { // consume units only from abstract balance aB := &abstractBalance{ @@ -257,7 +255,6 @@ func TestABCost0WithoutConcrete(t *testing.T) { t.Errorf("Unexpected units in abstract balance: %s", aB.blnCfg.Units) } } -*/ func TestABCost0Exceed(t *testing.T) { // consume more units that has an abstract balance @@ -297,8 +294,33 @@ func TestABCost0Exceed(t *testing.T) { } } -/* -TODO: discuss about this test try to debit usage from an abstract balance with Units -1 and unlimited options +func TestABCost0ExceedWithoutConcrete(t *testing.T) { + // consume more units that has an abstract balance + aB := &abstractBalance{ + blnCfg: &utils.Balance{ + ID: "AB_COST_0", + Type: utils.MetaAbstract, + Units: utils.NewDecimal(int64(time.Duration(60*time.Second)), 0), // 1 Minute + CostIncrements: []*utils.CostIncrement{ + { + Increment: utils.NewDecimal(int64(time.Duration(time.Second)), 0), + RecurrentFee: utils.NewDecimal(0, 0), + }, + }, + }, + fltrS: new(engine.FilterS), + } + + if ec, err := aB.debitUsage(utils.NewDecimal(int64(70*time.Second), 0), + new(utils.CGREvent)); err != nil { + t.Error(err) + } else if ec.Usage.Cmp(decimal.New(int64(60*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if aB.blnCfg.Units.Compare(utils.NewDecimal(0, 0)) != 0 { + t.Errorf("Unexpected units in abstract balance: %s", aB.blnCfg.Units) + } +} + func TestABCost0WithUnlimited(t *testing.T) { // consume more units that has an abstract balance aB := &abstractBalance{ @@ -316,15 +338,6 @@ func TestABCost0WithUnlimited(t *testing.T) { }, }, }, - cncrtBlncs: []*concreteBalance{ - { - blnCfg: &utils.Balance{ - ID: "CB", - Type: utils.MetaConcrete, - Units: utils.NewDecimal(10, 0), - }, - }, - }, fltrS: new(engine.FilterS), } @@ -335,8 +348,5 @@ func TestABCost0WithUnlimited(t *testing.T) { t.Errorf("Unexpected debited units: %s", ec.Usage) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(-int64(time.Duration(20*time.Second)), 0)) != 0 { t.Errorf("Unexpected units in abstract balance: %s", aB.blnCfg.Units) - } else if aB.cncrtBlncs[0].blnCfg.Units.Compare(utils.NewDecimal(10, 0)) != 0 { - t.Errorf("Unexpected units in concrete balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } } -*/ diff --git a/accounts/concretebalance_test.go b/accounts/concretebalance_test.go index cf454e62b..048797863 100644 --- a/accounts/concretebalance_test.go +++ b/accounts/concretebalance_test.go @@ -410,9 +410,9 @@ func TestCBDebitWithUnitFactorWithFiltersWithLimit(t *testing.T) { if dbted, _, err := cb.debitUnits(utils.NewDecimal(3, 0), "cgrates.org", mp); err != nil { t.Error(err) - } else if dbted.Compare(utils.NewDecimal(3, 0)) != 0 { + } else if dbted.Compare(utils.NewDecimal(2, 0)) != 0 { t.Errorf("debited: %s", dbted) - } else if cb.blnCfg.Units.Cmp(decimal.New(200, 0)) != 0 { + } else if cb.blnCfg.Units.Cmp(decimal.New(300, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } }