From 318145d7c5bc06f4291a8ef9d26aa93ef10a84b6 Mon Sep 17 00:00:00 2001 From: TeoV Date: Wed, 25 Nov 2020 15:47:49 +0200 Subject: [PATCH] Populate other fields for RateCost and add an example of test for CorrectCost --- engine/rateprofile.go | 4 ++-- engine/rateprofile_test.go | 18 ++++++++++++++++++ rates/rates.go | 8 +++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/engine/rateprofile.go b/engine/rateprofile.go index 500d41948..99dd453e4 100644 --- a/engine/rateprofile.go +++ b/engine/rateprofile.go @@ -203,11 +203,11 @@ func (rPc *RateProfileCost) CorrectCost(rndDec *int, rndMtd string) { } } - if rPc.Cost < rPc.MinCost { + if rPc.MinCost != 0 && rPc.Cost < rPc.MinCost { rPc.Cost = rPc.MinCost rPc.Altered = append(rPc.Altered, utils.MinCost) } - if rPc.Cost > rPc.MaxCost { + if rPc.MaxCost != 0 && rPc.Cost > rPc.MaxCost { rPc.Cost = rPc.MaxCost rPc.Altered = append(rPc.Altered, utils.MaxCost) } diff --git a/engine/rateprofile_test.go b/engine/rateprofile_test.go index 0be3f7aec..cf5b27aeb 100644 --- a/engine/rateprofile_test.go +++ b/engine/rateprofile_test.go @@ -656,3 +656,21 @@ func TestCostForIntervalsWIthFixedFee(t *testing.T) { t.Errorf("eDcml: %f, received: %+v", eDcml, cost) } } + +func TestRateProfileCostCorrectCost(t *testing.T) { + rPrfCost := &RateProfileCost{ + ID: "Test1", + Cost: 0.234, + } + rPrfCost.CorrectCost(utils.IntPointer(2), utils.ROUNDING_UP) + if rPrfCost.Cost != 0.24 { + t.Errorf("Expected: %+v, received: %+v", 0.24, rPrfCost.Cost) + } + if rPrfCost.RoundingDecimals != 2 { + t.Errorf("Expected: %+v, received: %+v", 2, rPrfCost.Cost) + } + if !reflect.DeepEqual(rPrfCost.Altered, []string{utils.RoundingDecimals}) { + t.Errorf("Expected: %+v, received: %+v", []string{utils.RoundingDecimals}, rPrfCost.Altered) + } + +} diff --git a/rates/rates.go b/rates/rates.go index 8df812a96..04f648a26 100644 --- a/rates/rates.go +++ b/rates/rates.go @@ -162,7 +162,13 @@ func (rS *RateS) rateProfileCostForEvent(rtPfl *engine.RateProfile, args *utils. if ordRts, err = orderRatesOnIntervals(aRates, sTime, usage, true, 1000000); err != nil { return } - rpCost = &engine.RateProfileCost{ID: rtPfl.ID} + rpCost = &engine.RateProfileCost{ + ID: rtPfl.ID, + MinCost: rtPfl.MinCost, + MaxCost: rtPfl.MaxCost, + RoundingDecimals: rtPfl.RoundingDecimals, + RoundingMethod: rtPfl.RoundingMethod, + } if rpCost.RateSIntervals, err = computeRateSIntervals(ordRts, 0, usage); err != nil { return nil, err }