diff --git a/accounts/abstractbalance_test.go b/accounts/abstractbalance_test.go index 0ba1a6836..738a20d17 100644 --- a/accounts/abstractbalance_test.go +++ b/accounts/abstractbalance_test.go @@ -19,6 +19,7 @@ along with this program. If not, see package accounts import ( + "reflect" "testing" "time" @@ -28,7 +29,6 @@ import ( "github.com/ericlagergren/decimal" ) -/* func TestABDebitUsageFromConcretes(t *testing.T) { aB := &abstractBalance{ cncrtBlncs: []*concreteBalance{ @@ -58,8 +58,11 @@ func TestABDebitUsageFromConcretes(t *testing.T) { }, }, }} + expectedEvCharg := &utils.EventCharges{ + Concretes: utils.NewDecimal(5, 0), + } // consume only from first balance - if err := debitAbstractsFromConcretes(aB.cncrtBlncs, + if evCh, err := debitAbstractsFromConcretes(aB.cncrtBlncs, decimal.New(int64(time.Duration(5*time.Minute)), 0), &utils.CostIncrement{ Increment: utils.NewDecimal(int64(time.Duration(time.Minute)), 0), @@ -70,13 +73,18 @@ func TestABDebitUsageFromConcretes(t *testing.T) { t.Errorf("Unexpected units in first balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } else if aB.cncrtBlncs[1].blnCfg.Units.Compare(utils.NewDecimal(125, 2)) != 0 { t.Errorf("Unexpected units in first balance: %s", aB.cncrtBlncs[1].blnCfg.Units) + } else if !reflect.DeepEqual(evCh, expectedEvCharg) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedEvCharg), utils.ToJSON(evCh)) } // consume from second also, remaining in second aB.cncrtBlncs[0].blnCfg.Units = utils.NewDecimal(500, 0) aB.cncrtBlncs[1].blnCfg.Units = utils.NewDecimal(125, 2) + expectedEvCharg = &utils.EventCharges{ + Concretes: utils.NewDecimal(9, 0), + } - if err := debitAbstractsFromConcretes(aB.cncrtBlncs, + if evCh, err := debitAbstractsFromConcretes(aB.cncrtBlncs, decimal.New(int64(time.Duration(9*time.Minute)), 0), &utils.CostIncrement{ Increment: utils.NewDecimal(int64(time.Duration(time.Minute)), 0), @@ -87,13 +95,16 @@ func TestABDebitUsageFromConcretes(t *testing.T) { t.Errorf("Unexpected units in first balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } else if aB.cncrtBlncs[1].blnCfg.Units.Compare(utils.NewDecimal(-75, 2)) != 0 { t.Errorf("Unexpected units in second balance: %s", aB.cncrtBlncs[1].blnCfg.Units) + } else if !reflect.DeepEqual(evCh, expectedEvCharg) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedEvCharg), utils.ToJSON(evCh)) } // not enough balance aB.cncrtBlncs[0].blnCfg.Units = utils.NewDecimal(500, 0) aB.cncrtBlncs[1].blnCfg.Units = utils.NewDecimal(125, 2) + expectedEvCharg = nil - if err := debitAbstractsFromConcretes(aB.cncrtBlncs, + if evCh, err := debitAbstractsFromConcretes(aB.cncrtBlncs, decimal.New(int64(time.Duration(10*time.Minute)), 0), &utils.CostIncrement{ Increment: utils.NewDecimal(int64(time.Duration(time.Minute)), 0), @@ -104,11 +115,12 @@ func TestABDebitUsageFromConcretes(t *testing.T) { t.Errorf("Unexpected units in first balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } else if aB.cncrtBlncs[1].blnCfg.Units.Compare(utils.NewDecimal(125, 2)) != 0 { t.Errorf("Unexpected units in first balance: %s", aB.cncrtBlncs[1].blnCfg.Units) + } else if !reflect.DeepEqual(evCh, expectedEvCharg) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedEvCharg), utils.ToJSON(evCh)) } } -*/ -/* -func TestABDebitUsage(t *testing.T) { + +func TestABDebitAbstracts(t *testing.T) { aB := &abstractBalance{ blnCfg: &utils.Balance{ ID: "AB1", @@ -140,8 +152,8 @@ func TestABDebitUsage(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(30*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(30*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(20, 0)) != 0 { @@ -155,8 +167,8 @@ func TestABDebitUsage(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(30*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(29*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(29*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(time.Duration(31*time.Second)), 0)) != 0 { // used 29 units t.Errorf("Unexpected units in abstract balance: %s", aB.blnCfg.Units) } else if aB.cncrtBlncs[0].blnCfg.Units.Compare(utils.NewDecimal(0, 0)) != 0 { @@ -169,8 +181,8 @@ func TestABDebitUsage(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(30*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(0, 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(0, 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(31*time.Second), 0)) != 0 { // same as above t.Errorf("Unexpected units in abstract balance: %s", aB.blnCfg.Units) } else if aB.cncrtBlncs[0].blnCfg.Units.Compare(utils.NewDecimal(0, 0)) != 0 { // same as above @@ -184,17 +196,15 @@ func TestABDebitUsage(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(30*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(29*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(29*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(0, 0)) != 0 { // should be all used t.Errorf("Unexpected units in abstract balance: %s", aB.blnCfg.Units) } else if aB.cncrtBlncs[0].blnCfg.Units.Compare(utils.NewDecimal(31, 0)) != 0 { t.Errorf("Unexpected units in concrete balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } - } -*/ -/* + func TestABCost0WithConcrete(t *testing.T) { // consume units only from abstract balance aB := &abstractBalance{ @@ -224,16 +234,15 @@ func TestABCost0WithConcrete(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(30*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(30*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) } } -*/ -/* + func TestABCost0WithoutConcrete(t *testing.T) { // consume units only from abstract balance aB := &abstractBalance{ @@ -254,14 +263,13 @@ func TestABCost0WithoutConcrete(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(30*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(30*time.Second), 0)) != 0 { t.Errorf("Unexpected units in abstract balance: %s", aB.blnCfg.Units) } } -*/ -/* + func TestABCost0Exceed(t *testing.T) { // consume more units that has an abstract balance aB := &abstractBalance{ @@ -291,16 +299,15 @@ func TestABCost0Exceed(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(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 ec.Abstracts.Cmp(decimal.New(int64(60*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(0, 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) } } -*/ -/* + func TestABCost0ExceedWithoutConcrete(t *testing.T) { // consume more units that has an abstract balance aB := &abstractBalance{ @@ -321,45 +328,13 @@ func TestABCost0ExceedWithoutConcrete(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(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 ec.Abstracts.Cmp(decimal.New(int64(60*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } 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{ - blnCfg: &utils.Balance{ - ID: "AB_COST_0", - Type: utils.MetaAbstract, - Units: utils.NewDecimal(int64(time.Duration(60*time.Second)), 0), // 1 Minute - Opts: map[string]interface{}{ - utils.MetaBalanceUnlimited: true, - }, - 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.debitAbstracts(decimal.New(int64(80*time.Second), 0), - new(utils.CGREvent)); err != nil { - t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(80*time.Second), 0)) != 0 { - 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) - } -} -*/ -/* func TestABCost0WithUnlimitedWithConcrete(t *testing.T) { // consume more units that has an abstract balance aB := &abstractBalance{ @@ -392,16 +367,15 @@ func TestABCost0WithUnlimitedWithConcrete(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(80*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(80*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(80*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } 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) } } -*/ -/* + func TestABCost0WithLimit(t *testing.T) { // consume more units that has an abstract balance aB := &abstractBalance{ @@ -425,14 +399,13 @@ func TestABCost0WithLimit(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(30*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(time.Duration(30*time.Second)), 0)) != 0 { t.Errorf("Unexpected units in abstract balance: %s", aB.blnCfg.Units) } } -*/ -/* + func TestABCost0WithLimitWithConcrete(t *testing.T) { // consume more units that has an abstract balance aB := &abstractBalance{ @@ -465,16 +438,15 @@ func TestABCost0WithLimitWithConcrete(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(30*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(time.Duration(30*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) } } -*/ -/* + func TestABCost0WithLimitExceed(t *testing.T) { // consume more units that has an abstract balance aB := &abstractBalance{ @@ -498,14 +470,13 @@ func TestABCost0WithLimitExceed(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(50*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(time.Duration(30*time.Second)), 0)) != 0 { t.Errorf("Unexpected units in abstract balance: %s", aB.blnCfg.Units) } } -*/ -/* + func TestABCost0WithLimitExceedWithConcrete(t *testing.T) { // consume more units that has an abstract balance aB := &abstractBalance{ @@ -538,15 +509,14 @@ func TestABCost0WithLimitExceedWithConcrete(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(50*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(time.Duration(30*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) } } -*/ func TestDebitUsageFiltersError(t *testing.T) { cfg := config.NewDefaultCGRConfig() @@ -630,7 +600,6 @@ func TestDebitUsageBalanceLimitErrors(t *testing.T) { } } -/* func TestDebitUsageUnitFactorsErrors(t *testing.T) { cfg := config.NewDefaultCGRConfig() filters := engine.NewFilterS(cfg, nil, nil) @@ -670,11 +639,10 @@ func TestDebitUsageUnitFactorsErrors(t *testing.T) { aB.blnCfg.UnitFactors[0].FilterIDs = []string{"*string:*~req.Usage:10s"} if ec, err := aB.debitAbstracts(decimal.New(int64(20*time.Second), 0), cgrEv); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(0, 0)) != 0 { + } else if ec.Abstracts.Cmp(decimal.New(0, 0)) != 0 { t.Error(err) } } -*/ func TestDebitUsageCostIncrementError(t *testing.T) { cfg := config.NewDefaultCGRConfig() @@ -717,7 +685,6 @@ func TestDebitUsageCostIncrementError(t *testing.T) { } } -/* func TestABCost(t *testing.T) { // debit 10 seconds with cost of 0.1 per second aB := &abstractBalance{ @@ -747,15 +714,14 @@ func TestABCost(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(10*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(10*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(10*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(time.Duration(50*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(9, 0)) != 0 { t.Errorf("Unexpected units in concrete balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } } -*/ func TestABCostWithFiltersNotMatch(t *testing.T) { cfg := config.NewDefaultCGRConfig() @@ -800,7 +766,6 @@ func TestABCostWithFiltersNotMatch(t *testing.T) { } } -/* func TestABCostWithFilters(t *testing.T) { // debit 10 seconds with cost of 0.1 per second cfg := config.NewDefaultCGRConfig() @@ -842,16 +807,15 @@ func TestABCostWithFilters(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(10*time.Second), 0), cgrEv); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(10*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(10*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(time.Duration(50*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(9, 0)) != 0 { t.Errorf("Unexpected units in concrete balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } } -*/ -/* + func TestABCostExceed(t *testing.T) { // debit 70 seconds with cost of 0.1 per second aB := &abstractBalance{ @@ -881,16 +845,15 @@ func TestABCostExceed(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(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 ec.Abstracts.Cmp(decimal.New(int64(60*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(0, 0)) != 0 { t.Errorf("Unexpected units in abstract balance: %s", aB.blnCfg.Units) } else if aB.cncrtBlncs[0].blnCfg.Units.Compare(utils.NewDecimal(4, 0)) != 0 { t.Errorf("Unexpected units in concrete balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } } -*/ -/* + func TestABCostUnlimitedExceed(t *testing.T) { // debit 70 seconds with cost of 0.1 per second aB := &abstractBalance{ @@ -923,16 +886,15 @@ func TestABCostUnlimitedExceed(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(70*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(70*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(70*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(-int64(time.Duration(10*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(3, 0)) != 0 { t.Errorf("Unexpected units in concrete balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } } -*/ -/* + func TestABCostLimit(t *testing.T) { // debit 70 seconds with cost of 0.1 per second aB := &abstractBalance{ @@ -965,16 +927,15 @@ func TestABCostLimit(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(30*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(time.Duration(30*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(7, 0)) != 0 { t.Errorf("Unexpected units in concrete balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } } -*/ -/* + func TestABCostLimitExceed(t *testing.T) { // debit 70 seconds with cost of 0.1 per second aB := &abstractBalance{ @@ -1007,16 +968,15 @@ func TestABCostLimitExceed(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(70*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(30*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(time.Duration(30*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(7, 0)) != 0 { t.Errorf("Unexpected units in concrete balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } } -*/ -/* + func TestABCostNotEnoughConcrete(t *testing.T) { // debit 55 seconds with cost of 0.1 per second aB := &abstractBalance{ @@ -1046,16 +1006,15 @@ func TestABCostNotEnoughConcrete(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(55*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(50*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(50*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(time.Duration(10*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(0, 0)) != 0 { t.Errorf("Unexpected units in concrete balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } } -*/ -/* + func TestABCostMultipleConcrete(t *testing.T) { // debit 55 seconds with cost of 0.1 per second aB := &abstractBalance{ @@ -1092,8 +1051,8 @@ func TestABCostMultipleConcrete(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(55*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(55*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(55*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(time.Duration(5*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(0, 0)) != 0 { @@ -1102,8 +1061,7 @@ func TestABCostMultipleConcrete(t *testing.T) { t.Errorf("Unexpected units in concrete balance: %s", aB.cncrtBlncs[1].blnCfg.Units) } } -*/ -/* + func TestABCostMultipleConcreteUnlimited(t *testing.T) { // debit 55 seconds with cost of 0.1 per second aB := &abstractBalance{ @@ -1143,8 +1101,8 @@ func TestABCostMultipleConcreteUnlimited(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(70*time.Second), 0), new(utils.CGREvent)); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(70*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(70*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(-int64(time.Duration(10*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(0, 0)) != 0 { @@ -1153,8 +1111,7 @@ func TestABCostMultipleConcreteUnlimited(t *testing.T) { t.Errorf("Unexpected units in concrete balance: %s", aB.cncrtBlncs[1].blnCfg.Units) } } -*/ -/* + func TestAMCostWithUnitFactor(t *testing.T) { cfg := config.NewDefaultCGRConfig() data := engine.NewInternalDB(nil, nil, true) @@ -1201,12 +1158,11 @@ func TestAMCostWithUnitFactor(t *testing.T) { if ec, err := aB.debitAbstracts(decimal.New(int64(10*time.Second), 0), cgrEv); err != nil { t.Error(err) - } else if ec.Usage.Cmp(decimal.New(int64(20*time.Second), 0)) != 0 { - t.Errorf("Unexpected debited units: %s", ec.Usage) + } else if ec.Abstracts.Cmp(decimal.New(int64(20*time.Second), 0)) != 0 { + t.Errorf("Unexpected debited units: %s", ec.Abstracts) } else if aB.blnCfg.Units.Compare(utils.NewDecimal(int64(time.Duration(40*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(30, 0)) != 0 { t.Errorf("Unexpected units in concrete balance: %s", aB.cncrtBlncs[0].blnCfg.Units) } } -*/ diff --git a/accounts/accounts.go b/accounts/accounts.go index ac4879873..6dd1e522a 100644 --- a/accounts/accounts.go +++ b/accounts/accounts.go @@ -179,9 +179,11 @@ func (aS *AccountS) accountDebit(acnt *utils.AccountProfile, usage *decimal.Big, } return } - used := ecDbt.Abstracts.Big + var used *decimal.Big if concretes { used = ecDbt.Concretes.Big + } else { + used = ecDbt.Abstracts.Big } usage = utils.SubstractBig(usage, used) ec.Merge(ecDbt) @@ -233,9 +235,11 @@ func (aS *AccountS) accountsDebit(acnts []*utils.AccountProfileWithWeight, return } } - used := ecDbt.Abstracts.Big + var used *decimal.Big if concretes { used = ecDbt.Concretes.Big + } else { + used = ecDbt.Abstracts.Big } usage = utils.SubstractBig(usage, used) ec.Merge(ecDbt) diff --git a/accounts/accounts_test.go b/accounts/accounts_test.go index 84cac9082..aede270e9 100644 --- a/accounts/accounts_test.go +++ b/accounts/accounts_test.go @@ -209,7 +209,6 @@ func TestMatchingAccountsForEvent(t *testing.T) { } } -/* func TestAccountDebit(t *testing.T) { engine.Cache.Clear(nil) cfg := config.NewDefaultCGRConfig() @@ -232,21 +231,20 @@ func TestAccountDebit(t *testing.T) { }, }, Type: utils.MetaConcrete, - Units: &utils.Decimal{decimal.New(200, 0)}, + Units: &utils.Decimal{decimal.New(150, 0)}, }, "AbstractBalance1": &utils.Balance{ ID: "AbstractBalance1", Weights: utils.DynamicWeights{ { - Weight: 5, + Weight: 25, }, }, Type: utils.MetaAbstract, - Units: &utils.Decimal{decimal.New(20, 0)}, + Units: &utils.Decimal{decimal.New(200, 0)}, CostIncrements: []*utils.CostIncrement{ &utils.CostIncrement{ - FilterIDs: []string{"*string:~*req.ToR:*data"}, - Increment: &utils.Decimal{decimal.New(int64(time.Second), 0)}, + Increment: &utils.Decimal{decimal.New(1, 0)}, FixedFee: &utils.Decimal{decimal.New(0, 0)}, RecurrentFee: &utils.Decimal{decimal.New(1, 0)}, }, @@ -263,8 +261,8 @@ func TestAccountDebit(t *testing.T) { utils.AccountField: "1004", }, } - usage := &utils.Decimal{decimal.New(210, 0)} + usage := &utils.Decimal{decimal.New(190, 0)} expected := "NOT_FOUND:invalid_filter_format" if _, err := accnts.accountDebit(accPrf, usage.Big, cgrEvent, true); err == nil || err.Error() != expected { t.Errorf("Expected %+v, received %+v", expected, err) @@ -282,7 +280,7 @@ func TestAccountDebit(t *testing.T) { if _, err := accnts.accountDebit(accPrf, usage.Big, cgrEvent, true); err != nil { t.Error(err) } - usage = &utils.Decimal{decimal.New(210, 0)} + usage = &utils.Decimal{decimal.New(190, 0)} accPrf.Balances["ConcreteBalance1"].UnitFactors = []*utils.UnitFactor{ { @@ -296,11 +294,11 @@ func TestAccountDebit(t *testing.T) { } accPrf.Balances["ConcreteBalance1"].UnitFactors[0].FilterIDs = []string{} - expectedUsage := &utils.Decimal{decimal.New(200, 0)} + expectedUsage := &utils.Decimal{decimal.New(150, 0)} if evCh, err := accnts.accountDebit(accPrf, usage.Big, cgrEvent, true); err != nil { t.Error(err) - } else if !reflect.DeepEqual(evCh.Usage.Big, expectedUsage.Big) { - t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedUsage), utils.ToJSON(evCh.Usage)) + } else if !reflect.DeepEqual(evCh.Concretes.Big, expectedUsage.Big) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedUsage.Big), utils.ToJSON(evCh.Concretes.Big)) } } @@ -323,14 +321,14 @@ func TestAccountsDebit(t *testing.T) { ID: "AbstractBalance1", Weights: utils.DynamicWeights{ { - Weight: 5, + Weight: 25, }, }, Type: utils.MetaAbstract, - Units: &utils.Decimal{decimal.New(int64(40*time.Second), 0)}, + Units: &utils.Decimal{decimal.New(40, 0)}, CostIncrements: []*utils.CostIncrement{ &utils.CostIncrement{ - Increment: &utils.Decimal{decimal.New(int64(time.Second), 0)}, + Increment: &utils.Decimal{decimal.New(1, 0)}, FixedFee: &utils.Decimal{decimal.New(0, 0)}, RecurrentFee: &utils.Decimal{decimal.New(1, 0)}, }, @@ -345,13 +343,6 @@ func TestAccountsDebit(t *testing.T) { }, Type: utils.MetaConcrete, Units: &utils.Decimal{decimal.New(213, 0)}, - CostIncrements: []*utils.CostIncrement{ - &utils.CostIncrement{ - Increment: &utils.Decimal{decimal.New(1, 0)}, - FixedFee: &utils.Decimal{decimal.New(0, 0)}, - RecurrentFee: &utils.Decimal{decimal.New(1, 0)}, - }, - }, }, }, }, @@ -381,7 +372,7 @@ func TestAccountsDebit(t *testing.T) { } delete(cgrEvent.Event, utils.MetaUsage) - if _, err := accnts.accountsDebit(accntsPrf, cgrEvent, false, false); err != nil { + if _, err := accnts.accountsDebit(accntsPrf, cgrEvent, true, false); err != nil { t.Error(err) } cgrEvent.Event[utils.MetaUsage] = "0" @@ -389,6 +380,7 @@ func TestAccountsDebit(t *testing.T) { if _, err := accnts.accountsDebit(accntsPrf, cgrEvent, false, false); err != nil { t.Error(err) } + cgrEvent.Event[utils.MetaUsage] = "55s" accntsPrf[0].Balances["AbstractBalance1"].Weights[0].FilterIDs = []string{"invalid_filter_format"} @@ -398,12 +390,12 @@ func TestAccountsDebit(t *testing.T) { } accntsPrf[0].Balances["AbstractBalance1"].Weights[0].FilterIDs = []string{} - cgrEvent.Event[utils.Usage] = "200ns" + cgrEvent.Event[utils.Usage] = "300ns" expectedUsage := &utils.Decimal{decimal.New(0, 0)} if evCh, err := accnts.accountsDebit(accntsPrf, cgrEvent, true, true); err != nil { t.Error(err) - } else if !reflect.DeepEqual(expectedUsage, evCh.Usage) { - t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedUsage), utils.ToJSON(evCh.Usage)) + } else if !reflect.DeepEqual(expectedUsage, evCh.Concretes) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedUsage), utils.ToJSON(evCh.Concretes)) } accntsPrf[0].Balances["ConcreteBalance2"].Units = &utils.Decimal{decimal.New(213, 0)} @@ -412,8 +404,8 @@ func TestAccountsDebit(t *testing.T) { if _, err := accnts.accountsDebit(accntsPrf, cgrEvent, true, true); err == nil || err.Error() != expected { t.Errorf("Expected %+v, received %+v", expected, err) } -} +} func TestV1AccountProfilesForEvent(t *testing.T) { engine.Cache.Clear(nil) @@ -555,7 +547,7 @@ func TestV1MaxAbstracts(t *testing.T) { delete(accPrf.Balances, "ConcreteBalance2") exEvCh := utils.ExtEventCharges{ - Usage: utils.Float64Pointer(210), + Abstracts: utils.Float64Pointer(210), } if err := accnts.V1MaxAbstracts(args, &reply); err != nil { t.Error(err) @@ -619,6 +611,7 @@ func TestV1DebitAbstracts(t *testing.T) { }, } reply := utils.ExtEventCharges{} + expected := "SERVER_ERROR: NOT_FOUND:invalid_filter" if err := accnts.V1DebitAbstracts(args, &reply); err == nil || err.Error() != expected { t.Errorf("Expected %+v, received %+v", expected, err) @@ -632,13 +625,21 @@ func TestV1DebitAbstracts(t *testing.T) { accPrf.Balances["AbstractBalance1"].Weights[0].FilterIDs = []string{} exEvCh := utils.ExtEventCharges{ - Usage: utils.Float64Pointer(float64(27 * time.Second)), + Abstracts: utils.Float64Pointer(float64(27 * time.Second)), } if err := accnts.V1DebitAbstracts(args, &reply); err != nil { t.Error(err) } else if !reflect.DeepEqual(exEvCh, reply) { t.Errorf("Expected %+v, received %+v", utils.ToJSON(exEvCh), utils.ToJSON(reply)) } + + //now we'll check the debited account + accPrf.Balances["AbstractBalance1"].Units = &utils.Decimal{decimal.New(39999999973, 0)} + if debitedAcc, err := accnts.dm.GetAccountProfile(accPrf.Tenant, accPrf.ID); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(accPrf, debitedAcc) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(accPrf), utils.ToJSON(debitedAcc)) + } } func TestV1MaxConcretes(t *testing.T) { @@ -743,7 +744,7 @@ func TestV1MaxConcretes(t *testing.T) { accPrf.Balances["AbstractBalance1"].Weights[0].FilterIDs = []string{} exEvCh := utils.ExtEventCharges{ - Usage: utils.Float64Pointer(float64(time.Minute + 30*time.Second)), + Concretes: utils.Float64Pointer(float64(time.Minute + 30*time.Second)), } if err := accnts.V1MaxConcretes(args, &reply); err != nil { t.Error(err) @@ -854,7 +855,7 @@ func TestV1DebitConcretes(t *testing.T) { accPrf.Balances["AbstractBalance1"].Weights[0].FilterIDs = []string{} exEvCh := utils.ExtEventCharges{ - Usage: utils.Float64Pointer(float64(time.Minute + 30*time.Second)), + Concretes: utils.Float64Pointer(float64(time.Minute + 30*time.Second)), } if err := accnts.V1DebitConcretes(args, &reply); err != nil { t.Error(err) @@ -875,7 +876,6 @@ func TestV1DebitConcretes(t *testing.T) { } -/* func TestMultipleAccountsFail(t *testing.T) { engine.Cache.Clear(nil) cfg := config.NewDefaultCGRConfig() @@ -996,16 +996,14 @@ func TestMultipleAccountsFail(t *testing.T) { expected := "NOT_FOUND:invalid_format" if _, err := accnts.matchingAccountsForEvent("cgrates.org", args, - []string{"TestV1MaxAbstracts", "TestV1MaxAbstracts2", "TestV1MaxAbstracts3"}, true); err != nil { + []string{"TestV1MaxAbstracts", "TestV1MaxAbstracts2", "TestV1MaxAbstracts3"}, true); err == nil || err.Error() != expected { t.Errorf("Expected %+v, received %+v", expected, err) } expected = "NOT_FOUND:invalid_format" if _, err := accnts.matchingAccountsForEvent("cgrates.org", args, - []string{"TestV1MaxAbstracts", "TestV1MaxAbstracts2", "TestV1MaxAbstracts3"}, true); err != nil { + []string{"TestV1MaxAbstracts", "TestV1MaxAbstracts2", "TestV1MaxAbstracts3"}, true); err == nil || err.Error() != expected { t.Errorf("Expected %+v, received %+v", expected, err) } } - -*/ diff --git a/accounts/concretebalance_test.go b/accounts/concretebalance_test.go index 88536569e..672525abf 100644 --- a/accounts/concretebalance_test.go +++ b/accounts/concretebalance_test.go @@ -19,6 +19,7 @@ along with this program. If not, see package accounts import ( + "reflect" "testing" "github.com/cgrates/rpcclient" @@ -30,7 +31,6 @@ import ( "github.com/ericlagergren/decimal" ) -/* func TestCBDebitUnits(t *testing.T) { // with limit and unit factor cb := &concreteBalance{ @@ -61,8 +61,8 @@ func TestCBDebitUnits(t *testing.T) { t.Error(err) } else if !reflect.DeepEqual(cb.blnCfg.UnitFactors[0], unitFct) { t.Errorf("received unit factor: %+v", unitFct) - } else if evChrgr.Usage.Compare(toDebit) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(toDebit) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(-100, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -83,8 +83,8 @@ func TestCBDebitUnits(t *testing.T) { if evChrgr, err := cb.debitConcretes(toDebit.Big, cgrEvent); err != nil { t.Error(err) - } else if evChrgr.Usage.Cmp(decimal.New(225, 2)) != 0 { // 2.25 debited - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Cmp(decimal.New(225, 2)) != 0 { // 2.25 debited + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(-1, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -105,8 +105,8 @@ func TestCBDebitUnits(t *testing.T) { if evChrgr, err := cb.debitConcretes(toDebit.Big, cgrEvent); err != nil { t.Error(err) - } else if evChrgr.Usage.Cmp(decimal.New(25, 1)) != 0 { // debit more than available since we have unlimited - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Cmp(decimal.New(25, 1)) != 0 { // debit more than available since we have unlimited + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(-125, 2)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -127,8 +127,8 @@ func TestCBDebitUnits(t *testing.T) { if evChrgr, err := cb.debitConcretes(toDebit.Big, cgrEvent); err != nil { t.Error(err) - } else if evChrgr.Usage.Cmp(decimal.New(75, 2)) != 0 { // limit is 0.5 - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Cmp(decimal.New(75, 2)) != 0 { // limit is 0.5 + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(5, 1)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -148,8 +148,8 @@ func TestCBSimpleDebit(t *testing.T) { if evChrgr, err := cb.debitConcretes(toDebit.Big, new(utils.CGREvent)); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(toDebit) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(toDebit) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(490, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -168,8 +168,8 @@ func TestCBDebitExceed(t *testing.T) { if evChrgr, err := cb.debitConcretes(utils.NewDecimal(510, 0).Big, new(utils.CGREvent)); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(utils.NewDecimal(500, 0)) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(utils.NewDecimal(500, 0)) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(0, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -191,8 +191,8 @@ func TestCBDebitUnlimited(t *testing.T) { if evChrgr, err := cb.debitConcretes(utils.NewDecimal(510, 0).Big, new(utils.CGREvent)); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(utils.NewDecimal(510, 0)) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(utils.NewDecimal(510, 0)) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(-410, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -215,8 +215,8 @@ func TestCBDebitLimit(t *testing.T) { if evChrgr, err := cb.debitConcretes(toDebit.Big, new(utils.CGREvent)); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(toDebit) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(toDebit) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(310, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -238,8 +238,8 @@ func TestCBDebitLimitExceed(t *testing.T) { if evChrgr, err := cb.debitConcretes(utils.NewDecimal(210, 0).Big, new(utils.CGREvent)); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(utils.NewDecimal(200, 0)) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(utils.NewDecimal(200, 0)) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(300, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -261,8 +261,8 @@ func TestCBDebitLimitExceed2(t *testing.T) { if evChrgr, err := cb.debitConcretes(utils.NewDecimal(510, 0).Big, new(utils.CGREvent)); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(utils.NewDecimal(200, 0)) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(utils.NewDecimal(200, 0)) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(300, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -287,8 +287,8 @@ func TestCBDebitWithUnitFactor(t *testing.T) { if evChrgr, err := cb.debitConcretes(toDebit.Big, new(utils.CGREvent)); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(toDebit) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(toDebit) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(400, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -315,8 +315,8 @@ func TestCBDebitWithUnitFactorWithLimit(t *testing.T) { if evChrgr, err := cb.debitConcretes(utils.NewDecimal(3, 0).Big, new(utils.CGREvent)); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(utils.NewDecimal(2, 0)) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(utils.NewDecimal(2, 0)) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(300, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -343,8 +343,8 @@ func TestCBDebitWithUnitFactorWithUnlimited(t *testing.T) { if evChrgr, err := cb.debitConcretes(utils.NewDecimal(7, 0).Big, new(utils.CGREvent)); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(utils.NewDecimal(7, 0)) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(utils.NewDecimal(7, 0)) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(-200, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -382,8 +382,8 @@ func TestCBDebitWithUnitFactorWithFilters1(t *testing.T) { if evChrgr, err := cb.debitConcretes(utils.NewDecimal(100, 0).Big, cgrEvent); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(utils.NewDecimal(100, 0)) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(utils.NewDecimal(100, 0)) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(400, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -420,8 +420,8 @@ func TestCBDebitWithUnitFactorWithFiltersWithLimit(t *testing.T) { if evChrgr, err := cb.debitConcretes(utils.NewDecimal(3, 0).Big, cgrEvent); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(utils.NewDecimal(2, 0)) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(utils.NewDecimal(2, 0)) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(300, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -459,8 +459,8 @@ func TestCBDebitWithMultipleUnitFactor(t *testing.T) { if evChrgr, err := cb.debitConcretes(utils.NewDecimal(3, 0).Big, cgrEvent); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(utils.NewDecimal(3, 0)) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(utils.NewDecimal(3, 0)) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(350, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -489,8 +489,8 @@ func TestCBDebitWithBalanceFilter(t *testing.T) { if evChrgr, err := cb.debitConcretes(utils.NewDecimal(3, 0).Big, cgrEvent); err != nil { t.Error(err) - } else if evChrgr.Usage.Compare(utils.NewDecimal(3, 0)) != 0 { - t.Errorf("debited: %s", evChrgr.Usage) + } else if evChrgr.Concretes.Compare(utils.NewDecimal(3, 0)) != 0 { + t.Errorf("debited: %s", evChrgr.Concretes) } else if cb.blnCfg.Units.Cmp(decimal.New(497, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } @@ -606,11 +606,8 @@ func TestCBDebitWithInvalidLimit(t *testing.T) { t.Error(err) } } -*/ -/* - -func TestCBSDebitUsage(t *testing.T) { +func TestCBSDebitAbstracts(t *testing.T) { // debit 10 units from a concrete balance with 500 units cb := &concreteBalance{ blnCfg: &utils.Balance{ @@ -630,15 +627,14 @@ func TestCBSDebitUsage(t *testing.T) { if dbted, err := cb.debitAbstracts(toDebit, new(utils.CGREvent)); err != nil { t.Error(err) - } else if dbted.Usage.Big.Cmp(toDebit) != 0 { + } else if dbted.Abstracts.Big.Cmp(toDebit) != 0 { t.Errorf("debited: %+v", dbted) } else if cb.blnCfg.Units.Cmp(decimal.New(498, 0)) != 0 { t.Errorf("balance remaining: %s", cb.blnCfg.Units) } } -*/ -func TestCBSDebitUsageInvalidFilter(t *testing.T) { +func TestCBSDebitAbstractsInvalidFilter(t *testing.T) { cfg := config.NewDefaultCGRConfig() data := engine.NewInternalDB(nil, nil, true) dm := engine.NewDataManager(data, config.CgrConfig().CacheCfg(), nil) @@ -666,7 +662,7 @@ func TestCBSDebitUsageInvalidFilter(t *testing.T) { } } -func TestCBSDebitUsageNoMatchFilter(t *testing.T) { +func TestCBSDebitAbstractsNoMatchFilter(t *testing.T) { cfg := config.NewDefaultCGRConfig() data := engine.NewInternalDB(nil, nil, true) dm := engine.NewDataManager(data, config.CgrConfig().CacheCfg(), nil) @@ -701,7 +697,7 @@ func TestCBSDebitUsageNoMatchFilter(t *testing.T) { } } -func TestCBSDebitUsageInvalidCostIncrementFilter(t *testing.T) { +func TestCBSDebitAbstractsInvalidCostIncrementFilter(t *testing.T) { cfg := config.NewDefaultCGRConfig() data := engine.NewInternalDB(nil, nil, true) dm := engine.NewDataManager(data, config.CgrConfig().CacheCfg(), nil) @@ -729,7 +725,7 @@ func TestCBSDebitUsageInvalidCostIncrementFilter(t *testing.T) { } } -func TestCBSDebitUsageCoverProcessAttributes(t *testing.T) { // coverage purpose +func TestCBSDebitAbstractsCoverProcessAttributes(t *testing.T) { // coverage purpose cfg := config.NewDefaultCGRConfig() data := engine.NewInternalDB(nil, nil, true) dm := engine.NewDataManager(data, config.CgrConfig().CacheCfg(), nil) @@ -774,7 +770,7 @@ func TestCBSDebitUsageCoverProcessAttributes(t *testing.T) { // coverage purpose } } -func TestCBSDebitUsageCoverProcessAttributes2(t *testing.T) { // coverage purpose +func TestCBSDebitAbstractsCoverProcessAttributes2(t *testing.T) { // coverage purpose cfg := config.NewDefaultCGRConfig() data := engine.NewInternalDB(nil, nil, true) dm := engine.NewDataManager(data, config.CgrConfig().CacheCfg(), nil) diff --git a/accounts/libaccounts.go b/accounts/libaccounts.go index 965741bd2..4750803af 100644 --- a/accounts/libaccounts.go +++ b/accounts/libaccounts.go @@ -190,7 +190,7 @@ func balanceLimit(optsCfg map[string]interface{}) (bL *utils.Decimal, err error) } // debitAbstractsFromConcretes attempts to debit the usage out of concrete balances -// returns utils.ErrInsufficientCredit if complete usage cannot be debitted +// returns utils.ErrInsufficientCredit if complete usage cannot be debited func debitAbstractsFromConcretes(cncrtBlncs []*concreteBalance, usage *decimal.Big, costIcrm *utils.CostIncrement, cgrEv *utils.CGREvent, connMgr *engine.ConnManager, rateSConns, rpIDs []string) (ec *utils.EventCharges, err error) { @@ -230,7 +230,7 @@ func debitAbstractsFromConcretes(cncrtBlncs []*concreteBalance, usage *decimal.B ec = utils.NewEventCharges() } ec.Merge(ecCncrt) - tCost = utils.SubstractBig(tCost, ecCncrt.Abstracts.Big) + tCost = utils.SubstractBig(tCost, ecCncrt.Concretes.Big) if tCost.Cmp(decimal.New(0, 0)) <= 0 { return // have debited all, total is smaller or equal to 0 } diff --git a/accounts/libaccounts_test.go b/accounts/libaccounts_test.go index 6968585cd..8917c3465 100644 --- a/accounts/libaccounts_test.go +++ b/accounts/libaccounts_test.go @@ -199,7 +199,6 @@ func TestRateSCostForEvent2(t *testing.T) { // coverage purpose } } -/* func TestDebitUsageFromConcretes(t *testing.T) { engine.Cache.Clear(nil) @@ -224,7 +223,11 @@ func TestDebitUsageFromConcretes(t *testing.T) { }, fltrS: filterS, } - if err := debitAbstractsFromConcretes([]*concreteBalance{cb1, cb2}, decimal.New(700, 0), &utils.CostIncrement{ + expectedEvCh := &utils.EventCharges{ + Concretes: utils.NewDecimal(710, 0), + } + + if evCh, err := debitAbstractsFromConcretes([]*concreteBalance{cb1, cb2}, decimal.New(700, 0), &utils.CostIncrement{ FixedFee: utils.NewDecimal(10, 0), Increment: utils.NewDecimal(1, 0), RecurrentFee: utils.NewDecimal(1, 0), @@ -234,11 +237,12 @@ func TestDebitUsageFromConcretes(t *testing.T) { t.Errorf("balance remaining: %s", cb1.blnCfg.Units) } else if cb2.blnCfg.Units.Cmp(decimal.New(290, 0)) != 0 { t.Errorf("balance remaining: %s", cb2.blnCfg.Units) + } else if !reflect.DeepEqual(expectedEvCh, evCh) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedEvCh), utils.ToJSON(evCh)) } } -*/ -/* + func TestDebitUsageFromConcretesFromRateS(t *testing.T) { engine.Cache.Clear(nil) @@ -286,7 +290,12 @@ func TestDebitUsageFromConcretesFromRateS(t *testing.T) { fltrS: filterS, connMgr: connMgr, } - if err := debitAbstractsFromConcretes([]*concreteBalance{cb1, cb2}, decimal.New(700, 0), &utils.CostIncrement{ + + expectedEvCh := &utils.EventCharges{ + Concretes: utils.NewDecimal(100, 0), + } + + if evCh, err := debitAbstractsFromConcretes([]*concreteBalance{cb1, cb2}, decimal.New(700, 0), &utils.CostIncrement{ Increment: utils.NewDecimal(1, 0), RecurrentFee: utils.NewDecimal(-1, 0), }, new(utils.CGREvent), connMgr, []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaRateS)}, nil); err != nil { @@ -295,11 +304,11 @@ func TestDebitUsageFromConcretesFromRateS(t *testing.T) { t.Errorf("balance remaining: %s", cb1.blnCfg.Units) } else if cb2.blnCfg.Units.Cmp(decimal.New(500, 0)) != 0 { t.Errorf("balance remaining: %s", cb2.blnCfg.Units) + } else if !reflect.DeepEqual(expectedEvCh, evCh) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedEvCh), utils.ToJSON(evCh)) } - } -*/ -/* + func TestDebitUsageFromConcretesRestore(t *testing.T) { engine.Cache.Clear(nil) @@ -325,7 +334,8 @@ func TestDebitUsageFromConcretesRestore(t *testing.T) { }, fltrS: filterS, } - if err := debitAbstractsFromConcretes([]*concreteBalance{cb1, cb2}, decimal.New(200, 0), &utils.CostIncrement{ + + if _, err := debitAbstractsFromConcretes([]*concreteBalance{cb1, cb2}, decimal.New(200, 0), &utils.CostIncrement{ Increment: utils.NewDecimal(1, 0), RecurrentFee: utils.NewDecimal(1, 0), }, new(utils.CGREvent), nil, nil, nil); err == nil || err.Error() != "inline parse error for string: <*string>" { @@ -336,8 +346,7 @@ func TestDebitUsageFromConcretesRestore(t *testing.T) { t.Errorf("balance remaining: %s", cb2.blnCfg.Units) } } -*/ -/* + func TestMaxDebitUsageFromConcretes(t *testing.T) { engine.Cache.Clear(nil) @@ -375,7 +384,6 @@ func TestMaxDebitUsageFromConcretes(t *testing.T) { t.Errorf("balance remaining: %s", cb2.blnCfg.Units) } } -*/ func TestRestoreAccount(t *testing.T) { //coverage purpose engine.Cache.Clear(nil) diff --git a/apier/v1/accountsv1_it_test.go b/apier/v1/accountsv1_it_test.go index 583ca7472..0dc4d4416 100644 --- a/apier/v1/accountsv1_it_test.go +++ b/apier/v1/accountsv1_it_test.go @@ -236,8 +236,8 @@ func testAccountSv1MaxAbstracts(t *testing.T) { utils.Usage: "15m", }}}, &eEc); err != nil { t.Error(err) - } else if eEc.Usage == nil || *eEc.Usage != 800000000000.0 { // 500s from first monetary + 300s from last monetary - t.Errorf("received usage: %v", *eEc.Usage) + } else if eEc.Abstracts == nil || *eEc.Abstracts != 800000000000.0 { // 500s from first monetary + 300s from last monetary + t.Errorf("received usage: %v", *eEc.Abstracts) } // Make sure we did not Debit anything from Account @@ -343,8 +343,8 @@ func testAccountSv1DebitAbstracts(t *testing.T) { utils.Usage: "15m", }}}, &eEc); err != nil { t.Error(err) - } else if eEc.Usage == nil || *eEc.Usage != 800000000000.0 { // 500s from first monetary + 300s from last monetary - t.Fatalf("received usage: %v", *eEc.Usage) + } else if eEc.Abstracts == nil || *eEc.Abstracts != 800000000000.0 { // 500s from first monetary + 300s from last monetary + t.Fatalf("received usage: %v", *eEc.Abstracts) } // Make sure we did not Debit anything from Account @@ -497,8 +497,8 @@ func testAccountSv1SimpleDebit(t *testing.T) { utils.Usage: "10", }}}, &eEc); err != nil { t.Error(err) - } else if eEc.Usage == nil || *eEc.Usage != 10.0 { - t.Fatalf("received usage: %v", *eEc.Usage) + } else if eEc.Abstracts == nil || *eEc.Abstracts != 10.0 { + t.Fatalf("received usage: %v", *eEc.Abstracts) } if err := acntSRPC.Call(utils.APIerSv1GetAccountProfile, &utils.TenantIDWithOpts{ @@ -601,8 +601,8 @@ func testAccountSv1DebitMultipleAcc(t *testing.T) { utils.Usage: "1400", }}}, &eEc); err != nil { t.Error(err) - } else if eEc.Usage == nil || *eEc.Usage != 1400.0 { - t.Fatalf("received usage: %v", *eEc.Usage) + } else if eEc.Abstracts == nil || *eEc.Abstracts != 1400.0 { + t.Fatalf("received usage: %v", *eEc.Abstracts) } if err := acntSRPC.Call(utils.APIerSv1GetAccountProfile, &utils.TenantIDWithOpts{ @@ -715,8 +715,8 @@ func testAccountSv1DebitMultipleAccLimited(t *testing.T) { utils.Usage: "900", }}}, &eEc); err != nil { t.Error(err) - } else if eEc.Usage == nil || *eEc.Usage != 900.0 { - t.Fatalf("received usage: %v", *eEc.Usage) + } else if eEc.Abstracts == nil || *eEc.Abstracts != 900.0 { + t.Fatalf("received usage: %v", *eEc.Abstracts) } if err := acntSRPC.Call(utils.APIerSv1GetAccountProfile, &utils.TenantIDWithOpts{ @@ -820,8 +820,8 @@ func testAccountSv1DebitWithAttributeSandRateS(t *testing.T) { utils.Usage: "10", }}}, &eEc); err != nil { t.Error(err) - } else if eEc.Usage == nil || *eEc.Usage != 10.0 { - t.Fatalf("received usage: %v", *eEc.Usage) + } else if eEc.Abstracts == nil || *eEc.Abstracts != 10.0 { + t.Fatalf("received usage: %v", *eEc.Abstracts) } if err := acntSRPC.Call(utils.APIerSv1GetAccountProfile, &utils.TenantIDWithOpts{ @@ -918,8 +918,8 @@ func testAccountSv1DebitWithRateS(t *testing.T) { utils.Usage: "20", }}}, &eEc); err != nil { t.Error(err) - } else if eEc.Usage == nil || *eEc.Usage != 20.0 { - t.Fatalf("received usage: %v", *eEc.Usage) + } else if eEc.Abstracts == nil || *eEc.Abstracts != 20.0 { + t.Fatalf("received usage: %v", *eEc.Abstracts) } if err := acntSRPC.Call(utils.APIerSv1GetAccountProfile, &utils.TenantIDWithOpts{ @@ -1023,8 +1023,8 @@ func testAccountSv1DebitWithRateS2(t *testing.T) { utils.Usage: "20", }}}, &eEc); err != nil { t.Error(err) - } else if eEc.Usage == nil || *eEc.Usage != 20.0 { - t.Fatalf("received usage: %v", *eEc.Usage) + } else if eEc.Abstracts == nil || *eEc.Abstracts != 20.0 { + t.Fatalf("received usage: %v", *eEc.Abstracts) } if err := acntSRPC.Call(utils.APIerSv1GetAccountProfile, &utils.TenantIDWithOpts{ @@ -1120,8 +1120,8 @@ func testAccountSv1MaxConcretes(t *testing.T) { }, }}, &eEc); err != nil { t.Error(err) - } else if eEc.Usage == nil || *eEc.Usage != 41 { - t.Errorf("received usage: %v", *eEc.Usage) + } else if eEc.Concretes == nil || *eEc.Concretes != 41 { + t.Errorf("received usage: %v", *eEc.Concretes) } //make sure we did not Debit from our Account @@ -1150,8 +1150,8 @@ func testAccountSv1DebitConcretes(t *testing.T) { }, }}, &eEc); err != nil { t.Error(err) - } else if eEc.Usage == nil || *eEc.Usage != 41 { - t.Errorf("received usage: %v", *eEc.Usage) + } else if eEc.Concretes == nil || *eEc.Concretes != 41 { + t.Errorf("received usage: %v", *eEc.Concretes) } exp := &utils.AccountProfile{